diff --git a/lib/kafo/configuration.rb b/lib/kafo/configuration.rb index ec5f7ad2..0fea8e15 100644 --- a/lib/kafo/configuration.rb +++ b/lib/kafo/configuration.rb @@ -169,8 +169,6 @@ def params_default_values execution_env = ExecutionEnvironment.new(self) KafoConfigure.exit_handler.register_cleanup_path(execution_env.directory) - puppetconf = execution_env.configure_puppet('noop' => true) - dump_manifest = < true}) + stdout, stderr, status = Open3.capture3(*command) @logger.debug stdout @logger.debug stderr diff --git a/lib/kafo/execution_environment.rb b/lib/kafo/execution_environment.rb index cc22bbf8..9c2137b2 100644 --- a/lib/kafo/execution_environment.rb +++ b/lib/kafo/execution_environment.rb @@ -42,6 +42,13 @@ def configure_puppet(settings = {}) PuppetConfigurer.new(puppet_conf, settings) end + def build_command(code, options: [], settings: {}, use_answers: false) + store_answers if use_answers + puppetconf = configure_puppet(settings) + command = Kafo::PuppetCommand.new(code, options, puppetconf, @config).command + Kafo::PuppetCommand.format_command(command) + end + private def environmentpath diff --git a/lib/kafo/hook_context.rb b/lib/kafo/hook_context.rb index ec5f7656..fcfe46ec 100644 --- a/lib/kafo/hook_context.rb +++ b/lib/kafo/hook_context.rb @@ -1,3 +1,4 @@ +require 'fileutils' require 'kafo/data_type' require 'kafo/base_context' @@ -134,5 +135,13 @@ def scenario_path def scenario_data self.kafo.config.app end + + # Yield a Puppet execution environment that's isolated from the system + def puppet_execution_environment + execution_env = Kafo::ExecutionEnvironment.new(self.kafo.config) + yield execution_env + ensure + FileUtils.rm_rf(execution_env.directory) + end end end diff --git a/lib/kafo/kafo_configure.rb b/lib/kafo/kafo_configure.rb index 5b9a8e01..db14adb0 100644 --- a/lib/kafo/kafo_configure.rb +++ b/lib/kafo/kafo_configure.rb @@ -425,14 +425,14 @@ def run_installation execution_env = ExecutionEnvironment.new(config) self.class.exit_handler.register_cleanup_path(execution_env.directory) - execution_env.store_answers - puppetconf = execution_env.configure_puppet( + code = 'include kafo_configure' + settings = { 'color' => false, 'evaltrace' => !!@progress_bar, 'noop' => !!noop?, 'profile' => !!profile?, 'show_diff' => true, - ) + } exit_code = 0 exit_status = nil @@ -442,11 +442,11 @@ def run_installation '--detailed-exitcodes', ] begin - command = PuppetCommand.new('include kafo_configure', options, puppetconf).command + command = execution_env.build_command(code, options: options, settings: settings, use_answers: true) log_parser = PuppetLogParser.new logger = Logger.new('configure') - PTY.spawn(*PuppetCommand.format_command(command)) do |stdin, stdout, pid| + PTY.spawn(*command) do |stdin, stdout, pid| begin stdin.each do |line| line = normalize_encoding(line) diff --git a/test/kafo/hook_context_test.rb b/test/kafo/hook_context_test.rb index ac35a0c4..9018368c 100644 --- a/test/kafo/hook_context_test.rb +++ b/test/kafo/hook_context_test.rb @@ -121,5 +121,50 @@ module Kafo assert_equal false, context.has_custom_fact?('not_my_custom_fact') end end + + describe '#puppet_execution_environment' do + let(:config) { Minitest::Mock.new } + let(:code) { } + + before do + kafo.expect :config, config + end + + specify 'creates directory' do + context.puppet_execution_environment do |env| + assert File.directory?(env.directory) + end + end + + specify 'deletes directory' do + directory = context.puppet_execution_environment do |env| + env.directory + end + + refute File.directory?(directory) + end + + specify 'builds command' do + def config.app + {} + end + def config.module_dirs + ['./modules'] + end + def config.kafo_modules_dir + ['./kafo_modules'] + end + config.expect :scenario_id, 'foobar' + config.expect :config_file, 'foobar.yaml' + + context.puppet_execution_environment do |env| + result = env.build_command("notice { 'Hello' }") + assert_kind_of(Array, result) + # TODO: result[0] can either be a Hash or ::ENV which is an Object + assert_match(%{notice { 'Hello' }}, result[1]) + assert_equal({unsetenv_others: false}, result[2]) + end + end + end end end