2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
2018-08-28 13:11:38 +00:00
|
|
|
|
2019-07-27 19:46:11 +00:00
|
|
|
require "inspec/plugin/v2"
|
|
|
|
require "inspec/cached_fetcher"
|
|
|
|
require "plugins/inspec-compliance/lib/inspec-compliance"
|
|
|
|
require "inspec/dependencies/cache"
|
|
|
|
|
2018-08-28 13:11:38 +00:00
|
|
|
describe Inspec::CachedFetcher do
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when original fetcher is Compliance::Fetcher" do
|
2018-08-28 13:11:38 +00:00
|
|
|
let(:profiles_result) do
|
2019-06-11 22:24:35 +00:00
|
|
|
[{ "name" => "ssh-baseline",
|
|
|
|
"title" => "InSpec Profile",
|
|
|
|
"maintainer" => "The Authors",
|
|
|
|
"copyright" => "The Authors",
|
|
|
|
"copyright_email" => "you@example.com",
|
|
|
|
"license" => "Apache-2.0",
|
|
|
|
"summary" => "An InSpec Compliance Profile",
|
|
|
|
"version" => "0.1.1",
|
|
|
|
"owner" => "admin",
|
|
|
|
"supports" => [],
|
|
|
|
"depends" => [],
|
|
|
|
"sha256" => "132j1kjdasfasdoaefaewo12312",
|
|
|
|
"groups" => [],
|
|
|
|
"controls" => [],
|
|
|
|
"inputs" => [],
|
|
|
|
"latest_version" => "" }]
|
2018-08-28 13:11:38 +00:00
|
|
|
end
|
|
|
|
before do
|
2019-07-27 19:46:11 +00:00
|
|
|
InspecPlugins::Compliance::Configuration.expects(:new).returns({ "token" => "123abc", "server" => "https://a2.instance.com" })
|
2018-08-28 13:11:38 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "downloads the profile from the compliance service when sha256 not in the cache" do
|
2018-08-28 13:11:38 +00:00
|
|
|
prof = profiles_result[0]
|
2019-07-27 19:46:11 +00:00
|
|
|
InspecPlugins::Compliance::API.stubs(:profiles).returns(["success", profiles_result])
|
2018-08-28 13:11:38 +00:00
|
|
|
cache = Inspec::Cache.new
|
2019-06-11 22:24:35 +00:00
|
|
|
entry_path = cache.base_path_for(prof["sha256"])
|
2018-08-28 13:11:38 +00:00
|
|
|
mock_fetch = Minitest::Mock.new
|
|
|
|
mock_fetch.expect :call, "#{entry_path}.tar.gz", [entry_path]
|
2019-07-09 00:20:30 +00:00
|
|
|
cf = Inspec::CachedFetcher.new("compliance://#{prof["owner"]}/#{prof["name"]}", cache)
|
2019-06-11 22:24:35 +00:00
|
|
|
cache.stubs(:exists?).with(prof["sha256"]).returns(false)
|
2018-08-28 13:11:38 +00:00
|
|
|
cf.fetcher.stub(:fetch, mock_fetch) do
|
|
|
|
cf.fetch
|
|
|
|
end
|
|
|
|
mock_fetch.verify
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "does not download the profile when the sha256 exists in the inspec cache" do
|
2018-08-28 13:11:38 +00:00
|
|
|
prof = profiles_result[0]
|
2019-07-27 19:46:11 +00:00
|
|
|
InspecPlugins::Compliance::API.stubs(:profiles).returns(["success", profiles_result])
|
2018-08-28 13:11:38 +00:00
|
|
|
cache = Inspec::Cache.new
|
2019-06-11 22:24:35 +00:00
|
|
|
entry_path = cache.base_path_for(prof["sha256"])
|
2018-08-28 13:11:38 +00:00
|
|
|
mock_prefered_entry_for = Minitest::Mock.new
|
2019-06-11 22:24:35 +00:00
|
|
|
mock_prefered_entry_for.expect :call, entry_path, [prof["sha256"]]
|
2019-07-09 00:20:30 +00:00
|
|
|
cf = Inspec::CachedFetcher.new("compliance://#{prof["owner"]}/#{prof["name"]}", cache)
|
2019-06-11 22:24:35 +00:00
|
|
|
cache.stubs(:exists?).with(prof["sha256"]).returns(true)
|
2018-08-28 13:11:38 +00:00
|
|
|
cache.stub(:prefered_entry_for, mock_prefered_entry_for) do
|
|
|
|
cf.fetch
|
|
|
|
end
|
|
|
|
mock_prefered_entry_for.verify
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|