Class: Beaker::TestCase
Overview
This class represents a single test case. A test case is necessarily contained all in one file though may have multiple dependent examples. They are executed in order (save for any teardown procs registered through DSL::Structure#teardown) and once completed the status of the TestCase is saved. Instance readers/accessors provide the test case access to various details of the environment and suite the test case is running within.
See DSL for more information about writing tests using the DSL.
Constant Summary collapse
- TEST_EXCEPTION_CLASS =
The Exception raised by Ruby’s STDLIB’s test framework (Ruby 1.9)
::MiniTest::Assertion
Constants included from DSL::InstallUtils
DSL::InstallUtils::GitHubSig, DSL::InstallUtils::GitURI, DSL::InstallUtils::SourcePath
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
A Hash of values taken from host config file.
-
#exception ⇒ Object
readonly
The exception that may have stopped this test’s execution.
-
#fail_flag ⇒ Object
readonly
I don’t know why this is here.
-
#hosts ⇒ Object
Necessary for implementing DSL::Helpers#confine.
-
#logger ⇒ Object
Necessary for many methods in DSL.
-
#options ⇒ Object
readonly
Parsed command line options.
-
#path ⇒ Object
readonly
The path to the file which contains this test case.
- #result ⇒ Object deprecated Deprecated.
- #runtime ⇒ Object readonly deprecated Deprecated.
-
#teardown_procs ⇒ Object
readonly
An Array of Procs to be called after test execution has stopped (whether by exception or not).
-
#test_status ⇒ Object
readonly
A Symbol denoting the status of this test (:fail, :pending, :skipped, :pass).
-
#usr_home ⇒ Object
readonly
The user that is running this tests home directory, needed by ‘net/ssh’.
-
#version ⇒ Object
readonly
A Hash of ‘product name’ => ‘version installed’, only set when products are installed via git or PE install steps.
Instance Method Summary collapse
- #exit_code ⇒ Object deprecated Deprecated.
-
#forge ⇒ String
This method retrieves the forge hostname from either: * The environment variable ‘forge_host’ * The parameter ‘forge_host’ from the CONFIG hash in a node definition.
-
#initialize(these_hosts, logger, config, options = {}, path = nil) ⇒ TestCase
constructor
A new instance of TestCase.
- #stderr ⇒ Object deprecated Deprecated.
- #stdout ⇒ Object deprecated Deprecated.
-
#to_hash ⇒ Hash
The TestCase as a hash.
Methods included from DSL::InstallUtils
#do_install, #extract_repo_info_from, #fetch_puppet, #find_git_repo_versions, #install_from_git, #install_pe, #installer_cmd, #link_exists?, #upgrade_pe, #version_is_less
Methods included from DSL::Helpers
#apply_manifest_on, #check_for_package, #confine, #create_remote_file, #curl_with_retries, #install_package, #on, #port_open_within?, #retry_command, #run_agent_on, #run_script_on, #scp_from, #scp_to, #sign_certificate, #sleep_until_puppetdb_started, #stop_agent, #stub_forge_on, #stub_hosts_on, #wait_for_host_in_dashboard, #with_puppet_running_on
Methods included from DSL::Wrappers
Methods included from DSL::Assertions
Methods included from DSL::Structure
Methods included from DSL::Outcomes
#fail_test, #pass_test, #pending_test, #skip_test
Methods included from DSL::Roles
#agents, #dashboard, #database, #find_only_one, #hosts_as, #master
Constructor Details
#initialize(these_hosts, logger, config, options = {}, path = nil) ⇒ TestCase
100 101 102 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/beaker/test_case.rb', line 100 def initialize(these_hosts, logger, config, ={}, path=nil) @config = config['CONFIG'] @hosts = these_hosts @logger = logger @options = @path = path @usr_home = ENV['HOME'] @test_status = :pass @exception = nil @runtime = nil @teardown_procs = [] # # We put this on each wrapper (rather than the class) so that methods # defined in the tests don't leak out to other tests. class << self def run_test @runtime = Benchmark.realtime do begin test = File.read(path) eval test,nil,path,1 rescue FailTest, TEST_EXCEPTION_CLASS => e @test_status = :fail @exception = e rescue PendingTest @test_status = :pending rescue SkipTest @test_status = :skip rescue StandardError, ScriptError => e log_and_fail_test(e) ensure @teardown_procs.each do |teardown| begin teardown.call rescue StandardError => e log_and_fail_test(e) end end end end return self end private # Log an error and mark the test as failed, passing through an # exception so it can be displayed at the end of the total run. # # We break out the complete exception backtrace and log each line # individually as well. # # @param exception [Exception] exception to fail with def log_and_fail_test(exception) logger.error(exception.inspect) bt = exception.backtrace logger.pretty_backtrace(bt).each_line do |line| logger.error(line) end @test_status = :error @exception = exception end end end |
Instance Attribute Details
#config ⇒ Object (readonly)
A Hash of values taken from host config file.
55 56 57 |
# File 'lib/beaker/test_case.rb', line 55 def config @config end |
#exception ⇒ Object (readonly)
The exception that may have stopped this test’s execution.
74 75 76 |
# File 'lib/beaker/test_case.rb', line 74 def exception @exception end |
#fail_flag ⇒ Object (readonly)
I don’t know why this is here
64 65 66 |
# File 'lib/beaker/test_case.rb', line 64 def fail_flag @fail_flag end |
#hosts ⇒ Object
Necessary for implementing DSL::Helpers#confine. Assumed to be an array of valid Host objects for this test case.
43 44 45 |
# File 'lib/beaker/test_case.rb', line 43 def hosts @hosts end |
#logger ⇒ Object
47 48 49 |
# File 'lib/beaker/test_case.rb', line 47 def logger @logger end |
#options ⇒ Object (readonly)
Parsed command line options.
58 59 60 |
# File 'lib/beaker/test_case.rb', line 58 def @options end |
#path ⇒ Object (readonly)
The path to the file which contains this test case.
61 62 63 |
# File 'lib/beaker/test_case.rb', line 61 def path @path end |
#result ⇒ Object
Legacy accessor from when test files would only contain one remote action. Contains the Result of the last call to utilize DSL::Helpers#on. Do not use as it is not safe in test files that use multiple calls to DSL::Helpers#on.
91 92 93 |
# File 'lib/beaker/test_case.rb', line 91 def result @result end |
#runtime ⇒ Object (readonly)
The amount of time taken to execute the test. Unused, probably soon to be removed or refactored.
79 80 81 |
# File 'lib/beaker/test_case.rb', line 79 def runtime @runtime end |
#teardown_procs ⇒ Object (readonly)
An Array of Procs to be called after test execution has stopped (whether by exception or not).
83 84 85 |
# File 'lib/beaker/test_case.rb', line 83 def teardown_procs @teardown_procs end |
#test_status ⇒ Object (readonly)
A Symbol denoting the status of this test (:fail, :pending, :skipped, :pass).
71 72 73 |
# File 'lib/beaker/test_case.rb', line 71 def test_status @test_status end |
#usr_home ⇒ Object (readonly)
The user that is running this tests home directory, needed by ‘net/ssh’.
67 68 69 |
# File 'lib/beaker/test_case.rb', line 67 def usr_home @usr_home end |
#version ⇒ Object (readonly)
A Hash of ‘product name’ => ‘version installed’, only set when products are installed via git or PE install steps. See the ‘git’ or ‘pe’ directories within ‘ROOT/setup’ for examples.
52 53 54 |
# File 'lib/beaker/test_case.rb', line 52 def version @version end |
Instance Method Details
#exit_code ⇒ Object
An proxy for the last Result#exit_code returned by a method that makes remote calls. Use the Result object returned by the method directly instead. For Usage see Result.
206 207 208 209 |
# File 'lib/beaker/test_case.rb', line 206 def exit_code return nil if @result.nil? @result.exit_code end |
#forge ⇒ String
This method retrieves the forge hostname from either:
-
The environment variable ‘forge_host’
-
The parameter ‘forge_host’ from the CONFIG hash in a node definition
If none of these are available, it falls back to the static ‘vulcan-acceptance.delivery.puppetlabs.net’ hostname
219 220 221 222 223 |
# File 'lib/beaker/test_case.rb', line 219 def forge ENV['forge_host'] || @config['forge_host'] || 'vulcan-acceptance.delivery.puppetlabs.net' end |
#stderr ⇒ Object
An proxy for the last Result#stderr returned by a method that makes remote calls. Use the Result object returned by the method directly instead. For Usage see Result.
196 197 198 199 |
# File 'lib/beaker/test_case.rb', line 196 def stderr return nil if @result.nil? @result.stderr end |
#stdout ⇒ Object
An proxy for the last Result#stdout returned by a method that makes remote calls. Use the Result object returned by the method directly instead. For Usage see Result.
186 187 188 189 |
# File 'lib/beaker/test_case.rb', line 186 def stdout return nil if @result.nil? @result.stdout end |
#to_hash ⇒ Hash
The visibility and semantics of this method are valid, but the structure of the Hash it returns may change without notice
The TestCase as a hash
171 172 173 174 175 176 177 178 179 |
# File 'lib/beaker/test_case.rb', line 171 def to_hash hash = {} hash['HOSTS'] = {} hash['CONFIG'] = @config @hosts.each do |host| hash['HOSTS'][host.name] = host.overrides end hash end |