Module: CemAcpt::Context

Extended by:
Logging
Defined in:
lib/cem_acpt/context.rb

Overview

Context provides the context in which the RunHandler creates and starts Runners.

Defined Under Namespace

Classes: ContextError, Ctx

Constant Summary collapse

KEY_PATH =
File.join([ENV['HOME'], '.ssh', 'acpt_test_key']).freeze
KH_PATH =
File.join([ENV['HOME'], '.ssh', 'acpt_test_known_hosts']).freeze

Constants included from Logging

Logging::LEVEL_MAP

Class Method Summary collapse

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

Class Method Details

.build_module_package(opts = {}) ⇒ String

Builds the Puppet module package

Parameters:

  • opts (Hash) (defaults to: {})

    config opts

Returns:

  • (String)

    The path to the Puppet module package



29
30
31
32
33
34
# File 'lib/cem_acpt/context.rb', line 29

def build_module_package(opts = {})
  module_dir = opts[:module_dir] || __dir__
  pkg_path = CemAcpt::Utils::Puppet.build_module_package(module_dir)
  log("Module package built at #{pkg_path}")
  pkg_path
end

.clean_test_ssh_keyObject

Deletes acceptance test suite SSH files



49
50
51
52
# File 'lib/cem_acpt/context.rb', line 49

def clean_test_ssh_key
  log('Deleting ephemeral ssh keys and acpt_known_hosts if they exist...')
  [@ssh_priv_key, @ssh_pub_key, @ssh_known_hosts].map { |f| File.delete(f) if File.exist?(f) }
end

.clean_up_test_suite(opts) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/cem_acpt/context.rb', line 64

def clean_up_test_suite(opts)
  @ctx.node_inventory.clear!
  @ctx.node_inventory.clean_local_files
  clean_test_ssh_key unless opts[:no_ephemeral_ssh_key]
  @run_handler&.destroy_test_nodes
  @keep_terminal_alive&.kill
end

.keep_terminal_alive(opts = {}) ⇒ Object

Prints a period to the terminal every 5 seconds in a single line to keep the terminal alive. This is used when running in CI mode. Does nothing unless the option `:CI` is `true`, or the environment variables CI or GITHUB_ACTION are set to a truthy value.

Parameters:

  • opts (Hash) (defaults to: {})

    config opts



58
59
60
61
62
# File 'lib/cem_acpt/context.rb', line 58

def keep_terminal_alive(opts = {})
  @keep_terminal_alive = if opts[:CI] || ENV['CI'] || ENV['GITHUB_ACTION']
                           CemAcpt::Utils::Terminal.keep_terminal_alive
                         end
end

.log(msg, level = :info) ⇒ Object



21
22
23
24
# File 'lib/cem_acpt/context.rb', line 21

def log(msg, level = :info)
  real_msg = "CONTEXT: #{msg}"
  logger.send(level, real_msg)
end

.new_test_ssh_keyObject

Creates a SSH key and a SSH known hosts file for the acceptance test suite



37
38
39
40
41
42
43
44
45
46
# File 'lib/cem_acpt/context.rb', line 37

def new_test_ssh_key
  log('Creating ephemeral SSH key and known hosts file for acceptance test suites...')
  @ssh_priv_key, @ssh_pub_key = CemAcpt::Utils::SSH.ephemeral_ssh_key
  @ssh_known_hosts = CemAcpt::Utils::SSH.acpt_known_hosts
  CemAcpt::Utils::SSH.set_ssh_file_permissions(@ssh_priv_key, @ssh_pub_key, @ssh_known_hosts)
  log('Successfully created SSH files...')
  log("SSH private key: #{@ssh_priv_key}", :debug)
  log("SSH public key: #{@ssh_pub_key}", :debug)
  log("SSH known hosts: #{@ssh_known_hosts}", :debug)
end

.with(**opts) ⇒ Object

Creates a context (CemAcpt::Context::Ctx) object for the RunHandler to create and start Runners. Provides the following objects for the Runners: a config object, the test data hash, the node inventory, and the local port allocator. Additionally, it creates the platform-specific node objects for each test suite in the test data. It then calls the provided block with the context objects nodes, config, test_data, and node_inventory.

Parameters:

  • config_opts (Hash)

    the config options



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
107
108
109
110
# File 'lib/cem_acpt/context.rb', line 80

def self.with(**opts)
  @opts = opts
  @start_time = Time.now
  raise CemAcpt::Context::ContextError, 'CemAcpt::Context.with requires a block' unless block_given?

  config_file = @opts[:config_file] || File.expand_path('./cem_acpt_config.yaml')
  logger.info("Running acceptance test suite at #{@start_time}")
  logger.debug("Config opts: #{@opts}")
  logger.debug("Config file: #{config_file}")
  logger.info("Using module directory: #{@opts[:module_dir]}")
  keep_terminal_alive(@opts)
  Dir.chdir(opts[:module_dir]) do
    new_test_ssh_key unless @opts[:no_ephemeral_ssh_key]
    pkg_path = build_module_package(@opts)
    @ctx = CemAcpt::Context::Ctx.new(opts: @opts, config_file: config_file, module_package_path: pkg_path)
    logger.debug("Created Ctx object #{@ctx}")
    @run_handler = CemAcpt::TestRunner::RunHandler.new(@ctx)
    logger.debug("Created RunHandler object #{@run_handler}")
    yield @run_handler
  end
  @exit_code = @run_handler.exit_code
rescue StandardError => e
  logger.fatal("Acceptance test suite encountered an error: #{e.message}")
  logger.fatal(e.backtrace.join("\n"))
  @exit_code = 1
ensure
  clean_up_test_suite(@opts)
  total_minutes = ((Time.now - @start_time) / 60).round
  logger.info("Test suite finished in ~#{total_minutes} minutes")
  @exit_code || 1
end