2015-10-06 16:55:44 +00:00
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
2015-09-21 07:51:00 +00:00
|
|
|
|
|
|
|
require 'helper'
|
2015-10-26 03:04:18 +00:00
|
|
|
require 'inspec/resource'
|
2015-09-21 07:51:00 +00:00
|
|
|
|
2015-10-26 03:04:18 +00:00
|
|
|
describe 'Inspec::Resources::JSON' do
|
2015-10-27 14:37:01 +00:00
|
|
|
describe 'when loading a valid json' do
|
|
|
|
let (:resource) { load_resource('json', 'policyfile.lock.json') }
|
|
|
|
|
|
|
|
it 'gets params as a hashmap' do
|
|
|
|
_(resource.params).must_be_kind_of Hash
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retrieves nil if a param is missing' do
|
|
|
|
_(resource.params['missing']).must_be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retrieves params by name' do
|
|
|
|
_(resource.send('name')).must_equal 'demo'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retrieves an array by name' do
|
|
|
|
_(resource.send('run_list')).must_equal %w{a b}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'doesnt resolve dot-notation names' do
|
|
|
|
_(resource.send('x.y.z')).must_be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'doesnt resolve symbol-notation names' do
|
|
|
|
_(resource.send(:'x.y.z')).must_be_nil
|
|
|
|
end
|
2018-04-26 15:54:16 +00:00
|
|
|
|
|
|
|
it 'is enumerability matches its data' do
|
|
|
|
enum = load_resource('json', content: '["a","b"]')
|
|
|
|
not_enum = load_resource('json', content: '525600')
|
|
|
|
|
|
|
|
_(enum.respond_to?(:each)).must_equal true
|
|
|
|
_(not_enum.respond_to?(:each)).must_equal false
|
|
|
|
end
|
2015-09-21 07:51:00 +00:00
|
|
|
end
|
2017-11-29 21:31:06 +00:00
|
|
|
|
2016-12-15 13:00:56 +00:00
|
|
|
describe 'when loading a nonexistent file' do
|
2017-11-29 21:31:06 +00:00
|
|
|
let(:resource) { load_resource('json', 'nonexistent.json') }
|
2016-12-15 13:00:56 +00:00
|
|
|
|
|
|
|
it 'produces an error' do
|
2018-03-22 12:25:45 +00:00
|
|
|
_(resource.resource_exception_message).must_equal "Can't find file: nonexistent.json"
|
2017-11-27 16:13:02 +00:00
|
|
|
end
|
2017-11-29 21:31:06 +00:00
|
|
|
|
|
|
|
it 'still provides an empty hash for params' do
|
|
|
|
_(resource.params).must_equal({})
|
|
|
|
end
|
2017-11-27 16:13:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#load_raw_from_file' do
|
|
|
|
let(:path) { '/path/to/file.txt' }
|
|
|
|
let(:resource) { Inspec::Resources::JsonConfig.allocate }
|
|
|
|
let(:inspec) { mock }
|
|
|
|
let(:file) { mock }
|
|
|
|
|
|
|
|
before do
|
|
|
|
resource.stubs(:inspec).returns(inspec)
|
|
|
|
inspec.expects(:file).with(path).returns(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an exception when the file does not exist' do
|
|
|
|
file.expects(:file?).returns(false)
|
|
|
|
proc { resource.send(:load_raw_from_file, path) }.must_raise Inspec::Exceptions::ResourceSkipped
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an exception if the file content is nil' do
|
|
|
|
file.expects(:file?).returns(true)
|
|
|
|
file.expects(:content).returns(nil)
|
|
|
|
proc { resource.send(:load_raw_from_file, path) }.must_raise Inspec::Exceptions::ResourceSkipped
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an exception if the file content is empty' do
|
|
|
|
file.expects(:file?).returns(true)
|
|
|
|
file.expects(:content).at_least_once.returns('')
|
|
|
|
proc { resource.send(:load_raw_from_file, path) }.must_raise Inspec::Exceptions::ResourceSkipped
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the file content' do
|
|
|
|
file.expects(:file?).returns(true)
|
|
|
|
file.expects(:content).at_least_once.returns('json goes here')
|
|
|
|
resource.send(:load_raw_from_file, path).must_equal 'json goes here'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#load_raw_from_file' do
|
|
|
|
let(:cmd_str) { 'curl localhost' }
|
|
|
|
let(:resource) { Inspec::Resources::JsonConfig.allocate }
|
|
|
|
let(:inspec) { mock }
|
|
|
|
let(:command) { mock }
|
|
|
|
|
|
|
|
before do
|
|
|
|
resource.stubs(:inspec).returns(inspec)
|
|
|
|
inspec.expects(:command).with(cmd_str).returns(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an exception if command stdout is nil' do
|
|
|
|
command.expects(:stdout).returns(nil)
|
|
|
|
proc { resource.send(:load_raw_from_command, cmd_str) }.must_raise Inspec::Exceptions::ResourceSkipped
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an exception if command stdout is empty' do
|
|
|
|
command.expects(:stdout).returns('')
|
|
|
|
proc { resource.send(:load_raw_from_command, cmd_str) }.must_raise Inspec::Exceptions::ResourceSkipped
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the command output' do
|
|
|
|
command.expects(:stdout).returns('json goes here')
|
|
|
|
resource.send(:load_raw_from_command, cmd_str).must_equal 'json goes here'
|
2016-12-15 13:00:56 +00:00
|
|
|
end
|
|
|
|
end
|
2015-09-21 07:51:00 +00:00
|
|
|
end
|