Module: PDQTest::Skeleton

Defined in:
lib/pdqtest/skeleton.rb

Constant Summary collapse

TEMP_PDK_MODULE =
"x"
FIXTURES =
'.fixtures.yml'
SPEC_DIR =
'spec'
ACCEPTANCE_DIR =
File.join(SPEC_DIR, 'acceptance')
SKELETON_DIR =
'skeleton'
EXAMPLES_DIR =
'examples'
HIERA_DIR =
File.join(SPEC_DIR, 'fixtures', 'hieradata')
HIERA_YAML =
'hiera.yaml'
HIERA_TEST =
'test.yaml'
PDK_FILES =
[
    "spec/spec_helper.rb",
    "spec/default_facts.yml",
    ".pdkignore",
    "Gemfile",
    "Rakefile",
    ".gitignore",
    ".gitattributes",
]
SYNC_YML_CONTENT =

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

{
    ".travis.yml" => {
        "unmanaged" => true,
    },
    "bitbucket-pipelines.yml" => {
        "unmanaged" => true,
    },
    "appveyor.yml" => {
        "unmanaged" => true,
    },
    ".gitignore" => {
        "paths" => [
            ".Puppetfile.pdqtest",
            "refresh.ps1",
        ],
    },
    ".gitattributes" => {
        "include" => {
            "*.epp" => "eol=lf",
            "*.json"=> "eol=lf",
            "*.yaml"=> "eol=lf",
            "*.yml" => "eol=lf",
            "*.md"  => "eol=lf",
        }
    }
}.freeze
@@pdk_metadata =

PDK adds custom metadata fields which we can ONLY get by creating a new module. We already do this so stash the details here when we have them

{}

Class Method Summary collapse

Class Method Details

.directory_structureObject



115
116
117
118
119
# 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) ⇒ Object

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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 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

.initObject



121
122
123
124
125
126
127
128
129
130
131
132
# 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") ⇒ Object



146
147
148
149
150
151
152
153
154
155
# 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_projectObject



108
109
110
111
112
113
# 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_skeletonsObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# 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.

            # 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) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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_skeletonsObject



76
77
78
# 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) ⇒ Object

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



100
101
102
103
104
105
106
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
# 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

.upgradeObject

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



136
137
138
139
140
141
142
143
144
# 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