2019-06-11 22:24:35 +00:00
|
|
|
require "functional/helper"
|
|
|
|
require "json-schema"
|
2016-04-20 04:50:18 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "inspec exec with json formatter" do
|
2016-04-20 04:50:18 +00:00
|
|
|
include FunctionalHelper
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "can execute a simple file and validate the json schema" do
|
|
|
|
out = inspec("exec " + example_control + " --reporter json --no-create-lockfile")
|
2019-07-23 01:44:43 +00:00
|
|
|
|
2017-03-14 15:50:10 +00:00
|
|
|
data = JSON.parse(out.stdout)
|
2019-06-11 22:24:35 +00:00
|
|
|
sout = inspec("schema exec-json")
|
2017-03-14 15:50:10 +00:00
|
|
|
schema = JSON.parse(sout.stdout)
|
2019-06-02 06:17:53 +00:00
|
|
|
JSON::Validator.validate(schema, data).wont_equal false
|
2019-07-23 01:44:43 +00:00
|
|
|
|
|
|
|
out.stderr.must_equal ""
|
|
|
|
|
|
|
|
assert_exit_code 0, out
|
2016-04-20 04:50:18 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "can execute a profile and validate the json schema" do
|
|
|
|
out = inspec("exec " + example_profile + " --reporter json --no-create-lockfile")
|
2019-07-23 01:44:43 +00:00
|
|
|
|
2017-03-14 15:50:10 +00:00
|
|
|
data = JSON.parse(out.stdout)
|
2019-06-11 22:24:35 +00:00
|
|
|
sout = inspec("schema exec-json")
|
2017-03-14 15:50:10 +00:00
|
|
|
schema = JSON.parse(sout.stdout)
|
2019-06-02 06:17:53 +00:00
|
|
|
JSON::Validator.validate(schema, data).wont_equal false
|
2019-07-23 01:44:43 +00:00
|
|
|
|
|
|
|
out.stderr.must_equal ""
|
|
|
|
|
|
|
|
assert_exit_code 101, out
|
2018-10-30 10:32:16 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "can execute a simple file while using end of options after reporter cli option" do
|
|
|
|
out = inspec("exec --no-create-lockfile --reporter json -- " + example_control)
|
2018-10-30 10:32:16 +00:00
|
|
|
data = JSON.parse(out.stdout)
|
2019-06-11 22:24:35 +00:00
|
|
|
sout = inspec("schema exec-json")
|
2018-10-30 10:32:16 +00:00
|
|
|
schema = JSON.parse(sout.stdout)
|
2019-06-02 06:17:53 +00:00
|
|
|
JSON::Validator.validate(schema, data).wont_equal false
|
2019-07-23 01:44:43 +00:00
|
|
|
|
|
|
|
out.stderr.must_equal ""
|
|
|
|
|
|
|
|
assert_exit_code 0, out
|
2016-04-20 04:50:18 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "can execute a profile and validate the json schema with target_id" do
|
|
|
|
out = inspec("exec " + example_profile + " --reporter json --no-create-lockfile --target-id 1d3e399f-4d71-4863-ac54-84d437fbc444")
|
2018-09-05 19:07:34 +00:00
|
|
|
data = JSON.parse(out.stdout)
|
2019-06-11 22:24:35 +00:00
|
|
|
data["platform"]["target_id"].must_equal "1d3e399f-4d71-4863-ac54-84d437fbc444"
|
|
|
|
sout = inspec("schema exec-json")
|
2018-09-05 19:07:34 +00:00
|
|
|
schema = JSON.parse(sout.stdout)
|
2019-06-02 06:17:53 +00:00
|
|
|
JSON::Validator.validate(schema, data).wont_equal false
|
2019-07-23 01:44:43 +00:00
|
|
|
|
|
|
|
out.stderr.must_equal ""
|
|
|
|
|
|
|
|
assert_exit_code 101, out
|
2018-09-05 19:07:34 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "does not report skipped dependent profiles" do
|
|
|
|
out = inspec("exec " + File.join(profile_path, "unsupported_dependencies", "wrapper-profile") + " --reporter json --no-create-lockfile")
|
2019-07-23 01:44:43 +00:00
|
|
|
|
2018-10-15 22:25:27 +00:00
|
|
|
data = JSON.parse(out.stdout)
|
2019-06-11 22:24:35 +00:00
|
|
|
data["profiles"].count.must_equal 1
|
|
|
|
profile = data["profiles"].first
|
|
|
|
profile["controls"].count.must_equal 1
|
2019-07-23 01:44:43 +00:00
|
|
|
|
|
|
|
out.stderr.must_include "WARN: Skipping profile: 'child_profile' on unsupported platform:"
|
|
|
|
out.stderr.must_include "WARN: Skipping profile: 'child_profile2' on unsupported platform:"
|
|
|
|
|
|
|
|
assert_exit_code 0, out
|
2018-10-15 22:25:27 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "flags skipped profiles with correct status" do
|
|
|
|
out = inspec("exec " + File.join(profile_path, "unsupported_dependencies", "wrapper-profile") + " --reporter json --no-create-lockfile")
|
2019-07-23 01:44:43 +00:00
|
|
|
|
2018-10-15 22:25:27 +00:00
|
|
|
data = JSON.parse(out.stdout)
|
2019-06-11 22:24:35 +00:00
|
|
|
data["profiles"].count.must_equal 1
|
|
|
|
profile = data["profiles"].first
|
|
|
|
profile["status"].must_equal "loaded"
|
|
|
|
profile["depends"].count.must_equal 2
|
|
|
|
profile["depends"].each do |d|
|
|
|
|
d["status"].must_equal "skipped"
|
|
|
|
d["skip_message"].must_include "Skipping profile: "
|
2018-10-15 22:25:27 +00:00
|
|
|
end
|
2019-07-23 01:44:43 +00:00
|
|
|
|
|
|
|
assert_exit_code 0, out
|
2018-10-15 22:25:27 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "flags loaded profiles with correct status" do
|
|
|
|
out = inspec("exec " + File.join(profile_path, "dependencies", "inheritance") + " --reporter json --no-create-lockfile")
|
2019-07-23 01:44:43 +00:00
|
|
|
|
2018-10-15 22:25:27 +00:00
|
|
|
data = JSON.parse(out.stdout)
|
2019-06-11 22:24:35 +00:00
|
|
|
profile = data["profiles"].first
|
|
|
|
profile["status"].must_equal "loaded"
|
|
|
|
profile["depends"].count.must_equal 2
|
|
|
|
profile["depends"].each do |d|
|
|
|
|
d["status"].must_equal "loaded"
|
|
|
|
d.key?("skip_message").must_equal false
|
2018-10-15 22:25:27 +00:00
|
|
|
end
|
2019-07-23 01:44:43 +00:00
|
|
|
|
|
|
|
out.stderr.must_equal ""
|
|
|
|
|
|
|
|
assert_exit_code 0, out
|
2018-10-15 22:25:27 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "flags profile with correct status when not supported" do
|
|
|
|
out = inspec("exec " + File.join(profile_path, "skippy-profile-os") + " --reporter json --no-create-lockfile")
|
2019-07-23 01:44:43 +00:00
|
|
|
|
2018-10-15 22:25:27 +00:00
|
|
|
data = JSON.parse(out.stdout)
|
2019-06-11 22:24:35 +00:00
|
|
|
profile = data["profiles"].first
|
|
|
|
profile["status"].must_equal "skipped"
|
|
|
|
profile["skip_message"].must_include "Skipping profile: 'skippy' on unsupported platform:"
|
2019-07-23 01:44:43 +00:00
|
|
|
|
|
|
|
out.stderr.must_equal ""
|
|
|
|
|
|
|
|
assert_exit_code 101, out
|
2018-10-15 22:25:27 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "execute a profile with json formatting" do
|
|
|
|
let(:json) { JSON.load(inspec("exec " + example_profile + " --reporter json --no-create-lockfile").stdout) }
|
|
|
|
let(:profile) { json["profiles"][0] }
|
|
|
|
let(:controls) { profile["controls"] }
|
|
|
|
let(:ex1) { controls.find { |x| x["id"] == "tmp-1.0" } }
|
|
|
|
let(:ex2) { controls.find { |x| x["id"] =~ /generated/ } }
|
|
|
|
let(:ex3) { profile["controls"].find { |x| x["id"] == "gordon-1.0" } }
|
|
|
|
let(:check_result) do
|
|
|
|
ex3["results"].find { |x| x["resource"] == "gordon_config" }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has only one profile" do
|
|
|
|
json["profiles"].must_be_kind_of(Array)
|
|
|
|
json["profiles"].length.must_equal 1
|
2016-07-04 09:47:46 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "maps impact symbols to numbers" do
|
|
|
|
ex3["impact"].must_equal 0.9
|
2018-09-13 18:14:05 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "has all the metadata" do
|
2016-04-20 04:50:18 +00:00
|
|
|
actual = profile.dup
|
2019-06-11 22:24:35 +00:00
|
|
|
key = actual.delete("controls")
|
2019-07-09 00:20:30 +00:00
|
|
|
.find { |x| x["id"] =~ /generated from example.rb/ }["id"]
|
2019-06-11 22:24:35 +00:00
|
|
|
groups = actual.delete("groups")
|
2016-04-20 04:50:18 +00:00
|
|
|
actual.must_equal({
|
2016-09-16 21:59:31 +00:00
|
|
|
"name" => "profile",
|
2016-04-20 04:50:18 +00:00
|
|
|
"title" => "InSpec Example Profile",
|
|
|
|
"maintainer" => "Chef Software, Inc.",
|
|
|
|
"copyright" => "Chef Software, Inc.",
|
|
|
|
"copyright_email" => "support@chef.io",
|
2017-05-29 22:16:09 +00:00
|
|
|
"license" => "Apache-2.0",
|
2016-04-20 04:50:18 +00:00
|
|
|
"summary" => "Demonstrates the use of InSpec Compliance Profile",
|
|
|
|
"version" => "1.0.0",
|
2019-06-11 23:13:05 +00:00
|
|
|
# TODO: this is brittle and nonsensical
|
|
|
|
"sha256" => "de67a044d7be7090982740755ff582af1cefaf37261c5adda57b9502ffefc973",
|
2019-06-11 22:24:35 +00:00
|
|
|
"supports" => [{ "platform-family" => "unix" }, { "platform-family" => "windows" }],
|
2018-10-15 22:25:27 +00:00
|
|
|
"status" => "loaded",
|
2019-06-11 22:24:35 +00:00
|
|
|
"attributes" => [],
|
2016-04-20 04:50:18 +00:00
|
|
|
})
|
2016-07-04 09:47:46 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
groups.sort_by { |x| x["id"] }.must_equal([
|
|
|
|
{ "id" => "controls/example.rb", "title" => "/tmp profile", "controls" => ["tmp-1.0", key] },
|
|
|
|
{ "id" => "controls/gordon.rb", "title" => "Gordon Config Checks", "controls" => ["gordon-1.0"] },
|
|
|
|
{ "id" => "controls/meta.rb", "title" => "SSH Server Configuration", "controls" => ["ssh-1"] },
|
2016-07-04 09:47:46 +00:00
|
|
|
])
|
2016-04-20 04:50:18 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "must have 4 controls" do
|
2016-04-20 04:50:18 +00:00
|
|
|
controls.length.must_equal 4
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "has an id for every control" do
|
|
|
|
controls.find { |x| x["id"].nil? }.must_be :nil?
|
2016-04-20 04:50:18 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "has results for every control" do
|
|
|
|
ex1["results"].length.must_equal 1
|
|
|
|
ex2["results"].length.must_equal 1
|
|
|
|
ex3["results"].length.must_equal 2
|
2016-04-20 04:50:18 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "has the right result for tmp-1.0" do
|
2016-04-20 04:50:18 +00:00
|
|
|
actual = ex1.dup
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
src = actual.delete("source_location")
|
|
|
|
src["ref"].must_match %r{test/unit/mock/profiles/old-examples/profile/controls/example.rb$}
|
|
|
|
src["line"].must_equal 6
|
2016-04-20 04:50:18 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
result = actual.delete("results")[0]
|
2016-04-20 04:50:18 +00:00
|
|
|
result.wont_be :nil?
|
2019-06-11 22:24:35 +00:00
|
|
|
result["status"].must_equal "passed"
|
|
|
|
result["code_desc"].must_equal "File /tmp should be directory"
|
|
|
|
result["run_time"].wont_be :nil?
|
|
|
|
result["start_time"].wont_be :nil?
|
2016-04-20 04:50:18 +00:00
|
|
|
|
2019-05-31 21:59:06 +00:00
|
|
|
example_rb_code = <<~END
|
2019-06-11 23:13:05 +00:00
|
|
|
control 'tmp-1.0' do # A unique ID for this control
|
2019-06-01 20:08:50 +00:00
|
|
|
impact 0.7 # The criticality, if this control fails.
|
2019-06-11 23:13:05 +00:00
|
|
|
title 'Create /tmp directory' # A human-readable title
|
|
|
|
desc 'An optional description...' # Describe why this is needed
|
|
|
|
desc 'label', 'An optional description with a label' # Pair a part of the description with a label
|
|
|
|
tag data: 'temp data' # A tag allows you to associate key information
|
|
|
|
tag 'security' # to the test
|
|
|
|
ref 'Document A-12', url: 'http://...' # Additional references
|
|
|
|
|
|
|
|
describe file('/tmp') do # The actual test
|
2019-06-01 20:08:50 +00:00
|
|
|
it { should be_directory }
|
|
|
|
end
|
2019-05-31 21:59:06 +00:00
|
|
|
end
|
|
|
|
END
|
|
|
|
|
2016-04-20 04:50:18 +00:00
|
|
|
actual.must_equal({
|
2019-06-11 22:24:35 +00:00
|
|
|
"id" => "tmp-1.0",
|
|
|
|
"title" => "Create /tmp directory",
|
|
|
|
"desc" => "An optional description...",
|
|
|
|
"descriptions" => [{ "label" => "default", "data" => "An optional description..." }, { "label" => "label", "data" => "An optional description with a label" }],
|
|
|
|
"impact" => 0.7,
|
|
|
|
"refs" => [{ "url" => "http://...", "ref" => "Document A-12" }],
|
|
|
|
"tags" => { "data" => "temp data", "security" => nil },
|
2019-05-31 21:59:06 +00:00
|
|
|
"code" => example_rb_code,
|
2016-04-20 04:50:18 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "with a profile that is not supported on this OS/platform" do
|
|
|
|
let(:out) { inspec("exec " + File.join(profile_path, "skippy-profile-os") + " --reporter json --no-create-lockfile") }
|
2016-04-20 04:50:18 +00:00
|
|
|
let(:json) { JSON.load(out.stdout) }
|
|
|
|
|
|
|
|
# TODO: failure handling in json formatters...
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "never runs the actual resource" do
|
|
|
|
File.exist?("/tmp/inspec_test_DONT_CREATE").must_equal false
|
2016-04-20 04:50:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|