Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple outputs based on platform/test suite variables (issue 121) #122

Merged
merged 1 commit into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions lib/kitchen/verifier/inspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
48 changes: 47 additions & 1 deletion spec/kitchen/verifier/inspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down