-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(PDK-1185) Implement allowances for device-specific providers
- Loading branch information
Showing
12 changed files
with
334 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
require 'open3' | ||
require 'puppet/version' | ||
require 'spec_helper' | ||
require 'tempfile' | ||
|
||
RSpec.describe 'exercising a type with device-specific providers' do | ||
let(:common_args) { '--verbose --trace --strict=error --modulepath spec/fixtures' } | ||
|
||
before(:all) do | ||
FileUtils.mkdir_p(File.expand_path('~/.puppetlabs/opt/puppet/cache/devices/some_node/state')) | ||
FileUtils.mkdir_p(File.expand_path('~/.puppetlabs/opt/puppet/cache/devices/other_node/state')) | ||
end | ||
|
||
describe 'using `puppet device`' do | ||
let(:common_args) { super() + " --deviceconfig #{device_conf.path} --target some_node --target other_node" } | ||
let(:device_conf) { Tempfile.new('device.conf') } | ||
let(:device_conf_content) do | ||
<<DEVICE_CONF | ||
[some_node] | ||
type some_device | ||
url file:///etc/credentials.txt | ||
[other_node] | ||
type other_device | ||
url file:///etc/credentials.txt | ||
DEVICE_CONF | ||
end | ||
|
||
def is_device_apply_supported? | ||
Gem::Version.new(Puppet::PUPPETVERSION) >= Gem::Version.new('5.3.6') && Gem::Version.new(Puppet::PUPPETVERSION) != Gem::Version.new('5.4.0') | ||
end | ||
|
||
before(:each) do | ||
skip "No device --apply in puppet before v5.3.6 nor in v5.4.0 (v#{Puppet::PUPPETVERSION} is installed)" unless is_device_apply_supported? | ||
device_conf.write(device_conf_content) | ||
device_conf.close | ||
end | ||
|
||
after(:each) do | ||
device_conf.unlink | ||
end | ||
|
||
it 'applies a catalog successfully' do | ||
pending "can't really test this without a puppetserver; when initially implementing this, it was tested using a hacked `puppet device` command allowing multiple --target params" | ||
|
||
# diff --git a/lib/puppet/application/device.rb b/lib/puppet/application/device.rb | ||
# index 5e7a5cd473..2d39527b47 100644 | ||
# --- a/lib/puppet/application/device.rb | ||
# +++ b/lib/puppet/application/device.rb | ||
# @@ -70,7 +70,8 @@ class Puppet::Application::Device < Puppet::Application | ||
# end | ||
|
||
# option("--target DEVICE", "-t") do |arg| | ||
# - options[:target] = arg.to_s | ||
# + options[:target] ||= [] | ||
# + options[:target] << arg.to_s | ||
# end | ||
|
||
# def summary | ||
# @@ -232,7 +233,7 @@ Licensed under the Apache 2.0 License | ||
# require 'puppet/util/network_device/config' | ||
# devices = Puppet::Util::NetworkDevice::Config.devices.dup | ||
# if options[:target] | ||
# - devices.select! { |key, value| key == options[:target] } | ||
# + devices.select! { |key, value| options[:target].include? key } | ||
# end | ||
# if devices.empty? | ||
# if options[:target] | ||
|
||
# david@davids:~/git/puppet-resource_api$ bundle exec puppet device --verbose --trace --strict=error --modulepath spec/fixtures \ | ||
# --target some_node --target other_node --resource multi_device multi_device multi_device | ||
# ["multi_device", "multi_device", "multi_device"] | ||
# Info: retrieving resource: multi_device from some_node at file:///etc/credentials.txt | ||
# multi_device { 'multi_device': | ||
# ensure => 'absent', | ||
# } | ||
# ["multi_device"] | ||
# Info: retrieving resource: multi_device from other_node at file:///etc/credentials.txt | ||
|
||
# david@davids:~/git/puppet-resource_api$ | ||
|
||
Tempfile.create('apply_success') do |f| | ||
f.write 'multi_device { "foo": }' | ||
f.close | ||
|
||
stdout_str, _status = Open3.capture2e("puppet device #{common_args} --apply #{f.path}") | ||
expect(stdout_str).to match %r{Compiled catalog for some_node} | ||
expect(stdout_str).to match %r{Compiled catalog for other_node} | ||
expect(stdout_str).not_to match %r{Error:} | ||
end | ||
end | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
spec/fixtures/test_module/lib/puppet/provider/multi_device/multi_device.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'puppet/resource_api/simple_provider' | ||
|
||
# Implementation for the multi_device type using the Resource API. | ||
# default provider class | ||
class Puppet::Provider::MultiDevice::MultiDevice < Puppet::ResourceApi::SimpleProvider | ||
def get(_context) | ||
[] | ||
end | ||
|
||
def create(context, name, should) | ||
context.notice("Creating '#{name}' with #{should.inspect}") | ||
end | ||
|
||
def update(context, name, should) | ||
context.notice("Updating '#{name}' with #{should.inspect}") | ||
end | ||
|
||
def delete(context, name) | ||
context.notice("Deleting '#{name}'") | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
spec/fixtures/test_module/lib/puppet/provider/multi_device/other_device.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'puppet/resource_api/simple_provider' | ||
|
||
# Implementation for the multi_device type using the Resource API. | ||
# device-specific class for `other_device` | ||
class Puppet::Provider::MultiDevice::OtherDevice < Puppet::ResourceApi::SimpleProvider | ||
def get(_context) | ||
[] | ||
end | ||
|
||
def create(context, name, should) | ||
context.notice("Creating '#{name}' with #{should.inspect}") | ||
end | ||
|
||
def update(context, name, should) | ||
context.notice("Updating '#{name}' with #{should.inspect}") | ||
end | ||
|
||
def delete(context, name) | ||
context.notice("Deleting '#{name}'") | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
spec/fixtures/test_module/lib/puppet/provider/multi_device/some_device.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'puppet/resource_api/simple_provider' | ||
|
||
# Implementation for the multi_device type using the Resource API. | ||
# device-specific class for `some_device` | ||
class Puppet::Provider::MultiDevice::SomeDevice < Puppet::ResourceApi::SimpleProvider | ||
def get(_context) | ||
[] | ||
end | ||
|
||
def create(context, name, should) | ||
context.notice("Creating '#{name}' with #{should.inspect}") | ||
end | ||
|
||
def update(context, name, should) | ||
context.notice("Updating '#{name}' with #{should.inspect}") | ||
end | ||
|
||
def delete(context, name) | ||
context.notice("Deleting '#{name}'") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'puppet/resource_api' | ||
|
||
Puppet::ResourceApi.register_type( | ||
name: 'multi_device', | ||
docs: <<-EOS, | ||
This type provides Puppet with the capabilities to manage ... | ||
EOS | ||
features: [ 'remote_resource' ], | ||
attributes: { | ||
ensure: { | ||
type: 'Enum[present, absent]', | ||
desc: 'Whether this resource should be present or absent on the target system.', | ||
default: 'present', | ||
}, | ||
name: { | ||
type: 'String', | ||
desc: 'The name of the resource you want to manage.', | ||
behaviour: :namevar, | ||
}, | ||
}, | ||
) |
10 changes: 10 additions & 0 deletions
10
spec/fixtures/test_module/lib/puppet/util/network_device/other_device/device.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'puppet/util/network_device/simple/device' | ||
|
||
module Puppet::Util::NetworkDevice::Other_device # rubocop:disable Style/ClassAndModuleCamelCase | ||
# A simple test device returning hardcoded facts | ||
class Device < Puppet::Util::NetworkDevice::Simple::Device | ||
def facts | ||
{ 'foo' => 'bar' } | ||
end | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
spec/fixtures/test_module/lib/puppet/util/network_device/some_device/device.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'puppet/util/network_device/simple/device' | ||
|
||
module Puppet::Util::NetworkDevice::Some_device # rubocop:disable Style/ClassAndModuleCamelCase | ||
# A simple test device returning hardcoded facts | ||
class Device < Puppet::Util::NetworkDevice::Simple::Device | ||
def facts | ||
{ 'foo' => 'bar' } | ||
end | ||
end | ||
end |
49 changes: 49 additions & 0 deletions
49
spec/fixtures/test_module/spec/unit/puppet/provider/multi_device/multi_device_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'spec_helper' | ||
|
||
ensure_module_defined('Puppet::Provider::MultiDevice') | ||
require 'puppet/provider/multi_device/multi_device' | ||
|
||
RSpec.describe Puppet::Provider::MultiDevice::MultiDevice do | ||
subject(:provider) { described_class.new } | ||
|
||
let(:context) { instance_double('Puppet::ResourceApi::BaseContext', 'context') } | ||
|
||
describe '#get' do | ||
it 'processes resources' do | ||
expect(provider.get(context)).to eq [ | ||
{ | ||
name: 'foo', | ||
ensure: 'present', | ||
}, | ||
{ | ||
name: 'bar', | ||
ensure: 'present', | ||
}, | ||
] | ||
end | ||
end | ||
|
||
describe 'create(context, name, should)' do | ||
it 'creates the resource' do | ||
expect(context).to receive(:notice).with(%r{\ACreating 'a'}) | ||
|
||
provider.create(context, 'a', name: 'a', ensure: 'present') | ||
end | ||
end | ||
|
||
describe 'update(context, name, should)' do | ||
it 'updates the resource' do | ||
expect(context).to receive(:notice).with(%r{\AUpdating 'foo'}) | ||
|
||
provider.update(context, 'foo', name: 'foo', ensure: 'present') | ||
end | ||
end | ||
|
||
describe 'delete(context, name, should)' do | ||
it 'deletes the resource' do | ||
expect(context).to receive(:notice).with(%r{\ADeleting 'foo'}) | ||
|
||
provider.delete(context, 'foo') | ||
end | ||
end | ||
end |
8 changes: 8 additions & 0 deletions
8
spec/fixtures/test_module/spec/unit/puppet/type/multi_device_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require 'spec_helper' | ||
require 'puppet/type/multi_device' | ||
|
||
RSpec.describe 'the multi_device type' do | ||
it 'loads' do | ||
expect(Puppet::Type.type(:multi_device)).not_to be_nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters