From 9e525a18e278e2ffcf111aeaa8c154dcc21a2a04 Mon Sep 17 00:00:00 2001 From: Juan Carlos Castillo Cano Date: Wed, 30 Nov 2016 11:08:09 +0000 Subject: [PATCH] Using custom path for test suite Signed-off-by: Juan Carlos Castillo Cano --- README.md | 13 ++++++++ lib/kitchen/verifier/inspec.rb | 6 ++-- spec/kitchen/verifier/inspec_spec.rb | 48 +++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 44b1c45..a6db9a4 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,19 @@ verifier: port: 22 ``` +If you want to customize the output file per platform or test suite +you can use template format for your output variable. Current flags +supported: + * _%{platform}_ + * _%{suite}_ + +```yaml +verifier: + name: inspec + format: junit + output: path/to/results/%{platform}_%{suite}_inspec.xml +``` + ### Directory Structure By default `kitchen-inspec` expects test to be in `test/integration/%suite%` directory structure (we use Chef as provisioner here): diff --git a/lib/kitchen/verifier/inspec.rb b/lib/kitchen/verifier/inspec.rb index 2226c5a..363dd8e 100644 --- a/lib/kitchen/verifier/inspec.rb +++ b/lib/kitchen/verifier/inspec.rb @@ -66,7 +66,7 @@ def call(state) logger.debug("Initialize InSpec") # gather connection options - opts = runner_options(instance.transport, state) + opts = runner_options(instance.transport, state, instance.platform.name, instance.suite.name) # add attributes opts[:attrs] = config[:attrs] @@ -159,7 +159,7 @@ def collect_tests # # @return [Hash] a configuration hash of string-based keys # @api private - def runner_options(transport, state = {}) # rubocop:disable Metrics/AbcSize + def runner_options(transport, state = {}, platform = nil, suite = nil) # rubocop:disable Metrics/AbcSize transport_data = transport.diagnose.merge(state) if transport.is_a?(Kitchen::Transport::Ssh) runner_options_for_ssh(transport_data) @@ -174,7 +174,7 @@ def runner_options(transport, state = {}) # rubocop:disable Metrics/AbcSize # default color to true to match InSpec behavior runner_options["color"] = (config[:color].nil? ? true : config[:color]) runner_options["format"] = config[:format] unless config[:format].nil? - runner_options["output"] = config[:output] unless config[:output].nil? + runner_options["output"] = config[:output] % { platform: platform, suite: suite } unless config[:output].nil? runner_options["profiles_path"] = config[:profiles_path] unless config[:profiles_path].nil? end end diff --git a/spec/kitchen/verifier/inspec_spec.rb b/spec/kitchen/verifier/inspec_spec.rb index 763229c..fe722c9 100644 --- a/spec/kitchen/verifier/inspec_spec.rb +++ b/spec/kitchen/verifier/inspec_spec.rb @@ -39,7 +39,7 @@ let(:kitchen_root) { Dir.mktmpdir } let(:platform) do - instance_double("Kitchen::Platform", os_type: nil, shell_type: nil) + instance_double("Kitchen::Platform", os_type: nil, shell_type: nil, name: "default") end let(:suite) do @@ -203,6 +203,52 @@ verifier.call(port: 123) end + it "provide platform and test suite to build output path" do + allow(Inspec::Runner).to receive(:new).and_return(runner) + + expect(verifier).to receive(:runner_options).with( + transport, + {}, + "default", + "germany" + ).and_return({}) + verifier.call({}) + end + + it "custom inspec output path" do + ensure_suite_directory("germany") + config[:output] = "/tmp/inspec_results.xml" + + allow(Inspec::Runner).to receive(:new).and_return(runner) + + expect(runner).to receive(:add_target).with({ :path => + File.join( + config[:test_base_path], + "germany" + ) }, hash_including( + "output" => "/tmp/inspec_results.xml" + )) + + verifier.call({}) + end + + it "resolve template format for inspec output path" do + ensure_suite_directory("germany") + config[:output] = "/tmp/%{platform}_%{suite}.xml" + + allow(Inspec::Runner).to receive(:new).and_return(runner) + + expect(runner).to receive(:add_target).with({ :path => + File.join( + config[:test_base_path], + "germany" + ) }, hash_including( + "output" => "/tmp/default_germany.xml" + )) + + verifier.call({}) + end + it "find test directory for runner" do ensure_suite_directory("germany") allow(Inspec::Runner).to receive(:new).and_return(runner)