2019-06-11 22:24:35 +00:00
|
|
|
|
require "helper"
|
|
|
|
|
require "inspec/reporters"
|
2018-02-08 09:06:58 +00:00
|
|
|
|
|
|
|
|
|
describe Inspec::Reporters::CLI do
|
|
|
|
|
WINDOWS = RUBY_PLATFORM =~ /windows|mswin|msys|mingw|cygwin/
|
|
|
|
|
let(:path) { File.expand_path(File.dirname(__FILE__)) }
|
2019-06-11 22:24:35 +00:00
|
|
|
|
let(:report) do
|
|
|
|
|
data = JSON.parse(File.read(path + "/../mock/reporters/run_data.json"), symbolize_names: true)
|
2018-02-08 09:06:58 +00:00
|
|
|
|
cli = Inspec::Reporters::CLI
|
|
|
|
|
cli.new({ run_data: data })
|
|
|
|
|
end
|
|
|
|
|
let(:profile) { report.run_data[:profiles].first }
|
|
|
|
|
let(:control) { Inspec::Reporters::CLI::Control }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
RSpec.configuration.color = true if defined?(RSpec.configuration)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def windowize(string)
|
2019-06-11 22:24:35 +00:00
|
|
|
|
string.gsub!("✔", "[PASS]")
|
|
|
|
|
string.gsub!("↺", "[SKIP]")
|
|
|
|
|
string.gsub!("×", "[FAIL]")
|
2018-02-08 09:06:58 +00:00
|
|
|
|
string.gsub!("\e[38;5;41m", "\e[0;1;32m")
|
|
|
|
|
string.gsub!("\e[38;5;9m", "\e[0;1;31m")
|
|
|
|
|
string.gsub!("\e[38;5;208m", "\e[0;1;31m")
|
|
|
|
|
string.gsub!("\e[38;5;247m", "\e[0;37m")
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#render" do
|
|
|
|
|
it "confirm render output" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
if WINDOWS
|
2019-06-11 22:24:35 +00:00
|
|
|
|
cli_output = File.open(path + "/../mock/reporters/cli_output_windows", "r:UTF-8").read
|
2018-02-08 09:06:58 +00:00
|
|
|
|
else
|
2019-06-11 22:24:35 +00:00
|
|
|
|
cli_output = File.open(path + "/../mock/reporters/cli_output", "r:UTF-8").read
|
2018-02-08 09:06:58 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
report.render
|
|
|
|
|
report.rendered_output.must_equal cli_output
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#print_profile_header" do
|
|
|
|
|
it "confirm header output" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
expected = <<~EOF
|
2019-06-11 22:24:35 +00:00
|
|
|
|
Profile: InSpec Profile (long_commands)
|
|
|
|
|
Version: 0.1.0
|
|
|
|
|
Target: local://
|
2018-02-08 09:06:58 +00:00
|
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
report.send(:print_profile_header, profile).must_equal expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#print_standard_control_results" do
|
|
|
|
|
it "confirm stand control output" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
report.instance_variable_set(:@control_count, 0)
|
|
|
|
|
expected = <<~EOF
|
2019-06-11 22:24:35 +00:00
|
|
|
|
\e[38;5;41m ✔ tmp-1.0: Create /tmp directory\e[0m
|
|
|
|
|
\e[38;5;41m ✔ File /tmp should be directory\e[0m
|
2018-02-08 09:06:58 +00:00
|
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
windowize(expected) if WINDOWS
|
|
|
|
|
|
|
|
|
|
report.send(:print_standard_control_results, profile)
|
|
|
|
|
output = report.instance_variable_get(:@output)
|
|
|
|
|
output.must_equal expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#print_anonymous_control_results" do
|
|
|
|
|
it "confirm anon control output" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
report.instance_variable_set(:@control_count, 0)
|
|
|
|
|
expected = <<~EOF
|
2019-06-11 22:24:35 +00:00
|
|
|
|
File /tmp
|
|
|
|
|
\e[38;5;41m ✔ should be directory\e[0m
|
|
|
|
|
gem package rubocop
|
|
|
|
|
\e[38;5;9m × should be installed
|
|
|
|
|
rubocop is not installed\e[0m
|
|
|
|
|
stdout
|
|
|
|
|
\e[38;5;41m ✔ stdout should eq \"jquick\\n\"\e[0m
|
2018-02-08 09:06:58 +00:00
|
|
|
|
EOF
|
|
|
|
|
windowize(expected) if WINDOWS
|
|
|
|
|
|
|
|
|
|
report.send(:print_anonymous_control_results, profile)
|
|
|
|
|
output = report.instance_variable_get(:@output)
|
|
|
|
|
output.must_equal expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#format_profile_name" do
|
|
|
|
|
it "confirm profile name format" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
expected = "InSpec Profile (long_commands)"
|
|
|
|
|
|
|
|
|
|
report.send(:format_profile_name, profile).must_equal expected
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
it "confirm unknown name" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
profile[:name] = nil
|
|
|
|
|
expected = "InSpec Profile (unknown)"
|
|
|
|
|
|
|
|
|
|
report.send(:format_profile_name, profile).must_equal expected
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
it "confirm unknown title" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
profile[:title] = nil
|
|
|
|
|
expected = "long_commands"
|
|
|
|
|
|
|
|
|
|
report.send(:format_profile_name, profile).must_equal expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#format_control_header" do
|
|
|
|
|
it "confirm control header output" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
expected = " File /tmp"
|
|
|
|
|
profile_control = control.new(profile[:controls].first)
|
|
|
|
|
|
|
|
|
|
report.send(:format_control_header, profile_control).must_equal expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#format_result" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
let(:profile_control) do
|
|
|
|
|
control.new(profile[:controls].first)
|
|
|
|
|
end
|
|
|
|
|
let(:result) { profile_control.data[:results].first }
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
it "confirm standard result" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
output = report.send(:format_result, profile_control, result, :standard)
|
|
|
|
|
expected = "\e[38;5;41m ✔ File /tmp should be directory\e[0m"
|
|
|
|
|
windowize(expected) if WINDOWS
|
|
|
|
|
|
|
|
|
|
output.must_equal expected
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
it "confirm anonymous result" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
output = report.send(:format_result, profile_control, result, :anonymous)
|
|
|
|
|
expected = "\e[38;5;41m ✔ should be directory\e[0m"
|
|
|
|
|
windowize(expected) if WINDOWS
|
|
|
|
|
|
|
|
|
|
output.must_equal expected
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
it "confirm skip result" do
|
|
|
|
|
result[:status] = "skipped"
|
2018-02-08 09:06:58 +00:00
|
|
|
|
output = report.send(:format_result, profile_control, result, :anonymous)
|
|
|
|
|
expected = "\e[38;5;247m ↺ \e[0m"
|
|
|
|
|
windowize(expected) if WINDOWS
|
|
|
|
|
|
|
|
|
|
output.must_equal expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#print_profile_summary" do
|
|
|
|
|
let(:control) { profile[:controls].select { |c| c[:id] == "tmp-1.0" } }
|
|
|
|
|
it "confirm profile summary" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
report.send(:print_profile_summary)
|
|
|
|
|
expected = "Profile Summary: \e[38;5;41m1 successful control\e[0m, 0 control failures, 0 controls skipped\n"
|
|
|
|
|
windowize(expected) if WINDOWS
|
|
|
|
|
|
|
|
|
|
output = report.instance_variable_get(:@output)
|
|
|
|
|
output.must_equal expected
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
it "confirm profile summary with skip" do
|
|
|
|
|
control.first[:results].first[:status] = "skipped"
|
2018-02-08 09:06:58 +00:00
|
|
|
|
report.send(:print_profile_summary)
|
|
|
|
|
expected = "Profile Summary: 0 successful controls, 0 control failures, \e[38;5;247m1 control skipped\e[0m\n"
|
|
|
|
|
windowize(expected) if WINDOWS
|
|
|
|
|
|
|
|
|
|
output = report.instance_variable_get(:@output)
|
|
|
|
|
output.must_equal expected
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
it "confirm profile summary with fail" do
|
|
|
|
|
control.first[:results].first[:status] = "failed"
|
2018-02-08 09:06:58 +00:00
|
|
|
|
report.send(:print_profile_summary)
|
|
|
|
|
expected = "Profile Summary: 0 successful controls, \e[38;5;9m1 control failure\e[0m, 0 controls skipped\n"
|
|
|
|
|
windowize(expected) if WINDOWS
|
|
|
|
|
|
|
|
|
|
output = report.instance_variable_get(:@output)
|
|
|
|
|
output.must_equal expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#print_tests_summary" do
|
|
|
|
|
it "confirm tests summary" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
report.send(:print_tests_summary)
|
|
|
|
|
output = report.instance_variable_get(:@output)
|
|
|
|
|
expected = "Test Summary: \e[38;5;41m3 successful\e[0m, \e[38;5;9m1 failure\e[0m, 0 skipped\n"
|
|
|
|
|
windowize(expected) if WINDOWS
|
|
|
|
|
|
|
|
|
|
output.must_equal expected
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
it "confirm tests summary skip" do
|
|
|
|
|
profile[:controls].first[:results].first[:status] = "skipped"
|
2018-02-08 09:06:58 +00:00
|
|
|
|
report.send(:print_tests_summary)
|
|
|
|
|
output = report.instance_variable_get(:@output)
|
|
|
|
|
expected = "Test Summary: \e[38;5;41m2 successful\e[0m, \e[38;5;9m1 failure\e[0m, \e[38;5;247m1 skipped\e[0m\n"
|
|
|
|
|
windowize(expected) if WINDOWS
|
|
|
|
|
|
|
|
|
|
output.must_equal expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#format_with_color" do
|
|
|
|
|
it "confirm color format passed" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
expected = "\e[38;5;41mtest text\e[0m"
|
|
|
|
|
windowize(expected) if WINDOWS
|
2019-06-11 22:24:35 +00:00
|
|
|
|
report.send(:format_with_color, "passed", "test text").must_equal expected
|
2018-02-08 09:06:58 +00:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
it "confirm color format failed" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
expected = "\e[38;5;9mtest text\e[0m"
|
|
|
|
|
windowize(expected) if WINDOWS
|
2019-06-11 22:24:35 +00:00
|
|
|
|
report.send(:format_with_color, "failed", "test text").must_equal expected
|
2018-02-08 09:06:58 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#standard_controls_from_profile" do
|
|
|
|
|
it "confirm controls" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
result = report.send(:standard_controls_from_profile, profile)
|
|
|
|
|
result.count.must_equal 1
|
2019-06-11 22:24:35 +00:00
|
|
|
|
result.first[:id].must_equal "tmp-1.0"
|
2018-02-08 09:06:58 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#anonymous_controls_from_profile" do
|
|
|
|
|
it "confirm controls" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
result = report.send(:anonymous_controls_from_profile, profile)
|
|
|
|
|
result.count.must_equal 3
|
2018-03-28 15:22:01 +00:00
|
|
|
|
result.first[:id].must_match(/generated/)
|
2018-02-08 09:06:58 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#is_anonymous_control?" do
|
|
|
|
|
it "confirm anonymous control" do
|
|
|
|
|
controls = profile[:controls].select { |c| c[:id] != "tmp-1.0" }
|
2018-02-08 09:06:58 +00:00
|
|
|
|
report.send(:is_anonymous_control?, controls.first).must_equal true
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
it "confirm anonymous control false" do
|
|
|
|
|
controls = profile[:controls].select { |c| c[:id] == "tmp-1.0" }
|
2018-02-08 09:06:58 +00:00
|
|
|
|
report.send(:is_anonymous_control?, controls.first).must_equal false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#format_message" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
let(:message_info) do
|
|
|
|
|
{
|
2019-06-11 22:24:35 +00:00
|
|
|
|
indicator: "passed",
|
|
|
|
|
color: "passed",
|
2018-02-08 09:06:58 +00:00
|
|
|
|
indentation: 2,
|
2019-06-11 22:24:35 +00:00
|
|
|
|
message: "this is a test message",
|
2018-02-08 09:06:58 +00:00
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
it "confirm message format" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
expected = "\e[38;5;41m ✔ this is a test message\e[0m"
|
|
|
|
|
windowize(expected) if WINDOWS
|
|
|
|
|
report.send(:format_message, message_info).must_equal expected
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
it "confirm message format failed" do
|
|
|
|
|
message_info[:indicator] = "failed"
|
|
|
|
|
message_info[:color] = "failed"
|
2018-02-08 09:06:58 +00:00
|
|
|
|
expected = "\e[38;5;9m × this is a test message\e[0m"
|
|
|
|
|
windowize(expected) if WINDOWS
|
|
|
|
|
report.send(:format_message, message_info).must_equal expected
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
it "confirm message format skipped" do
|
|
|
|
|
message_info[:indicator] = "skipped"
|
|
|
|
|
message_info[:color] = "skipped"
|
2018-02-08 09:06:58 +00:00
|
|
|
|
expected = "\e[38;5;247m ↺ this is a test message\e[0m"
|
|
|
|
|
windowize(expected) if WINDOWS
|
|
|
|
|
report.send(:format_message, message_info).must_equal expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#indent_lines" do
|
|
|
|
|
it "confirm line indent" do
|
|
|
|
|
report.send(:indent_lines, "test", 2).must_equal " test"
|
2018-02-08 09:06:58 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#all_unique_controls" do
|
|
|
|
|
it "return unique controls" do
|
2018-02-08 09:06:58 +00:00
|
|
|
|
report.send(:all_unique_controls).count.must_equal 4
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#profile_summary" do
|
|
|
|
|
it "correct profile summary" do
|
|
|
|
|
expect = { "total" => 1, "failed" => 0, "skipped" => 0, "passed" => 1 }
|
2018-02-08 09:06:58 +00:00
|
|
|
|
report.send(:profile_summary).must_equal expect
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
|
describe "#tests_summary" do
|
|
|
|
|
it "correct tests summary" do
|
|
|
|
|
expect = { "total" => 0, "failed" => 1, "skipped" => 0, "passed" => 3 }
|
2018-02-08 09:06:58 +00:00
|
|
|
|
report.send(:tests_summary).must_equal expect
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|