Class: Beaker::TestSuite

Inherits:
Object
  • Object
show all
Defined in:
lib/beaker/test_suite.rb

Overview

A collection of TestCase objects are considered a TestSuite. Handles executing the set of TestCase instances and reporting results as post summary text and JUnit XML.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, hosts, options, timestamp, fail_mode = nil) ⇒ TestSuite

Create Beaker::TestSuite instance

Parameters:

  • name (String)

    The name of the Beaker::TestSuite

  • hosts (Array<Host>)

    An Array of Hosts to act upon.

  • options (Hash{Symbol=>String})

    Options for this object

  • fail_mode (Symbol) (defaults to: nil)

    One of :slow, :fast

  • timestamp (Time)

    Beaker execution start time

Options Hash (options):

  • :logger (Logger)

    The Logger object to report information to

  • :log_dir (String)

    The directory where text run logs will be written

  • :xml_dir (String)

    The directory where JUnit XML file will be written

  • :xml_file (String)

    The name of the JUnit XML file to be written to

  • :project_root (String)

    The full path to the Beaker lib directory

  • :xml_stylesheet (String)

    The path to a stylesheet to be applied to the generated XML output



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/beaker/test_suite.rb', line 24

def initialize(name, hosts, options, timestamp, fail_mode = nil)
  @logger     = options[:logger]
  @test_cases = []
  @test_files = options[name]
  @name       = name.to_s.gsub(/\s+/, '-')
  @hosts      = hosts
  @run        = false
  @options    = options
  @fail_mode  = fail_mode || @options[:fail_mode] || :slow
  @test_suite_results = TestSuiteResult.new(@options, name)
  @timestamp = timestamp

  report_and_raise(@logger, RuntimeError.new("#{@name}: no test files found..."), "TestSuite: initialize") if @test_files.empty?
rescue => e
  report_and_raise(@logger, e, "TestSuite: initialize")
end

Instance Attribute Details

#fail_modeObject (readonly)

Returns the value of attribute fail_mode.



10
11
12
# File 'lib/beaker/test_suite.rb', line 10

def fail_mode
  @fail_mode
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/beaker/test_suite.rb', line 10

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/beaker/test_suite.rb', line 10

def options
  @options
end

Instance Method Details

#log_path(name, log_dir) ⇒ Object

Gives a full file path for output to be written to, maintaining the latest symlink

Examples:

log_path('output.txt', 'log/2014-06-02_16_31_22')

  This will create the structure:

./log/2014-06-02_16_31_22/output.txt
./log/latest -> 2014-06-02_16_31_22
log_path('foo.log', 'log/man/date')

  This will create the structure:

./log/man/date/foo.log
./log/latest -> man/date

Parameters:

  • name (String)

    The file name that we want to write to.

  • log_dir (String)

    The desired output directory. A symlink will be made from ./basedir/latest to that.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/beaker/test_suite.rb', line 142

def log_path(name, log_dir)
  FileUtils.mkdir_p(log_dir) unless File.directory?(log_dir)

  base_dir = log_dir
  link_dir = ''
  while File.dirname(base_dir) != '.'
    link_dir = link_dir == '' ? File.basename(base_dir) : File.join(File.basename(base_dir), link_dir)
    base_dir = File.dirname(base_dir)
  end

  latest = File.join(base_dir, "latest")
  if !File.exist?(latest) or File.symlink?(latest) then
    File.delete(latest) if File.exist?(latest) || File.symlink?(latest)
    File.symlink(link_dir, latest)
  end

  File.join(log_dir, name)
end

#runObject

Execute all the Beaker::TestCase instances and then report the results as both plain text and xml. The text result is reported to a newly created run log. Execution is dependent upon the fail_mode. If mode is :fast then stop running any additional Beaker::TestCase instances after first failure, if mode is :slow continue execution no matter what Beaker::TestCase results are.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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/test_suite.rb', line 45

def run
  @run = true
  start_time = Time.now

  # Create a run log for this TestSuite.
  run_log = log_path("#{@name}-run.log", @options[:log_dated_dir])
  @logger.add_destination(run_log)

  # This is an awful hack to maintain backward compatibility until tests
  # are ported to use logger.  Still in use in PuppetDB tests
  Beaker.const_set(:Log, @logger) unless defined?(Log)

  @test_suite_results.start_time = start_time
  @test_suite_results.total_tests = @test_files.length

  @test_files.each do |test_file|
    @logger.info "Begin #{test_file}"
    start = Time.now
    test_case = TestCase.new(@hosts, @logger, options, test_file).run_test
    duration = Time.now - start
    @test_suite_results.add_test_case(test_case)
    @test_cases << test_case

    state = test_case.test_status == :skip ? 'skipp' : test_case.test_status
    msg = "#{test_file} #{state}ed in %.2f seconds" % duration.to_f
    case test_case.test_status
    when :pass
      @logger.success msg
    when :skip
      @logger.warn msg
    when :fail
      @logger.error msg
      break if !@fail_mode.to_s.include?('slow') # all failure modes except slow cause us to kick out early on failure
    when :error
      @logger.warn msg
      break if !@fail_mode.to_s.include?('slow') # all failure modes except slow cause us to kick out early on failure
    end
  end
  @test_suite_results.stop_time = Time.now

  # REVISIT: This changes global state, breaking logging in any future runs
  # of the suite – or, at least, making them highly confusing for anyone who
  # has not studied the implementation in detail. --daniel 2011-03-14
  @test_suite_results.summarize(Logger.new(log_path("#{name}-summary.txt", @options[:log_dated_dir]), STDOUT))

  junit_file_log = log_path(@options[:xml_file], @options[:xml_dated_dir])
  if @options[:xml_time_enabled]
    junit_file_time = log_path(@options[:xml_time], @options[:xml_dated_dir])
    @test_suite_results.write_junit_xml(junit_file_log, @options[:xml_time])
    @test_suite_results.write_junit_xml(junit_file_time, @options[:xml_file], true)
  else
    @test_suite_results.write_junit_xml(junit_file_log)
  end

  @test_suite_results.persist_test_results(@options[:test_results_file])

  # All done with this run, remove run log
  @logger.remove_destination(run_log)

  # Allow chaining operations...
  return self
end

#run_and_raise_on_failureObject

Execute all the TestCases in this suite. This is a wrapper that catches any failures generated during TestSuite::run.



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/beaker/test_suite.rb', line 110

def run_and_raise_on_failure
  begin
    run
    return self if @test_suite_results.success?
  rescue => e
    # failed during run
    report_and_raise(@logger, e, "TestSuite :run_and_raise_on_failure")
  else
    # failed during test
    report_and_raise(@logger, RuntimeError.new("Failed while running the #{name} suite"), "TestSuite: report_and_raise_on_failure")
  end
end