Class: PDQTest::Puppet
- Inherits:
-
Object
- Object
- PDQTest::Puppet
- Defined in:
- lib/pdqtest/puppet.rb
Constant Summary collapse
- METADATA =
'metadata.json'- MODULE_DIR =
'/etc/puppetlabs/code/modules'- MAGIC_MARKER =
'@PDQTest'- MAGIC_MARKER_RE =
/#\s*#{MAGIC_MARKER}/- BATS_TESTS =
'./spec/acceptance'- SETUP_SUFFIX =
'__setup.sh'- BEFORE_SUFFIX =
'__before.bats'- AFTER_SUFFIX =
'.bats'- EXAMPLES_DIR =
'./examples'- MANIFESTS_DIR =
'./manifests'- CLASS_RE =
/^class /- FIXTURES =
'fixtures.yml'- @@bats_executed =
[]
- @@setup_executed =
[]
Class Method Summary collapse
- .bats_test(container, example, suffix) ⇒ Object
- .class2filename(c) ⇒ Object
- .filename2class(f) ⇒ Object
-
.find_classes ⇒ Object
find the available classes in this module.
- .find_examples ⇒ Object
- .get_bats_executed ⇒ Object
- .get_setup_executed ⇒ Object
-
.git_fixtures ⇒ Object
process fixtures->repositories->* from fixtures.yml if present to generate an array of commands to run ON THE DOCKER VM to checkout the required modules from git.
- .info ⇒ Object
-
.link_deps ⇒ Object
Link all modules - this also saves re-downloading in the acceptance test environment.
-
.link_merge_facts ⇒ Object
link /etc/facter/facts.d to /testcase/spec/merge_facts to allow additional facts supplied by user to work automatically.
- .link_module ⇒ Object
- .module_metadata ⇒ Object
- .module_name ⇒ Object
- .puppet_apply(example) ⇒ Object
- .reset_bats_executed ⇒ Object
- .reset_setup_executed ⇒ Object
- .run(container, example = nil) ⇒ Object
- .run_example(container, example) ⇒ Object
- .setup_test(container, example) ⇒ Object
- .test_basename(t) ⇒ Object
Class Method Details
.bats_test(container, example, suffix) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/pdqtest/puppet.rb', line 158 def self.bats_test(container, example, suffix) testcase = BATS_TESTS + '/' + test_basename(example) + suffix if File.exists?(testcase) Escort::Logger.output.puts "*** bats test **** bats #{PDQTest::Instance::TEST_DIR}/#{testcase}" res = PDQTest::Docker.exec(container, "bats #{PDQTest::Instance::TEST_DIR}/#{testcase}") status = PDQTest::Docker.exec_status(res) PDQTest::Docker.log_out(res) @@bats_executed << testcase else Escort::Logger.error.error "no #{suffix} tests for #{example} (should be at #{testcase})" status = true end status end |
.class2filename(c) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/pdqtest/puppet.rb', line 67 def self.class2filename(c) if c == module_name f = "#{MANIFESTS_DIR}/init.pp" else f = c.gsub(module_name, MANIFESTS_DIR).gsub('::', File::SEPARATOR) + '.pp' end f end |
.filename2class(f) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/pdqtest/puppet.rb', line 77 def self.filename2class(f) if f == "#{MANIFESTS_DIR}/init.pp" c = module_name else c = f.gsub(MANIFESTS_DIR, "#{module_name}").gsub(File::SEPARATOR, '::').gsub('.pp','') end c end |
.find_classes ⇒ Object
find the available classes in this module
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/pdqtest/puppet.rb', line 131 def self.find_classes() mod_name = module_name classes = [] if Dir.exists?(MANIFESTS_DIR) Find.find(MANIFESTS_DIR) do |m| if m =~ /\.pp$/ # check the file contains a valid class if ! File.readlines(m).grep(CLASS_RE).empty? # Class detected, work out class name and add to list of found classes classes << filename2class(m) else Escort::Logger.output.puts "no puppet class found in #{m}" end end end end classes end |
.find_examples ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/pdqtest/puppet.rb', line 87 def self.find_examples() examples = [] if Dir.exists?(EXAMPLES_DIR) Find.find(EXAMPLES_DIR) do |e| if ! File.directory?(e) and ! File.readlines(e).grep(MAGIC_MARKER_RE).empty? examples << e end end end Escort::Logger.output.puts "examples to run" + examples.to_s examples end |
.get_bats_executed ⇒ Object
33 34 35 |
# File 'lib/pdqtest/puppet.rb', line 33 def self.get_bats_executed @@bats_executed end |
.get_setup_executed ⇒ Object
37 38 39 |
# File 'lib/pdqtest/puppet.rb', line 37 def self.get_setup_executed @@setup_executed end |
.git_fixtures ⇒ Object
process fixtures->repositories->* from fixtures.yml if present to generate an array of commands to run ON THE DOCKER VM to checkout the required modules from git
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/pdqtest/puppet.rb', line 103 def self.git_fixtures() refresh_cmd = [] if File.exists?(FIXTURES) fixtures = YAML.load_file(FIXTURES) if fixtures.has_key?('repositories') fixtures['repositories'].each { |fixture, opts| target = "spec/fixtures/modules/#{fixture}" if opts.instance_of?(String) source = opts ref = 'master' elsif opts.instance_of?(Hash) source = opts['repo'] if opts.has_key? 'ref' ref = opts['ref'] else ref = 'master' end end refresh_cmd << "git_refresh refresh --target-dir #{target} --source-url #{source} --ref #{ref}" } end end refresh_cmd end |
.info ⇒ Object
298 299 300 301 |
# File 'lib/pdqtest/puppet.rb', line 298 def self.info Escort::Logger.output.puts "Parsed module name: #{module_name}" Escort::Logger.output.puts "Link module command: #{link_module}" end |
.link_deps ⇒ Object
Link all modules - this also saves re-downloading in the acceptance test environment. Of course it means that you must have already run ‘make` to download the modules on your host computer
57 58 59 |
# File 'lib/pdqtest/puppet.rb', line 57 def self.link_deps "test -e #{MODULE_DIR} || mkdir -p #{MODULE_DIR} && ln -s #{PDQTest::Instance::TEST_DIR}/spec/fixtures/modules/* #{MODULE_DIR}" end |
.link_merge_facts ⇒ Object
link /etc/facter/facts.d to /testcase/spec/merge_facts to allow additional facts supplied by user to work automatically
63 64 65 |
# File 'lib/pdqtest/puppet.rb', line 63 def self.link_merge_facts "mkdir -p /etc/facter/ && ln -s #{PDQTest::Instance::TEST_DIR}/spec/merge_facts /etc/facter/facts.d" end |
.link_module ⇒ Object
50 51 52 |
# File 'lib/pdqtest/puppet.rb', line 50 def self.link_module "test -e #{MODULE_DIR} || mkdir -p #{MODULE_DIR} && ln -s #{PDQTest::Instance::TEST_DIR} #{MODULE_DIR}/#{module_name}" end |
.module_metadata ⇒ Object
41 42 43 44 |
# File 'lib/pdqtest/puppet.rb', line 41 def self. file = File.read(Dir.pwd + File::SEPARATOR + METADATA) JSON.parse(file) end |
.module_name ⇒ Object
46 47 48 |
# File 'lib/pdqtest/puppet.rb', line 46 def self.module_name ['name'].split(/[\/-]/)[1] end |
.puppet_apply(example) ⇒ Object
294 295 296 |
# File 'lib/pdqtest/puppet.rb', line 294 def self.puppet_apply(example) "cd #{PDQTest::Instance::TEST_DIR} && puppet apply --detailed-exitcodes #{example}" end |
.reset_bats_executed ⇒ Object
25 26 27 |
# File 'lib/pdqtest/puppet.rb', line 25 def self.reset_bats_executed @@bats_executed = [] end |
.reset_setup_executed ⇒ Object
29 30 31 |
# File 'lib/pdqtest/puppet.rb', line 29 def self.reset_setup_executed @@setup_executed = [] end |
.run(container, example = nil) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/pdqtest/puppet.rb', line 234 def self.run(container, example=nil) # we must always have ./spec/fixtures/modules because we need to create a # symlink back to the main module inside here... # (spec/fixtures/modules/foo -> /testcase) if ! Dir.exists?('spec/fixtures/modules') Escort::Logger.output.puts "creating empty spec/fixtures/modules, if you module fails to run due " "to missing dependencies run `make` or `pdqtest all` to retrieve them" FileUtils.mkdir_p('spec/fixtures/modules') end status = true Escort::Logger.output.puts "...linking dependencies" cmd = link_deps res = PDQTest::Docker.exec(container, cmd) status &= PDQTest::Docker.exec_status(res) if status Escort::Logger.output.puts "...linking testcase (this module)" cmd = link_module res = PDQTest::Docker.exec(container, cmd) status &= PDQTest::Docker.exec_status(res) if status Escort::Logger.output.puts "...linking spec/merge_facts" cmd = link_merge_facts res = PDQTest::Docker.exec(container, cmd) status &= PDQTest::Docker.exec_status(res) if status Escort::Logger.output.puts "...run tests" if example status &= run_example(container, example) if ! status Escort::Logger.error.error "Example #{example} failed!" end else find_examples.each { |e| if status status &= run_example(container, e) if ! status Escort::Logger.error.error "Example #{e} failed! - skipping rest of tests" end end } end else PDQTest::Docker.log_all(res) Escort::Logger.error.error "Error linking ./spec/merge_facts directory, see previous error, command was: #{cmd}" end else PDQTest::Docker.log_all(res) Escort::Logger.error.error "Error linking testcase (this) module, see previous error, command was: #{cmd}" end else PDQTest::Docker.log_all(res) Escort::Logger.error.error "Error linking module, see previous error, command was: #{cmd}" end PDQTest::Emoji.partial_status(status, 'Puppet') status end |
.run_example(container, example) ⇒ Object
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 |
# File 'lib/pdqtest/puppet.rb', line 192 def self.run_example(container, example) if ! example.start_with?('./') # must prepend ./ to the example or we will not match the correct regexp # in test_basename example = "./#{example}" end Escort::Logger.output.puts "testing #{example}" status = false if setup_test(container, example) # see if we should run a bats test before running puppet if bats_test(container, example, BEFORE_SUFFIX) # run puppet apply - 1st run res = PDQTest::Docker.exec(container, puppet_apply(example)) PDQTest::Docker.log_out(res) if PDQTest::Docker.exec_status(res, true) # allow 2 as exit status # run puppet apply - 2nd run (check for idempotencey/no more changes) res = PDQTest::Docker.exec(container, puppet_apply(example)) PDQTest::Docker.log_out(res) # run the bats test if nothing failed yet if PDQTest::Docker.exec_status(res) # only allow 0 as exit status status = bats_test(container, example, AFTER_SUFFIX) else Escort::Logger.error.error "Not idempotent: #{example}" end else Escort::Logger.error.error "First puppet run of #{example} failed" end else Escort::Logger.error.error "Bats tests to run before #{example} failed" end else Escort::Logger.error.error "Setup script for #{example} failed" end status end |
.setup_test(container, example) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/pdqtest/puppet.rb', line 174 def self.setup_test(container, example) setup = BATS_TESTS + '/' + test_basename(example) + SETUP_SUFFIX if File.exists?(setup) Escort::Logger.output.puts "Setting up test for #{example}" script = File.read(setup) res = PDQTest::Docker.exec(container, script) status = PDQTest::Docker.exec_status(res) PDQTest::Docker.log_out(res) @@setup_executed << setup else Escort::Logger.output.puts "no setup file for #{example} (should be in #{setup})" status = true end status end |
.test_basename(t) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/pdqtest/puppet.rb', line 151 def self.test_basename(t) # remove examples/ and .pp # eg ./examples/apache/mod/mod_php.pp --> apache/mod/mod_php t.gsub(EXAMPLES_DIR + '/','').gsub('.pp','') end |