2
0
Fork 0
mirror of https://github.com/inspec/inspec synced 2025-03-04 15:27:35 +00:00

Enhanced outcomes changes in json based and yaml reporter

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>
This commit is contained in:
Nikita Mathur 2022-06-30 18:51:48 +05:30
parent cfbddb82a5
commit b26506b741
5 changed files with 52 additions and 2 deletions

View file

@ -114,7 +114,7 @@ module Inspec::Reporters
def profile_controls(profile)
(profile[:controls] || []).map { |c|
{
control_hash = {
id: c[:id],
title: c[:title],
desc: c.dig(:descriptions, :default),
@ -130,6 +130,8 @@ module Inspec::Reporters
waiver_data: c[:waiver_data] || {},
results: profile_results(c),
}
control_hash.merge!({ status: c[:status] }) if enhanced_outcomes
control_hash
}
end

View file

@ -3,7 +3,9 @@ require "yaml"
module Inspec::Reporters
class Yaml < Base
def render
output(Inspec::Reporters::Json.new({ run_data: run_data }).report.to_yaml, false)
json_reporter_obj = Inspec::Reporters::Json.new({ run_data: run_data })
json_reporter_obj.enhanced_outcomes = enhanced_outcomes
output(json_reporter_obj.report.to_yaml, false)
end
def report

View file

@ -39,6 +39,7 @@ module FunctionalHelper
let(:simple_inheritance) { File.join(profile_path, "simple-inheritance") }
let(:sensitive_profile) { File.join(examples_path, "profile-sensitive") }
let(:config_dir_path) { File.join(mock_path, "config_dirs") }
let(:enhanced_outcome_profile) { "#{profile_path}/enhanced-outcomes-test" }
let(:dst) do
# create a temporary path, but we only want an auto-clean helper

View file

@ -547,4 +547,21 @@ describe "inspec exec with json formatter" do
end
end
describe "when running a profile with enhanced_outcomes option" do
it "can execute a profile and validate the json schema" do
out = inspec("exec " + enhanced_outcome_profile + " --reporter json --no-create-lockfile --enhanced-outcomes")
data = JSON.parse(out.stdout)
sout = inspec("schema exec-json")
schema = JSONSchemer.schema(sout.stdout)
_(schema.validate(data).to_a).must_equal []
_(out.stderr).must_equal ""
_(data["profiles"].first["controls"][0]["status"]).must_equal "error"
_(data["profiles"].first["controls"][2]["status"]).must_equal "not_applicable"
_(data["profiles"].first["controls"][4]["status"]).must_equal "not_reviewed"
_(data["profiles"].first["controls"][6]["status"]).must_equal "failed"
_(data["profiles"].first["controls"][7]["status"]).must_equal "passed"
assert_exit_code 100, out
end
end
end

View file

@ -1365,4 +1365,32 @@ EOT
_(run_result.stdout).must_include "1 successful control"
end
end
describe "when running profile with enhanced_outcomes option and yaml reporter" do
let(:run_result) { run_inspec_process("exec #{profile} --no-create-lockfile --reporter yaml", enhanced_outcomes: true) }
let(:profile) { "#{profile_path}/enhanced-outcomes-test" }
it "should evaluate all test controls correctly" do
_(run_result.stderr).must_be_empty
end
it "should show enhanced_outcomes for skipped tests in controls" do
_(run_result.stdout).must_include ":status: not_reviewed"
end
it "should show enhanced_outcomes for controls with impact 0" do
_(run_result.stdout).must_include ":status: not_applicable"
end
it "should show enhanced_outcomes for controls with errors" do
_(run_result.stdout).must_include ":status: error"
end
it "should show enhanced_outcomes for controls with failures" do
_(run_result.stdout).must_include ":status: failed"
end
it "should show enhanced_outcomes for passed controls" do
_(run_result.stdout).must_include ":status: passed"
end
end
end