2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
2019-06-07 23:33:56 +00:00
|
|
|
require "inspec/resource"
|
|
|
|
require "inspec/resources/os"
|
2016-03-17 12:27:13 +00:00
|
|
|
|
2019-12-30 22:54:37 +00:00
|
|
|
# TODO: remove or merge tests with resource_test.rb
|
|
|
|
|
|
|
|
describe Inspec::Resource do
|
|
|
|
let(:base) { Inspec::Resource }
|
2016-03-17 12:27:13 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#name" do
|
2016-03-17 12:27:13 +00:00
|
|
|
it "won't register a nil resource" do
|
2019-06-11 22:24:35 +00:00
|
|
|
Class.new(base) { name nil; }
|
2019-09-30 22:31:55 +00:00
|
|
|
_(Inspec::Resource.registry.keys).wont_include nil
|
|
|
|
_(Inspec::Resource.registry.keys).wont_include ""
|
2016-03-17 12:27:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "will register a valid name" do
|
2019-06-11 22:24:35 +00:00
|
|
|
Class.new(base) { name "hello"; }
|
2019-09-30 22:31:55 +00:00
|
|
|
_(Inspec::Resource.registry["hello"]).wont_be :nil?
|
2016-03-17 12:27:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(&block)
|
2019-06-07 23:33:56 +00:00
|
|
|
# random_name = (0...50).map { (65 + rand(26)).chr }.join
|
|
|
|
random_name = "NotSoRandomName"
|
2016-03-17 12:27:13 +00:00
|
|
|
Class.new(base) do
|
|
|
|
name random_name
|
2016-09-07 11:10:35 +00:00
|
|
|
instance_eval(&block)
|
2016-03-17 12:27:13 +00:00
|
|
|
end
|
|
|
|
Inspec::Resource.registry[random_name]
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#desc" do
|
2016-03-17 12:27:13 +00:00
|
|
|
it "will register a description" do
|
|
|
|
expected = rand.to_s
|
2019-09-30 22:31:55 +00:00
|
|
|
_(create { desc expected }.desc).must_equal expected
|
2016-03-17 12:27:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can change the description" do
|
|
|
|
c = create { desc rand.to_s }
|
|
|
|
c.desc(x = rand.to_s)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(c.desc).must_equal x
|
2016-03-17 12:27:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#example" do
|
2022-03-04 04:48:32 +00:00
|
|
|
it "will register a description" do
|
2016-03-17 12:27:13 +00:00
|
|
|
expected = rand.to_s
|
2019-09-30 22:31:55 +00:00
|
|
|
_(create { example expected }.example).must_equal expected
|
2016-03-17 12:27:13 +00:00
|
|
|
end
|
|
|
|
|
2022-03-04 04:48:32 +00:00
|
|
|
it "can change the description" do
|
2016-03-17 12:27:13 +00:00
|
|
|
c = create { example rand.to_s }
|
|
|
|
c.example(x = rand.to_s)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(c.example).must_equal x
|
2016-03-17 12:27:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "supported platform" do
|
2018-01-02 19:04:13 +00:00
|
|
|
def supports_meta(supports)
|
2019-12-30 22:47:47 +00:00
|
|
|
@old = Inspec::Resource.support_registry[:os]
|
|
|
|
Inspec::Resource.support_registry[:os] = supports
|
2019-06-11 22:24:35 +00:00
|
|
|
load_resource("os")
|
2018-01-02 19:04:13 +00:00
|
|
|
end
|
|
|
|
|
2019-08-28 00:30:03 +00:00
|
|
|
after do
|
2019-12-30 22:47:47 +00:00
|
|
|
Inspec::Resource.support_registry[:os] = @old
|
2019-08-28 00:30:03 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads a profile which supports multiple families" do
|
2018-01-02 19:04:13 +00:00
|
|
|
m = supports_meta([
|
2019-06-11 22:24:35 +00:00
|
|
|
{ os_family: "windows" },
|
2019-07-09 00:20:30 +00:00
|
|
|
{ os_family: "unix" },
|
2018-01-02 19:04:13 +00:00
|
|
|
])
|
2019-09-30 22:31:55 +00:00
|
|
|
_(m.check_supports).must_equal true
|
2019-12-30 22:47:47 +00:00
|
|
|
Inspec::Resource.support_registry["os"] = nil
|
2018-01-02 19:04:13 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads a profile which supports multiple names" do
|
2018-01-02 19:04:13 +00:00
|
|
|
m = supports_meta([
|
2019-06-11 22:24:35 +00:00
|
|
|
{ os_family: "windows", os_name: "windows_2000" },
|
2019-07-09 00:20:30 +00:00
|
|
|
{ os_family: "unix", os_name: "ubuntu" },
|
2018-01-02 19:04:13 +00:00
|
|
|
])
|
2019-09-30 22:31:55 +00:00
|
|
|
_(m.check_supports).must_equal true
|
2019-12-30 22:47:47 +00:00
|
|
|
Inspec::Resource.support_registry["os"] = nil
|
2018-01-02 19:04:13 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "reject a profile which supports multiple families" do
|
2018-01-02 19:04:13 +00:00
|
|
|
m = supports_meta([
|
2019-06-11 22:24:35 +00:00
|
|
|
{ os_family: "windows" },
|
2019-07-09 00:20:30 +00:00
|
|
|
{ os_family: "redhat" },
|
2018-01-02 19:04:13 +00:00
|
|
|
])
|
2019-09-30 22:31:55 +00:00
|
|
|
_(m.check_supports).must_equal false
|
2019-12-30 22:47:47 +00:00
|
|
|
Inspec::Resource.support_registry["os"] = nil
|
2018-01-02 19:04:13 +00:00
|
|
|
end
|
|
|
|
end
|
2016-03-17 12:27:13 +00:00
|
|
|
end
|