2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
2019-06-07 23:33:56 +00:00
|
|
|
require "inspec/source_reader"
|
|
|
|
require "source_readers/inspec" # TODO: break circular
|
2016-02-21 12:26:31 +00:00
|
|
|
|
|
|
|
describe SourceReaders::InspecReader do
|
|
|
|
let(:reader) { SourceReaders::InspecReader }
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "registers with the source readers registry" do
|
2016-02-21 12:26:31 +00:00
|
|
|
reg = Inspec::SourceReader.registry
|
2019-06-11 22:24:35 +00:00
|
|
|
_(reg["inspec"]).must_equal reader
|
2016-02-21 12:26:31 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "with a valid profile" do
|
|
|
|
let(:mock_file) { MockLoader.profile_tgz("complete-profile") }
|
2016-09-08 09:11:44 +00:00
|
|
|
let(:target) { Inspec::FileProvider.for_path(mock_file) }
|
2016-02-21 12:26:31 +00:00
|
|
|
let(:res) { Inspec::SourceReader.resolve(target) }
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "resolves the target to inspec" do
|
2016-02-21 12:26:31 +00:00
|
|
|
_(res).must_be_kind_of reader
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "retrieves metadata" do
|
|
|
|
_(res.metadata.params[:name]).must_equal "complete"
|
2016-02-21 12:26:31 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "retrieves all files" do
|
2018-10-15 22:25:27 +00:00
|
|
|
_(res.tests.keys).must_equal %w{controls/host_spec.rb}
|
2019-06-11 23:13:05 +00:00
|
|
|
_(res.tests.values[0]).must_match(/^control 'test01' do$/)
|
2016-02-21 12:26:31 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "retrieves all libraries" do
|
2016-02-21 12:26:31 +00:00
|
|
|
_(res.libraries.keys).must_equal %w{libraries/testlib.rb}
|
2016-09-07 11:10:35 +00:00
|
|
|
_(res.libraries.values[0]).must_match(/^# Library resource$/)
|
2016-02-21 12:26:31 +00:00
|
|
|
end
|
2017-04-26 01:07:39 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "retrieves all extra files" do
|
2018-03-14 13:35:43 +00:00
|
|
|
_(res.data_files.keys.sort).must_equal %w{files/a_sub_dir/sub_items.conf files/items.conf}
|
2019-06-11 22:24:35 +00:00
|
|
|
_(res.data_files["files/items.conf"]).must_equal "one\ntwo\nthree\n"
|
|
|
|
_(res.data_files["files/a_sub_dir/sub_items.conf"]).must_equal "[section]\nkey = value\n"
|
2017-04-26 01:07:39 +00:00
|
|
|
end
|
2016-02-21 12:26:31 +00:00
|
|
|
end
|
2017-03-08 20:22:24 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "with an invalid inspec.yml" do
|
|
|
|
let(:mock_file) { MockLoader.profile_tgz("profile-with-bad-metadata") }
|
2017-03-08 20:22:24 +00:00
|
|
|
let(:target) { Inspec::FileProvider.for_path(mock_file) }
|
|
|
|
let(:res) { Inspec::SourceReader.resolve(target) }
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "raises an exception" do
|
2017-03-08 20:22:24 +00:00
|
|
|
err = proc { _(res.metadata) }.must_raise RuntimeError
|
|
|
|
err.message.must_match(/Unable to parse inspec\.yml: line \d+/)
|
|
|
|
end
|
|
|
|
end
|
2016-02-21 12:26:31 +00:00
|
|
|
end
|