inspec/test/unit/bundles/inspec-compliance/target_test.rb
Josh Hudson 2d44b6e5e0 Cached profiles with Compliance Fetcher (#3221)
* Leverage existance check in Compliance::Fetcher.resolve to not re-download locally cached profiles
* Move logic from Compliance::API.exist? to Compliance::API.profiles to reuse code in cases where we need to access profiles' metadata directly.
* Declare @upstream_sha256 if target is a string
* Handle other fetchers that don't support upstream_sha256 within Inspec::CachedFetcher.initialize
* Add initialize for Compliance::Fetcher to not pollute Fetchers::Url with its logic
* Add Compliance::Fetcher.sha256 to leverage upstream_sha256 instead of relying on inherited method from Fetchers::Url
* Revert changes to cached fetcher that are unnecessary after refactor
* Pacify the god of ruby syntax
* Move Compliance::API.profiles filtering logic to end of method to leverage normalization of mapped_profiles
* Add and update unit tests to support caching with Compliance::Fetcher.upstream_sha256

Signed-off-by: Josh Hudson <jhudson@chef.io>
2018-08-28 09:11:38 -04:00

139 lines
5.3 KiB
Ruby

require 'helper'
describe Compliance::Fetcher do
let(:config) { { 'server' => 'myserver' } }
describe 'when the server is an automate2 server' do
before { Compliance::API.expects(:is_automate2_server?).with(config).returns(true) }
it 'returns the correct owner and profile name' do
config['profile'] = ['admin', 'ssh-baseline', nil]
fetcher = Compliance::Fetcher.new('myserver/profile', config)
fetcher.send(:compliance_profile_name).must_equal 'admin/ssh-baseline'
end
end
describe 'when the server is an automate server pre-0.8.0' do
before { Compliance::API.expects(:is_automate_server_pre_080?).with(config).returns(true) }
it 'returns the correct profile name when the url is correct' do
fetcher = Compliance::Fetcher.new('myserver/myowner/myprofile/tar', config)
fetcher.send(:compliance_profile_name).must_equal 'myowner/myprofile'
end
it 'raises an exception if the url is malformed' do
fetcher = Compliance::Fetcher.new('a/bad/url', config)
proc { fetcher.send(:compliance_profile_name) }.must_raise RuntimeError
end
end
describe 'when the server is an automate server 0.8.0-or-later' do
before do
Compliance::API.expects(:is_automate_server_pre_080?).with(config).returns(false)
Compliance::API.expects(:is_automate_server_080_and_later?).with(config).returns(true)
end
it 'returns the correct profile name when the url is correct' do
fetcher = Compliance::Fetcher.new('myserver/profiles/myowner/myprofile/tar', config)
fetcher.send(:compliance_profile_name).must_equal 'myowner/myprofile'
end
it 'raises an exception if the url is malformed' do
fetcher = Compliance::Fetcher.new('a/bad/url', config)
proc { fetcher.send(:compliance_profile_name) }.must_raise RuntimeError
end
end
describe 'when the server is not an automate server (likely a compliance server)' do
before do
Compliance::API.expects(:is_automate_server_pre_080?).with(config).returns(false)
Compliance::API.expects(:is_automate_server_080_and_later?).with(config).returns(false)
end
it 'returns the correct profile name when the url is correct' do
fetcher = Compliance::Fetcher.new('myserver/owners/myowner/compliance/myprofile/tar', config)
fetcher.send(:compliance_profile_name).must_equal 'myowner/myprofile'
end
it 'raises an exception if the url is malformed' do
fetcher = Compliance::Fetcher.new('a/bad/url', config)
proc { fetcher.send(:compliance_profile_name) }.must_raise RuntimeError
end
end
describe 'when the server calls an automate profile' do
let(:profiles_result) do
[{ 'name'=>'ssh-baseline',
'title'=>'InSpec Profile',
'maintainer'=>'The Authors',
'copyright'=>'The Authors',
'copyright_email'=>'you@example.com',
'license'=>'Apache-2.0',
'summary'=>'An InSpec Compliance Profile',
'version'=>'0.1.1',
'owner'=>'admin',
'supports'=>[],
'depends'=>[],
'sha256'=>'132j1kjdasfasdoaefaewo12312',
'groups'=>[],
'controls'=>[],
'attributes'=>[],
'latest_version'=>'' }]
end
before do
Compliance::Configuration.expects(:new).returns({ 'token' => '123abc', 'server' => 'https://a2.instance.com' })
end
it 'returns the correct profile name when parsing url' do
Compliance::API.stubs(:profiles).returns(['success', profiles_result])
fetcher = Compliance::Fetcher.resolve('compliance://admin/ssh-baseline')
assert = ['admin', 'ssh-baseline', nil]
fetcher.instance_variable_get(:"@config")['profile'].must_equal assert
end
it 'returns the correct profile name when parsing compliance hash' do
Compliance::API.stubs(:profiles).returns(['success', profiles_result])
hash = {
target: 'https://a2.instance.com/api/v0/compliance/tar',
compliance: 'admin/ssh-baseline',
sha256: '132j1kjdasfasdoaefaewo12312',
}
fetcher = Compliance::Fetcher.resolve(hash)
assert = ['admin', 'ssh-baseline', nil]
fetcher.instance_variable_get(:"@config")['profile'].must_equal assert
end
end
describe 'when the server provides a sha256 in the profiles_result' do
let(:profiles_result) do
[{ 'name'=>'ssh-baseline',
'title'=>'InSpec Profile',
'maintainer'=>'The Authors',
'copyright'=>'The Authors',
'copyright_email'=>'you@example.com',
'license'=>'Apache-2.0',
'summary'=>'An InSpec Compliance Profile',
'version'=>'0.1.1',
'owner'=>'admin',
'supports'=>[],
'depends'=>[],
'sha256'=>'132j1kjdasfasdoaefaewo12312',
'groups'=>[],
'controls'=>[],
'attributes'=>[],
'latest_version'=>'' }]
end
before do
Compliance::Configuration.expects(:new).returns({ 'token' => '123abc', 'server' => 'https://a2.instance.com' })
end
it 'contains the upstream_sha256' do
Compliance::API.stubs(:profiles).returns(['success', profiles_result])
prof = profiles_result[0]
target = "compliance://#{prof['owner']}/#{prof['name']}"
fetcher = Compliance::Fetcher.resolve(target)
fetcher.upstream_sha256.must_equal prof['sha256']
end
end
end