inspec/test/unit/runner_test.rb
Jared Quick d49f4e3fe1 Enable caching for backend calls (#2309)
* Enable caching for command and file calls to train
* Moved transport conn to connection and refactored tests
* Update caching flag to use train caching.
* Move caching flag to cli option.
* Add backed cache default from thor.
* Add hard disable for cache option and remove all cache from debug shell.
* Add comment to caching settings conditional.
* Force file cache on when caching enabled.
* Update gemspec for train 0.30.0.

Signed-off-by: Jared Quick <jquick@chef.io>
2017-12-04 16:40:14 -05:00

111 lines
4.2 KiB
Ruby

# encoding: utf-8
# copyright: 2017, Chef Software Inc.
require 'helper'
describe Inspec::Runner do
describe '#load_attributes' do
let(:runner) { Inspec::Runner.new }
before do
Inspec::Runner.any_instance.stubs(:validate_attributes_file_readability!)
end
describe 'when backend caching is enabled' do
it 'returns a backend with caching' do
opts = { backend_cache: true }
runner = Inspec::Runner.new(opts)
backend = runner.instance_variable_get(:@backend)
backend.backend.cache_enabled?(:command).must_equal true
end
end
describe 'when backend caching is disabled' do
it 'returns a backend without caching' do
opts = { backend_cache: false }
runner = Inspec::Runner.new(opts)
backend = runner.instance_variable_get(:@backend)
backend.backend.cache_enabled?(:command).must_equal false
end
it 'returns a backend without caching as default' do
backend = runner.instance_variable_get(:@backend)
backend.backend.cache_enabled?(:command).must_equal false
end
end
describe 'when no attrs are specified' do
it 'returns an empty hash' do
options = {}
runner.load_attributes(options).must_equal({})
end
end
describe 'when an attr is provided and does not resolve' do
it 'raises an exception' do
options = { attrs: ['nope.jpg'] }
Inspec::SecretsBackend.expects(:resolve).with('nope.jpg').returns(nil)
proc { runner.load_attributes(options) }.must_raise Inspec::Exceptions::SecretsBackendNotFound
end
end
describe 'when an attr is provided and has no attributes' do
it 'returns an empty hash' do
secrets = mock
secrets.stubs(:attributes).returns(nil)
options = { attrs: ['empty.yaml'] }
Inspec::SecretsBackend.expects(:resolve).with('empty.yaml').returns(secrets)
runner.load_attributes(options).must_equal({})
end
end
describe 'when an attr is provided and has attributes' do
it 'returns a hash containing the attributes' do
options = { attrs: ['file1.yaml'] }
attributes = { foo: 'bar' }
secrets = mock
secrets.stubs(:attributes).returns(attributes)
Inspec::SecretsBackend.expects(:resolve).with('file1.yaml').returns(secrets)
runner.load_attributes(options).must_equal(attributes)
end
end
describe 'when multiple attrs are provided and one fails' do
it 'raises an exception' do
options = { attrs: ['file1.yaml', 'file2.yaml'] }
secrets = mock
secrets.stubs(:attributes).returns(nil)
Inspec::SecretsBackend.expects(:resolve).with('file1.yaml').returns(secrets)
Inspec::SecretsBackend.expects(:resolve).with('file2.yaml').returns(nil)
proc { runner.load_attributes(options) }.must_raise Inspec::Exceptions::SecretsBackendNotFound
end
end
describe 'when multiple attrs are provided and one has no attributes' do
it 'returns a hash containing the attributes from the valid files' do
options = { attrs: ['file1.yaml', 'file2.yaml'] }
attributes = { foo: 'bar' }
secrets1 = mock
secrets1.stubs(:attributes).returns(nil)
secrets2 = mock
secrets2.stubs(:attributes).returns(attributes)
Inspec::SecretsBackend.expects(:resolve).with('file1.yaml').returns(secrets1)
Inspec::SecretsBackend.expects(:resolve).with('file2.yaml').returns(secrets2)
runner.load_attributes(options).must_equal(attributes)
end
end
describe 'when multiple attrs are provided and all have attributes' do
it 'returns a hash containing all the attributes' do
options = { attrs: ['file1.yaml', 'file2.yaml'] }
secrets1 = mock
secrets1.stubs(:attributes).returns({ key1: 'value1' })
secrets2 = mock
secrets2.stubs(:attributes).returns({ key2: 'value2' })
Inspec::SecretsBackend.expects(:resolve).with('file1.yaml').returns(secrets1)
Inspec::SecretsBackend.expects(:resolve).with('file2.yaml').returns(secrets2)
runner.load_attributes(options).must_equal({ key1: 'value1', key2: 'value2' })
end
end
end
end