2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/input_registry"
|
|
|
|
require "inspec/secrets"
|
2018-09-12 20:42:58 +00:00
|
|
|
|
2019-02-12 03:52:27 +00:00
|
|
|
describe Inspec::InputRegistry do
|
|
|
|
let(:registry) { Inspec::InputRegistry }
|
2018-09-12 20:42:58 +00:00
|
|
|
|
|
|
|
def setup
|
2019-02-12 03:52:27 +00:00
|
|
|
Inspec::InputRegistry.instance.__reset
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "creating a profile input" do
|
|
|
|
it "creates an input without options" do
|
|
|
|
registry.find_or_register_input("test_input", "dummy_profile")
|
2019-03-07 18:52:03 +00:00
|
|
|
# confirm we get the dummy value
|
2019-06-11 22:24:35 +00:00
|
|
|
registry.find_or_register_input("test_input", "dummy_profile").value.class.must_equal Inspec::Input::NO_VALUE_SET
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "creates an input with a value" do
|
|
|
|
registry.find_or_register_input("color", "dummy_profile", value: "silver")
|
|
|
|
registry.find_or_register_input("color", "dummy_profile").value.must_equal "silver"
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "creating a profile with a name alias" do
|
|
|
|
it "creates a value input on a profile with an alias" do
|
|
|
|
registry.register_profile_alias("old_profile", "new_profile")
|
|
|
|
registry.find_or_register_input("color", "new_profile", value: "blue")
|
|
|
|
registry.find_or_register_input("color", "new_profile").value.must_equal "blue"
|
|
|
|
registry.find_or_register_input("color", "old_profile").value.must_equal "blue"
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "creating a profile and select it" do
|
|
|
|
it "creates a profile with inputs" do
|
|
|
|
registry.find_or_register_input("color", "dummy_profile", value: "silver")
|
|
|
|
registry.find_or_register_input("color2", "dummy_profile", value: "blue")
|
|
|
|
registry.find_or_register_input("color3", "dummy_profile", value: "green")
|
|
|
|
registry.list_inputs_for_profile("dummy_profile").size.must_equal 3
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "validate the correct objects are getting created" do
|
|
|
|
it "creates a profile with inputs" do
|
|
|
|
registry.find_or_register_input("color", "dummy_profile", value: "silver").class.must_equal Inspec::Input
|
|
|
|
registry.list_inputs_for_profile("dummy_profile").size.must_equal 1
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "validate find_or_register_input method" do
|
|
|
|
it "find an input which exist" do
|
|
|
|
input = registry.find_or_register_input("color", "dummy_profile")
|
|
|
|
input.value = "black"
|
2018-09-12 20:42:58 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
registry.find_or_register_input("color", "dummy_profile").value.must_equal "black"
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
end
|
2019-02-26 02:05:18 +00:00
|
|
|
|
|
|
|
# =============================================================== #
|
|
|
|
# Loading inputs from --attrs
|
|
|
|
# =============================================================== #
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#bind_profile_inputs" do
|
2019-02-26 02:05:18 +00:00
|
|
|
before do
|
|
|
|
Inspec::InputRegistry.any_instance.stubs(:validate_inputs_file_readability!)
|
|
|
|
end
|
|
|
|
let(:seen_inputs) do
|
2019-06-11 22:24:35 +00:00
|
|
|
registry.bind_profile_inputs("test_fixture_profile", sources)
|
|
|
|
inputs = registry.list_inputs_for_profile("test_fixture_profile")
|
2019-02-26 02:05:18 +00:00
|
|
|
# Flatten Input objects down to their values
|
|
|
|
inputs.keys.each { |input_name| inputs[input_name] = inputs[input_name].value }
|
|
|
|
inputs
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when no CLI --attrs are specified" do
|
2019-02-26 02:05:18 +00:00
|
|
|
let(:sources) { { cli_input_files: [] } }
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns an empty hash" do
|
2019-02-26 02:05:18 +00:00
|
|
|
seen_inputs.must_equal({})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when a CLI --attrs option is provided and does not resolve" do
|
|
|
|
let(:sources) { { cli_input_files: ["nope.jpg"] } }
|
|
|
|
it "raises an exception" do
|
|
|
|
Inspec::SecretsBackend.expects(:resolve).with("nope.jpg").returns(nil)
|
2019-02-26 02:05:18 +00:00
|
|
|
proc { seen_inputs }.must_raise Inspec::Exceptions::SecretsBackendNotFound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when a CLI --attrs option is provided and has no inputs" do
|
|
|
|
let(:sources) { { cli_input_files: ["empty.yaml"] } }
|
|
|
|
it "returns an empty hash" do
|
2019-02-26 02:05:18 +00:00
|
|
|
secrets = mock
|
|
|
|
secrets.stubs(:inputs).returns(nil)
|
2019-06-11 22:24:35 +00:00
|
|
|
Inspec::SecretsBackend.expects(:resolve).with("empty.yaml").returns(secrets)
|
2019-02-26 02:05:18 +00:00
|
|
|
|
|
|
|
seen_inputs.must_equal({})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when a CLI --attrs file is provided and has inputs" do
|
|
|
|
let(:sources) { { cli_input_files: ["file1.yaml"] } }
|
|
|
|
it "returns a hash containing the inputs" do
|
|
|
|
fixture_inputs = { foo: "bar" }
|
2019-02-26 02:05:18 +00:00
|
|
|
secrets = mock
|
|
|
|
secrets.stubs(:inputs).returns(fixture_inputs)
|
2019-06-11 22:24:35 +00:00
|
|
|
Inspec::SecretsBackend.expects(:resolve).with("file1.yaml").returns(secrets)
|
2019-02-26 02:05:18 +00:00
|
|
|
|
|
|
|
seen_inputs.must_equal(fixture_inputs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when multiple CLI --attrs option args are provided and one fails" do
|
|
|
|
let(:sources) { { cli_input_files: ["file1.yaml", "file2.yaml"] } }
|
|
|
|
it "raises an exception" do
|
2019-02-26 02:05:18 +00:00
|
|
|
secrets = mock
|
|
|
|
secrets.stubs(:inputs).returns(nil)
|
2019-06-11 22:24:35 +00:00
|
|
|
Inspec::SecretsBackend.expects(:resolve).with("file1.yaml").returns(secrets)
|
|
|
|
Inspec::SecretsBackend.expects(:resolve).with("file2.yaml").returns(nil)
|
2019-02-26 02:05:18 +00:00
|
|
|
proc { seen_inputs }.must_raise Inspec::Exceptions::SecretsBackendNotFound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when multiple CLI --attrs option args are provided and one has no inputs" do
|
|
|
|
let(:sources) { { cli_input_files: ["file1.yaml", "file2.yaml"] } }
|
2019-02-26 02:05:18 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns a hash containing the inputs from the valid files" do
|
|
|
|
inputs = { foo: "bar" }
|
2019-02-26 02:05:18 +00:00
|
|
|
secrets1 = mock
|
|
|
|
secrets1.stubs(:inputs).returns(nil)
|
|
|
|
secrets2 = mock
|
|
|
|
secrets2.stubs(:inputs).returns(inputs)
|
2019-06-11 22:24:35 +00:00
|
|
|
Inspec::SecretsBackend.expects(:resolve).with("file1.yaml").returns(secrets1)
|
|
|
|
Inspec::SecretsBackend.expects(:resolve).with("file2.yaml").returns(secrets2)
|
2019-02-26 02:05:18 +00:00
|
|
|
seen_inputs.must_equal(inputs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when multiple CLI --attrs option args are provided and all have inputs" do
|
|
|
|
let(:sources) { { cli_input_files: ["file1.yaml", "file2.yaml"] } }
|
|
|
|
it "returns a hash containing all the inputs" do
|
2019-02-26 02:05:18 +00:00
|
|
|
secrets1 = mock
|
2019-06-11 22:24:35 +00:00
|
|
|
secrets1.stubs(:inputs).returns({ key1: "value1" })
|
2019-02-26 02:05:18 +00:00
|
|
|
secrets2 = mock
|
2019-06-11 22:24:35 +00:00
|
|
|
secrets2.stubs(:inputs).returns({ key2: "value2" })
|
|
|
|
Inspec::SecretsBackend.expects(:resolve).with("file1.yaml").returns(secrets1)
|
|
|
|
Inspec::SecretsBackend.expects(:resolve).with("file2.yaml").returns(secrets2)
|
|
|
|
seen_inputs.must_equal({ key1: "value1", key2: "value2" })
|
2019-02-26 02:05:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|