2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
2019-02-07 07:41:26 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Backend" do # rubocop:disable Metrics/BlockLength
|
2019-02-07 07:41:26 +00:00
|
|
|
let(:backend) { Inspec::Backend.create(Inspec::Config.mock) }
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "create" do # rubocop:disable Metrics/BlockLength
|
|
|
|
it "accepts an Inspec::Config" do
|
2020-01-01 00:03:44 +00:00
|
|
|
_(backend.is_a?(Inspec::Backend)).must_equal true
|
2019-02-07 07:41:26 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "raises an error if no transport backend can be found" do
|
2019-02-07 07:41:26 +00:00
|
|
|
Train.stub :create, nil do
|
2019-09-30 22:31:55 +00:00
|
|
|
err = _ { backend }.must_raise RuntimeError
|
|
|
|
_(err.message).must_equal "Can't find transport backend 'mock'."
|
2019-02-07 07:41:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "raises an error if no connection can be created" do
|
2019-02-07 07:41:26 +00:00
|
|
|
mock_transport = Minitest::Mock.new
|
|
|
|
mock_bad_connection = Minitest::Mock.new
|
|
|
|
mock_transport.expect :nil?, false
|
|
|
|
mock_transport.expect :connection, mock_bad_connection
|
|
|
|
mock_bad_connection.expect :nil?, true
|
|
|
|
|
|
|
|
Train.stub :create, mock_transport do
|
2019-09-30 22:31:55 +00:00
|
|
|
err = _ { backend }.must_raise RuntimeError
|
|
|
|
_(err.message).must_equal "Can't connect to transport backend 'mock'."
|
2019-02-07 07:41:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "enables/disables caching based on `config[:backend_cache]`" do
|
2019-02-07 18:42:19 +00:00
|
|
|
cache_enabled_config = Inspec::Config.new(backend_cache: true)
|
|
|
|
cache_enabled_backend = Inspec::Backend.create(cache_enabled_config)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cache_enabled_backend.backend.cache_enabled?(:file)).must_equal true
|
|
|
|
_(cache_enabled_backend.backend.cache_enabled?(:command)).must_equal true
|
2019-02-07 07:41:26 +00:00
|
|
|
|
2019-02-07 18:42:19 +00:00
|
|
|
cache_disabled_config = Inspec::Config.new(backend_cache: false)
|
|
|
|
cache_disabled_backend = Inspec::Backend.create(cache_disabled_config)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cache_disabled_backend.backend.cache_enabled?(:file)).must_equal false
|
|
|
|
_(cache_disabled_backend.backend.cache_enabled?(:command)).must_equal false
|
2019-02-07 07:41:26 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "enables caching when using a mock backend" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(backend.backend.cache_enabled?(:file)).must_equal true
|
|
|
|
_(backend.backend.cache_enabled?(:command)).must_equal true
|
2019-02-07 07:41:26 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "disables caching when `config[:debug_shell]` is true" do
|
2019-02-07 18:42:19 +00:00
|
|
|
debug_shell_config = Inspec::Config.new(debug_shell: true)
|
|
|
|
debug_shell_backend = Inspec::Backend.create(debug_shell_config)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(debug_shell_backend.backend.cache_enabled?(:file)).must_equal false
|
|
|
|
_(debug_shell_backend.backend.cache_enabled?(:command)).must_equal false
|
2019-02-07 07:41:26 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "captures Train::ClientError" do
|
2019-02-07 07:41:26 +00:00
|
|
|
Train.stub(:create, proc { raise Train::ClientError }) do
|
2019-09-30 22:31:55 +00:00
|
|
|
err = _ { backend }.must_raise RuntimeError
|
|
|
|
_(err.message).must_equal "Client error, can't connect to 'mock' backend: "
|
2019-02-07 07:41:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "captures Train::TransportError" do
|
2019-02-07 07:41:26 +00:00
|
|
|
Train.stub(:create, proc { raise Train::TransportError }) do
|
2019-09-30 22:31:55 +00:00
|
|
|
err = _ { backend }.must_raise RuntimeError
|
|
|
|
_(err.message).must_equal "Transport error, can't connect to 'mock' backend: "
|
2019-02-07 07:41:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "version" do
|
|
|
|
it "returns the current InSpec version" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(backend.version).must_equal Inspec::VERSION
|
2019-02-07 07:41:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "local_transport?" do
|
|
|
|
it "returns false when using a Mock transport" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(backend.local_transport?).must_equal false
|
2019-02-07 07:41:26 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns true when using a Local transport" do
|
2019-02-07 07:41:26 +00:00
|
|
|
local_backend = Inspec::Backend.create(Inspec::Config.new)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(local_backend.local_transport?).must_equal true
|
2019-02-07 07:41:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "to_s" do
|
|
|
|
it "returns the correct string" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(backend.to_s).must_equal "Inspec::Backend::Class"
|
2019-02-07 07:41:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "inspect" do
|
|
|
|
it "returns the correct string" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(backend.inspect).must_equal "Inspec::Backend::Class @transport=Train::Transports::Mock::Connection"
|
2019-02-07 07:41:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|