2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/config"
|
2019-06-07 23:33:56 +00:00
|
|
|
require "inspec/profile"
|
2019-06-11 22:24:35 +00:00
|
|
|
require "inspec/runner_mock"
|
|
|
|
require "fetchers/mock"
|
2016-03-17 22:07:42 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "controls" do
|
2016-03-17 22:07:42 +00:00
|
|
|
def load(content)
|
|
|
|
data = {
|
2019-06-11 22:24:35 +00:00
|
|
|
"inspec.yml" => "name: mock",
|
|
|
|
"controls/mock.rb" => "control '1' do\n#{content}\nend\n",
|
2016-03-17 22:07:42 +00:00
|
|
|
}
|
2019-05-24 23:47:59 +00:00
|
|
|
opts = {
|
|
|
|
test_collector: Inspec::RunnerMock.new,
|
2019-06-11 22:24:35 +00:00
|
|
|
backend: Inspec::Backend.create(Inspec::Config.mock),
|
2019-05-24 23:47:59 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 22:07:42 +00:00
|
|
|
Inspec::Profile.for_target(data, opts)
|
2019-07-09 00:20:30 +00:00
|
|
|
.params[:controls]["1"]
|
2016-03-17 22:07:42 +00:00
|
|
|
end
|
|
|
|
|
2017-04-25 10:39:25 +00:00
|
|
|
let(:rand_string) { rand.to_s }
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "adds a title" do
|
2017-04-25 10:39:25 +00:00
|
|
|
load("title #{rand_string.inspect}")[:title].must_equal rand_string
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "adds a description" do
|
2018-09-26 17:28:58 +00:00
|
|
|
load("desc #{rand_string.inspect}")[:descriptions][:default].must_equal rand_string
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "allows multiple desc with keys and values" do
|
2018-09-26 17:28:58 +00:00
|
|
|
lines = <<-DESC_EXAMPLES
|
|
|
|
desc 'this is default'
|
|
|
|
desc 'example', 'this is foo'
|
|
|
|
desc 'a space', 'this label has a space'
|
|
|
|
desc 'newline', 'this value has
|
|
|
|
a newline'
|
|
|
|
desc 'heredoc', <<-EOF
|
|
|
|
This is a heredoc
|
|
|
|
EOF
|
|
|
|
DESC_EXAMPLES
|
|
|
|
|
|
|
|
result = load(lines)
|
2019-06-11 22:24:35 +00:00
|
|
|
result[:descriptions][:default].must_equal("this is default")
|
|
|
|
result[:descriptions][:example].must_equal("this is foo")
|
|
|
|
result[:descriptions][:'a space'].must_equal("this label has a space")
|
2018-09-26 17:28:58 +00:00
|
|
|
result[:descriptions][:newline].must_match(/this value has\n\s*a newline/)
|
2019-06-11 22:24:35 +00:00
|
|
|
result[:descriptions][:heredoc].must_equal("This is a heredoc")
|
2017-04-25 10:39:25 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "adds a multiline description" do
|
2017-04-25 10:39:25 +00:00
|
|
|
t = rand_string + "\n" + rand_string
|
2018-09-26 17:28:58 +00:00
|
|
|
load("desc #{t.inspect}")[:descriptions][:default].must_equal t
|
2017-04-25 10:39:25 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "strips empty lines and spaces in description at start and end" do
|
2017-04-25 10:39:25 +00:00
|
|
|
t = " \n" + rand_string + "\n "
|
2018-09-26 17:28:58 +00:00
|
|
|
load("desc #{t.inspect}")[:descriptions][:default].must_equal rand_string
|
2017-04-25 10:39:25 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "unindents properly" do
|
2017-04-25 10:39:25 +00:00
|
|
|
t = "\n #{rand_string}\n \n\t\t #{rand_string}\n "
|
2018-09-26 17:28:58 +00:00
|
|
|
load("desc #{t.inspect}")[:descriptions][:default].must_equal "#{rand_string}\n \n #{rand_string}"
|
2017-04-25 10:39:25 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "works with empty refs" do
|
|
|
|
load("ref")[:refs].must_be :empty?
|
2016-03-17 22:07:42 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "defines a simple ref" do
|
2016-03-17 22:07:42 +00:00
|
|
|
s = rand.to_s
|
2019-06-11 22:24:35 +00:00
|
|
|
load("ref #{s.inspect}")[:refs].must_equal [{ ref: s }]
|
2016-03-17 22:07:42 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "defines a ref with url" do
|
2016-03-17 22:07:42 +00:00
|
|
|
s = rand.to_s
|
|
|
|
u = rand.to_s
|
2019-06-11 22:24:35 +00:00
|
|
|
load("ref #{s.inspect}, url: #{u.inspect}")[:refs].must_equal [{ ref: s, url: u }]
|
2016-03-17 22:07:42 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "defines a ref without content but with url" do
|
2016-03-17 22:07:42 +00:00
|
|
|
u = rand.to_s
|
2019-06-11 22:24:35 +00:00
|
|
|
load("ref url: #{u.inspect}")[:refs].must_equal [{ url: u }]
|
2016-03-17 22:07:42 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "works with empty tags" do
|
|
|
|
load("tag")[:tags].must_be :empty?
|
2016-03-17 22:07:42 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "defines a simple tag" do
|
2016-03-17 22:07:42 +00:00
|
|
|
s = rand.to_s
|
|
|
|
load("tag #{s.inspect}")[:tags].must_equal({ s => nil })
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "define multiple tags" do
|
2016-03-17 22:07:42 +00:00
|
|
|
a, b, c = rand.to_s, rand.to_s, rand.to_s
|
|
|
|
load("tag #{a.inspect}, #{b.inspect}, #{c.inspect}")[:tags].must_equal(
|
2019-07-09 00:20:30 +00:00
|
|
|
{ a => nil, b => nil, c => nil }
|
|
|
|
)
|
2016-03-17 22:07:42 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "tag by key=value" do
|
2016-03-17 22:07:42 +00:00
|
|
|
a, b = rand.to_s, rand.to_s
|
|
|
|
load("tag #{a.inspect} => #{b.inspect}")[:tags].must_equal(
|
2019-07-09 00:20:30 +00:00
|
|
|
{ a => b }
|
|
|
|
)
|
2016-03-17 22:07:42 +00:00
|
|
|
end
|
|
|
|
end
|