Class: Beaker::TestCase

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/beaker/test_case.rb

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::EZBakeUtils

DSL::InstallUtils::EZBakeUtils::LOCAL_COMMANDS_REQUIRED

Constants included from DSL::InstallUtils::ModuleUtils

DSL::InstallUtils::ModuleUtils::PUPPET_MODULE_INSTALL_IGNORE

Constants included from DSL::InstallUtils::FOSSUtils

DSL::InstallUtils::FOSSUtils::DEFAULT_DOWNLOAD_URLS, DSL::InstallUtils::FOSSUtils::GitHubSig, DSL::InstallUtils::FOSSUtils::GitURI, DSL::InstallUtils::FOSSUtils::SourcePath

Constants included from DSL::InstallUtils::FOSSDefaults

DSL::InstallUtils::FOSSDefaults::FOSS_DEFAULTS

Constants included from DSL::InstallUtils::AIODefaults

DSL::InstallUtils::AIODefaults::AIO_DEFAULTS

Constants included from DSL::InstallUtils::PEDefaults

DSL::InstallUtils::PEDefaults::PE_DEFAULTS

Instance Attribute Summary collapse

Attributes included from DSL::Assertions

#assertions

Instance Method Summary collapse

Methods included from DSL::Patterns

#block_on

Methods included from DSL::InstallUtils::EZBakeUtils

#conditionally_clone, #ezbake_config, #ezbake_dev_build, #ezbake_install_dir, #ezbake_install_name, #ezbake_installsh, #ezbake_lein_prefix, #ezbake_local_cmd, #ezbake_stage, #ezbake_tools_available?, #ezbake_validate_support, #install_ezbake_tarball_on_host, #install_from_ezbake, #install_termini_from_ezbake

Methods included from DSL::InstallUtils::ModuleUtils

#build_ignore_list, #copy_module_to, #get_module_name, #install_dev_puppet_module, #install_dev_puppet_module_on, #install_puppet_module_via_pmt, #install_puppet_module_via_pmt_on, #parse_for_modulename, #parse_for_moduleroot, #split_author_modulename

Methods included from DSL::InstallUtils::FOSSUtils

#compute_puppet_msi_name, #configure_foss_defaults_on, #configure_puppet, #configure_puppet_on, #extract_repo_info_from, #find_git_repo_versions, #install_a_puppet_msi_on, #install_cert_on_windows, #install_from_git, #install_packages_from_local_dev_repo, #install_puppet, #install_puppet_agent_from_dmg_on, #install_puppet_agent_from_msi_on, #install_puppet_agent_on, #install_puppet_from_deb_on, #install_puppet_from_dmg_on, #install_puppet_from_gem_on, #install_puppet_from_msi_on, #install_puppet_from_rpm_on, #install_puppet_on, #install_puppetagent_dev_repo, #install_puppetlabs_dev_repo, #install_puppetlabs_release_repo_on

Methods included from DSL::InstallUtils::PuppetUtils

#add_puppet_paths_on, #construct_puppet_path, #remove_puppet_paths_on

Methods included from DSL::InstallUtils::FOSSDefaults

#add_foss_defaults_on, #add_platform_foss_defaults, #remove_foss_defaults_on, #remove_platform_foss_defaults

Methods included from DSL::InstallUtils::AIODefaults

#add_aio_defaults_on, #add_platform_aio_defaults, #remove_aio_defaults_on, #remove_platform_aio_defaults

Methods included from DSL::InstallUtils::PEUtils

#configure_pe_defaults_on, #deploy_frictionless_to_master, #do_higgs_install, #do_install, #fetch_and_push_pe, #fetch_pe, #fetch_pe_on_mac, #fetch_pe_on_unix, #fetch_pe_on_windows, #higgs_installer_cmd, #install_higgs, #install_pe, #install_pe_on, #installer_cmd, #upgrade_pe, #upgrade_pe_on

Methods included from DSL::InstallUtils::PEDefaults

#add_pe_defaults_on, #add_platform_pe_defaults, #remove_pe_defaults_on, #remove_platform_pe_defaults

Methods included from DSL::Helpers::WebHelpers

#link_exists?, #port_open_within?

Methods included from DSL::Helpers::TKHelpers

#modify_tk_config

Methods included from DSL::Helpers::TestHelpers

#current_step_name, #current_test_filename, #current_test_name, #set_current_step_name, #set_current_test_filename, #set_current_test_name

Methods included from DSL::Helpers::PuppetHelpers

#apply_manifest, #apply_manifest_on, #create_tmpdir_for_user, #puppet_group, #puppet_user, #run_agent_on, #sign_certificate, #sign_certificate_for, #sleep_until_nc_started, #sleep_until_puppetdb_started, #sleep_until_puppetserver_started, #stop_agent, #stop_agent_on, #stub_forge, #stub_forge_on, #stub_hosts, #stub_hosts_on, #wait_for_host_in_dashboard, #with_forge_stubbed, #with_forge_stubbed_on, #with_host_stubbed_on, #with_puppet_running, #with_puppet_running_on

Methods included from DSL::Helpers::HostHelpers

#add_system32_hosts_entry, #check_for_package, #create_remote_file, #create_tmpdir_on, #curl_on, #curl_with_retries, #deploy_package_repo, #echo_on, #exit_code, #install_package, #on, #retry_on, #rsync_to, #run_script, #run_script_on, #scp_from, #scp_to, #shell, #stderr, #stdout, #upgrade_package

Methods included from DSL::Helpers::HieraHelpers

#copy_hiera_data, #copy_hiera_data_to, #hiera_datadir, #write_hiera_config, #write_hiera_config_on

Methods included from DSL::Helpers::FacterHelpers

#fact, #fact_on

Methods included from DSL::Wrappers

#cfacter, #facter, #hiera, #powershell, #puppet

Methods included from DSL::Assertions

#assert_no_match, #assert_output

Methods included from DSL::Structure

#confine, #confine_block, #expect_failure, #select_hosts, #step, #teardown, #test_name

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_at_most_one, #find_host_with_role, #find_only_one, #hosts_as, #master

Constructor Details

#initialize(these_hosts, logger, options = {}, path = nil) ⇒ TestCase

Returns a new instance of TestCase.

Parameters:

  • these_hosts (Hosts, Array<Host>)

    The hosts to execute this test against/on.

  • logger (Logger)

    A logger that implements Logger‘s interface.

  • options (Hash{Symbol=>String}) (defaults to: {})

    Parsed command line options.

  • path (String) (defaults to: nil)

    The local path to a test file to be executed.



93
94
95
96
97
98
99
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
164
165
166
167
168
169
170
171
172
# File 'lib/beaker/test_case.rb', line 93

def initialize(these_hosts, logger, options={}, path=nil)
  @hosts   = these_hosts
  @logger = logger
  @sublog = ""
  @options = options
  @path    = path
  @usr_home = options[:home]
  @test_status = :pass
  @exception = nil
  @runtime = nil
  @teardown_procs = []
  @metadata = {}
  set_current_test_filename(@path ? File.basename(@path, '.rb') : nil)


  #
  # 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

      set_current_step_name(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

#exceptionObject (readonly)

The exception that may have stopped this test’s execution.



68
69
70
# File 'lib/beaker/test_case.rb', line 68

def exception
  @exception
end

#fail_flagObject (readonly)

I don’t know why this is here



58
59
60
# File 'lib/beaker/test_case.rb', line 58

def fail_flag
  @fail_flag
end

#hostsObject

Necessary for implementing DSL::Helpers#confine. Assumed to be an array of valid Host objects for this test case.



30
31
32
# File 'lib/beaker/test_case.rb', line 30

def hosts
  @hosts
end

#last_resultObject

The result for the last command run



44
45
46
# File 'lib/beaker/test_case.rb', line 44

def last_result
  @last_result
end

#loggerObject

Necessary for many methods in DSL. Assumed to be an instance of Logger.



34
35
36
# File 'lib/beaker/test_case.rb', line 34

def logger
  @logger
end

#metadataObject

Necessary for many methods in DSL::Helpers. Assumed to be a hash.



38
39
40
# File 'lib/beaker/test_case.rb', line 38

def 
  @metadata
end

#optionsObject (readonly)

Parsed command line options.



52
53
54
# File 'lib/beaker/test_case.rb', line 52

def options
  @options
end

#pathObject (readonly)

The path to the file which contains this test case.



55
56
57
# File 'lib/beaker/test_case.rb', line 55

def path
  @path
end

#resultObject

Deprecated.

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.



85
86
87
# File 'lib/beaker/test_case.rb', line 85

def result
  @result
end

#runtimeObject (readonly)

Deprecated.

The amount of time taken to execute the test. Unused, probably soon to be removed or refactored.



73
74
75
# File 'lib/beaker/test_case.rb', line 73

def runtime
  @runtime
end

#sublogObject

The full log for this test



41
42
43
# File 'lib/beaker/test_case.rb', line 41

def sublog
  @sublog
end

#teardown_procsObject (readonly)

An Array of Procs to be called after test execution has stopped (whether by exception or not).



77
78
79
# File 'lib/beaker/test_case.rb', line 77

def teardown_procs
  @teardown_procs
end

#test_statusObject (readonly)

A Symbol denoting the status of this test (:fail, :pending, :skipped, :pass).



65
66
67
# File 'lib/beaker/test_case.rb', line 65

def test_status
  @test_status
end

#usr_homeObject (readonly)

The user that is running this tests home directory, needed by ‘net/ssh’.



61
62
63
# File 'lib/beaker/test_case.rb', line 61

def usr_home
  @usr_home
end

#versionObject (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.



49
50
51
# File 'lib/beaker/test_case.rb', line 49

def version
  @version
end

Instance Method Details

#to_hashHash

Note:

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

Returns:

  • (Hash)

    A Hash representation of this test.



180
181
182
183
184
185
186
187
# File 'lib/beaker/test_case.rb', line 180

def to_hash
  hash = {}
  hash['HOSTS'] = {}
  @hosts.each do |host|
    hash['HOSTS'][host.name] = host.overrides
  end
  hash
end