2017-09-21 16:17:44 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
require 'helper'
|
2019-02-11 16:57:40 +00:00
|
|
|
require 'inspec/objects/input'
|
2017-09-21 16:17:44 +00:00
|
|
|
|
2019-02-12 03:39:25 +00:00
|
|
|
describe Inspec::Input do
|
|
|
|
let(:input) { Inspec::Input.new('test_input') }
|
2017-09-21 16:17:44 +00:00
|
|
|
|
|
|
|
it 'support storing and returning false' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.value = false
|
|
|
|
input.value.must_equal false
|
2017-09-21 16:17:44 +00:00
|
|
|
end
|
2017-09-22 12:57:51 +00:00
|
|
|
|
2019-01-28 05:08:40 +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
|
2019-02-12 03:39:25 +00:00
|
|
|
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-01-28 05:08:40 +00:00
|
|
|
it 'returns the dummy value if no value is assigned' do
|
2019-02-21 01:40:35 +00:00
|
|
|
input.value.must_be_kind_of Inspec::Attribute::DEFAULT_ATTRIBUTE # TODO - test for new class too
|
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-01-28 05:08:40 +00:00
|
|
|
it 'has a dummy value that can be called like a nested map' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.value['hello']['world'][1][2]['three'].wont_be_nil
|
2019-01-28 05:08:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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-02-12 03:39:25 +00:00
|
|
|
describe 'input with a value set' do
|
2019-01-28 05:08:40 +00:00
|
|
|
it 'returns the user-configured value' do
|
2019-02-12 03:39:25 +00:00
|
|
|
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-01-28 05:08:40 +00:00
|
|
|
it 'returns the user-configured value if nil is explicitly assigned' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input = Inspec::Input.new('test_input', value: nil)
|
|
|
|
input.value.must_be_nil
|
2017-12-21 13:20:59 +00:00
|
|
|
end
|
|
|
|
|
2019-01-28 05:08:40 +00:00
|
|
|
it 'returns the user-configured value if false is explicitly assigned' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input = Inspec::Input.new('test_input', value: false)
|
|
|
|
input.value.must_equal false
|
2017-12-21 13:20:59 +00:00
|
|
|
end
|
2018-09-12 20:42:58 +00:00
|
|
|
|
2019-01-28 05:08:40 +00:00
|
|
|
it 'returns a new value if the value has been assigned by value=' do
|
2019-02-12 03:39:25 +00:00
|
|
|
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-02-12 03:39:25 +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-02-12 03:39:25 +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
|
|
|
|
|
|
|
|
describe 'validate required method' do
|
2019-01-28 05:08:40 +00:00
|
|
|
it 'does not error if a value is set' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input = Inspec::Input.new('test_input', value: 'some_value', required: true)
|
|
|
|
input.value.must_equal 'some_value'
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
2019-01-28 05:08:40 +00:00
|
|
|
it 'does not error if a value is specified by value=' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input = Inspec::Input.new('test_input', required: true)
|
|
|
|
input.value = 'test_value'
|
|
|
|
input.value.must_equal 'test_value'
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error if no value is set' do
|
2019-01-28 05:08:40 +00:00
|
|
|
# Assigning the cli_command is needed because in check mode, we don't error
|
2019-02-12 03:39:25 +00:00
|
|
|
# on unset inputs. This is how you tell the input system we are not in
|
2019-01-28 05:08:40 +00:00
|
|
|
# check mode, apparently.
|
2018-10-05 20:24:26 +00:00
|
|
|
Inspec::BaseCLI.inspec_cli_command = :exec
|
2019-02-12 03:39:25 +00:00
|
|
|
input = Inspec::Input.new('test_input', required: true)
|
|
|
|
ex = assert_raises(Inspec::Input::RequiredError) { input.value }
|
|
|
|
ex.message.must_match /Input 'test_input' is required and does not have a value./
|
2018-10-05 20:24:26 +00:00
|
|
|
Inspec::BaseCLI.inspec_cli_command = nil
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validate value type method' do
|
|
|
|
let(:opts) { {} }
|
2019-02-12 03:39:25 +00:00
|
|
|
let(:input) { Inspec::Input.new('test_input', opts) }
|
2018-09-12 20:42:58 +00:00
|
|
|
|
|
|
|
it 'validates a string type' do
|
|
|
|
opts[:type] = 'string'
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:validate_value_type, 'string')
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error if a invalid string is set' do
|
|
|
|
opts[:type] = 'string'
|
2019-02-12 03:39:25 +00:00
|
|
|
ex = assert_raises(Inspec::Input::ValidationError) { input.send(:validate_value_type, 123) }
|
|
|
|
ex.message.must_match /Input 'test_input' with value '123' does not validate to type 'String'./
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates a numeric type' do
|
|
|
|
opts[:type] = 'numeric'
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:validate_value_type, 123.33)
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error if a invalid numeric is set' do
|
|
|
|
opts[:type] = 'numeric'
|
2019-02-12 03:39:25 +00:00
|
|
|
ex = assert_raises(Inspec::Input::ValidationError) { input.send(:validate_value_type, 'invalid') }
|
|
|
|
ex.message.must_match /Input 'test_input' with value 'invalid' does not validate to type 'Numeric'./
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates a regex type' do
|
|
|
|
opts[:type] = 'regex'
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:validate_value_type, '/^\d*$/')
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error if a invalid regex is set' do
|
|
|
|
opts[:type] = 'regex'
|
2019-02-12 03:39:25 +00:00
|
|
|
ex = assert_raises(Inspec::Input::ValidationError) { input.send(:validate_value_type, '/(.+/') }
|
|
|
|
ex.message.must_match "Input 'test_input' with value '/(.+/' does not validate to type 'Regexp'."
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates a array type' do
|
|
|
|
opts[:type] = 'Array'
|
|
|
|
value = [1, 2, 3]
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:validate_value_type, value)
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error if a invalid array is set' do
|
|
|
|
opts[:type] = 'Array'
|
|
|
|
value = { a: 1, b: 2, c: 3 }
|
2019-02-12 03:39:25 +00:00
|
|
|
ex = assert_raises(Inspec::Input::ValidationError) { input.send(:validate_value_type, value) }
|
|
|
|
ex.message.must_match /Input 'test_input' with value '{:a=>1, :b=>2, :c=>3}' does not validate to type 'Array'./
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates a hash type' do
|
|
|
|
opts[:type] = 'Hash'
|
|
|
|
value = { a: 1, b: 2, c: 3 }
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:validate_value_type, value)
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error if a invalid hash is set' do
|
|
|
|
opts[:type] = 'hash'
|
2019-02-12 03:39:25 +00:00
|
|
|
ex = assert_raises(Inspec::Input::ValidationError) { input.send(:validate_value_type, 'invalid') }
|
|
|
|
ex.message.must_match /Input 'test_input' with value 'invalid' does not validate to type 'Hash'./
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates a boolean type' do
|
|
|
|
opts[:type] = 'boolean'
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:validate_value_type, false)
|
|
|
|
input.send(:validate_value_type, true)
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error if a invalid boolean is set' do
|
|
|
|
opts[:type] = 'boolean'
|
2019-02-12 03:39:25 +00:00
|
|
|
ex = assert_raises(Inspec::Input::ValidationError) { input.send(:validate_value_type, 'not_true') }
|
|
|
|
ex.message.must_match /Input 'test_input' with value 'not_true' does not validate to type 'Boolean'./
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates a any type' do
|
|
|
|
opts[:type] = 'any'
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:validate_value_type, false)
|
|
|
|
input.send(:validate_value_type, true)
|
|
|
|
input.send(:validate_value_type, 1)
|
|
|
|
input.send(:validate_value_type, 'bob')
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validate type method' do
|
|
|
|
it 'converts regex to Regexp' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:validate_type, 'regex').must_equal 'Regexp'
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the same value if there is nothing to clean' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:validate_type, 'String').must_equal 'String'
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error if a invalid type is sent' do
|
2019-02-12 03:39:25 +00:00
|
|
|
ex = assert_raises(Inspec::Input::TypeError) { input.send(:validate_type, 'dressing') }
|
|
|
|
ex.message.must_match /Type 'Dressing' is not a valid input type./
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'valid_regexp? method' do
|
|
|
|
it 'validates a string regex' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:valid_regexp?, '/.*/').must_equal true
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates a slash regex' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:valid_regexp?, /.*/).must_equal true
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not vaildate a invalid regex' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:valid_regexp?, '/.*(/').must_equal false
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'valid_numeric? method' do
|
|
|
|
it 'validates a string number' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:valid_numeric?, '123').must_equal true
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates a float number' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:valid_numeric?, 44.55).must_equal true
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'validats a wrong padded number' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:valid_numeric?, '00080').must_equal true
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not vaildate a invalid number' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:valid_numeric?, '55.55.55.5').must_equal false
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not vaildate a invalid string' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:valid_numeric?, 'one').must_equal false
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not vaildate a fraction' do
|
2019-02-12 03:39:25 +00:00
|
|
|
input.send(:valid_numeric?, '1/2').must_equal false
|
2018-09-12 20:42:58 +00:00
|
|
|
end
|
2017-09-22 12:57:51 +00:00
|
|
|
end
|
2019-01-30 17:12:56 +00:00
|
|
|
|
|
|
|
describe 'to_ruby method' do
|
2019-02-12 03:39:25 +00:00
|
|
|
it 'generates the code for the input' do
|
|
|
|
input = Inspec::Input.new('application_port', description: 'The port my application uses', value: 80)
|
2019-01-30 19:29:18 +00:00
|
|
|
|
2019-02-12 03:39:25 +00:00
|
|
|
ruby_code = input.to_ruby
|
2019-01-30 19:29:18 +00:00
|
|
|
ruby_code.must_include "attr_application_port = " # Should assign to a var
|
|
|
|
ruby_code.must_include "attribute('application_port'" # Should have the DSL call
|
|
|
|
ruby_code.must_include 'value: 80'
|
|
|
|
ruby_code.must_include 'default: 80'
|
|
|
|
ruby_code.must_include "description: 'The port my application uses'"
|
|
|
|
|
|
|
|
# Try to eval the code to verify that the generated code was valid ruby.
|
|
|
|
# Note that the attribute() method is part of the DSL, so we need to
|
|
|
|
# alter the call into something that can respond - the constructor will do
|
2019-02-12 03:39:25 +00:00
|
|
|
ruby_code_for_eval = ruby_code.sub(/attribute\(/,'Inspec::Input.new(')
|
2019-01-30 19:29:18 +00:00
|
|
|
|
|
|
|
# This will throw exceptions if there is a problem
|
|
|
|
new_attr = eval(ruby_code_for_eval) # Could use ripper!
|
|
|
|
new_attr.value.must_equal 80
|
2019-01-30 17:12:56 +00:00
|
|
|
end
|
|
|
|
end
|
2017-09-21 16:17:44 +00:00
|
|
|
end
|