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

(SERVER-2470) list_all_transports implementation for puppetserver #187

Merged
merged 2 commits into from
Jun 28, 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
24 changes: 24 additions & 0 deletions lib/puppet/resource_api/transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,35 @@ def register(schema)
module_function :register # rubocop:disable Style/AccessModifierDeclarations

# retrieve a Hash of transport schemas, keyed by their name.
# Only already loaded transports are returned.
def list
Marshal.load(Marshal.dump(transports))
end
module_function :list # rubocop:disable Style/AccessModifierDeclarations

# retrieve a Hash of transport schemas, keyed by their name.
# This uses the Puppet autoloader, provide a environment name as `force_environment`
# to choose where to load from.
# @api private
def list_all_transports(force_environment)
env = Puppet.lookup(:environments).get!(force_environment)
Puppet.override({ current_environment: env }, 'current env for list_all_transports') do
load_all_schemas
Marshal.load(Marshal.dump(transports))
end
end
module_function :list_all_transports # rubocop:disable Style/AccessModifierDeclarations

# Loads all schemas using the Puppet Autoloader.
def self.load_all_schemas
require 'puppet'
require 'puppet/settings'
require 'puppet/util/autoload'
@autoloader ||= Puppet::Util::Autoload.new(self, 'puppet/transport/schema')
@autoloader.loadall(Puppet.lookup(:current_environment))
end
private_class_method :load_all_schemas

def connect(name, connection_info)
validate(name, connection_info)
require "puppet/transport/#{name}"
Expand Down
21 changes: 21 additions & 0 deletions spec/integration/resource_api/transport_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

RSpec.describe 'Resource API Transport integration tests:' do
describe '#list_all_transports' do
subject(:transports) { Puppet::ResourceApi::Transport.list_all_transports('rp_env') }

it 'can be called twice' do
expect {
Puppet::ResourceApi::Transport.list_all_transports('rp_env')
Puppet::ResourceApi::Transport.list_all_transports('rp_env')
}.not_to raise_error
end

it 'loads all transports' do
expect(transports).to have_key 'test_device'
expect(transports).to have_key 'test_device_sensitive'
expect(transports['test_device']).to be_a Puppet::ResourceApi::TransportSchemaDef
expect(transports['test_device'].definition).to include(name: 'test_device')
end
end
end
5 changes: 0 additions & 5 deletions spec/puppet/resource_api/transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ def change_environment(name = nil)
Puppet.debug = true
end

after(:each) do
# reset registered transports between tests to reduce cross-test poisoning
described_class.instance_variable_set(:@transports, nil)
end

describe '#register(schema)' do
context 'when registering a schema with missing keys' do
it { expect { described_class.register([]) }.to raise_error(Puppet::DevError, %r{requires a hash as schema}) }
Expand Down
2 changes: 1 addition & 1 deletion spec/puppet/resource_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,7 @@ def set(_context, changes) end
context 'when loading a type with unknown features' do
let(:definition) do
{
name: 'test_noop_support',
name: 'test_noop_support_2',
desc: 'a test resource',
features: ['no such feature'],
attributes: {},
Expand Down
15 changes: 15 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'bundler/setup'
require 'rspec-puppet'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand All @@ -14,6 +15,9 @@
# override legacy default from puppetlabs_spec_helper
config.mock_with :rspec

# enable rspec-puppet support everywhere
config.include RSpec::Puppet::Support

# reset the warning suppression count
config.before(:each) do
Puppet::ResourceApi.warning_count = 0
Expand All @@ -26,3 +30,14 @@

# exclude the `version.rb` which already gets loaded by bundler via the gemspec, and doesn't need coverage testing anyways.
SimpleCov.add_filter 'lib/puppet/resource_api/version.rb' if ENV['SIMPLECOV'] == 'yes'

# configure this hook after Resource API is loaded to get access to Puppet::ResourceApi::Transport
RSpec.configure do |config|
config.after(:each) do
# reset registered transports between tests to reduce cross-test poisoning
Puppet::ResourceApi::Transport.instance_variable_set(:@transports, nil)
if (autoloader = Puppet::ResourceApi::Transport.instance_variable_get(:@autoloader))
autoloader.class.loaded.clear
end
end
end