module PDQTest::Skeleton

Constants

ACCEPTANCE_DIR
EXAMPLES_DIR
FIXTURES
HIERA_DIR
HIERA_TEST
HIERA_YAML
PDK_FILES
SKELETON_DIR
SPEC_DIR
SYNC_YML_CONTENT

Every time we `pdqtest upgrade`, update .sync.yml (merges)

TEMP_PDK_MODULE

Public Class Methods

directory_structure() click to toggle source
# File lib/pdqtest/skeleton.rb, line 115
def self.directory_structure
  FileUtils.mkdir_p(ACCEPTANCE_DIR)
  FileUtils.mkdir_p(EXAMPLES_DIR)
  FileUtils.mkdir_p(HIERA_DIR)
end
generate_acceptance(example=nil) click to toggle source

Scan the examples directory and create a set of acceptance tests. If a specific file is given as `example` then only the listed example will be processed (and it will be created if missing). If files already exist, they will not be touched

# File lib/pdqtest/skeleton.rb, line 161
def self.generate_acceptance(example=nil)
  examples = []
  if example
    # specific file only
    examples << example
  else
    # Each .pp file in /examples (don't worry about magic markers yet, user
    # will be told on execution if no testscases are present as a reminder to
    # add it
    examples += Dir["examples/*.pp"]
  end

  examples.each { |e|
    install_acceptance(e)
  }
end
init() click to toggle source
# File lib/pdqtest/skeleton.rb, line 121
def self.init
  directory_structure

  install_pdk_skeletons
  install_skeletons
  install_acceptance
  Upgrade.upgrade()

  # the very _last_ thing we do is enable PDK in metadata. Once this switch
  # is set, we never touch the files in PDK_FILES again
  PDQTest::Pdk.enable_pdk(@@pdk_metadata)
end
install_acceptance(example_file ="init.pp") click to toggle source
# File lib/pdqtest/skeleton.rb, line 146
def self.install_acceptance(example_file ="init.pp")
  directory_structure

  example_name = File.basename(example_file).gsub(/\.pp$/, '')
  install_template("#{EXAMPLES_DIR}/#{File.basename(example_file)}",'examples_init.pp.erb', {})

  install_skeleton(File.join('spec', 'acceptance', "#{example_name}.bats"), '../acceptance/init.bats', false)
  install_skeleton(File.join('spec', 'acceptance', "#{example_name}__before.bats"), '../acceptance/init__before.bats', false)
  install_skeleton(File.join('spec', 'acceptance', "#{example_name}__setup.sh"), '../acceptance/init__setup.sh', false)
end
install_gemfile_project() click to toggle source
# File lib/pdqtest/skeleton.rb, line 108
def self.install_gemfile_project
  install_skeleton(GEMFILE, GEMFILE)

  # upgrade the gemfile to *this* version of pdqtest + puppet-strings
  Upgrade.upgrade()
end
install_pdk_skeletons() click to toggle source
# File lib/pdqtest/skeleton.rb, line 179
    def self.install_pdk_skeletons

      if ! PDQTest::Pdk.is_pdk_enabled
        $logger.info "Doing one-time upgrade to PDK - Generating fresh set of files..."
        project_dir = File.expand_path Dir.pwd
        Dir.mktmpdir do |tmpdir|
          Dir.chdir(tmpdir) do
            status = PDQTest::Pdk.run("new module #{TEMP_PDK_MODULE} --skip-interview")

            if status
              # snag generated metadata now we are in the temporary module dir
              Dir.chdir TEMP_PDK_MODULE do
                @@pdk_metadata = PDQTest::Puppet.module_metadata

                # Now we need to install .sync.yml and re-install otherwise not
                # applied until `pdk update`
                PDQTest::Pdk.amend_sync_yml(SYNC_YML_CONTENT)

                # Next do a forced update to make PDK process sync.yml
                PDQTest::Pdk.run("update --force")
              end

              PDK_FILES.each do |pdk_file|
                upstream_file = File.join(tmpdir, TEMP_PDK_MODULE, pdk_file)

                # check if we are trying to install a file from PDQTest or have
                # some random/customised file in place
                Dir.chdir project_dir do
                  if PDQTest1x.was_pdqtest_file(pdk_file)
                    if ! File.exists?(pdk_file) || PDQTest1x.is_pdqtest_file(pdk_file)
                      # overwrite missing or PDQTest 1x files
                      install = true
                    else
                      raise(<<~END)
                        Detected an unknown/customised file at
                          #{pdk_file}
                        Please see the PDQTest 1x->2x upgrade guide at
                        https://github.com/declarativesystems/pdqtest/blob/master/doc/upgrading.md
    
                        If your sure you don't want this file any more, move it out
                        of the way and re-run the previous command
                      END
                    end
                  else
                    install = true
                  end

                  if install
                    $logger.info("Detected PDQTest 1.x file at #{pdk_file} (will upgrade to PDK)")
                    FileUtils.cp(upstream_file, pdk_file)
                  end
                end
              end
            else
              raise("error running PDK - unable to init")
            end
          end
        end
      else
        $logger.debug "PDK already enabled, no skeletons needed"
      end
    end
install_skeleton(target_file, skeleton, replace=true) click to toggle source
# File lib/pdqtest/skeleton.rb, line 81
def self.install_skeleton(target_file, skeleton, replace=true)
  skeleton_file = Util.resource_path(File.join(SKELETON_DIR, skeleton))
  install = false
  if File.exists?(target_file)
    if replace && should_replace_file(target_file, skeleton_file)
      install = true
    else
      $logger.debug "#{target_file} exists and will not be replaced"
    end
  else
    install = true
  end
  if install
    $logger.debug "Installing skeleton file at #{target_file}"
    FileUtils.cp(skeleton_file, target_file)
  end
end
install_skeletons() click to toggle source
# File lib/pdqtest/skeleton.rb, line 76
def self.install_skeletons
  FileUtils.cp_r(Util.resource_path(File.join(SKELETON_DIR) + "/."), ".")
end
install_template(target, template_file, vars) click to toggle source

vars is a hash of variables that can be accessed in template

# File lib/pdqtest/skeleton.rb, line 100
def self.install_template(target, template_file, vars)
  if ! File.exists?(target)
    template = File.read(Util::resource_path(File.join('templates', template_file)))
    content  = ERB.new(template, nil, '-').result(binding)
    File.write(target, content)
  end
end
should_replace_file(target, skeleton) click to toggle source
# File lib/pdqtest/skeleton.rb, line 66
def self.should_replace_file(target, skeleton)
  target_hash   = Digest::SHA256.file target
  skeleton_hash = Digest::SHA256.file skeleton

  should = (target_hash != skeleton_hash)
  $logger.debug "should replace: #{should}"

  should
end
upgrade() click to toggle source

on upgrade, do a more limited skeleton copy - just our own integration points

# File lib/pdqtest/skeleton.rb, line 136
def self.upgrade
  install_skeleton('Makefile', 'Makefile')
  install_skeleton('make.ps1', 'make.ps1')
  install_skeleton('bitbucket-pipelines.yml', 'bitbucket-pipelines.yml')
  install_skeleton('.travis.yml', '.travis.yml')
  install_skeleton('appveyor.yml', 'appveyor.yml')
  install_skeleton('.ci_custom.sh', '.ci_custom.sh', false)
  Pdk.amend_sync_yml(SYNC_YML_CONTENT)
end