Skip to content

Commit

Permalink
(SERVER-2470) list_all_transports implementation for puppetserver
Browse files Browse the repository at this point in the history
This change implements a `list_all_transports(env)` method that loads
all available transports from a specified environment.

To test out, run `rake spec_prep` and use the following setup in pry:

```
require 'puppet'
require 'puppet/resource_api/transport'
require 'puppet/settings'

Puppet.initialize_settings
Puppet[:modulepath] = 'spec/fixtures/modules'

Puppet::ResourceApi::Transport.list_all_transports('production')
```
  • Loading branch information
DavidS committed Jun 27, 2019
1 parent 6d77e7d commit e04f7a0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
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
28 changes: 28 additions & 0 deletions spec/integration/resource_api/transport_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

RSpec.describe 'Resource API Transport integration tests:' do
after(:each) do
# reset registered transports between tests to reduce cross-test poisoning
Puppet::ResourceApi::Transport.instance_variable_set(:@transports, nil)
autoloader = Puppet::ResourceApi::Transport.instance_variable_get(:@autoloader)
autoloader.class.loaded.clear
end

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
4 changes: 4 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 Down

0 comments on commit e04f7a0

Please sign in to comment.