mirror of
https://github.com/inspec/inspec
synced 2024-09-21 15:02:01 +00:00
Fix Inspec::Config regression in Inspec::Backend
With the changes in PR #3750, `Inspec::Backend.create` needs to support both being passed a Hash and being passed an Inspec::Config. This adds a line to convert a passed Hash to an Inspec::Config. This also adds unit tests for Inspec::Backend because they were missing. Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>
This commit is contained in:
parent
9d7ca85e7d
commit
a6f74a33b2
2 changed files with 105 additions and 0 deletions
|
@ -4,6 +4,7 @@
|
|||
# author: Christoph Hartmann
|
||||
|
||||
require 'train'
|
||||
require 'inspec/config'
|
||||
|
||||
module Inspec
|
||||
module Backend
|
||||
|
@ -41,6 +42,8 @@ module Inspec
|
|||
# @param [Hash] config for the transport backend
|
||||
# @return [TransportBackend] enriched transport instance
|
||||
def self.create(config) # rubocop:disable Metrics/AbcSize
|
||||
config = Inspec::Config.new(config) if config.is_a?(Hash)
|
||||
|
||||
train_credentials = config.unpack_train_credentials
|
||||
transport_name = Train.validate_backend(train_credentials)
|
||||
transport = Train.create(transport_name, train_credentials)
|
||||
|
|
102
test/unit/backend_test.rb
Normal file
102
test/unit/backend_test.rb
Normal file
|
@ -0,0 +1,102 @@
|
|||
# encoding: utf-8
|
||||
|
||||
require 'helper'
|
||||
|
||||
describe 'Backend' do # rubocop:disable Metrics/BlockLength
|
||||
let(:backend) { Inspec::Backend.create(Inspec::Config.mock) }
|
||||
|
||||
describe 'create' do # rubocop:disable Metrics/BlockLength
|
||||
it 'accepts a Hash' do
|
||||
backend_from_hash = Inspec::Backend.create(backend: 'mock')
|
||||
backend_from_hash.is_a?(Inspec::Backend::Base).must_equal true
|
||||
end
|
||||
|
||||
it 'accepts an Inspec::Config' do
|
||||
backend.is_a?(Inspec::Backend::Base).must_equal true
|
||||
end
|
||||
|
||||
it 'raises an error if no transport backend can be found' do
|
||||
Train.stub :create, nil do
|
||||
err = proc { backend }.must_raise RuntimeError
|
||||
err.message.must_equal "Can't find transport backend 'mock'."
|
||||
end
|
||||
end
|
||||
|
||||
it 'raises an error if no connection can be created' do
|
||||
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
|
||||
err = proc { backend }.must_raise RuntimeError
|
||||
err.message.must_equal "Can't connect to transport backend 'mock'."
|
||||
end
|
||||
end
|
||||
|
||||
it 'enables/disables caching based on `config[:backend_cache]`' do
|
||||
cache_enabled_backend = Inspec::Backend.create(backend_cache: true)
|
||||
cache_enabled_backend.backend.cache_enabled?(:file).must_equal true
|
||||
cache_enabled_backend.backend.cache_enabled?(:command).must_equal true
|
||||
|
||||
cache_disabled_backend = Inspec::Backend.create(backend_cache: false)
|
||||
cache_disabled_backend.backend.cache_enabled?(:file).must_equal false
|
||||
cache_disabled_backend.backend.cache_enabled?(:command).must_equal false
|
||||
end
|
||||
|
||||
it 'enables caching when using a mock backend' do
|
||||
backend.backend.cache_enabled?(:file).must_equal true
|
||||
backend.backend.cache_enabled?(:command).must_equal true
|
||||
end
|
||||
|
||||
it 'disables caching when `config[:debug_shell]` is true' do
|
||||
debug_shell_backend = Inspec::Backend.create(debug_shell: true)
|
||||
debug_shell_backend.backend.cache_enabled?(:file).must_equal false
|
||||
debug_shell_backend.backend.cache_enabled?(:command).must_equal false
|
||||
end
|
||||
|
||||
it 'captures Train::ClientError' do
|
||||
Train.stub(:create, proc { raise Train::ClientError }) do
|
||||
err = proc { backend }.must_raise RuntimeError
|
||||
err.message.must_equal "Client error, can't connect to 'mock' backend: "
|
||||
end
|
||||
end
|
||||
|
||||
it 'captures Train::TransportError' do
|
||||
Train.stub(:create, proc { raise Train::TransportError }) do
|
||||
err = proc { backend }.must_raise RuntimeError
|
||||
err.message.must_equal "Transport error, can't connect to 'mock' backend: "
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'version' do
|
||||
it 'returns the current InSpec version' do
|
||||
backend.version.must_equal Inspec::VERSION
|
||||
end
|
||||
end
|
||||
|
||||
describe 'local_transport?' do
|
||||
it 'returns false when using a Mock transport' do
|
||||
backend.local_transport?.must_equal false
|
||||
end
|
||||
|
||||
it 'returns true when using a Local transport' do
|
||||
local_backend = Inspec::Backend.create(Inspec::Config.new)
|
||||
local_backend.local_transport?.must_equal true
|
||||
end
|
||||
end
|
||||
|
||||
describe 'to_s' do
|
||||
it 'returns the correct string' do
|
||||
backend.to_s.must_equal 'Inspec::Backend::Class'
|
||||
end
|
||||
end
|
||||
|
||||
describe 'inspect' do
|
||||
it 'returns the correct string' do
|
||||
backend.inspect.must_equal 'Inspec::Backend::Class @transport=Train::Transports::Mock::Connection'
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue