Skip to content

Commit

Permalink
Rename attributes options to input options (#229)
Browse files Browse the repository at this point in the history
Rename attributes options to input options
  • Loading branch information
clintoncwolfe committed Jun 6, 2019
2 parents eb67d7b + ac84b5f commit 9c88256
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ matrix:
- rvm: 2.4.5
bundler_args: "--without guard tools"
script: bundle exec rake $SUITE
env: SUITE="test:integration" OS='attributes-inline attributes-file'
env: SUITE="test:integration" OS='inputs-inline inputs-file'
- rvm: 2.5.3
- rvm: 2.6.0
- rvm: ruby-head
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,26 +241,28 @@ suites:
compliance: base/ssh
```

### Use attributes with your inspec profiles
### Use inputs with your inspec profiles

To run a profile with attributes defined inline, you can adapt your `.kitchen.yml`:
Note: InSpec Inputs were formerly known as InSpec Attributes. As they are not related to Chef Attributes, they have been renamed to reduce confusion.

To run a profile with inputs defined inline, you can adapt your `.kitchen.yml`:

```yaml
verifier:
inspec_tests:
- path: test/integration/attributes
attributes:
inputs:
user: bob
password: secret
```

You can also define your attributes in an external file. Adapt your `.kitchen.yml` to point to that file:
You can also define your inputs in external files. Adapt your `.kitchen.yml` to point to those files:

```yaml
verifier:
inspec_tests:
- path: test/integration/attributes
attrs:
input_files:
- test/integration/profile-attribute.yml
```

Expand Down
20 changes: 10 additions & 10 deletions kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ transport:

provisioner:
name: dokken
chef_license: accept

verifier:
name: inspec
Expand Down Expand Up @@ -51,24 +52,23 @@ suites:
inspec_tests:
- path: ./test/integration/duplicates
- path: ./test/integration/duplicates
- name: attributes_inline
- name: inputs_inline
run_list:
- recipe[os_prepare]
verifier:
inspec_tests:
- path: ./test/integration/attributes
attributes:
user: bob
password: secret
- name: attributes_file
- path: ./test/integration/inputs-inline
inputs:
user: value_from_kitchen_yml_1
password: value_from_kitchen_yml_2
- name: inputs_file
run_list:
- recipe[os_prepare]
verifier:
inspec_tests:
- test/integration/attributes
# - path: ./test/integration/attributes
attrs:
- test/integration/profile-attribute.yml
- test/integration/inputs-file
input_files:
- test/integration/profile-inputs.yml
# before you are able to use the compliance plugin, you need to run
# insecure is only required if you use self-signed certificates
# $ inspec compliance login https://compliance.test --user admin --insecure --token ''
Expand Down
31 changes: 28 additions & 3 deletions lib/kitchen/verifier/inspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ def call(state)
opts = runner_options(instance.transport, state, instance.platform.name, instance.suite.name)
logger.debug "Options #{opts.inspect}"

# add attributes
opts[:attrs] = config[:attrs]
opts[:attributes] = Hashie.stringify_keys config[:attributes] unless config[:attributes].nil?
# add inputs
setup_inputs(opts, config)

# setup logger
::Inspec::Log.init(STDERR)
Expand Down Expand Up @@ -100,6 +99,32 @@ def call(state)

private

def setup_inputs(opts, config)
inspec_version = Gem::Version.new(::Inspec::VERSION)

# Handle input files
if config[:attrs]
logger.warn("kitchen-inspec: please use 'input-files' instead of 'attrs'")
config[:input_files] = config[:attrs]
end
if config[:input_files]
# Note that inspec expects the singular inflection, input_file
files_key = inspec_version >= Gem::Version.new("3.10") ? :input_file : :attrs
opts[files_key] = config[:input_files]
end

# Handle YAML => Hash inputs
if config[:attributes]
logger.warn("kitchen-inspec: please use 'inputs' instead of 'attributes'")
config[:inputs] = config[:attributes]
end
if config[:inputs]
# Version here is dependent on https://github.com/inspec/inspec/issues/3856
inputs_key = inspec_version >= Gem::Version.new("4.10") ? :inputs : :attributes
opts[inputs_key] = Hashie.stringify_keys config[:inputs]
end
end

# (see Base#load_needed_dependencies!)
def load_needed_dependencies!
require "inspec"
Expand Down
92 changes: 92 additions & 0 deletions spec/kitchen/verifier/inspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,98 @@
end
end

describe "#setup_inputs" do
let(:input_opts) { {} }
let(:input_cfg) { {} }
context "when InSpec is recent" do
context "when file inputs are provided" do
context "using modern syntax" do
it "should place them in input_file" do
stub_const("Inspec::VERSION", "3.99.0")
input_cfg[:input_files] = ["a.yaml", "b.yaml"]
verifier.send(:setup_inputs, input_opts, input_cfg)
expect(input_opts.keys).to include :input_file
expect(input_opts[:input_file]).to eq(input_cfg[:input_files])
end
end
context "using legacy syntax" do
it "should place them in input_file" do
stub_const("Inspec::VERSION", "3.99.0")
input_cfg[:attrs] = ["a.yaml", "b.yaml"]
# TODO: this should emit a warning
verifier.send(:setup_inputs, input_opts, input_cfg)
expect(input_opts.keys).to include :input_file
expect(input_opts[:input_file]).to eq(input_cfg[:attrs])
end
end
end
context "when hash inputs are provided" do
context "using modern syntax" do
it "should place them in attributes" do
stub_const("Inspec::VERSION", "3.99.0")
input_cfg[:inputs] = { a: 1, b: 2 }
verifier.send(:setup_inputs, input_opts, input_cfg)
expect(input_opts.keys).to include :attributes
expect(input_opts[:attributes]).to eq({ "a" => 1, "b" => 2 })
end
end
context "using legacy syntax" do
it "should place them in attributes" do
stub_const("Inspec::VERSION", "3.99.0")
input_cfg[:attributes] = { a: 1, b: 2 }
verifier.send(:setup_inputs, input_opts, input_cfg)
expect(input_opts.keys).to include :attributes
expect(input_opts[:attributes]).to eq({ "a" => 1, "b" => 2 })
end
end
end
end

context "when InSpec is old" do
context "when file inputs are provided" do
context "using modern syntax" do
it "should place them in attrs" do
stub_const("Inspec::VERSION", "3.0.0")
input_cfg[:input_files] = ["a.yaml", "b.yaml"]
verifier.send(:setup_inputs, input_opts, input_cfg)
expect(input_opts.keys).to include :attrs
expect(input_opts[:attrs]).to eq(input_cfg[:input_files])
end
end
context "using legacy syntax" do
it "should place them in input_file" do
stub_const("Inspec::VERSION", "3.0.0")
input_cfg[:attrs] = ["a.yaml", "b.yaml"]
# TODO: this should emit a warning
verifier.send(:setup_inputs, input_opts, input_cfg)
expect(input_opts.keys).to include :attrs
expect(input_opts[:attrs]).to eq(input_cfg[:attrs])
end
end
end
context "when hash inputs are provided" do
context "using modern syntax" do
it "should place them in attributes" do
stub_const("Inspec::VERSION", "3.0.0")
input_cfg[:inputs] = { a: 1, b: 2 }
verifier.send(:setup_inputs, input_opts, input_cfg)
expect(input_opts.keys).to include :attributes
expect(input_opts[:attributes]).to eq({ "a" => 1, "b" => 2 })
end
end
context "using legacy syntax" do
it "should place them in attributes" do
stub_const("Inspec::VERSION", "3.0.0")
input_cfg[:attributes] = { a: 1, b: 2 }
verifier.send(:setup_inputs, input_opts, input_cfg)
expect(input_opts.keys).to include :attributes
expect(input_opts[:attributes]).to eq({ "a" => 1, "b" => 2 })
end
end
end
end
end

describe "#resolve_config_inspec_tests" do
context "when the entry is a string" do
context "when the path does not exist" do
Expand Down
11 changes: 0 additions & 11 deletions test/integration/attributes/controls/example.rb

This file was deleted.

7 changes: 7 additions & 0 deletions test/integration/inputs-file/controls/controls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe attribute("user", value: "value_from_dsl") do
it { should eq "value_from_input_file_1" }
end

describe attribute("password") do
it { should eq "value_from_input_file_2" }
end
File renamed without changes.
7 changes: 7 additions & 0 deletions test/integration/inputs-inline/controls/controls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe attribute("user", value: "value_from_dsl") do
it { should eq "value_from_kitchen_yml_1" }
end

describe attribute("password") do
it { should eq "value_from_kitchen_yml_2" }
end
8 changes: 8 additions & 0 deletions test/integration/inputs-inline/inspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: attributes
title: InSpec Profile
maintainer: The Authors
copyright: The Authors
copyright_email: you@example.com
license: All Rights Reserved
summary: An InSpec Compliance Profile
version: 0.1.0
2 changes: 0 additions & 2 deletions test/integration/profile-attribute.yml

This file was deleted.

2 changes: 2 additions & 0 deletions test/integration/profile-inputs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
user: value_from_input_file_1
password: value_from_input_file_2

0 comments on commit 9c88256

Please sign in to comment.