Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(maint) test cleanups #172

Merged
merged 3 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions lib/puppet/resource_api/transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ def register(schema)
raise Puppet::DevError, 'requires `:connection_info`' unless schema.key? :connection_info
raise Puppet::DevError, '`:connection_info` must be a hash, not `%{other_type}`' % { other_type: schema[:connection_info].class } unless schema[:connection_info].is_a?(Hash)

init_transports
unless @transports[@environment][schema[:name]].nil?
unless transports[schema[:name]].nil?
raise Puppet::DevError, 'Transport `%{name}` is already registered for `%{environment}`' % {
name: schema[:name],
environment: @environment,
environment: current_environment,
}
end
@transports[@environment][schema[:name]] = Puppet::ResourceApi::TransportSchemaDef.new(schema)
transports[schema[:name]] = Puppet::ResourceApi::TransportSchemaDef.new(schema)
end
module_function :register # rubocop:disable Style/AccessModifierDeclarations

# retrieve a Hash of transport schemas, keyed by their name.
def list
init_transports
Marshal.load(Marshal.dump(@transports[@environment]))
Marshal.load(Marshal.dump(transports))
end
module_function :list # rubocop:disable Style/AccessModifierDeclarations

Expand All @@ -47,13 +45,12 @@ def inject_device(name, transport)
module_function :inject_device # rubocop:disable Style/AccessModifierDeclarations

def self.validate(name, connection_info)
init_transports
require "puppet/transport/schema/#{name}" unless @transports[@environment].key? name
transport_schema = @transports[@environment][name]
require "puppet/transport/schema/#{name}" unless transports.key? name
transport_schema = transports[name]
if transport_schema.nil?
raise Puppet::DevError, 'Transport for `%{target}` not registered with `%{environment}`' % {
target: name,
environment: @environment,
environment: current_environment,
}
end
message_prefix = 'The connection info provided does not match the Transport Schema'
Expand All @@ -64,24 +61,12 @@ def self.validate(name, connection_info)

def self.get_context(name)
require 'puppet/resource_api/puppet_context'
Puppet::ResourceApi::PuppetContext.new(@transports[@environment][name])
Puppet::ResourceApi::PuppetContext.new(transports[name])
end
private_class_method :get_context

def self.init_transports
lookup = Puppet.lookup(:current_environment) if Puppet.respond_to? :lookup
@environment = if lookup.nil?
:transports_default
else
lookup.name
end
@transports ||= {}
@transports[@environment] ||= {}
end
private_class_method :init_transports

def self.wrap_sensitive(name, connection_info)
transport_schema = @transports[@environment][name]
transport_schema = transports[name]
if transport_schema
transport_schema.definition[:connection_info].each do |attr_name, options|
if options.key?(:sensitive) && (options[:sensitive] == true) && connection_info.key?(attr_name)
Expand All @@ -92,4 +77,20 @@ def self.wrap_sensitive(name, connection_info)
connection_info
end
private_class_method :wrap_sensitive

def self.transports
@transports ||= {}
@transports[current_environment] ||= {}
end
private_class_method :transports

def self.current_environment
if Puppet.respond_to? :lookup
env = Puppet.lookup(:current_environment)
env.nil? ? :transports_default : env.name
else
:transports_default
end
end
private_class_method :current_environment
end
2 changes: 1 addition & 1 deletion spec/puppet/resource_api/transport/wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class SomethingSomethingDarkside; end

context 'when using method?' do
it 'will return false' do
expect { instance.method :wibble }.to raise_error
expect { instance.method :wibble }.to raise_error NameError, %r{undefined method `wibble'}
end
end
end
Expand Down
50 changes: 15 additions & 35 deletions spec/puppet/resource_api/transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,29 @@ def change_environment(name = nil)
end

context 'when a transports are added to multiple environments' do
let(:transports) { described_class.instance_variable_get(:@transports) }

it 'will record the schemas in the correct structure' do
change_environment(:wibble)
change_environment(nil)
described_class.register(schema)
expect(described_class.instance_variable_get(:@transports)).to be_key(:wibble)
expect(described_class.instance_variable_get(:@transports)[:wibble][schema[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef)
expect(described_class.instance_variable_get(:@transports)[:wibble][schema[:name]].definition).to eq(schema)
expect(transports).to have_key(:transports_default)
expect(transports[:transports_default][schema[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef)
expect(transports[:transports_default][schema[:name]].definition).to eq(schema)

change_environment(:foo)
change_environment('foo')
described_class.register(schema)
described_class.register(schema2)
expect(described_class.instance_variable_get(:@transports)).to be_key(:foo)
expect(described_class.instance_variable_get(:@transports)[:foo][schema[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef)
expect(described_class.instance_variable_get(:@transports)[:foo][schema[:name]].definition).to eq(schema)
expect(described_class.instance_variable_get(:@transports)[:foo][schema2[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef)
expect(described_class.instance_variable_get(:@transports)[:foo][schema2[:name]].definition).to eq(schema2)
expect(transports).to have_key('foo')
expect(transports['foo'][schema[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef)
expect(transports['foo'][schema[:name]].definition).to eq(schema)
expect(transports['foo'][schema2[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef)
expect(transports['foo'][schema2[:name]].definition).to eq(schema2)

change_environment(:bar)
described_class.register(schema3)
expect(described_class.instance_variable_get(:@transports)).to be_key(:bar)
expect(described_class.instance_variable_get(:@transports)[:bar][schema3[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef)
expect(described_class.instance_variable_get(:@transports)[:bar][schema3[:name]].definition).to eq(schema3)
expect(transports).to have_key(:bar)
expect(transports[:bar][schema3[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef)
expect(transports[:bar][schema3[:name]].definition).to eq(schema3)
end
end
end
Expand Down Expand Up @@ -310,28 +312,6 @@ class Wibble; end
end
end

describe '#init_transports' do
context 'when there is not a current_environment' do
it 'will return the default transport environment name' do
change_environment

described_class.send :init_transports

expect(described_class.instance_variable_get(:@environment)).to eq(:transports_default)
end
end

context 'when there is a current_environment' do
it 'will return the set environment name' do
change_environment(:something)

described_class.send :init_transports

expect(described_class.instance_variable_get(:@environment)).to eq(:something)
end
end
end

describe '#wrap_sensitive(name, connection_info)' do
let(:schema) do
{
Expand Down