Improved the unit/profiles/* tests.

Use better assertions for better failure messages.

Signed-off-by: Ryan Davis <zenspider@chef.io>
This commit is contained in:
Ryan Davis 2020-01-06 17:16:43 -08:00
parent 89a0b44c64
commit 07d5e84eb7
3 changed files with 73 additions and 70 deletions

View file

@ -66,14 +66,14 @@ EOF
res = Inspec::Metadata.from_yaml("mock", "---\nversion: '1.1.0'", nil)
Inspec::Metadata.finalize(res, "mock", empty_options)
_(res.params[:version]).must_equal("1.1.0")
_(res.valid_version?(res.params[:version])).must_equal(true)
_(res).must_be :valid_version?, res.params[:version]
end
it "does not accept invalid version from metadata" do
res = Inspec::Metadata.from_yaml("mock", "---\nversion: '1.1.0.1'", nil)
Inspec::Metadata.finalize(res, "mock", empty_options)
_(res.params[:version]).must_equal("1.1.0.1")
_(res.valid_version?(res.params[:version])).must_equal(false)
_(res).wont_be :valid_version?, res.params[:version]
end
it "finalizes a loaded metadata by turning strings into symbols" do
@ -128,62 +128,62 @@ EOF
it "load a profile with empty supports clause" do
m = supports_meta(nil)
_(m.supports_platform?(backend)).must_equal true
_(m).must_be :supports_platform?, backend
end
it "loads a profile which supports os ubuntu" do
m = supports_meta({ "os" => "ubuntu" })
_(m.supports_platform?(backend)).must_equal true
_(m).must_be :supports_platform?, backend
end
it "loads a profile which supports os name ubuntu" do
m = supports_meta({ "os-name" => "ubuntu" })
_(m.supports_platform?(backend)).must_equal true
_(m).must_be :supports_platform?, backend
end
it "loads a profile which supports os family linux" do
m = supports_meta({ "os-family" => "linux" })
_(m.supports_platform?(backend)).must_equal true
_(m).must_be :supports_platform?, backend
end
it "loads a profile which supports release 14.04" do
m = supports_meta({ "release" => "14.04" })
_(m.supports_platform?(backend)).must_equal true
_(m).must_be :supports_platform?, backend
end
it "rejects a profile which supports release 12.04" do
m = supports_meta({ "release" => "12.04" })
_(m.supports_platform?(backend)).must_equal false
_(m).wont_be :supports_platform?, backend
end
it "loads a profile which supports ubuntu 14.04" do
m = supports_meta({ "os-name" => "ubuntu", "release" => "14.04" })
_(m.supports_platform?(backend)).must_equal true
_(m).must_be :supports_platform?, backend
end
it "loads a profile which supports ubuntu 14.*" do
m = supports_meta({ "os-name" => "ubuntu", "release" => "14.*" })
_(m.supports_platform?(backend)).must_equal true
_(m).must_be :supports_platform?, backend
end
it "rejects a profile which supports ubuntu 12.04" do
m = supports_meta({ "os-name" => "ubuntu", "release" => "12.04" })
_(m.supports_platform?(backend)).must_equal false
_(m).wont_be :supports_platform?, backend
end
it "rejects a profile which supports ubuntu 12.*" do
m = supports_meta({ "os-name" => "ubuntu", "release" => "12.*" })
_(m.supports_platform?(backend)).must_equal false
_(m).wont_be :supports_platform?, backend
end
it "loads a profile which supports ubuntu float 14.04 as parsed by yml" do
m = supports_meta({ "os-name" => "ubuntu", "release" => 14.04 })
_(m.supports_platform?(backend)).must_equal true
_(m).must_be :supports_platform?, backend
end
it "reject unsupported os" do
m = supports_meta({ "os-name" => "windows" })
_(m.supports_platform?(backend)).must_equal false
_(m).wont_be :supports_platform?, backend
end
it "loads a profile which supports multiple families" do
@ -191,7 +191,7 @@ EOF
{ "os-family" => "windows" },
{ "os-family" => "unix" },
])
_(m.supports_platform?(backend)).must_equal true
_(m).must_be :supports_platform?, backend
end
it "loads a profile which supports multiple names" do
@ -199,7 +199,7 @@ EOF
{ "os-family" => "windows", "os-name" => "windows_2000" },
{ "os-family" => "unix", "os-name" => "ubuntu" },
])
_(m.supports_platform?(backend)).must_equal true
_(m).must_be :supports_platform?, backend
end
it "reject a profile which supports multiple families" do
@ -207,7 +207,7 @@ EOF
{ "os-family" => "windows" },
{ "os-family" => "redhat" },
])
_(m.supports_platform?(backend)).must_equal false
_(m).wont_be :supports_platform?, backend
end
end
@ -224,32 +224,32 @@ EOF
it "returns true on testing the current version" do
m = version_meta(current_version)
_(m.supports_runtime?).must_equal true
_(m).must_be :supports_runtime?
end
it "returns true on testing the current version" do
m = version_meta("= " + current_version)
_(m.supports_runtime?).must_equal true
_(m).must_be :supports_runtime?
end
it "returns true on testing >= current version" do
m = version_meta(">= " + current_version)
_(m.supports_runtime?).must_equal true
_(m).must_be :supports_runtime?
end
it "returns false on testing >= the next version" do
m = version_meta(">= " + next_version)
_(m.supports_runtime?).must_equal false
_(m).wont_be :supports_runtime?
end
it "returns false on testing > the next version" do
m = version_meta("> " + next_version)
_(m.supports_runtime?).must_equal false
_(m).wont_be :supports_runtime?
end
it "is included in valid?" do
m = version_meta("> #{next_version}")
refute m.valid?
refute_predicate m, :valid?
end
end
end

View file

@ -10,7 +10,7 @@ end
module DescribeOneTest
it "loads an empty describe.one" do
profile.load(format(context_format, "describe.one"))
_(get_checks).must_equal([])
_(get_checks).must_be_empty
end
it "loads an empty describe.one block" do
@ -66,8 +66,12 @@ describe Inspec::ProfileContext do
end
describe "its default DSL" do
def load(call)
_ { profile.load(call) }
def assert_load(src, output)
_ { profile.load(src) }.must_output output
end
def assert_load_raises(src, exception)
_ { profile.load(src) }.must_raise exception
end
let(:context_format) { "%s" }
@ -75,34 +79,33 @@ describe Inspec::ProfileContext do
include DescribeOneTest
it "must provide os resource" do
load("print os[:family]").must_output "debian"
assert_load("print os[:family]", "debian")
end
it "must provide file resource" do
load('print file("/etc/passwd").type').must_output "file"
assert_load('print file("/etc/passwd").type', "file")
end
it "must provide command resource" do
load('print command("").stdout').must_output ""
assert_load('print command("").stdout', "")
end
it "supports empty describe calls" do
load("describe").must_output ""
assert_load("describe", "")
_(profile.rules.keys.length).must_equal 1
_(profile.rules.keys[0]).must_match(/^\(generated from \(eval\):1 [0-9a-f]+\)$/)
_(profile.rules.values[0]).must_be_kind_of Inspec::Rule
end
it "provides the describe keyword in the global DSL" do
load("describe true do; it { should_eq true }; end").must_output ""
assert_load("describe true do; it { should_eq true }; end", "")
_(profile.rules.keys.length).must_equal 1
_(profile.rules.keys[0]).must_match(/^\(generated from \(eval\):1 [0-9a-f]+\)$/)
_(profile.rules.values[0]).must_be_kind_of Inspec::Rule
end
it "loads multiple computed calls to describe correctly" do
load("%w{1 2 3}.each do\ndescribe true do; it { should_eq true }; end\nend")
.must_output ""
assert_load("%w{1 2 3}.each do\ndescribe true do; it { should_eq true }; end\nend", "")
_(profile.rules.keys.length).must_equal 3
[0, 1, 2].each do |i|
_(profile.rules.keys[i]).must_match(/^\(generated from \(eval\):2 [0-9a-f]+\)$/)
@ -111,7 +114,7 @@ describe Inspec::ProfileContext do
end
it "does not provide the expect keyword in the global DSL" do
load("expect(true).to_eq true").must_raise NoMethodError
assert_load_raises("expect(true).to_eq true", NoMethodError)
end
describe "global only_if" do
@ -123,7 +126,7 @@ describe Inspec::ProfileContext do
it "provides the keyword" do
profile.load(if_true)
_(profile.rules).must_equal({})
_(profile.rules).must_be_empty
end
it "doesnt affect controls when positive" do
@ -139,17 +142,17 @@ describe Inspec::ProfileContext do
it "alters controls when positive" do
profile.load(if_false + control)
_(get_checks.length).must_equal 1
_(get_checks[0][1][0].resource_skipped?).must_equal true
_(get_checks[0][1][0]).must_be :resource_skipped?
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
_(get_checks[0][1][0].resource_failed?).must_equal false
_(get_checks[0][1][0]).wont_be :resource_failed?
end
it "alters non-controls when positive" do
profile.load(if_false + describe)
_(get_checks.length).must_equal 1
_(get_checks[0][1][0].resource_skipped?).must_equal true
_(get_checks[0][1][0]).must_be :resource_skipped?
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
_(get_checks[0][1][0].resource_failed?).must_equal false
_(get_checks[0][1][0]).wont_be :resource_failed?
end
it "doesnt alter controls when negative" do
@ -167,26 +170,26 @@ describe Inspec::ProfileContext do
it "doesnt overwrite falsy only_ifs" do
profile.load(if_false + if_true + control)
_(get_checks.length).must_equal 1
_(get_checks[0][1][0].resource_skipped?).must_equal true
_(get_checks[0][1][0]).must_be :resource_skipped?
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
_(get_checks[0][1][0].resource_failed?).must_equal false
_(get_checks[0][1][0]).wont_be :resource_failed?
end
it "doesnt overwrite falsy only_ifs" do
profile.load(if_true + if_false + control)
_(get_checks.length).must_equal 1
_(get_checks[0][1][0].resource_skipped?).must_equal true
_(get_checks[0][1][0]).must_be :resource_skipped?
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
_(get_checks[0][1][0].resource_failed?).must_equal false
_(get_checks[0][1][0]).wont_be :resource_failed?
end
it "allows specifying a message with true only_if" do
profile.load("only_if('this is a only_if skipped message') { false }\n" + control)
_(get_checks.length).must_equal 1
_(get_checks[0][1][0].resource_skipped?).must_equal true
_(get_checks[0][1][0]).must_be :resource_skipped?
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped" \
" control due to only_if condition: this is a only_if skipped message"
_(get_checks[0][1][0].resource_failed?).must_equal false
_(get_checks[0][1][0]).wont_be :resource_failed?
end
it "doesnt extend into other control files" do
@ -256,14 +259,14 @@ describe Inspec::ProfileContext do
it "doesnt add any checks if none are provided" do
profile.load("rule #{rule_id.inspect}")
rule = profile.rules[rule_id]
_(::Inspec::Rule.prepare_checks(rule)).must_equal([])
_(::Inspec::Rule.prepare_checks(rule)).must_be_empty
end
describe "supports empty describe blocks" do
it "doesnt crash, but doesnt add anything either" do
profile.load(format(context_format, "describe"))
_(profile.rules.keys).must_include(rule_id)
_(get_checks).must_equal([])
_(get_checks).must_be_empty
end
end
@ -338,36 +341,36 @@ describe Inspec::ProfileContext do
describe "with only_if" do
it "provides the only_if keyword" do
profile.load(format(context_format, "only_if"))
_(get_checks).must_equal([])
_(get_checks).must_be_empty
end
it "skips with only_if == false" do
profile.load(format(context_format, "only_if { false }"))
_(get_checks.length).must_equal 1
_(get_checks[0][1][0].resource_skipped?).must_equal true
_(get_checks[0][1][0]).must_be :resource_skipped?
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
_(get_checks[0][1][0].resource_failed?).must_equal false
_(get_checks[0][1][0]).wont_be :resource_failed?
end
it "does nothing with only_if == false" do
profile.load(format(context_format, "only_if { true }"))
_(get_checks.length).must_equal 0
_(get_checks).must_be_empty
end
it "doesnt overwrite falsy only_ifs" do
profile.load(format(context_format, "only_if { false }\nonly_if { true }"))
_(get_checks.length).must_equal 1
_(get_checks[0][1][0].resource_skipped?).must_equal true
_(get_checks[0][1][0]).must_be :resource_skipped?
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
_(get_checks[0][1][0].resource_failed?).must_equal false
_(get_checks[0][1][0]).wont_be :resource_failed?
end
it "doesnt overwrite falsy only_ifs" do
profile.load(format(context_format, "only_if { true }\nonly_if { false }"))
_(get_checks.length).must_equal 1
_(get_checks[0][1][0].resource_skipped?).must_equal true
_(get_checks[0][1][0]).must_be :resource_skipped?
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
_(get_checks[0][1][0].resource_failed?).must_equal false
_(get_checks[0][1][0]).wont_be :resource_failed?
end
end
end

View file

@ -17,7 +17,7 @@ describe Inspec::Profile do
end
it "has no controls" do
_(profile.params[:controls]).must_equal({})
_(profile.params[:controls]).must_be_empty
end
end
@ -30,7 +30,7 @@ describe Inspec::Profile do
end
it "has no controls" do
_(profile.params[:controls]).must_equal({})
_(profile.params[:controls]).must_be_empty
end
it "can overwrite the profile ID" do
@ -146,7 +146,7 @@ describe Inspec::Profile do
_(result[:summary][:location]).must_equal "#{home}/test/fixtures/profiles/#{profile_id}"
_(result[:summary][:profile]).must_equal "name"
_(result[:summary][:controls]).must_equal 0
_(result[:errors].length).must_equal 0
_(result[:errors]).must_be_empty
_(result[:warnings].length).must_equal 1
end
end
@ -169,8 +169,8 @@ describe Inspec::Profile do
_(result[:summary][:location]).must_equal "#{home}/test/fixtures/profiles/#{profile_id}"
_(result[:summary][:profile]).must_equal "complete"
_(result[:summary][:controls]).must_equal 1
_(result[:errors].length).must_equal 0
_(result[:warnings].length).must_equal 0
_(result[:errors]).must_be_empty
_(result[:warnings]).must_be_empty
end
end
@ -194,8 +194,8 @@ describe Inspec::Profile do
_(result[:summary][:location]).must_equal "#{home}/test/fixtures/profiles/#{profile_id}"
_(result[:summary][:profile]).must_equal "complete"
_(result[:summary][:controls]).must_equal 1
_(result[:errors].length).must_equal 0
_(result[:warnings].length).must_equal 0
_(result[:errors]).must_be_empty
_(result[:warnings]).must_be_empty
end
end
@ -219,8 +219,8 @@ describe Inspec::Profile do
_(result[:summary][:location]).must_equal "#{home}/test/fixtures/profiles/#{profile_id}"
_(result[:summary][:profile]).must_equal "complete"
_(result[:summary][:controls]).must_equal 1
_(result[:errors].length).must_equal 0
_(result[:warnings].length).must_equal 0
_(result[:errors]).must_be_empty
_(result[:warnings]).must_be_empty
end
end
@ -244,8 +244,8 @@ describe Inspec::Profile do
_(result[:summary][:location]).must_equal "#{home}/test/fixtures/profiles/#{profile_id}"
_(result[:summary][:profile]).must_equal "complete"
_(result[:summary][:controls]).must_equal 1
_(result[:errors].length).must_equal 0
_(result[:warnings].length).must_equal 0
_(result[:errors]).must_be_empty
_(result[:warnings]).must_be_empty
end
end
@ -287,7 +287,7 @@ describe Inspec::Profile do
result = MockLoader.load_profile(profile_path, { logger: logger }).check
logger.verify
_(result[:warnings].length).must_equal 0
_(result[:warnings]).must_be_empty
_(result[:errors].length).must_equal 1
end
end
@ -314,7 +314,7 @@ describe Inspec::Profile do
_(result[:summary][:profile]).must_equal "license-invalid"
_(result[:summary][:controls]).must_equal 0
_(result[:errors].length).must_equal 0
_(result[:errors]).must_be_empty
_(result[:warnings].length).must_equal 2
end
@ -339,7 +339,7 @@ describe Inspec::Profile do
_(result[:summary][:profile]).must_equal "license-spdx"
_(result[:summary][:controls]).must_equal 0
_(result[:errors].length).must_equal 0
_(result[:errors]).must_be_empty
_(result[:warnings].length).must_equal 1
end
end
@ -365,7 +365,7 @@ describe Inspec::Profile do
_(result[:summary][:profile]).must_equal "license-proprietary"
_(result[:summary][:controls]).must_equal 0
_(result[:errors].length).must_equal 0
_(result[:errors]).must_be_empty
_(result[:warnings].length).must_equal 1
end
end