2016-03-25 00:31:19 +00:00
|
|
|
# author: Dominik Richter
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
|
|
|
|
require 'functional/helper'
|
2017-10-10 09:36:57 +00:00
|
|
|
require 'mixlib/shellout'
|
2016-03-25 00:31:19 +00:00
|
|
|
|
|
|
|
describe 'inspec json' do
|
|
|
|
include FunctionalHelper
|
|
|
|
|
|
|
|
it 'read the profile json' do
|
|
|
|
out = inspec('json ' + example_profile)
|
|
|
|
out.stderr.must_equal ''
|
|
|
|
out.exit_status.must_equal 0
|
|
|
|
s = out.stdout
|
|
|
|
JSON.load(s).must_be_kind_of Hash
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'json profile data' do
|
|
|
|
let(:json) { JSON.load(inspec('json ' + example_profile).stdout) }
|
|
|
|
|
2018-02-08 09:06:58 +00:00
|
|
|
it 'has a generator name' do
|
|
|
|
json['generator']['name'].must_equal 'inspec'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a generator inspec version' do
|
|
|
|
json['generator']['version'].must_equal Inspec::VERSION
|
|
|
|
end
|
|
|
|
|
2016-03-25 00:31:19 +00:00
|
|
|
it 'has a name' do
|
|
|
|
json['name'].must_equal 'profile'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a title' do
|
|
|
|
json['title'].must_equal 'InSpec Example Profile'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a summary' do
|
|
|
|
json['summary'].must_equal 'Demonstrates the use of InSpec Compliance Profile'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a version' do
|
|
|
|
json['version'].must_equal '1.0.0'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a maintainer' do
|
|
|
|
json['maintainer'].must_equal 'Chef Software, Inc.'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a copyright' do
|
|
|
|
json['copyright'].must_equal 'Chef Software, Inc.'
|
|
|
|
end
|
|
|
|
|
2016-04-20 04:50:18 +00:00
|
|
|
it 'has controls' do
|
|
|
|
json['controls'].length.must_equal 4
|
2016-03-25 00:31:19 +00:00
|
|
|
end
|
|
|
|
|
2016-04-20 04:50:18 +00:00
|
|
|
describe 'a control' do
|
2016-07-04 09:47:46 +00:00
|
|
|
let(:control) { json['controls'].find { |x| x['id'] == 'tmp-1.0' } }
|
2016-03-25 00:31:19 +00:00
|
|
|
|
|
|
|
it 'has a title' do
|
2016-04-20 04:50:18 +00:00
|
|
|
control['title'].must_equal 'Create /tmp directory'
|
2016-03-25 00:31:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a description' do
|
2016-04-20 04:50:18 +00:00
|
|
|
control['desc'].must_equal 'An optional description...'
|
2016-03-25 00:31:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has an impact' do
|
2016-04-20 04:50:18 +00:00
|
|
|
control['impact'].must_equal 0.7
|
2016-03-25 00:31:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a ref' do
|
2016-04-20 04:50:18 +00:00
|
|
|
control['refs'].must_equal([{'ref' => 'Document A-12', 'url' => 'http://...'}])
|
2016-03-25 00:31:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a source location' do
|
|
|
|
loc = File.join(example_profile, '/controls/example.rb')
|
2016-06-28 10:03:20 +00:00
|
|
|
control['source_location']['ref'].must_equal loc
|
2019-05-08 23:17:25 +00:00
|
|
|
control['source_location']['line'].must_equal 6
|
2016-03-25 00:31:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a the source code' do
|
2016-09-07 11:10:35 +00:00
|
|
|
control['code'].must_match(/\Acontrol \"tmp-1.0\" do.*end\n\Z/m)
|
2016-03-25 00:31:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-25 00:58:59 +00:00
|
|
|
describe 'filter with --controls' do
|
|
|
|
let(:out) { inspec('json ' + example_profile + ' --controls tmp-1.0') }
|
|
|
|
|
|
|
|
it 'still succeeds' do
|
|
|
|
out.stderr.must_equal ''
|
|
|
|
out.exit_status.must_equal 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'only has one control included' do
|
|
|
|
json = JSON.load(out.stdout)
|
2016-07-04 09:47:46 +00:00
|
|
|
json['controls'].length.must_equal 1
|
|
|
|
json['controls'][0]['id'].must_equal 'tmp-1.0'
|
|
|
|
json['groups'].length.must_equal 1
|
|
|
|
json['groups'][0]['id'].must_equal 'controls/example.rb'
|
2016-03-25 00:58:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-25 00:31:19 +00:00
|
|
|
it 'writes json to file' do
|
|
|
|
out = inspec('json ' + example_profile + ' --output ' + dst.path)
|
|
|
|
out.stderr.must_equal ''
|
|
|
|
out.exit_status.must_equal 0
|
|
|
|
hm = JSON.load(File.read(dst.path))
|
|
|
|
hm['name'].must_equal 'profile'
|
2016-04-20 04:50:18 +00:00
|
|
|
hm['controls'].length.must_equal 4
|
2016-03-25 00:31:19 +00:00
|
|
|
end
|
2017-10-10 09:36:57 +00:00
|
|
|
|
|
|
|
describe 'json test for pax header archives' do
|
|
|
|
let(:profile_tgz) { File.join(Dir.mktmpdir, "pax-profile-test.tar.gz") }
|
|
|
|
|
2018-11-08 17:00:14 +00:00
|
|
|
it 'successfully reads a pax-formatted tar file' do
|
|
|
|
# TODO: this needs updated to also support windows taring
|
|
|
|
return if is_windows?
|
2017-10-10 09:36:57 +00:00
|
|
|
files = Dir.glob("#{example_profile}/**/*").delete_if { |x| !File.file?(x) }
|
|
|
|
relatives = files.map { |e| Pathname.new(e).relative_path_from(Pathname.new(example_profile)).to_s }
|
|
|
|
|
|
|
|
cmd = Mixlib::ShellOut.new("tar --format=pax -czf #{profile_tgz} #{relatives.join(' ')}", cwd: example_profile)
|
|
|
|
cmd.run_command
|
|
|
|
cmd.error!
|
|
|
|
|
|
|
|
out = inspec("json #{profile_tgz}")
|
|
|
|
out.exit_status.must_equal 0
|
|
|
|
end
|
|
|
|
end
|
2018-01-03 17:10:35 +00:00
|
|
|
|
2018-09-12 22:02:24 +00:00
|
|
|
describe 'inspec json with a inheritance profile' do
|
2019-01-05 17:21:34 +00:00
|
|
|
let(:profile) { File.join(profile_path, 'export-json', 'empty-wrapper') }
|
2018-09-12 22:02:24 +00:00
|
|
|
|
2019-01-05 17:21:34 +00:00
|
|
|
it 'can export a profile that uses inheritance' do
|
2018-09-12 22:02:24 +00:00
|
|
|
out = inspec('json ' + profile)
|
2019-01-05 17:21:34 +00:00
|
|
|
out.stderr.must_be_empty
|
2018-09-12 22:02:24 +00:00
|
|
|
out.exit_status.must_equal 0
|
|
|
|
|
2019-01-05 17:21:34 +00:00
|
|
|
# This will throw an exception if it is garbled
|
2018-09-12 22:02:24 +00:00
|
|
|
json = JSON.load(out.stdout)
|
2019-01-05 17:21:34 +00:00
|
|
|
# and here we verify (very passingly!) that is a structure we expect
|
|
|
|
json.must_be_kind_of Hash
|
|
|
|
|
2018-09-12 22:02:24 +00:00
|
|
|
json['controls'].each do |control|
|
|
|
|
control['code'].empty?.must_equal false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-13 17:05:10 +00:00
|
|
|
describe 'inspec json does not write logs to STDOUT' do
|
|
|
|
it 'can execute a profile with warn calls and parse STDOUT as valid JSON' do
|
|
|
|
out = inspec('json ' + File.join(profile_path, 'warn_logs'))
|
|
|
|
out.exit_status.must_equal 0
|
|
|
|
JSON.load(out.stdout)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-03 17:10:35 +00:00
|
|
|
describe 'inspec json with a profile containing only_if' do
|
|
|
|
it 'ignores the `only_if`' do
|
|
|
|
out = inspec('json ' + File.join(profile_path, 'only-if-os-nope'))
|
|
|
|
out.exit_status.must_equal 0
|
|
|
|
end
|
|
|
|
end
|
2016-03-25 00:31:19 +00:00
|
|
|
end
|