2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/objects"
|
2016-05-13 16:53:20 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Objects" do
|
|
|
|
describe "Inspec::Test" do
|
2016-05-13 16:53:20 +00:00
|
|
|
let(:obj) { Inspec::Test.new }
|
2017-02-03 19:26:02 +00:00
|
|
|
it 'constructs a simple resource + its("argument")' do
|
2019-06-11 22:24:35 +00:00
|
|
|
obj.qualifier = [["resource"], ["version"]]
|
|
|
|
obj.matcher = "cmp >="
|
|
|
|
obj.expectation = "2.4.2"
|
2017-01-30 10:58:56 +00:00
|
|
|
obj.to_ruby.must_equal '
|
2016-05-13 16:53:20 +00:00
|
|
|
describe resource do
|
2017-01-30 11:21:57 +00:00
|
|
|
its("version") { should cmp >= "2.4.2" }
|
2016-05-13 16:53:20 +00:00
|
|
|
end
|
2017-01-30 10:58:56 +00:00
|
|
|
'.strip
|
2016-05-13 16:53:20 +00:00
|
|
|
end
|
|
|
|
|
2017-02-03 19:26:02 +00:00
|
|
|
# same as the above test but with it
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a simple resource.argument" do
|
2017-02-03 19:26:02 +00:00
|
|
|
# [''] forces an 'it' instead of 'its':
|
2019-06-11 22:24:35 +00:00
|
|
|
obj.qualifier = [["resource"], ["version"], [""]]
|
|
|
|
obj.matcher = "cmp >="
|
|
|
|
obj.expectation = "2.4.2"
|
2017-02-03 19:26:02 +00:00
|
|
|
obj.to_ruby.must_equal '
|
|
|
|
describe resource.version do
|
|
|
|
it { should cmp >= "2.4.2" }
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a simple resource+argument with to_s" do
|
|
|
|
obj.qualifier = [["resource"], ["to_s"]]
|
|
|
|
obj.matcher = "cmp"
|
|
|
|
obj.expectation = Regexp.new("^Desc.+$")
|
2017-01-30 11:21:57 +00:00
|
|
|
obj.to_ruby.must_equal '
|
2016-05-13 16:53:20 +00:00
|
|
|
describe resource.to_s do
|
2017-01-30 11:51:06 +00:00
|
|
|
it { should cmp(/^Desc.+$/) }
|
2016-05-13 16:53:20 +00:00
|
|
|
end
|
2017-01-30 11:21:57 +00:00
|
|
|
'.strip
|
2016-05-13 16:53:20 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a simple resource+argument with to_i" do
|
|
|
|
obj.qualifier = [["resource"], ["to_i"]]
|
|
|
|
obj.matcher = "cmp >"
|
2017-01-30 11:21:57 +00:00
|
|
|
obj.expectation = 3
|
|
|
|
obj.to_ruby.must_equal '
|
2016-05-13 16:53:20 +00:00
|
|
|
describe resource.to_i do
|
2017-01-30 11:21:57 +00:00
|
|
|
it { should cmp > 3 }
|
2016-05-13 16:53:20 +00:00
|
|
|
end
|
2017-01-30 11:21:57 +00:00
|
|
|
'.strip
|
2016-05-13 16:53:20 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a simple resource+argument with array accessors" do
|
|
|
|
obj.qualifier = [["resource"], ["name[2]"]]
|
|
|
|
obj.matcher = "exist"
|
|
|
|
obj.matcher = "eq"
|
|
|
|
obj.expectation = "mytest"
|
2017-01-30 11:21:57 +00:00
|
|
|
obj.to_ruby.must_equal '
|
2016-05-13 16:53:20 +00:00
|
|
|
describe resource.name[2] do
|
2017-01-30 11:21:57 +00:00
|
|
|
it { should eq "mytest" }
|
2016-05-13 16:53:20 +00:00
|
|
|
end
|
2017-01-30 11:21:57 +00:00
|
|
|
'.strip
|
2016-05-13 16:53:20 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a simple resource+argument with method calls" do
|
|
|
|
obj.qualifier = [["resource"], %w{hello world}]
|
|
|
|
obj.matcher = "eq"
|
|
|
|
obj.expectation = "mytest"
|
2017-01-30 10:58:56 +00:00
|
|
|
obj.to_ruby.must_equal '
|
|
|
|
describe resource.hello("world") do
|
2017-01-30 11:21:57 +00:00
|
|
|
it { should eq "mytest" }
|
2016-05-13 16:53:20 +00:00
|
|
|
end
|
2017-01-30 10:58:56 +00:00
|
|
|
'.strip
|
2016-05-13 18:33:28 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a simple resource+argument with method calls" do
|
|
|
|
obj.qualifier = [["resource"], %w{hello world}]
|
|
|
|
obj.matcher = "be_in"
|
|
|
|
obj.expectation = %w{mytest mytest2 mytest3}
|
2017-08-07 14:05:22 +00:00
|
|
|
obj.to_ruby.must_equal '
|
|
|
|
describe resource.hello("world") do
|
|
|
|
it { should be_in ["mytest", "mytest2", "mytest3"] }
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a simple resource+argument with method calls" do
|
|
|
|
obj.qualifier = [["resource"], %w{hello world}]
|
|
|
|
obj.matcher = "be_in"
|
2017-08-07 14:05:22 +00:00
|
|
|
obj.negate!
|
2019-06-11 22:24:35 +00:00
|
|
|
obj.expectation = %w{mytest2 mytest3 mytest4}
|
2017-08-07 14:05:22 +00:00
|
|
|
obj.to_ruby.must_equal '
|
|
|
|
describe resource.hello("world") do
|
|
|
|
it { should_not be_in ["mytest2", "mytest3", "mytest4"] }
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a simple resource+argument with method calls" do
|
2017-08-07 14:05:22 +00:00
|
|
|
obj.qualifier = [['["item1","item2","item3"]']]
|
2019-06-11 22:24:35 +00:00
|
|
|
obj.matcher = "be_in"
|
|
|
|
obj.expectation = %w{item1 item2 item3 item4 item5}
|
2017-08-07 14:05:22 +00:00
|
|
|
obj.to_ruby.must_equal '
|
|
|
|
describe ["item1","item2","item3"] do
|
|
|
|
it { should be_in ["item1", "item2", "item3", "item4", "item5"] }
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a simple resource+argument with method calls" do
|
|
|
|
obj.qualifier = [["resource"], [:mode]]
|
|
|
|
obj.matcher = "cmp"
|
|
|
|
obj.expectation = "0755"
|
2017-01-30 10:58:56 +00:00
|
|
|
obj.to_ruby.must_equal '
|
2016-05-13 18:33:28 +00:00
|
|
|
describe resource do
|
2017-01-30 11:21:57 +00:00
|
|
|
its("mode") { should cmp "0755" }
|
2016-05-13 18:33:28 +00:00
|
|
|
end
|
2017-01-30 10:58:56 +00:00
|
|
|
'.strip
|
2016-05-13 16:53:20 +00:00
|
|
|
end
|
2017-01-27 17:19:57 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a resource+argument block with method call, matcher and expectation" do
|
|
|
|
obj.qualifier = [["command", "ls /etc"], ["exit_status"]]
|
|
|
|
obj.matcher = "eq"
|
2017-01-27 17:19:57 +00:00
|
|
|
obj.expectation = 0
|
|
|
|
|
|
|
|
obj.to_ruby.must_equal '
|
|
|
|
describe command("ls /etc") do
|
|
|
|
its("exit_status") { should eq 0 }
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a simple describe with static data, negated regex matcher and expectation" do
|
2017-01-27 17:19:57 +00:00
|
|
|
obj.qualifier = [['"aaa"']]
|
2019-06-11 22:24:35 +00:00
|
|
|
obj.matcher = "match"
|
2017-01-27 17:19:57 +00:00
|
|
|
obj.negate!
|
2019-06-11 22:24:35 +00:00
|
|
|
obj.expectation = Regexp.new("^aa.*")
|
2017-01-27 17:19:57 +00:00
|
|
|
|
|
|
|
obj.to_ruby.must_equal '
|
|
|
|
describe "aaa" do
|
|
|
|
it { should_not match(/^aa.*/) }
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a resource+argument block without a property call" do
|
2019-07-03 21:24:41 +00:00
|
|
|
obj.qualifier = [%w{service avahi-daemon}]
|
2017-01-27 17:19:57 +00:00
|
|
|
obj.qualifier.push(["info['properties']['UnitFileState']"])
|
2019-06-11 22:24:35 +00:00
|
|
|
obj.expectation = "enabled"
|
|
|
|
obj.matcher = "eq"
|
2017-01-27 17:19:57 +00:00
|
|
|
obj.to_ruby.must_equal '
|
|
|
|
describe service("avahi-daemon").info[\'properties\'][\'UnitFileState\'] do
|
|
|
|
it { should eq "enabled" }
|
|
|
|
end
|
2018-11-08 17:46:19 +00:00
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a simple resource + only_if" do
|
|
|
|
obj.qualifier = [["resource"], ["version"]]
|
|
|
|
obj.matcher = "cmp >="
|
|
|
|
obj.expectation = "2.4.2"
|
2018-11-08 17:46:19 +00:00
|
|
|
obj.only_if = "package('ntp').installed?"
|
|
|
|
obj.to_ruby.must_equal '
|
|
|
|
only_if { package(\'ntp\').installed? }
|
|
|
|
describe resource do
|
|
|
|
its("version") { should cmp >= "2.4.2" }
|
|
|
|
end
|
2017-01-27 17:19:57 +00:00
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Inspec::EachLoop, each_loop" do
|
|
|
|
it "constructs an each loop to match listening addresses" do
|
2017-01-27 17:19:57 +00:00
|
|
|
loop_obj = Inspec::EachLoop.new
|
2019-06-11 22:24:35 +00:00
|
|
|
loop_obj.qualifier = [["port", 25]]
|
|
|
|
loop_obj.qualifier.push(["addresses"])
|
2017-01-27 17:19:57 +00:00
|
|
|
obj = Inspec::Test.new
|
2019-06-11 22:24:35 +00:00
|
|
|
obj.matcher = "match"
|
2017-01-27 17:19:57 +00:00
|
|
|
obj.negate!
|
2019-06-11 22:24:35 +00:00
|
|
|
obj.expectation = "0.0.0.0"
|
2017-01-27 17:19:57 +00:00
|
|
|
loop_obj.add_test(obj)
|
|
|
|
loop_obj.to_ruby.must_equal '
|
|
|
|
port(25).addresses.each do |entry|
|
|
|
|
describe entry do
|
2017-01-30 11:51:06 +00:00
|
|
|
it { should_not match "0.0.0.0" }
|
2017-01-27 17:19:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Inspec::List" do
|
|
|
|
it "constructs a list filtering test" do
|
|
|
|
list_obj = Inspec::List.new([["passwd"]])
|
2017-01-30 10:16:35 +00:00
|
|
|
list_obj.qualifier.push(["where { user =~ /^(?!root|sync|shutdown|halt).*$/ }"])
|
|
|
|
|
|
|
|
obj = Inspec::Test.new
|
|
|
|
obj.qualifier = list_obj.qualifier
|
2019-06-11 22:24:35 +00:00
|
|
|
obj.matcher = "be_empty"
|
|
|
|
obj.qualifier.push(["entries"])
|
2017-01-30 10:16:35 +00:00
|
|
|
obj.negate!
|
|
|
|
obj.to_ruby.must_equal '
|
|
|
|
describe passwd.where { user =~ /^(?!root|sync|shutdown|halt).*$/ } do
|
|
|
|
its("entries") { should_not be_empty }
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Inspec::OrTest and Inspec::Control" do
|
2017-01-27 17:19:57 +00:00
|
|
|
let(:obj1) do
|
|
|
|
obj1 = Inspec::Test.new
|
2019-06-11 22:24:35 +00:00
|
|
|
obj1.qualifier = [["command", "ls /etc"], ["exit_status"]]
|
|
|
|
obj1.matcher = "eq"
|
2017-01-27 17:19:57 +00:00
|
|
|
obj1.expectation = 0
|
|
|
|
obj1
|
|
|
|
end
|
|
|
|
let(:obj2) do
|
|
|
|
obj2 = obj1.dup
|
|
|
|
obj2.negate!
|
|
|
|
obj2.expectation = 100
|
|
|
|
obj2
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a simple describe.one block wrapping two tests" do
|
|
|
|
or_obj = Inspec::OrTest.new([obj1, obj2])
|
2017-01-27 17:19:57 +00:00
|
|
|
or_obj.to_ruby.must_equal '
|
|
|
|
describe.one do
|
|
|
|
describe command("ls /etc") do
|
|
|
|
its("exit_status") { should eq 0 }
|
|
|
|
end
|
|
|
|
describe command("ls /etc") do
|
|
|
|
its("exit_status") { should_not eq 100 }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "negates a describe.one block, wow!" do
|
|
|
|
or_obj = Inspec::OrTest.new([obj1, obj2])
|
2017-01-27 17:19:57 +00:00
|
|
|
or_obj.negate!
|
|
|
|
or_obj.to_ruby.must_equal '
|
|
|
|
describe command("ls /etc") do
|
|
|
|
its("exit_status") { should_not eq 0 }
|
|
|
|
end
|
|
|
|
describe command("ls /etc") do
|
|
|
|
its("exit_status") { should eq 100 }
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loops a describe.one block, ooooooo!" do
|
2017-02-03 14:58:51 +00:00
|
|
|
res = Inspec::EachLoop.new
|
2019-06-11 22:24:35 +00:00
|
|
|
res.qualifier.push(["(1..5)"])
|
2017-02-03 14:58:51 +00:00
|
|
|
# already defined in the let block:
|
2019-06-11 22:24:35 +00:00
|
|
|
obj1.matcher = "eq entity"
|
|
|
|
obj2.matcher = "eq entity"
|
2017-02-03 14:58:51 +00:00
|
|
|
obj1.remove_expectation
|
|
|
|
obj2.remove_expectation
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
or_obj = Inspec::OrTest.new([obj1, obj2])
|
2017-02-03 14:58:51 +00:00
|
|
|
res.tests = [or_obj]
|
|
|
|
|
|
|
|
res.to_ruby.must_equal '
|
|
|
|
(1..5).each do |entry|
|
|
|
|
describe.one do
|
|
|
|
describe command("ls /etc") do
|
|
|
|
its("exit_status") { should eq entity }
|
|
|
|
end
|
|
|
|
describe command("ls /etc") do
|
|
|
|
its("exit_status") { should_not eq entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a control" do
|
2017-01-30 10:16:35 +00:00
|
|
|
control = Inspec::Control.new
|
|
|
|
control.add_test(obj1)
|
2019-06-11 22:24:35 +00:00
|
|
|
control.id = "sample.control.id"
|
|
|
|
control.title = "Sample Control Important Title"
|
2018-09-26 17:28:58 +00:00
|
|
|
control.descriptions = {
|
2019-06-11 22:24:35 +00:00
|
|
|
default: "The most critical control the world has ever seen",
|
|
|
|
rationale: "It is needed to save the planet",
|
|
|
|
'more info': "Insert clever joke here",
|
2018-09-26 17:28:58 +00:00
|
|
|
}
|
2019-06-11 22:24:35 +00:00
|
|
|
control.refs = ["simple ref", { ref: "title", url: "my url" }]
|
2017-01-30 10:16:35 +00:00
|
|
|
control.impact = 1.0
|
|
|
|
control.to_ruby.must_equal '
|
2017-01-30 10:58:56 +00:00
|
|
|
control "sample.control.id" do
|
|
|
|
title "Sample Control Important Title"
|
2017-01-30 10:16:35 +00:00
|
|
|
desc "The most critical control the world has ever seen"
|
2018-09-26 17:28:58 +00:00
|
|
|
desc "rationale", "It is needed to save the planet"
|
|
|
|
desc "more info", "Insert clever joke here"
|
2017-01-30 10:16:35 +00:00
|
|
|
impact 1.0
|
2017-10-24 21:50:23 +00:00
|
|
|
ref "simple ref"
|
|
|
|
ref ({:ref=>"title", :url=>"my url"})
|
2017-01-30 10:16:35 +00:00
|
|
|
describe command("ls /etc") do
|
|
|
|
its("exit_status") { should eq 0 }
|
2016-05-13 16:53:20 +00:00
|
|
|
end
|
|
|
|
end
|
2017-04-22 10:50:47 +00:00
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a control with only_if" do
|
2018-11-08 17:46:19 +00:00
|
|
|
control = Inspec::Control.new
|
|
|
|
control.add_test(obj1)
|
|
|
|
control.only_if = "package('ntp').installed?"
|
2019-06-11 22:24:35 +00:00
|
|
|
control.id = "sample.control.id"
|
|
|
|
control.title = "Sample Control Important Title"
|
2018-11-08 17:46:19 +00:00
|
|
|
control.descriptions = {
|
2019-06-11 22:24:35 +00:00
|
|
|
default: "The most critical control the world has ever seen",
|
|
|
|
rationale: "It is needed to save the planet",
|
|
|
|
'more info': "Insert clever joke here",
|
2018-11-08 17:46:19 +00:00
|
|
|
}
|
2019-06-11 22:24:35 +00:00
|
|
|
control.refs = ["simple ref", { ref: "title", url: "my url" }]
|
2018-11-08 17:46:19 +00:00
|
|
|
control.impact = 1.0
|
|
|
|
control.to_ruby.must_equal '
|
|
|
|
control "sample.control.id" do
|
|
|
|
title "Sample Control Important Title"
|
|
|
|
desc "The most critical control the world has ever seen"
|
|
|
|
desc "rationale", "It is needed to save the planet"
|
|
|
|
desc "more info", "Insert clever joke here"
|
|
|
|
impact 1.0
|
|
|
|
ref "simple ref"
|
|
|
|
ref ({:ref=>"title", :url=>"my url"})
|
|
|
|
only_if { package(\'ntp\').installed? }
|
|
|
|
describe command("ls /etc") do
|
|
|
|
its("exit_status") { should eq 0 }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "constructs a multiline desc in a control with indentation" do
|
2017-04-22 10:50:47 +00:00
|
|
|
control = Inspec::Control.new
|
2018-09-26 17:28:58 +00:00
|
|
|
control.descriptions[:default] = "Multiline\n control"
|
2017-04-22 10:50:47 +00:00
|
|
|
control.to_ruby.must_equal '
|
|
|
|
control nil do
|
2017-04-25 14:20:29 +00:00
|
|
|
desc "
|
|
|
|
Multiline
|
|
|
|
control
|
|
|
|
"
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "ignores empty control descriptions" do
|
2017-04-25 14:20:29 +00:00
|
|
|
control = Inspec::Control.new
|
|
|
|
x = '
|
|
|
|
control nil do
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
control.descriptions[:default] = ""
|
2017-04-25 14:20:29 +00:00
|
|
|
control.to_ruby.must_equal x
|
|
|
|
|
2018-09-26 17:28:58 +00:00
|
|
|
control.descriptions[:default] = nil
|
2017-04-25 14:20:29 +00:00
|
|
|
control.to_ruby.must_equal x
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "handles non-string descriptions" do
|
2017-04-25 14:20:29 +00:00
|
|
|
control = Inspec::Control.new
|
2018-09-26 17:28:58 +00:00
|
|
|
control.descriptions[:default] = 123
|
2017-04-25 14:20:29 +00:00
|
|
|
control.to_ruby.must_equal '
|
|
|
|
control nil do
|
|
|
|
desc "123"
|
2017-04-22 10:50:47 +00:00
|
|
|
end
|
2017-01-30 10:16:35 +00:00
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Inspec::Variable, take #1" do
|
|
|
|
it "constructs a control with variable to instantiate a resource only once" do
|
2017-01-30 10:58:56 +00:00
|
|
|
control = Inspec::Control.new
|
2019-06-11 22:24:35 +00:00
|
|
|
variable = Inspec::Value.new([["command", "which grep"]])
|
2017-01-30 10:58:56 +00:00
|
|
|
variable_id = variable.name_variable.to_s
|
|
|
|
|
|
|
|
obj1 = Inspec::Test.new
|
|
|
|
obj1.variables.push(variable)
|
|
|
|
obj1.qualifier.push([variable_id])
|
2019-06-11 22:24:35 +00:00
|
|
|
obj1.qualifier.push(["exit_status"])
|
|
|
|
obj1.matcher = "eq"
|
2017-01-30 10:58:56 +00:00
|
|
|
obj1.expectation = 0
|
|
|
|
control.add_test(obj1)
|
|
|
|
|
|
|
|
obj2 = Inspec::Test.new
|
|
|
|
obj2.qualifier.push([variable_id.to_s])
|
2019-06-11 22:24:35 +00:00
|
|
|
obj2.qualifier.push(["stdout"])
|
|
|
|
obj2.matcher = "contain"
|
|
|
|
obj2.expectation = "grep"
|
2017-01-30 10:58:56 +00:00
|
|
|
control.add_test(obj2)
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
control.id = "variable.control.id"
|
|
|
|
control.title = "Variable Control Important Title"
|
|
|
|
control.descriptions[:default] = "The most variable control the world has ever seen"
|
2017-01-30 10:58:56 +00:00
|
|
|
control.impact = 1.0
|
|
|
|
control.to_ruby.must_equal '
|
|
|
|
control "variable.control.id" do
|
|
|
|
title "Variable Control Important Title"
|
|
|
|
desc "The most variable control the world has ever seen"
|
|
|
|
impact 1.0
|
|
|
|
a = command("which grep")
|
|
|
|
describe a do
|
|
|
|
its("exit_status") { should eq 0 }
|
|
|
|
end
|
|
|
|
describe a do
|
|
|
|
its("stdout") { should contain "grep" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
end
|
2017-01-30 10:16:35 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Inspec::Variable, take #2" do
|
|
|
|
it "constructs a control with variable, loop and var reference" do
|
2017-02-03 08:27:51 +00:00
|
|
|
control = Inspec::Control.new
|
2019-07-09 00:20:30 +00:00
|
|
|
command_value = %r{^/usr/bin/chrony}
|
2019-06-11 22:24:35 +00:00
|
|
|
pid_filter = ">"
|
2017-02-03 08:27:51 +00:00
|
|
|
pid_value = 0
|
|
|
|
loopy = Inspec::EachLoop.new
|
2019-06-11 22:24:35 +00:00
|
|
|
loopy.qualifier = [["processes", command_value]]
|
2017-02-03 08:27:51 +00:00
|
|
|
loopy.qualifier.push(["where { pid #{pid_filter} #{pid_value} }.entries"])
|
|
|
|
obj = loopy.add_test
|
|
|
|
|
|
|
|
variable = Inspec::Value.new([['passwd.where { user == "_chrony" }.uids.first']])
|
|
|
|
variable_id = variable.name_variable.to_s
|
|
|
|
obj.variables.push(variable)
|
2019-06-11 22:24:35 +00:00
|
|
|
obj.qualifier = [["user(entry.user)"], ["uid"]]
|
2017-02-03 08:27:51 +00:00
|
|
|
obj.matcher = "cmp #{variable_id}"
|
|
|
|
|
|
|
|
control.add_test(obj)
|
2019-06-11 22:24:35 +00:00
|
|
|
control.id = "variable.control.id"
|
2017-02-03 08:27:51 +00:00
|
|
|
control.impact = 0.1
|
|
|
|
control.to_ruby.must_equal '
|
|
|
|
control "variable.control.id" do
|
|
|
|
impact 0.1
|
|
|
|
a = passwd.where { user == "_chrony" }.uids.first
|
|
|
|
describe user(entry.user) do
|
|
|
|
its("uid") { should cmp a }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
'.strip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Inspec::Tag" do
|
|
|
|
it "constructs a tag with key and value" do
|
2017-03-22 17:41:44 +00:00
|
|
|
control = Inspec::Control.new
|
|
|
|
|
|
|
|
res1 = { name: "key", value: "value" }
|
|
|
|
tag1 = Inspec::Tag.new(res1[:name], res1[:value])
|
|
|
|
tag1.to_hash.must_equal res1
|
|
|
|
control.add_tag(tag1)
|
|
|
|
|
|
|
|
res2 = { name: "key2'", value: "value'" }
|
|
|
|
tag2 = Inspec::Tag.new(res2[:name], res2[:value])
|
|
|
|
tag2.to_hash.must_equal res2
|
|
|
|
control.add_tag(tag2)
|
|
|
|
|
|
|
|
res3 = { name: "key3\"", value: "value\"" }
|
|
|
|
tag3 = Inspec::Tag.new(res3[:name], res3[:value])
|
|
|
|
tag3.to_hash.must_equal res3
|
|
|
|
control.add_tag(tag3)
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
res4 = { name: "key4", value: %w{a b} }
|
2017-03-22 17:41:44 +00:00
|
|
|
tag4 = Inspec::Tag.new(res4[:name], res4[:value])
|
|
|
|
tag4.to_hash.must_equal res4
|
|
|
|
control.add_tag(tag4)
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
control.id = "tag.control.id"
|
2017-03-22 17:41:44 +00:00
|
|
|
control.to_ruby.must_equal '
|
|
|
|
control "tag.control.id" do
|
|
|
|
tag "key": "value"
|
|
|
|
tag "key2\'": "value\'"
|
|
|
|
tag "key3\"": "value\""
|
|
|
|
tag "key4": ["a", "b"]
|
|
|
|
end
|
|
|
|
'.strip
|
2017-02-03 08:27:51 +00:00
|
|
|
|
2017-03-22 17:41:44 +00:00
|
|
|
control_hash = {
|
2019-06-11 22:24:35 +00:00
|
|
|
id: "tag.control.id",
|
2017-03-22 17:41:44 +00:00
|
|
|
title: nil,
|
2018-09-26 17:28:58 +00:00
|
|
|
descriptions: {},
|
2017-03-22 17:41:44 +00:00
|
|
|
impact: nil,
|
|
|
|
tests: [],
|
2019-06-11 22:24:35 +00:00
|
|
|
tags: [{
|
|
|
|
name: "key",
|
|
|
|
value: "value",
|
2017-03-22 17:41:44 +00:00
|
|
|
}, {
|
2019-06-11 22:24:35 +00:00
|
|
|
name: "key2'",
|
|
|
|
value: "value'",
|
2017-03-22 17:41:44 +00:00
|
|
|
}, {
|
2019-06-11 22:24:35 +00:00
|
|
|
name: "key3\"",
|
|
|
|
value: "value\"",
|
2017-03-22 17:41:44 +00:00
|
|
|
}, {
|
2019-06-11 22:24:35 +00:00
|
|
|
name: "key4",
|
|
|
|
value: %w{a b},
|
|
|
|
}],
|
2017-03-22 17:41:44 +00:00
|
|
|
}
|
|
|
|
control.to_hash.must_equal control_hash
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 04:10:59 +00:00
|
|
|
|
|
|
|
describe "Inspec::Input" do
|
|
|
|
describe "to_ruby method" do
|
|
|
|
it "generates the code for the input" do
|
|
|
|
input = Inspec::Input.new("application_port", description: "The port my application uses", value: 80)
|
|
|
|
|
|
|
|
ruby_code = input.to_ruby
|
|
|
|
ruby_code.must_include "attr_application_port = " # Should assign to a var
|
|
|
|
# Should have the DSL call. This should be attribute(), not input(), for the
|
|
|
|
# foreseeable future, to maintain backwards compatibility.
|
|
|
|
ruby_code.must_include "attribute('application_port'"
|
|
|
|
ruby_code.must_include "value: 80"
|
|
|
|
ruby_code.must_include "default: 80"
|
|
|
|
ruby_code.must_include "description: 'The port my application uses'"
|
|
|
|
|
|
|
|
# Try to eval the code to verify that the generated code was valid ruby.
|
|
|
|
# Note that the input() method is part of the DSL, so we need to
|
|
|
|
# alter the call into something that can respond - the constructor will do
|
|
|
|
ruby_code_for_eval = ruby_code.sub(/attribute\(/, "Inspec::Input.new(")
|
|
|
|
|
|
|
|
# This will throw exceptions if there is a problem
|
|
|
|
new_attr = eval(ruby_code_for_eval) # rubocop:disable Security/Eval # Could use ripper!
|
|
|
|
new_attr.value.must_equal 80
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO - deprecate this, not sure it is used
|
|
|
|
describe "to_hash method" do
|
|
|
|
it "generates a similar hash" do
|
|
|
|
ipt = Inspec::Input.new(
|
|
|
|
"some_attr",
|
|
|
|
description: "The port my application uses",
|
|
|
|
value: 80,
|
|
|
|
identifier: "app_port",
|
|
|
|
required: false,
|
|
|
|
type: "numeric"
|
|
|
|
)
|
|
|
|
expected = {
|
|
|
|
name: "some_attr",
|
|
|
|
options: {
|
|
|
|
description: "The port my application uses",
|
|
|
|
value: 80,
|
|
|
|
identifier: "app_port",
|
|
|
|
required: false,
|
|
|
|
type: "Numeric", # This gets normalized
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ipt.to_hash.must_equal expected
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-01-30 10:16:35 +00:00
|
|
|
end
|