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

DSL::InstallUtils::GitHubSig, DSL::InstallUtils::GitURI, DSL::InstallUtils::SourcePath

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL::InstallUtils

#deploy_frictionless_to_master, #do_install, #extract_repo_info_from, #fetch_puppet, #find_git_repo_versions, #install_from_git, #install_pe, #install_puppet, #installer_cmd, #link_exists?, #upgrade_pe

Methods included from DSL::Helpers

#apply_manifest, #apply_manifest_on, #check_for_package, #confine, #confine_block, #create_remote_file, #curl_with_retries, #deploy_package_repo, #exit_code, #fact, #fact_on, #install_package, #on, #port_open_within?, #retry_command, #run_agent_on, #run_script, #run_script_on, #scp_from, #scp_to, #shell, #sign_certificate, #sign_certificate_for, #sleep_until_puppetdb_started, #stderr, #stdout, #stop_agent, #stop_agent_on, #stub_forge, #stub_forge_on, #stub_hosts, #stub_hosts_on, #upgrade_package, #wait_for_host_in_dashboard, #with_puppet_running, #with_puppet_running_on

Methods included from DSL::Wrappers

#facter, #hiera, #puppet

Methods included from DSL::Assertions

#assert_output

Methods included from DSL::Structure

#step, #teardown, #test_name

Methods included from DSL::Outcomes

#fail_test, #pass_test, #pending_test, #skip_test

Methods included from DSL::Roles

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

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.



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
# File 'lib/beaker/test_case.rb', line 106

def initialize(these_hosts, logger, options={}, path=nil)
  @hosts   = these_hosts
  @logger = logger
  @options = 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

#exceptionObject (readonly)

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



81
82
83
# File 'lib/beaker/test_case.rb', line 81

def exception
  @exception
end

#fail_flagObject (readonly)

I don’t know why this is here



71
72
73
# File 'lib/beaker/test_case.rb', line 71

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.



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

def hosts
  @hosts
end

#loggerObject

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



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

def logger
  @logger
end

#optionsObject (readonly)

Parsed command line options.



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

def options
  @options
end

#pathObject (readonly)

The path to the file which contains this test case.



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

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.



98
99
100
# File 'lib/beaker/test_case.rb', line 98

def result
  @result
end

#runtimeObject (readonly)

Deprecated.

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



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

def runtime
  @runtime
end

#teardown_procsObject (readonly)

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



90
91
92
# File 'lib/beaker/test_case.rb', line 90

def teardown_procs
  @teardown_procs
end

#test_statusObject (readonly)

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



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

def test_status
  @test_status
end

#usr_homeObject (readonly)

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



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

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.



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

def version
  @version
end

Instance Method Details

#forgeString

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

Returns:

  • (String)

    hostname of test forge



193
194
195
196
197
# File 'lib/beaker/test_case.rb', line 193

def forge
  ENV['forge_host'] ||
    @options['forge_host'] ||
    'vulcan-acceptance.delivery.puppetlabs.net'
end

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



176
177
178
179
180
181
182
183
# File 'lib/beaker/test_case.rb', line 176

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