diff --git a/lib/inspec/input_registry.rb b/lib/inspec/input_registry.rb index f6fad5d85..1b2ea9667 100644 --- a/lib/inspec/input_registry.rb +++ b/lib/inspec/input_registry.rb @@ -82,6 +82,7 @@ module Inspec def find_or_register_input(input_name, profile_name, options = {}) input_name = input_name.to_s profile_name = profile_name.to_s + options[:event].value = Thor::CoreExt::HashWithIndifferentAccess.new(options[:event].value) if options[:event]&.value.is_a?(Hash) if profile_alias?(profile_name) && !profile_aliases[profile_name].nil? alias_name = profile_name diff --git a/test/fixtures/profiles/inputs/hashmap/controls/hashmap_input.rb b/test/fixtures/profiles/inputs/hashmap/controls/hashmap_input.rb new file mode 100644 index 000000000..f26c3d25f --- /dev/null +++ b/test/fixtures/profiles/inputs/hashmap/controls/hashmap_input.rb @@ -0,0 +1,32 @@ +# copyright: 2021, Chef Software, Inc. +title "Verifying loading of hashmap inputs using metadata and external file" + +control "hashmap-metadata" do + title "Verifying loading of hashmap inputs using metadata file" + + describe input("metadata_hash") do + its(["metadata_hash_key_str"]) { should eq "metadata_hash_value_str" } + its([:metadata_hash_key_sym]) { should eq "metadata_hash_value_sym" } + end +end + +control "hashmap-external-file" do + title "Verifying loading of hashmap inputs using external file" + + describe input("external_input_hash") do + its(["external_input_hash_key_str"]) { should eq "external_input_hash_value_str" } + its([:external_input_hash_key_sym]) { should eq "external_input_hash_value_sym" } + end +end + +control "hashmap-profile-DSL" do + title "Verifying loading of hashmap inputs using profile DSL" + + describe input("dsl_hash_string", value: { "dsl_hash_string_key": "dsl_hash_string_value" } ) do + its(["dsl_hash_string_key"]) { should eq "dsl_hash_string_value" } + end + + describe input("dsl_hash_symbol", value: { dsl_hash_symbol_key: :dsl_hash_symbol_value } ) do + its([:dsl_hash_symbol_key]) { should eq :dsl_hash_symbol_value } + end +end \ No newline at end of file diff --git a/test/fixtures/profiles/inputs/hashmap/files/inputs.yml b/test/fixtures/profiles/inputs/hashmap/files/inputs.yml new file mode 100644 index 000000000..779447013 --- /dev/null +++ b/test/fixtures/profiles/inputs/hashmap/files/inputs.yml @@ -0,0 +1,3 @@ +external_input_hash: + external_input_hash_key_str: external_input_hash_value_str + external_input_hash_key_sym: external_input_hash_value_sym \ No newline at end of file diff --git a/test/fixtures/profiles/inputs/hashmap/inspec.yml b/test/fixtures/profiles/inputs/hashmap/inspec.yml new file mode 100644 index 000000000..07bce703c --- /dev/null +++ b/test/fixtures/profiles/inputs/hashmap/inspec.yml @@ -0,0 +1,14 @@ +name: hashmap +title: InSpec Profile to verify hashmap inputs +maintainer: Chef Software, Inc. +copyright: Chef Software, Inc. +license: Apache-2.0 +summary: A profile that checks loading of hashmap inputs +version: 0.1.0 +supports: + platform: os +inputs: +- name: metadata_hash + value: + metadata_hash_key_str: metadata_hash_value_str + metadata_hash_key_sym: metadata_hash_value_sym \ No newline at end of file diff --git a/test/fixtures/profiles/inputs/via-runner/controls/via-runner.rb b/test/fixtures/profiles/inputs/via-runner/controls/via-runner.rb index 49f3b7f56..5935155a9 100644 --- a/test/fixtures/profiles/inputs/via-runner/controls/via-runner.rb +++ b/test/fixtures/profiles/inputs/via-runner/controls/via-runner.rb @@ -2,4 +2,12 @@ control "test_control_01" do describe input("test_input_01", value: "value_from_dsl") do it { should cmp "value_from_api" } end + + describe input("test_input_hash_string", value: { "string_key": "string_value_dsl" }) do + its(["string_key"]) { should eq "string_value" } + end + + describe input("test_input_hash_symbol", value: { symbol_key: :symbol_value_dsl }) do + its([:symbol_key]) { should eq :symbol_value } + end end diff --git a/test/functional/inputs_test.rb b/test/functional/inputs_test.rb index c14c722a6..499651081 100644 --- a/test/functional/inputs_test.rb +++ b/test/functional/inputs_test.rb @@ -6,6 +6,7 @@ require "tempfile" describe "inputs" do include FunctionalHelper let(:inputs_profiles_path) { File.join(profile_path, "inputs") } + let(:external_attributes_file_path) { "#{inputs_profiles_path}/hashmap/files/inputs.yml" } parallelize_me! @@ -123,7 +124,6 @@ describe "inputs" do describe "when using the current :inputs key" do let(:runner_options) { common_options.merge({ inputs: { test_input_01: "value_from_api" } }) } - it "finds the values and does not issue any warnings" do output = run_result.stdout refute_includes output, "DEPRECATION" @@ -132,6 +132,18 @@ describe "inputs" do end end + describe "when using the current :inputs key with both string and symbol key in hashes" do + let(:runner_options) { common_options.merge({ inputs: { test_input_01: "value_from_api", test_input_hash_string: { "string_key": "string_value" }, test_input_hash_symbol: { symbol_key: :symbol_value } } }) } + + it "finds the values and runs successfully" do + output = run_result.stdout + structured_output = JSON.parse(output) + assert_equal "passed", structured_output["profiles"][0]["controls"][0]["results"][0]["status"] + assert_equal "passed", structured_output["profiles"][0]["controls"][0]["results"][1]["status"] + assert_equal "passed", structured_output["profiles"][0]["controls"][0]["results"][2]["status"] + end + end + describe "when using the legacy :attributes key" do let(:runner_options) { common_options.merge({ attributes: { test_input_01: "value_from_api" } }) } it "finds the values but issues a DEPRECATION warning" do @@ -442,4 +454,12 @@ describe "inputs" do _(inputs[2]["options"]["value"]).wont_include "***" # Explicit sensitive = false end end + + describe "when a profile is executed with inputs through external file, metadata file and profile DSL" do + it "should access the values successfully from all input ways" do + result = run_inspec_process("exec #{inputs_profiles_path}/hashmap --input-file #{external_attributes_file_path}", json: true) + _(result.stderr).must_be_empty + assert_json_controls_passing(result) + end + end end