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'
- @@bats_executed =
[]
- @@setup_executed =
[]
Class Method Summary collapse
- .bats_test(container, example, suffix) ⇒ Object
- .find_examples ⇒ Object
- .get_bats_executed ⇒ Object
- .get_setup_executed ⇒ Object
- .install_deps ⇒ Object
- .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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/pdqtest/puppet.rb', line 74 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 |
.find_examples ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pdqtest/puppet.rb', line 54 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
28 29 30 |
# File 'lib/pdqtest/puppet.rb', line 28 def self.get_bats_executed @@bats_executed end |
.get_setup_executed ⇒ Object
32 33 34 |
# File 'lib/pdqtest/puppet.rb', line 32 def self.get_setup_executed @@setup_executed end |
.install_deps ⇒ Object
49 50 51 52 |
# File 'lib/pdqtest/puppet.rb', line 49 def self.install_deps # Install dependencies for module "cd #{PDQTest::Instance::TEST_DIR} && librarian-puppet install --path #{MODULE_DIR} --destructive" end |
.link_module ⇒ Object
45 46 47 |
# File 'lib/pdqtest/puppet.rb', line 45 def self.link_module "mkdir -p #{MODULE_DIR} && ln -s #{PDQTest::Instance::TEST_DIR} #{MODULE_DIR}/#{module_name}" end |
.module_metadata ⇒ Object
36 37 38 39 |
# File 'lib/pdqtest/puppet.rb', line 36 def self. file = File.read(Dir.pwd + File::SEPARATOR + METADATA) JSON.parse(file) end |
.module_name ⇒ Object
41 42 43 |
# File 'lib/pdqtest/puppet.rb', line 41 def self.module_name ['name'].split('-')[1] end |
.puppet_apply(example) ⇒ Object
184 185 186 |
# File 'lib/pdqtest/puppet.rb', line 184 def self.puppet_apply(example) "cd #{PDQTest::Instance::TEST_DIR} && puppet apply #{example}" end |
.reset_bats_executed ⇒ Object
20 21 22 |
# File 'lib/pdqtest/puppet.rb', line 20 def self.reset_bats_executed @@bats_executed = [] end |
.reset_setup_executed ⇒ Object
24 25 26 |
# File 'lib/pdqtest/puppet.rb', line 24 def self.reset_setup_executed @@setup_executed = [] end |
.run(container, example = nil) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/pdqtest/puppet.rb', line 150 def self.run(container, example=nil) status = true Escort::Logger.output.puts "fetch deps" res = PDQTest::Docker.exec(container, install_deps) status &= PDQTest::Docker.exec_status(res) Escort::Logger.output.puts "linking" res = PDQTest::Docker.exec(container, link_module) status &= PDQTest::Docker.exec_status(res) Escort::Logger.output.puts "run tests" if example status &= run_example(container, example) else find_examples.each { |e| status &= run_example(container, e) # Escort::Logger.output.puts "testing #{e} #{status}" # # status &= setup_test(container, e) # # # see if we should run a bats test before running puppet # status &= bats_test(container, e, BEFORE_SUFFIX) # # # run puppet apply # res = PDQTest::Docker.exec(container, puppet_apply(e)) # status &= PDQTest::Docker.exec_status(res, true) # Escort::Logger.output.puts res # # # see if we should run a bats test after running puppet # status &= bats_test(container, e, AFTER_SUFFIX) } end status end |
.run_example(container, example) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/pdqtest/puppet.rb', line 108 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
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/pdqtest/puppet.rb', line 90 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
67 68 69 70 71 72 |
# File 'lib/pdqtest/puppet.rb', line 67 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 |