Enhanced outcomes changes for streaming progress bar reporter

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>
This commit is contained in:
Nikita Mathur 2022-06-27 16:56:18 +05:30
parent 2b9f4d4bbf
commit 1fc0076f1a
7 changed files with 142 additions and 28 deletions

View file

@ -126,7 +126,7 @@ module Inspec::Formatters
elsif control_has_all_tests_skipped(control)
{ name: "Not Reviewed", abbrev: "N/R" }
elsif control[:results] && control[:results].any? { |r| r[:status] == "failed" }
{ name: "Failed", abbrev: "fail"}
{ name: "Failed", abbrev: "fail" }
else
{ name: "Passed", abbrev: "pass" }
end

View file

@ -366,7 +366,7 @@ module Inspec::Reporters
format_with_color(failed_color, failed_str),
format_with_color(not_rev_color, not_rev_str),
format_with_color(not_app_color, not_app_str),
format_with_color(error_color, error_str),
format_with_color(error_color, error_str)
)
output(s) if summary["total"] > 0
end

View file

@ -20,6 +20,9 @@ module InspecPlugins::StreamingReporterProgressBar
"passed" => "\033[0;1;32m",
"skipped" => "\033[0;37m",
"reset" => "\033[0m",
"error" => "\033[34m",
"not_applicable" => "\033[36m",
"not_reviewed" => "\033[33m",
}.freeze
# Most currently available Windows terminals have poor support
@ -28,6 +31,9 @@ module InspecPlugins::StreamingReporterProgressBar
"failed" => "[FAIL]",
"skipped" => "[SKIP]",
"passed" => "[PASS]",
"error" => " [ERROR] ",
"not_applicable" => " [N/A] ",
"not_reviewed" => " [N/R] ",
}.freeze
else
# Extended colors for everyone else
@ -36,6 +42,9 @@ module InspecPlugins::StreamingReporterProgressBar
"passed" => "\033[38;5;41m",
"skipped" => "\033[38;5;247m",
"reset" => "\033[0m",
"error" => "\033[0;38;5;21m",
"not_applicable" => "\033[0;38;5;117m",
"not_reviewed" => "\033[0;38;5;214m",
}.freeze
# Groovy UTF-8 characters for everyone else...
@ -44,6 +53,9 @@ module InspecPlugins::StreamingReporterProgressBar
"failed" => "× [FAILED] ",
"skipped" => "↺ [SKIPPED]",
"passed" => "✔ [PASSED] ",
"error" => "× [ERROR] ",
"not_applicable" => " [N/A] ",
"not_reviewed" => " [N/R] ",
}.freeze
end
@ -71,34 +83,37 @@ module InspecPlugins::StreamingReporterProgressBar
control_id = notification.example.metadata[:id]
title = notification.example.metadata[:title]
full_description = notification.example.metadata[:full_description]
control_impact = notification.example.metadata[:impact]
set_status_mapping(control_id, status)
collect_notifications(notification, control_id, status)
control_ended = control_ended?(control_id)
if control_ended
add_enhanced_outcomes(control_id) if enhanced_outcomes
show_progress(control_id, title, full_description, control_impact)
control_outcome = add_enhanced_outcomes(control_id) if enhanced_outcomes
show_progress(control_id, title, full_description, control_outcome)
end
end
def show_progress(control_id, title, full_description, control_impact)
def show_progress(control_id, title, full_description, control_outcome)
@bar ||= ProgressBar.new(controls_count, :bar, :counter, :percentage)
sleep 0.1
@bar.increment!
@bar.puts format_it(control_id, title, full_description, control_impact)
@bar.puts format_it(control_id, title, full_description, control_outcome)
rescue StandardError => e
raise "Exception in Progress Bar streaming reporter: #{e}"
end
def format_it(control_id, title, full_description, control_impact)
control_status = if @status_mapping[control_id].include? "failed"
"failed"
elsif @status_mapping[control_id].include? "passed"
"passed"
else
@status_mapping[control_id].include? "skipped"
"skipped"
end
def format_it(control_id, title, full_description, control_outcome)
if control_outcome
control_status = control_outcome[:name].downcase.gsub(" ", "_")
else
control_status = if @status_mapping[control_id].include? "failed"
"failed"
elsif @status_mapping[control_id].include? "passed"
"passed"
else
@status_mapping[control_id].include? "skipped"
"skipped"
end
end
indicator = INDICATORS[control_status]
message_to_format = ""
message_to_format += "#{indicator} "

View file

@ -0,0 +1,67 @@
# Error
control "tmp-1.0.1" do
impact 0.7
describe file("/tmp") do
it { should_bot be_directory }
end
end
control "tmp-1.0.2" do
impact 0.0
describe file("/tmp") do
it { should_bot be_directory }
end
end
# Not Applicable
control "tmp-2.0.1" do
impact 0.0
describe file("/tmp") do
it { should be_directory }
end
end
control "tmp-2.0.2" do
impact 0.0
only_if { false }
describe file("/tmp") do
it { should be_directory }
end
end
# Not Reviewed
control "tmp-3.0.1" do
only_if { false }
describe file("/tmp") do
it { should be_directory }
end
end
control "tmp-3.0.2" do
only_if { false }
describe file("/tmp") do
it { should_bot be_directory }
end
end
# Failed
control "tmp-4.0" do
impact 0.7
title "Create /tmp directory"
desc "An optional description..."
describe file("/tmp") do
it { should_not be_directory }
it { should be_directory }
end
end
# Passed
control "tmp-5.0" do
impact 0.7
title "Create /tmp directory"
desc "An optional description..."
describe file("/tmp") do
it { should be_directory }
it { should exist }
end
end

View file

@ -0,0 +1,10 @@
name: enhanced-outcomes-test
title: InSpec Profile
maintainer: The Authors
copyright: The Authors
copyright_email: you@example.com
license: Apache-2.0
summary: An InSpec Compliance Profile
version: 0.1.0
supports:
platform: os

View file

@ -77,4 +77,20 @@ describe "inspec exec with streaming progress bar reporter" do
assert_exit_code 101, out
end
it "shows enhanced_outcomes with enhanced_outcomes flag" do
skip_windows!
out = inspec("exec " + File.join(profile_path, "enhanced-outcomes-test") + " --no-create-lockfile --reporter progress-bar --enhanced-outcomes")
_(out.stderr).must_include "[ERROR] tmp-1.0.1"
_(out.stderr).must_include "[ERROR] tmp-1.0.2"
_(out.stderr).must_include "[N/A] tmp-2.0.1"
_(out.stderr).must_include "[N/A] tmp-2.0.2"
_(out.stderr).must_include "[N/R] tmp-3.0.1"
_(out.stderr).must_include "[N/R] tmp-3.0.2"
_(out.stderr).must_include "[N/R] tmp-3.0.2"
_(out.stderr).must_include "[FAILED] tmp-4.0"
_(out.stderr).must_include "[PASSED] tmp-5.0"
assert_exit_code 100, out
end
end

View file

@ -1334,29 +1334,35 @@ EOT
describe "when running profile with enhanced_outcomes option" do
let(:run_result) { run_inspec_process("exec #{profile} --no-create-lockfile", enhanced_outcomes: true) }
let(:profile) { "#{profile_path}/only_if/skip-control" }
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 "6 skipped"
_(run_result.stdout).must_include "5 controls not reviewed"
_(run_result.stdout).must_include "3 skipped"
_(run_result.stdout).must_include "2 controls not reviewed"
_(run_result.stdout).must_include "N/R"
end
it "should show enhanced_outcomes for controls with impact 0" do
_(run_result.stdout).must_include "6 skipped"
_(run_result.stdout).must_include "1 control not applicable"
_(run_result.stdout).must_include "3 skipped"
_(run_result.stdout).must_include "2 controls not applicable"
_(run_result.stdout).must_include "N/A"
end
end
describe "when running profile with errors and enhanced_outcomes option" do
let(:run_result) { run_inspec_process("exec #{profile} --no-create-lockfile", enhanced_outcomes: true) }
let(:profile) { "#{profile_path}/exception-in-control" }
it "should show enhanced_outcomes for controls with errors" do
_(run_result.stdout).must_include "4 failures"
_(run_result.stdout).must_include "4 controls have error"
_(run_result.stdout).must_include "3 failures"
_(run_result.stdout).must_include "2 controls have error"
_(run_result.stdout).must_include "ERR"
end
it "should show enhanced_outcomes for controls with failures" do
_(run_result.stdout).must_include "1 control failure"
end
it "should show enhanced_outcomes for passed controls" do
_(run_result.stdout).must_include "1 successful control"
end
end
end