Class: CemAcpt::Bolt::TestRunner

Inherits:
Object
  • Object
show all
Includes:
Helpers, Logging
Defined in:
lib/cem_acpt/bolt.rb

Overview

Class for running Bolt tests

Constant Summary

Constants included from Helpers

Helpers::BOLT_PROJECT_FILE, Helpers::INVENTORY_FILE

Constants included from Logging

Logging::LEVEL_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#bolt_bin, #load_object_test, #new_bolt_project_hash, #new_inventory_hash

Methods included from Logging

current_log_config, #current_log_config, current_log_format, #current_log_format, #current_log_level, current_log_level, included, #logger, logger, new_log_config, #new_log_config, new_log_formatter, #new_log_formatter, #new_log_level, new_log_level, #new_logger, new_logger, verbose?, #verbose?

Constructor Details

#initialize(config, run_data: {}, hosts: []) ⇒ TestRunner

Returns a new instance of TestRunner.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cem_acpt/bolt.rb', line 28

def initialize(config, run_data: {}, hosts: [])
  @config = config
  @run_data = run_data
  @tests = nil
  @hosts = hosts
  @module_name = File.basename(Dir.pwd).gsub(%r{puppetlabs-}, '')
  @bolt_project_setup = false
  @inventory = CemAcpt::Bolt::Inventory.new(config, hosts, run_data[:private_key])
  @project = CemAcpt::Bolt::Project.new(config, @module_name)
  @results = CemAcpt::Bolt::SummaryResults.new
end

Instance Attribute Details

#hostsObject

Returns the value of attribute hosts.



26
27
28
# File 'lib/cem_acpt/bolt.rb', line 26

def hosts
  @hosts
end

#resultsObject (readonly)

Returns the value of attribute results.



26
27
28
# File 'lib/cem_acpt/bolt.rb', line 26

def results
  @results
end

#run_dataObject

Returns the value of attribute run_data.



26
27
28
# File 'lib/cem_acpt/bolt.rb', line 26

def run_data
  @run_data
end

Instance Method Details

#run(threaded: true) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cem_acpt/bolt.rb', line 72

def run(threaded: true)
  raise BoltActionError, 'Cannot run Bolt tests, no hosts specified' if hosts.nil? || hosts.empty?

  logger.info('CemAcpt::Bolt') { "Preparing to run Bolt tests for module #{@module_name}..." }
  if threaded
    run_in_context { |t| run_threaded(t) }
  else
    run_in_context { |t| run_synchronous(t) }
  end
rescue StandardError => e
  errmsg = "Error running Bolt tests for module #{@module_name}"
  wrapped_error = CemAcpt::Bolt::BoltActionError.new(errmsg, e, "#{self.class.name}#run")
  logger.error('CemAcpt::Bolt') { errmsg }
  logger.debug('CemAcpt::Bolt') { "Backtrace:\n#{e.backtrace.join("\n")}" }
  @results << CemAcpt::Bolt::Tests::TestResult.new(
    'Bolt test runner error',
    nil,
    nil,
    error: wrapped_error,
  )
ensure
  @results.finalize!
  @results
end

#setup!Object



44
45
46
47
48
49
50
# File 'lib/cem_acpt/bolt.rb', line 44

def setup!
  return if setup?

  @project.save! unless @project.latest_saved?
  @inventory.save! unless @inventory.latest_saved?
  tests.setup! unless tests.setup?
end

#setup?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/cem_acpt/bolt.rb', line 52

def setup?
  @project.latest_saved? && @inventory.latest_saved? && tests.setup?
end

#teardown!Object



56
57
58
59
60
# File 'lib/cem_acpt/bolt.rb', line 56

def teardown!
  logger.debug('CemAcpt::Bolt') { 'Tearing down Bolt project...' }
  @inventory.delete!
  @project.delete!
end

#testsObject



40
41
42
# File 'lib/cem_acpt/bolt.rb', line 40

def tests
  @tests ||= CemAcpt::Bolt::Tests::TestList.new(@config, @run_data, @inventory, @project)
end