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.

Defined Under Namespace

Classes: TestSuiteResult

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create Beaker::TestSuite instance

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



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/beaker/test_suite.rb', line 276

def initialize(name, hosts, options, timestamp, fail_mode = :slow)
  @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
  @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.



262
263
264
# File 'lib/beaker/test_suite.rb', line 262

def fail_mode
  @fail_mode
end

#nameObject (readonly)

Returns the value of attribute name.



262
263
264
# File 'lib/beaker/test_suite.rb', line 262

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



262
263
264
# File 'lib/beaker/test_suite.rb', line 262

def options
  @options
end

Instance Method Details

#log_path(timestamp, name, basedir) ⇒ Object

Create a full file path for output to be written to, using the provided timestamp, name and output directory.

Examples:

log_path('2014-06-02 16:31:22 -0700','output.txt', 'log')

  This will create the structure:

./log/2014-06-02_16_31_22/output.txt
./log/latest -> 2014-06-02_16_31_22


378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/beaker/test_suite.rb', line 378

def log_path(timestamp, name, basedir)
  log_dir = File.join(basedir, timestamp.strftime("%F_%H_%M_%S"))
  unless File.directory?(log_dir) then
    FileUtils.mkdir_p(log_dir)

    latest = File.join(basedir, "latest")
    if !File.exist?(latest) or File.symlink?(latest) then
      File.delete(latest) if File.exist?(latest)
      File.symlink(File.basename(log_dir), latest)
    end
  end

  File.join(basedir, 'latest', 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.



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/beaker/test_suite.rb', line 298

def run
  @run = true
  start_time = Time.now

  #Create a run log for this TestSuite.
  run_log = log_path(@timestamp, "#{@name}-run.log", @options[:log_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_files.each do |test_file|
    @logger.notify "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.debug msg
    when :fail
      @logger.error msg
      break if @fail_mode !~ /slow/ #all failure modes except slow cause us to kick out early on failure
    when :error
      @logger.warn msg
      break if @fail_mode !~ /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(@timestamp, "#{name}-summary.txt", @options[:log_dir]), STDOUT) )
  @test_suite_results.write_junit_xml( log_path(@timestamp, @options[:xml_file], @options[:xml_dir]), File.join(@options[:project_root], @options[:xml_stylesheet]) )

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



352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/beaker/test_suite.rb', line 352

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