inspec/test/unit/attribute_registry_test.rb
Jared Quick ac106a090e
Implement InSpec global attributes (#3318)
* Add yml attribute option.
* Add type matching.
* Add testing profile for global attributes testing all types.
* Allow attributes to be called within a control block.
* Fix attribut test issues and allow value to be set at runtime.
* Allow setting attr value after creation.
* Move attributes to global namespace.
* Move attributes to a singleton object.
* Add unit and updated functional testing.
* Rename attributes to attributes_test so the testhelper picks it up.
* Add attribute object tests and error types.
* Update with feedback changes.
* Remove extra line.
* Move attribute registry class file.
* Add documentation for attributes
* Rename rspec_extensions.
* Add some failing functional tests.
* Update docs and fix typos.

Signed-off-by: Jared Quick <jquick@chef.io>
2018-09-12 16:42:58 -04:00

71 lines
3 KiB
Ruby

# encoding: utf-8
require 'helper'
require 'inspec/attribute_registry'
describe Inspec::AttributeRegistry do
let(:attributes) { Inspec::AttributeRegistry }
def setup
Inspec::AttributeRegistry.instance.__reset
end
describe 'creating a profile attribute' do
it 'creates a attribute without options' do
attributes.register_attribute('test_attribute', 'dummy_profile')
# confirm we get the dummy default
attributes.find_attribute('test_attribute', 'dummy_profile').value.class.must_equal Inspec::Attribute::DEFAULT_ATTRIBUTE
end
it 'creates a attribute with a default value' do
attributes.register_attribute('color', 'dummy_profile', default: 'silver')
attributes.find_attribute('color', 'dummy_profile').value.must_equal 'silver'
end
end
describe 'creating a profile with a name alias' do
it 'creates a default attribute on a profile with a alias' do
attributes.register_profile_alias('old_profile', 'new_profile')
attributes.register_attribute('color', 'new_profile', default: 'blue')
attributes.find_attribute('color', 'new_profile').value.must_equal 'blue'
attributes.find_attribute('color', 'old_profile').value.must_equal 'blue'
end
end
describe 'creating a profile and select it' do
it 'creates a profile with attributes' do
attributes.register_attribute('color', 'dummy_profile', default: 'silver')
attributes.register_attribute('color2', 'dummy_profile', default: 'blue')
attributes.register_attribute('color3', 'dummy_profile', default: 'green')
attributes.list_attributes_for_profile('dummy_profile').size.must_equal 3
end
end
describe 'validate the correct objects are getting created' do
it 'creates a profile with attributes' do
attributes.register_attribute('color', 'dummy_profile', default: 'silver').class.must_equal Inspec::Attribute
attributes.list_attributes_for_profile('dummy_profile').size.must_equal 1
end
end
describe 'validate find_attribute method' do
it 'find a attribute which exist' do
attribute = attributes.register_attribute('color', 'dummy_profile')
attribute.value = 'black'
attributes.find_attribute('color', 'dummy_profile').value.must_equal 'black'
end
it 'errors when trying to find a attribute on a unknown profile' do
attribute = attributes.register_attribute('color', 'dummy_profile')
ex = assert_raises(Inspec::AttributeRegistry::ProfileError) { attributes.find_attribute('color', 'unknown_profile') }
ex.message.must_match "Profile 'unknown_profile' does not have any attributes"
end
it 'errors when trying to find a unknown attribute on a known profile' do
attribute = attributes.register_attribute('color', 'dummy_profile')
ex = assert_raises(Inspec::AttributeRegistry::AttributeError) { attributes.find_attribute('unknown_attribute', 'dummy_profile') }
ex.message.must_match "Profile 'dummy_profile' does not have a attribute with name 'unknown_attribute'"
end
end
end