Module: Beaker::DSL::Structure
- Included in:
- Beaker::DSL
- Defined in:
- lib/beaker/dsl/structure.rb
Overview
These are simple structural elements necessary for writing understandable tests and ensuring cleanup actions happen. If using a third party test runner they are unnecessary.
To include this in your own test runner a method #logger should be available to yield a logger that implements Logger‘s interface. As well as a method #teardown_procs that yields an array.
Instance Method Summary collapse
-
#confine(type, criteria, host_array = nil, &block) ⇒ Array<Host>
Limit the hosts a test case is run against.
-
#confine_block(type, criteria, host_array = nil) ⇒ Object
Ensures that host restrictions as specifid by type, criteria and host_array are confined to activity within the passed block.
-
#expect_failure(explanation) ⇒ Object
Wrap an assert that is supposed to fail due to a product bug, an undelivered feature, or some similar situation.
-
#manual_step(step_name) ⇒ Object
Provides a method to help manual tests.
-
#manual_test(manual_test_name, &block) ⇒ Object
Provides a method to mark manual tests.
-
#select_hosts(criteria, host_array = nil) ⇒ Array<Host>
Return a set of hosts that meet the given criteria.
-
#step(step_name, &block) ⇒ Object
Provides a method to help structure tests into coherent steps.
-
#teardown(&block) ⇒ Object
Declare a teardown process that will be called after a test case is complete.
-
#test_name(my_name) ⇒ Object
Provides a method to name tests.
Instance Method Details
#confine(type, criteria, host_array = nil, &block) ⇒ Array<Host>
This will modify the TestCase#hosts member in place unless an array of hosts is passed into it and TestCase#logger yielding an object that responds like Logger#warn, as well as Outcomes#skip_test, and optionally TestCase#hosts.
Limit the hosts a test case is run against
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/beaker/dsl/structure.rb', line 270 def confine(type, criteria, host_array = nil, &block) hosts_to_modify = Array( host_array || hosts ) hosts_not_modified = hosts - hosts_to_modify #we aren't examining these hosts case type when :except if criteria and ( not criteria.empty? ) hosts_to_modify = hosts_to_modify - select_hosts(criteria, hosts_to_modify, &block) + hosts_not_modified else # confining to all hosts *except* provided array of hosts hosts_to_modify = hosts_not_modified end if hosts_to_modify.empty? logger.warn "No suitable hosts without: #{criteria.inspect}" skip_test "No suitable hosts found without #{criteria.inspect}" end when :to if criteria and ( not criteria.empty? ) hosts_to_modify = select_hosts(criteria, hosts_to_modify, &block) + hosts_not_modified else # confining to only hosts in provided array of hosts end if hosts_to_modify.empty? logger.warn "No suitable hosts with: #{criteria.inspect}" skip_test "No suitable hosts found with #{criteria.inspect}" end else raise "Unknown option #{type}" end self.hosts = hosts_to_modify hosts_to_modify end |
#confine_block(type, criteria, host_array = nil) ⇒ Object
Ensures that host restrictions as specifid by type, criteria and host_array are confined to activity within the passed block. TestCase#hosts is reset after block has executed.
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/beaker/dsl/structure.rb', line 307 def confine_block(type, criteria, host_array = nil) host_array = Array( host_array || hosts ) original_hosts = self.hosts.dup confine(type, criteria, host_array) yield rescue Beaker::DSL::Outcomes::SkipTest => e # I don't like this much, but adding options to confine is a breaking change # to the DSL that would involve a major version bump if !e..include?('No suitable hosts found') # a skip generated from the provided block, pass it up the chain raise e end ensure self.hosts = original_hosts end |
#expect_failure(explanation) ⇒ Object
Wrap an assert that is supposed to fail due to a product bug, an undelivered feature, or some similar situation.
This converts failing asserts into passing asserts (so we can continue to run the test even though there are underlying product bugs), and converts passing asserts into failing asserts (so we know when the underlying product bug has been fixed).
Pass an assert as a code block, and pass an explanatory message as a parameter. The assert’s logic will be inverted (so passes turn into fails and fails turn into passes).
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/beaker/dsl/structure.rb', line 193 def expect_failure(explanation) begin yield if block_given? # code block should contain an assert that you expect to fail rescue Beaker::DSL::Assertions, Minitest::Assertion => e # Yay! The assert in the code block failed, as expected. # Swallow the failure so the test passes. logger.notify 'An assertion was expected to fail, and did. ' + 'This is probably due to a known product bug, ' + 'and is probably not a problem. ' + "Additional info: '#{explanation}' " + "Failed assertion: '#{e}'" return end # Uh-oh! The assert in the code block unexpectedly passed. fail('An assertion was expected to fail, but passed. ' + 'This is probably because a product bug was fixed, and ' + '"expect_failure()" needs to be removed from this test. ' + "Additional info: '#{explanation}'") end |
#manual_step(step_name) ⇒ Object
Provides a method to help manual tests. So we can use beaker to set up the environment, then prompt a user to manually check the setup.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/beaker/dsl/structure.rb', line 72 def manual_step step_name require 'readline' logger.notify "\n* #{step_name}\n" if(!@options.has_key?(:exec_manual_tests)) # if the option -exec-manual-tests is not set then this has executed outside of a manual tests # so we raise an error to avoid issues raise('--exec-manual-tests option not set, this means a manual_step was used outside a manual_test') end set_current_step_name(step_name) # Here we prompt the user to tell us if the step passed or failed loop do input = Readline.readline('Did this step pass, Y/n? ', true).squeeze(" ").strip.downcase if %w(y yes).include?(input) break elsif %w(n no).include?(input) # if the step failed, the user can enter a fail message. # we loops to ensure they give use a fail message = '' loop do = Readline.readline('What was the reason for failure? ', true).squeeze(" ").strip if == '' # if nothing is entered we tell the user to enter something puts "No reason for failure given, please enter reason for failure." else break end end raise Beaker::DSL::FailTest, else # if something other than Y or n is returned we ask them again puts "Please enter Y or n." end end end |
#manual_test(manual_test_name, &block) ⇒ Object
Provides a method to mark manual tests. If the –exec-manual-tests param is not set then we skip the test this is so manual tests do not execute by mistake
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/beaker/dsl/structure.rb', line 114 def manual_test manual_test_name, &block if(@options.has_key?(:exec_manual_tests) && @options[:exec_manual_tests] == true) # here the option is set so we run the test as normal test_name manual_test_name, &block else # here no option was set so we log the test name and skip it test_name manual_test_name raise( Beaker::DSL::SkipTest, '--exec-manual-tests option not set, so skipping manual test' ) end end |
#select_hosts(criteria, host_array = nil) ⇒ Array<Host>
Return a set of hosts that meet the given criteria
343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/beaker/dsl/structure.rb', line 343 def select_hosts(criteria, host_array = nil) hosts_to_select_from = host_array || hosts criteria.each_pair do |property, value| hosts_to_select_from = hosts_to_select_from.select do |host| inspect_host host, property, value end end if block_given? hosts_to_select_from = hosts_to_select_from.select do |host| yield host end end hosts_to_select_from end |
#step(step_name, &block) ⇒ Object
Provides a method to help structure tests into coherent steps.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/beaker/dsl/structure.rb', line 37 def step step_name, &block logger.notify "\n* #{step_name}\n" set_current_step_name(step_name) if block begin logger.with_indent do yield end rescue Exception => e if(@options.has_key?(:debug_errors) && @options[:debug_errors] == true) begin require 'pry' rescue LoadError begin require 'debug' rescue LoadError logger.exception('Unable to load pry and debug while debug_errors was true') else logger.info("Exception raised during step execution and debug-errors option is set, entering debug. Exception was: #{e.inspect}") binding.break end else logger.info("Exception raised during step execution and debug-errors option is set, entering pry. Exception was: #{e.inspect}") logger.info("HINT: Use the pry 'backtrace' and 'up' commands to navigate to the test code") binding.pry # rubocop:disable Lint/Debugger end end raise e end end end |
#teardown(&block) ⇒ Object
Declare a teardown process that will be called after a test case is complete.
150 151 152 |
# File 'lib/beaker/dsl/structure.rb', line 150 def teardown &block @teardown_procs << block end |
#test_name(my_name) ⇒ Object
Provides a method to name tests.
131 132 133 134 135 136 137 138 139 |
# File 'lib/beaker/dsl/structure.rb', line 131 def test_name my_name logger.notify "\n#{my_name}\n" set_current_test_name(my_name) if block_given? logger.with_indent do yield end end end |