2019-06-11 22:24:35 +00:00
|
|
|
require "functional/helper"
|
2019-07-24 18:10:15 +00:00
|
|
|
require "json_schemer"
|
2016-06-14 12:41:45 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "inspec exec" do
|
2016-06-14 12:41:45 +00:00
|
|
|
include FunctionalHelper
|
|
|
|
|
2019-09-17 00:40:51 +00:00
|
|
|
parallelize_me!
|
|
|
|
|
2020-01-28 23:52:02 +00:00
|
|
|
let(:out) { inspec("exec " + complete_profile + " --reporter json-min --no-create-lockfile") }
|
2019-11-06 22:42:44 +00:00
|
|
|
let(:json) { JSON.load(out.stdout) }
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "can execute a profile with the mini json formatter and validate its schema" do
|
2017-03-14 15:50:10 +00:00
|
|
|
data = JSON.parse(out.stdout)
|
2019-07-24 18:10:15 +00:00
|
|
|
sout = inspec("schema exec-jsonmin")
|
|
|
|
schema = JSONSchemer.schema(sout.stdout)
|
2020-01-28 23:52:02 +00:00
|
|
|
_(schema.validate(data).to_a).must_equal []
|
2019-07-23 01:44:43 +00:00
|
|
|
|
2020-01-28 23:52:02 +00:00
|
|
|
_(out.stderr).must_equal ""
|
2019-07-23 01:44:43 +00:00
|
|
|
|
2020-01-28 23:52:02 +00:00
|
|
|
assert_exit_code 0, out
|
2016-06-14 12:41:45 +00:00
|
|
|
end
|
|
|
|
|
2019-07-24 18:10:15 +00:00
|
|
|
it "can execute a simple file with the mini json formatter and validate its schema" do
|
|
|
|
out = inspec("exec " + example_control + " --reporter json-min --no-create-lockfile")
|
2020-01-28 23:52:02 +00:00
|
|
|
_(out.stderr).must_equal ""
|
|
|
|
_(out.exit_status).must_equal 0
|
2019-07-24 18:10:15 +00:00
|
|
|
data = JSON.parse(out.stdout)
|
|
|
|
sout = inspec("schema exec-jsonmin")
|
|
|
|
schema = JSONSchemer.schema(sout.stdout)
|
2020-01-28 23:52:02 +00:00
|
|
|
_(schema.validate(data).to_a).must_equal []
|
2019-07-24 18:10:15 +00:00
|
|
|
|
2020-01-28 23:52:02 +00:00
|
|
|
_(out.stderr).must_equal ""
|
2019-07-24 18:10:15 +00:00
|
|
|
|
|
|
|
assert_exit_code 0, out
|
|
|
|
end
|
|
|
|
|
|
|
|
it "properly validates all (valid) unit tests against the schema" do
|
|
|
|
schema = JSONSchemer.schema(JSON.parse(inspec("schema exec-jsonmin").stdout))
|
2020-01-28 23:52:02 +00:00
|
|
|
all_profile_folders.first(1).each do |folder|
|
2019-07-24 18:10:15 +00:00
|
|
|
begin
|
|
|
|
out = inspec("exec " + folder + " --reporter json-min --no-create-lockfile")
|
|
|
|
# Ensure it parses properly; discard the result
|
|
|
|
out = JSON.parse(out.stdout)
|
|
|
|
failures = schema.validate(out).to_a
|
2020-01-28 23:52:02 +00:00
|
|
|
_(failures).must_equal []
|
2019-07-24 18:10:15 +00:00
|
|
|
rescue JSON::ParserError
|
|
|
|
# We don't actually care about these; cannot validate if parsing fails!
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not contain any dupilcate results with describe.one" do
|
2018-02-08 09:06:58 +00:00
|
|
|
out = inspec("shell -c 'describe.one do describe 1 do it { should cmp 2 } end end' --reporter=json-min")
|
2017-06-06 20:39:10 +00:00
|
|
|
data = JSON.parse(out.stdout)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(data["controls"].length).must_equal 1
|
|
|
|
_(data["controls"][0]["message"]).must_equal "\nexpected: 2\n got: 1\n\n(compared using `cmp` matcher)\n"
|
2019-07-23 01:44:43 +00:00
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(out.stderr).must_equal ""
|
2019-07-23 01:44:43 +00:00
|
|
|
|
|
|
|
assert_exit_code 100, out
|
2017-06-06 20:39:10 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "execute a profile with mini json formatting" do
|
|
|
|
let(:controls) { json["controls"] }
|
2020-01-28 23:52:02 +00:00
|
|
|
let(:ex1) { controls.find { |x| x["id"] == "test01" } }
|
2016-06-14 12:41:45 +00:00
|
|
|
|
2020-01-28 23:52:02 +00:00
|
|
|
it "must have 1 example" do
|
|
|
|
_(json["controls"].length).must_equal 1
|
2016-06-14 12:41:45 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "has an id" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(controls.find { |ex| !ex.key? "id" }).must_be :nil?
|
2016-06-14 12:41:45 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "has a profile_id" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(controls.find { |ex| !ex.key? "profile_id" }).must_be :nil?
|
2016-06-14 12:41:45 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "has a code_desc" do
|
2020-01-28 23:52:02 +00:00
|
|
|
_(ex1["code_desc"]).must_equal "Host example.com is expected to be resolvable"
|
2019-09-30 22:31:55 +00:00
|
|
|
_(controls.find { |ex| !ex.key? "code_desc" }).must_be :nil?
|
2016-06-14 12:41:45 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "has a status" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(ex1["status"]).must_equal "passed"
|
2016-06-14 12:41:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|