Merge pull request #5446 from inspec/nm/fix-input-hash-access

Bug fix for loading hashmap inputs consistently
This commit is contained in:
Clinton Wolfe 2021-04-07 08:49:53 -04:00 committed by GitHub
commit fa41586910
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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