2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/objects/input"
|
2017-09-21 16:17:44 +00:00
|
|
|
|
2019-02-12 03:39:25 +00:00
|
|
|
describe Inspec::Input do
|
2019-06-11 22:24:35 +00:00
|
|
|
let(:opts) { {} }
|
|
|
|
let(:input) { Inspec::Input.new("test_input", opts) }
|
2017-09-21 16:17:44 +00:00
|
|
|
|
2019-02-07 16:45:49 +00:00
|
|
|
#==============================================================#
|
|
|
|
# Metadata
|
|
|
|
#==============================================================#
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "setting and reading metadata" do
|
2019-02-07 17:31:17 +00:00
|
|
|
{
|
2019-06-11 22:24:35 +00:00
|
|
|
description: "My favorite attribute",
|
|
|
|
identifier: "a_ruby_permitted_name",
|
2019-02-07 17:31:17 +00:00
|
|
|
required: true,
|
2019-06-11 22:24:35 +00:00
|
|
|
title: "how is this different than description",
|
|
|
|
type: "Numeric",
|
2019-02-07 17:31:17 +00:00
|
|
|
}.each do |field, value|
|
|
|
|
it "should be able to recall the #{field} field" do
|
|
|
|
opts[field] = value
|
2019-06-11 22:24:35 +00:00
|
|
|
ipt = Inspec::Input.new("test_attribute", opts)
|
2019-02-07 17:31:17 +00:00
|
|
|
seen_value = ipt.send(field)
|
|
|
|
seen_value.must_equal value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-07 16:45:49 +00:00
|
|
|
|
|
|
|
#==============================================================#
|
|
|
|
# Code Generation
|
|
|
|
#==============================================================#
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "to_ruby method" do
|
|
|
|
it "generates the code for the input" do
|
|
|
|
input = Inspec::Input.new("application_port", description: "The port my application uses", value: 80)
|
2019-02-07 16:45:49 +00:00
|
|
|
|
|
|
|
ruby_code = input.to_ruby
|
|
|
|
ruby_code.must_include "attr_application_port = " # Should assign to a var
|
2019-06-10 21:57:42 +00:00
|
|
|
# Should have the DSL call. This should be attribute(), not input(), for the
|
|
|
|
# foreseeable future, to maintain backwards compatibility.
|
|
|
|
ruby_code.must_include "attribute('application_port'"
|
2019-06-12 17:05:31 +00:00
|
|
|
ruby_code.must_include "value: 80"
|
|
|
|
ruby_code.must_include "default: 80"
|
2019-02-07 16:45:49 +00:00
|
|
|
ruby_code.must_include "description: 'The port my application uses'"
|
|
|
|
|
|
|
|
# Try to eval the code to verify that the generated code was valid ruby.
|
2019-06-10 21:57:42 +00:00
|
|
|
# Note that the input() method is part of the DSL, so we need to
|
2019-02-07 16:45:49 +00:00
|
|
|
# alter the call into something that can respond - the constructor will do
|
2019-06-12 17:05:31 +00:00
|
|
|
ruby_code_for_eval = ruby_code.sub(/attribute\(/, "Inspec::Input.new(")
|
2019-02-07 16:45:49 +00:00
|
|
|
|
|
|
|
# This will throw exceptions if there is a problem
|
2019-06-04 06:21:26 +00:00
|
|
|
new_attr = eval(ruby_code_for_eval) # rubocop:disable Security/Eval # Could use ripper!
|
2019-02-07 16:45:49 +00:00
|
|
|
new_attr.value.must_equal 80
|
|
|
|
end
|
|
|
|
end
|
2019-02-07 17:31:17 +00:00
|
|
|
# TODO - deprecate this, not sure it is used
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "to_hash method" do
|
|
|
|
it "generates a similar hash" do
|
2019-02-07 17:31:17 +00:00
|
|
|
ipt = Inspec::Input.new(
|
2019-06-11 22:24:35 +00:00
|
|
|
"some_attr",
|
|
|
|
description: "The port my application uses",
|
2019-02-07 17:31:17 +00:00
|
|
|
value: 80,
|
2019-06-11 22:24:35 +00:00
|
|
|
identifier: "app_port",
|
2019-02-07 17:31:17 +00:00
|
|
|
required: false,
|
2019-06-11 22:24:35 +00:00
|
|
|
type: "numeric"
|
2019-02-07 17:31:17 +00:00
|
|
|
)
|
|
|
|
expected = {
|
2019-06-11 22:24:35 +00:00
|
|
|
name: "some_attr",
|
2019-02-07 17:31:17 +00:00
|
|
|
options: {
|
2019-06-11 22:24:35 +00:00
|
|
|
description: "The port my application uses",
|
2019-02-07 17:31:17 +00:00
|
|
|
value: 80,
|
2019-06-11 22:24:35 +00:00
|
|
|
identifier: "app_port",
|
2019-02-07 17:31:17 +00:00
|
|
|
required: false,
|
2019-06-11 22:24:35 +00:00
|
|
|
type: "Numeric", # This gets normalized
|
|
|
|
},
|
2019-02-07 17:31:17 +00:00
|
|
|
}
|
|
|
|
ipt.to_hash.must_equal expected
|
|
|
|
end
|
2019-02-07 18:05:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#==============================================================#
|
2019-02-07 03:00:03 +00:00
|
|
|
# Setting Value - One Shot
|
|
|
|
# (see events_test.rb for overwrite support)
|
|
|
|
#==============================================================#
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "the dummy value used when value is not set" do
|
|
|
|
it "returns the actual value, not the dummy object, if one is assigned" do
|
|
|
|
input.value = "new_value"
|
|
|
|
input.value.must_equal "new_value"
|
2019-01-28 05:08:40 +00:00
|
|
|
end
|
2017-09-22 12:57:51 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns the dummy value if no value is assigned" do
|
2019-02-22 15:05:31 +00:00
|
|
|
input.value.must_be_kind_of Inspec::Input::NO_VALUE_SET
|
|
|
|
input.value.is_a?(Inspec::Input::NO_VALUE_SET).must_equal true
|
2019-02-12 03:39:25 +00:00
|
|
|
input.value.to_s.must_equal "Input 'test_input' does not have a value. Skipping test."
|
2019-01-28 05:08:40 +00:00
|
|
|
end
|
2017-09-22 12:57:51 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "the dummy value responds true to the legacy class checks" do
|
2019-02-07 18:05:46 +00:00
|
|
|
input.value.is_a?(Inspec::Attribute::DEFAULT_ATTRIBUTE).must_equal true
|
2019-02-22 15:05:31 +00:00
|
|
|
input.value.must_be_kind_of Inspec::Attribute::DEFAULT_ATTRIBUTE
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "has a dummy value that can be called like a nested map" do
|
|
|
|
input.value["hello"]["world"][1][2]["three"].wont_be_nil
|
2019-01-28 05:08:40 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "has a dummy value that can take any nested method calls" do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.value.call.some.fancy.functions.wont_be_nil
|
2019-01-28 05:08:40 +00:00
|
|
|
end
|
2017-09-22 12:57:51 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "setting a value in the constructor using value:" do
|
|
|
|
it "returns the user-configured value" do
|
|
|
|
input = Inspec::Input.new("test_input", value: "some_value")
|
|
|
|
input.value.must_equal "some_value"
|
2017-09-22 12:57:51 +00:00
|
|
|
end
|
2017-12-21 13:20:59 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns the user-configured value if nil is explicitly assigned" do
|
|
|
|
input = Inspec::Input.new("test_input", value: nil)
|
2019-02-12 03:39:25 +00:00
|
|
|
input.value.must_be_nil
|
2017-12-21 13:20:59 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns the user-configured value if false is explicitly assigned" do
|
|
|
|
input = Inspec::Input.new("test_input", value: false)
|
2019-02-12 03:39:25 +00:00
|
|
|
input.value.must_equal false
|
2017-12-21 13:20:59 +00:00
|
|
|
end
|
2018-09-12 20:42:58 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns a new value if the value has been assigned by value=" do
|
|
|
|
input = Inspec::Input.new("test_input", value: "original_value")
|
|
|
|
input.value = "new_value"
|
|
|
|
input.value.must_equal "new_value"
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
2019-01-28 05:40:46 +00:00
|
|
|
|
|
|
|
it 'accepts the legacy ":default" option' do
|
2019-06-11 22:24:35 +00:00
|
|
|
input = Inspec::Input.new("test_input", default: "a_default")
|
|
|
|
input.value.must_equal "a_default"
|
2019-01-28 05:40:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts the legacy ":default" and ":value" options' do
|
2019-06-11 22:24:35 +00:00
|
|
|
input = Inspec::Input.new("test_input", default: "a_default", value: "a_value")
|
|
|
|
input.value.must_equal "a_value"
|
2019-01-28 05:40:46 +00:00
|
|
|
end
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "setting a value using value=" do
|
|
|
|
it "supports storing and returning a value" do
|
|
|
|
input.value = "a_value"
|
|
|
|
input.value.must_equal "a_value"
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "supports storing and returning false" do
|
2019-02-07 03:00:03 +00:00
|
|
|
input.value = false
|
|
|
|
input.value.must_equal false
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "supports storing and returning nil" do
|
2019-02-07 03:00:03 +00:00
|
|
|
input.value = nil
|
|
|
|
input.value.must_be_nil
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
2017-09-22 12:57:51 +00:00
|
|
|
end
|
2017-09-21 16:17:44 +00:00
|
|
|
end
|