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
Constants included from DSL::EZBakeUtils
DSL::EZBakeUtils::LOCAL_COMMANDS_REQUIRED, DSL::EZBakeUtils::REMOTE_PACKAGES_REQUIRED
Constants included from DSL::Helpers
DSL::Helpers::PUPPET_MODULE_INSTALL_IGNORE
Instance Attribute Summary collapse
-
#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.
-
#last_result ⇒ Object
The result for the last command run.
-
#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.
-
#sublog ⇒ Object
The full log for this test.
-
#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
-
#initialize(these_hosts, logger, options = {}, path = nil) ⇒ TestCase
constructor
A new instance of TestCase.
-
#to_hash ⇒ Hash
The TestCase as a hash.
Methods included from DSL::Patterns
Methods included from DSL::InstallUtils
#deploy_frictionless_to_master, #do_higgs_install, #do_install, #extract_repo_info_from, #fetch_puppet, #fetch_puppet_on_mac, #fetch_puppet_on_unix, #fetch_puppet_on_windows, #find_git_repo_versions, #higgs_installer_cmd, #install_from_git, #install_higgs, #install_pe, #install_puppet, #install_puppet_from_deb, #install_puppet_from_dmg, #install_puppet_from_gem, #install_puppet_from_msi, #install_puppet_from_rpm, #install_puppetlabs_dev_repo, #install_puppetlabs_release_repo, #installer_cmd, #link_exists?, #upgrade_pe
Methods included from DSL::EZBakeUtils
#ezbake_config, #ezbake_stage, #ezbake_tools_available?, #install_ezbake_deps, #install_from_ezbake
Methods included from DSL::Helpers
#apply_manifest, #apply_manifest_on, #build_ignore_list, #check_for_package, #confine, #confine_block, #copy_module_to, #create_remote_file, #create_tmpdir_for_user, #curl_on, #curl_with_retries, #deploy_package_repo, #exit_code, #fact, #fact_on, #get_module_name, #install_package, #modify_tk_config, #on, #parse_for_modulename, #parse_for_moduleroot, #port_open_within?, #puppet_group, #puppet_module_install, #puppet_module_install_on, #puppet_user, #retry_command, #run_agent_on, #run_script, #run_script_on, #scp_from, #scp_to, #select_hosts, #shell, #sign_certificate, #sign_certificate_for, #sleep_until_puppetdb_started, #split_author_modulename, #stderr, #stdout, #stop_agent, #stop_agent_on, #stub_forge, #stub_forge_on, #stub_hosts, #stub_hosts_on, #upgrade_package, #version_is_less, #wait_for_host_in_dashboard, #with_puppet_running, #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
#add_role_def, #agents, #any_hosts_as?, #dashboard, #database, #default, #find_only_one, #hosts_as, #master
Constructor Details
#initialize(these_hosts, logger, options = {}, path = nil) ⇒ TestCase
Returns a new instance of TestCase.
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/beaker/test_case.rb', line 108 def initialize(these_hosts, logger, ={}, path=nil) @hosts = these_hosts @logger = logger @sublog = "" @options = @path = path @usr_home = [: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 @logger.start_sublog @logger.last_result = nil #add arbitrary role methods roles = [] @hosts.each do |host| roles << host[:roles] end add_role_def( roles.flatten.uniq ) @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, SignalException => e log_and_fail_test(e) ensure @teardown_procs.each do |teardown| begin teardown.call rescue StandardError, SignalException => e log_and_fail_test(e) end end end end @sublog = @logger.get_sublog @last_result = @logger.last_result 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
#exception ⇒ Object (readonly)
The exception that may have stopped this test’s execution.
83 84 85 |
# File 'lib/beaker/test_case.rb', line 83 def exception @exception end |
#fail_flag ⇒ Object (readonly)
I don’t know why this is here
73 74 75 |
# File 'lib/beaker/test_case.rb', line 73 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.
49 50 51 |
# File 'lib/beaker/test_case.rb', line 49 def hosts @hosts end |
#last_result ⇒ Object
The result for the last command run
59 60 61 |
# File 'lib/beaker/test_case.rb', line 59 def last_result @last_result end |
#logger ⇒ Object
53 54 55 |
# File 'lib/beaker/test_case.rb', line 53 def logger @logger end |
#options ⇒ Object (readonly)
Parsed command line options.
67 68 69 |
# File 'lib/beaker/test_case.rb', line 67 def @options end |
#path ⇒ Object (readonly)
The path to the file which contains this test case.
70 71 72 |
# File 'lib/beaker/test_case.rb', line 70 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.
100 101 102 |
# File 'lib/beaker/test_case.rb', line 100 def result @result end |
#runtime ⇒ Object (readonly)
The amount of time taken to execute the test. Unused, probably soon to be removed or refactored.
88 89 90 |
# File 'lib/beaker/test_case.rb', line 88 def runtime @runtime end |
#sublog ⇒ Object
The full log for this test
56 57 58 |
# File 'lib/beaker/test_case.rb', line 56 def sublog @sublog end |
#teardown_procs ⇒ Object (readonly)
An Array of Procs to be called after test execution has stopped (whether by exception or not).
92 93 94 |
# File 'lib/beaker/test_case.rb', line 92 def teardown_procs @teardown_procs end |
#test_status ⇒ Object (readonly)
A Symbol denoting the status of this test (:fail, :pending, :skipped, :pass).
80 81 82 |
# File 'lib/beaker/test_case.rb', line 80 def test_status @test_status end |
#usr_home ⇒ Object (readonly)
The user that is running this tests home directory, needed by ‘net/ssh’.
76 77 78 |
# File 'lib/beaker/test_case.rb', line 76 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.
64 65 66 |
# File 'lib/beaker/test_case.rb', line 64 def version @version end |
Instance Method Details
#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
191 192 193 194 195 196 197 198 |
# File 'lib/beaker/test_case.rb', line 191 def to_hash hash = {} hash['HOSTS'] = {} @hosts.each do |host| hash['HOSTS'][host.name] = host.overrides end hash end |