Skip to content

Commit

Permalink
(MODULES-9428) Pass through full composite namevar values for simple_…
Browse files Browse the repository at this point in the history
…get_filter

To make sure that `simple_get_filter` can work for types with composite
namevars, we need to pass through the full set of namevar values, since
`resource_hash[type_definition.namevars.first]` is not enough for the
provider to return an instance.

When a provider implements `simple_get_filter`, and composite namevars, the Resource API does not provide sufficient information to `get` to retrieve the required resource state:

```
Puppet::ResourceApi.register_type(
  name: 'gpgkey',
  docs: <<-EOS,
      This type provides Puppet with the capabilities to manage ...
    EOS
  title_patterns: [
    {
      pattern: %r{^(?<gpgdir>.*)/(?<name>[^/]*)$},
      desc: 'Where the gpgdir and the key_name is provided as the last field of the path',
    },
    {
      pattern: %r{^(?<name>.*)$},
      desc: 'Where only the key_name is provided, if using the default folder',
    },
  ],
  features: ['simple_get_filter'],
  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,
    },
    gpgdir: {
      type:    'String',
      desc:    'Path to store the GPG key.',
      default: '/root',
      behaviour: :namevar,
    },
  },
)
```

```
class Puppet::Provider::Gpgkey::Gpgkey < Puppet::ResourceApi::SimpleProvider
  def get(context, name = [])
    context.debug('Returning pre-canned example data for: %s' % name.inspect)
    [
      {
        title: '/root/foo',
        name: 'foo',
        gpgdir: '/root',
        ensure: 'present',
      },
      #{
      #  title: '/root/bar',
      #  name: 'bar',
      #  gpgdir: '/root',
      #  ensure: 'present',
      #},
    ]
  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
```

```
gpgkey {
  'baz1':
    ensure => present,
    name   => 'foo',
    gpgdir => '/root';
  '/root/bar':
    ensure => present;
  '/root/foo2':
    ensure => present;
}
```

```
Info: Applying configuration version '1558363097'
Debug: gpgkey supports `simple_get_filter`
Debug: gpgkey: Returning pre-canned example data for: ["foo"]
Debug: gpgkey does not support `canonicalize`
Debug: Current State: {:title=>"/root/foo", :name=>"foo", :gpgdir=>"/root", :ensure=>"present"}
Debug: gpgkey supports `simple_get_filter`
Debug: gpgkey: Returning pre-canned example data for: ["bar"]
Debug: Current State: {:title=>"bar", :ensure=>:absent}
Notice: /Stage[main]/Main/Gpgkey[bar]/ensure: defined 'ensure' as 'present'
Debug: gpgkey does not support `canonicalize`
Debug: Target State: {:name=>"bar", :ensure=>"present", :gpgdir=>"/root"}
Debug: gpgkey does not support `supports_noop`
Debug: gpgkey supports `simple_get_filter`
Debug: gpgkey[bar]: Creating: Start
Notice: gpgkey[bar]: Creating: Creating 'bar' with {:name=>"bar", :ensure=>"present", :gpgdir=>"/root"}
Notice: gpgkey[bar]: Creating: Finished in 0.000085 seconds
Debug: /Stage[main]/Main/Gpgkey[bar]: The container Class[Main] will propagate my refresh event
Debug: gpgkey supports `simple_get_filter`
Debug: gpgkey: Returning pre-canned example data for: ["foo2"]
Debug: Current State: {:title=>"foo2", :ensure=>:absent}
Notice: /Stage[main]/Main/Gpgkey[foo2]/ensure: defined 'ensure' as 'present'
Debug: gpgkey does not support `canonicalize`
Debug: Target State: {:name=>"foo2", :ensure=>"present", :gpgdir=>"/root"}
Debug: gpgkey does not support `supports_noop`
Debug: gpgkey supports `simple_get_filter`
Debug: gpgkey[foo2]: Creating: Start
Notice: gpgkey[foo2]: Creating: Creating 'foo2' with {:name=>"foo2", :ensure=>"present", :gpgdir=>"/root"}
Notice: gpgkey[foo2]: Creating: Finished in 0.000069 seconds
Debug: /Stage[main]/Main/Gpgkey[foo2]: The container Class[Main] will propagate my refresh event
Debug: Class[Main]: The container Stage[main] will propagate my refresh event
Debug: Finishing transaction 47323787575300
Debug: Storing state
Debug: Pruned old state cache entries in 0.00 seconds
Debug: Stored state in 0.01 seconds
Notice: Applied catalog in 0.03 seconds
```

As can be seen in the `Returning pre-canned example data for` messages, only `name`, but not `gpgdir` is passed through. This is because of the weakness of title generation in https://github.com/puppetlabs/puppet-resource_api/blob/d249941cf7544005ad66b9bb54e079ffdfc0ac45/lib/puppet/resource_api.rb#L230-L235

After these changes, a hash of namevars and their values is used as a title when requesting resources from the provider:

```
Info: Applying configuration version '1561397939'
Debug: gpgkey: Returning pre-canned example data for: [{:name=>"foo", :gpgdir=>"/root"}]
Debug: Current State: {:title=>"/root/foo", :name=>"foo", :gpgdir=>"/root", :ensure=>"present"}
Debug: gpgkey: Returning pre-canned example data for: [{:name=>"bar", :gpgdir=>"/root"}]
Debug: Current State: {:name=>"bar", :gpgdir=>"/root", :ensure=>:absent}
Notice: /Stage[main]/Main/Gpgkey[bar]/ensure: defined 'ensure' as 'present'
Debug: Target State: {:name=>"bar", :gpgdir=>"/root", :ensure=>"present"}
Debug: gpgkey[{:name=>"bar", :gpgdir=>"/root"}]: Creating: Start
Notice: gpgkey[{:name=>"bar", :gpgdir=>"/root"}]: Creating: Creating '{:title=>{:name=>"bar", :gpgdir=>"/root"}, :name=>"bar", :gpgdir=>"/root"}' with {:name=>"bar", :gpgdir=>"/root", :ensure=>"present"}
Notice: gpgkey[{:name=>"bar", :gpgdir=>"/root"}]: Creating: Finished in 0.000091 seconds
Debug: /Stage[main]/Main/Gpgkey[bar]: The container Class[Main] will propagate my refresh event
Debug: gpgkey: Returning pre-canned example data for: [{:name=>"foo2", :gpgdir=>"/root"}]
Debug: Current State: {:name=>"foo2", :gpgdir=>"/root", :ensure=>:absent}
Notice: /Stage[main]/Main/Gpgkey[foo2]/ensure: defined 'ensure' as 'present'
Debug: Target State: {:name=>"foo2", :gpgdir=>"/root", :ensure=>"present"}
Debug: gpgkey[{:name=>"foo2", :gpgdir=>"/root"}]: Creating: Start
Notice: gpgkey[{:name=>"foo2", :gpgdir=>"/root"}]: Creating: Creating '{:title=>{:name=>"foo2", :gpgdir=>"/root"}, :name=>"foo2", :gpgdir=>"/root"}' with {:name=>"foo2", :gpgdir=>"/root", :ensure=>"present"}
Notice: gpgkey[{:name=>"foo2", :gpgdir=>"/root"}]: Creating: Finished in 0.000116 seconds
Debug: /Stage[main]/Main/Gpgkey[foo2]: The container Class[Main] will propagate my refresh event
Debug: Class[Main]: The container Stage[main] will propagate my refresh event
Debug: Finishing transaction 47326046031800
Debug: Storing state
Debug: Pruned old state cache entries in 0.00 seconds
Debug: Stored state in 0.01 seconds
Notice: Applied catalog in 0.02 seconds
```

The provider should return a properly formatted `title` value from `get` to enable pretty formatting in logs and `puppet resource` output.

# Conflicts:
#	spec/acceptance/composite_namevar_spec.rb
  • Loading branch information
DavidS committed Jun 28, 2019
1 parent 3f52b58 commit 7ab1d9e
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 46 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ RSpec::Core::RakeTask.new(:spec) do |t|
if RUBY_PLATFORM == 'java'
excludes += ['acceptance/**/*.rb', 'integration/**/*.rb', 'puppet/resource_api/*_context_spec.rb', 'puppet/util/network_device/simple/device_spec.rb']
t.rspec_opts = '--tag ~agent_test'
t.rspec_opts << ' --tag ~j17_exclude' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.0.0')
end
t.exclude_pattern = "spec/{#{excludes.join ','}}"
end
Expand Down
28 changes: 23 additions & 5 deletions lib/puppet/resource_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ def name
title
end

def self.build_title(type_definition, resource_hash)
if type_definition.namevars.size > 1
# use a MonkeyHash to allow searching in Puppet's RAL
Puppet::ResourceApi::MonkeyHash[type_definition.namevars.map { |attr| [attr, resource_hash[attr]] }]
else
resource_hash[type_definition.namevars[0]]
end
end

def rsapi_title
@rsapi_title ||= self.class.build_title(type_definition, self)
@rsapi_title
end

def to_resource
to_resource_shim(super)
end
Expand Down Expand Up @@ -261,7 +275,7 @@ def self.instances
result = if resource_hash.key? :title
new(title: resource_hash[:title])
else
new(title: resource_hash[type_definition.namevars.first])
new(title: build_title(type_definition, resource_hash))
end
result.cache_current_state(resource_hash)
result
Expand All @@ -270,7 +284,7 @@ def self.instances

def refresh_current_state
@rsapi_current_state = if type_definition.feature?('simple_get_filter')
my_provider.get(context, [title]).find { |h| namevar_match?(h) }
my_provider.get(context, [rsapi_title]).find { |h| namevar_match?(h) }
else
my_provider.get(context).find { |h| namevar_match?(h) }
end
Expand All @@ -279,7 +293,11 @@ def refresh_current_state
type_definition.check_schema(@rsapi_current_state)
strict_check(@rsapi_current_state) if type_definition.feature?('canonicalize')
else
@rsapi_current_state = { title: title }
@rsapi_current_state = if rsapi_title.is_a? Hash
rsapi_title.dup
else
{ title: rsapi_title }
end
@rsapi_current_state[:ensure] = :absent if type_definition.ensurable?
end
end
Expand Down Expand Up @@ -340,9 +358,9 @@ def flush
end

if type_definition.feature?('supports_noop')
my_provider.set(context, { title => { is: @rsapi_current_state, should: target_state } }, noop: noop?)
my_provider.set(context, { rsapi_title => { is: @rsapi_current_state, should: target_state } }, noop: noop?)
else
my_provider.set(context, title => { is: @rsapi_current_state, should: target_state }) unless noop?
my_provider.set(context, rsapi_title => { is: @rsapi_current_state, should: target_state }) unless noop?
end
raise 'Execution encountered an error' if context.failed?

Expand Down
15 changes: 15 additions & 0 deletions lib/puppet/resource_api/glue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,19 @@ def filtered_keys
values.keys.reject { |k| k == :title || !attr_def[k] || (attr_def[k][:behaviour] == :namevar && @namevars.size == 1) }
end
end

# this hash allows key-value based ordering comparisons between instances of this and instances of this and other classes
# this is required for `lib/puppet/indirector/resource/ral.rb`'s `search` method which expects all titles to be comparable
class MonkeyHash < Hash
def <=>(other)
result = self.class.name <=> other.class.name
if result.zero?
result = keys.sort <=> other.keys.sort
end
if result.zero?
result = keys.sort.map { |k| self[k] } <=> other.keys.sort.map { |k| other[k] }
end
result
end
end
end
26 changes: 13 additions & 13 deletions spec/acceptance/composite_namevar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
stdout_str, status = Open3.capture2e("puppet resource #{common_args} composite_namevar php/gem")
expect(stdout_str.strip).to match %r{^composite_namevar \{ \'php/gem\'}
expect(stdout_str.strip).to match %r{ensure\s*=> \'present\'}
expect(stdout_str.strip).to match %r{Looking for \["php/gem"\]}
expect(stdout_str.strip).to match %r{Looking for \[\{:package=>"php", :manager=>"gem"\}\]}
expect(status.exitstatus).to eq 0
end
it 'properly identifies an absent resource if only the title is provided' do
stdout_str, status = Open3.capture2e("puppet resource #{common_args} composite_namevar php-wibble")
expect(stdout_str.strip).to match %r{^composite_namevar \{ \'php-wibble\'}
expect(stdout_str.strip).to match %r{ensure\s*=> \'absent\'}
expect(stdout_str.strip).to match %r{Looking for \["php-wibble"\]}
expect(stdout_str.strip).to match %r{Looking for \[\{:package=>"php", :manager=>"wibble"\}\]}
expect(status.exitstatus).to eq 0
end
it 'creates a previously absent resource' do
Expand All @@ -47,7 +47,7 @@
expect(stdout_str.strip).to match %r{ensure\s*=> \'present\'}
expect(stdout_str.strip).to match %r{package\s*=> \'php\'}
expect(stdout_str.strip).to match %r{manager\s*=> \'wibble\'}
expect(stdout_str.strip).to match %r{Looking for \["php-wibble"\]}
expect(stdout_str.strip).to match %r{Looking for \[\{:package=>"php", :manager=>"wibble"\}\]}
expect(status.exitstatus).to eq 0
end
it 'will remove an existing resource' do
Expand All @@ -56,7 +56,7 @@
expect(stdout_str.strip).to match %r{package\s*=> \'php\'}
expect(stdout_str.strip).to match %r{manager\s*=> \'gem\'}
expect(stdout_str.strip).to match %r{ensure\s*=> \'absent\'}
expect(stdout_str.strip).to match %r{Looking for \["php-gem"\]}
expect(stdout_str.strip).to match %r{Looking for \[\{:package=>"php", :manager=>"gem"\}\]}
expect(status.exitstatus).to eq 0
end
end
Expand All @@ -79,31 +79,31 @@
let(:manifest) { 'composite_namevar { php-gem: }' }

it { expect(@stdout_str).to match %r{Current State: \{:title=>"php-gem", :package=>"php", :manager=>"gem", :ensure=>"present", :value=>"b"\}} }
it { expect(@stdout_str).to match %r{Looking for \["php-gem"\]} }
it { expect(@stdout_str).to match %r{Looking for \[\{:package=>"php", :manager=>"gem"\}\]} }
it { expect(@status.exitstatus).to eq 0 }
end

context 'when managing an absent instance' do
let(:manifest) { 'composite_namevar { php-wibble: ensure=>\'absent\' }' }

it { expect(@stdout_str).to match %r{Composite_namevar\[php-wibble\]: Nothing to manage: no ensure and the resource doesn't exist} }
it { expect(@stdout_str).to match %r{Looking for \["php-wibble"\]} }
it { expect(@stdout_str).to match %r{Looking for \[\{:package=>"php", :manager=>"wibble"\}\]} }
it { expect(@status.exitstatus).to eq 0 }
end

context 'when creating a previously absent instance' do
let(:manifest) { 'composite_namevar { php-wibble: ensure=>\'present\' }' }

it { expect(@stdout_str).to match %r{Composite_namevar\[php-wibble\]/ensure: defined 'ensure' as 'present'} }
it { expect(@stdout_str).to match %r{Looking for \["php-wibble"\]} }
it { expect(@stdout_str).to match %r{Looking for \[\{:package=>"php", :manager=>"wibble"\}\]} }
it { expect(@status.exitstatus).to eq 2 }
end

context 'when removing a previously present instance' do
let(:manifest) { 'composite_namevar { php-yum: ensure=>\'absent\' }' }

it { expect(@stdout_str).to match %r{Composite_namevar\[php-yum\]/ensure: undefined 'ensure' from 'present'} }
it { expect(@stdout_str).to match %r{Looking for \["php-yum"\]} }
it { expect(@stdout_str).to match %r{Looking for \[\{:package=>"php", :manager=>"yum"\}\]} }
it { expect(@status.exitstatus).to eq 2 }
end

Expand All @@ -112,7 +112,7 @@

it { expect(@stdout_str).to match %r{Current State: \{:title=>"php-gem", :package=>"php", :manager=>"gem", :ensure=>"present", :value=>"b"\}} }
it { expect(@stdout_str).to match %r{Target State: \{:package=>"php", :manager=>"gem", :value=>"c", :ensure=>"present"\}} }
it { expect(@stdout_str).to match %r{Looking for \["php/gem"\]} }
it { expect(@stdout_str).to match %r{Looking for \[\{:package=>"php", :manager=>"gem"\}\]} }
it { expect(@status.exitstatus).to eq 2 }
end
end
Expand All @@ -122,31 +122,31 @@
let(:manifest) { 'composite_namevar { "sometitle": package => "php", manager => "gem" }' }

it { expect(@stdout_str).to match %r{Current State: \{:title=>"php-gem", :package=>"php", :manager=>"gem", :ensure=>"present", :value=>"b"\}} }
it { expect(@stdout_str).to match %r{Looking for \["sometitle"\]} }
it { expect(@stdout_str).to match %r{Looking for \[\{:package=>"php", :manager=>"gem"\}\]} }
it { expect(@status.exitstatus).to eq 0 }
end

context 'when managing an absent instance' do
let(:manifest) { 'composite_namevar { "sometitle": ensure => "absent", package => "php", manager => "wibble" }' }

it { expect(@stdout_str).to match %r{Composite_namevar\[sometitle\]: Nothing to manage: no ensure and the resource doesn't exist} }
it { expect(@stdout_str).to match %r{Looking for \["sometitle"\]} }
it { expect(@stdout_str).to match %r{Looking for \[\{:package=>"php", :manager=>"wibble"\}\]} }
it { expect(@status.exitstatus).to eq 0 }
end

context 'when creating a previously absent instance' do
let(:manifest) { 'composite_namevar { "sometitle": ensure => "present", package => "php", manager => "wibble" }' }

it { expect(@stdout_str).to match %r{Composite_namevar\[sometitle\]/ensure: defined 'ensure' as 'present'} }
it { expect(@stdout_str).to match %r{Looking for \["sometitle"\]} }
it { expect(@stdout_str).to match %r{Looking for \[\{:package=>"php", :manager=>"wibble"\}\]} }
it { expect(@status.exitstatus).to eq 2 }
end

context 'when removing a previously present instance' do
let(:manifest) { 'composite_namevar { "sometitle": ensure => "absent", package => "php", manager => "yum" }' }

it { expect(@stdout_str).to match %r{Composite_namevar\[sometitle\]/ensure: undefined 'ensure' from 'present'} }
it { expect(@stdout_str).to match %r{Looking for \["sometitle"\]} }
it { expect(@stdout_str).to match %r{Looking for \[\{:package=>"php", :manager=>"yum"\}\]} }
it { expect(@status.exitstatus).to eq 2 }
end
end
Expand Down
7 changes: 4 additions & 3 deletions spec/acceptance/namevar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
expect(stdout_str.strip).to match %r{ensure\s*=> \'present\'}
expect(status).to eq 0
end
it 'returns the match if title matches a namevar value' do
stdout_str, status = Open3.capture2e("puppet resource #{common_args} multiple_namevar php")
expect(stdout_str.strip).to match %r{^multiple_namevar \{ \'php\'}
it 'returns the match if title matches a title value' do
stdout_str, status = Open3.capture2e("puppet resource #{common_args} multiple_namevar php-gem")
expect(stdout_str.strip).to match %r{^multiple_namevar \{ \'php-gem\'}
expect(stdout_str.strip).to match %r{ensure\s*=> \'present\'}
expect(stdout_str.strip).to match %r{package\s*=> \'php\'}
expect(stdout_str.strip).to match %r{manager\s*=> \'gem\'}
expect(status).to eq 0
end
it 'creates a previously absent resource if all namevars are provided' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'puppet/resource_api'
require 'puppet/resource_api/simple_provider'

# Implementation for the title_provider type using the Resource API.
# Implementation for the composite_namevar type using the Resource API.
class Puppet::Provider::CompositeNamevar::CompositeNamevar < Puppet::ResourceApi::SimpleProvider
def initialize
@current_values ||= [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
class Puppet::Provider::MultipleNamevar::MultipleNamevar
def initialize
@current_values ||= [
{ package: 'php', manager: 'yum', ensure: 'present' },
{ package: 'php', manager: 'gem', ensure: 'present' },
{ package: 'mysql', manager: 'yum', ensure: 'present' },
{ package: 'mysql', manager: 'gem', ensure: 'present' },
{ package: 'foo', manager: 'bar', ensure: 'present' },
{ package: 'bar', manager: 'foo', ensure: 'present' },
{ title: 'php-yum', package: 'php', manager: 'yum', ensure: 'present' },
{ title: 'php-gem', package: 'php', manager: 'gem', ensure: 'present' },
{ title: 'mysql-yum', package: 'mysql', manager: 'yum', ensure: 'present' },
{ title: 'mysql-gem', package: 'mysql', manager: 'gem', ensure: 'present' },
{ title: 'foo-bar', package: 'foo', manager: 'bar', ensure: 'present' },
{ title: 'bar-foo', package: 'bar', manager: 'foo', ensure: 'present' },
]
end

Expand Down
32 changes: 29 additions & 3 deletions spec/integration/resource_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,27 @@
let(:definition) do
{
name: 'integration',
title_patterns: [
{
pattern: %r{^(?<name>.*[^-])-(?<name2>.*)$},
desc: 'Where the name and the name2 are provided with a hyphen separator',
},
{
pattern: %r{^(?<name>.*)$},
desc: 'Where only the name is provided',
},
],
attributes: {
name: {
type: 'String',
behaviour: :namevar,
desc: 'the title',
},
name2: {
type: 'String',
behaviour: :namevar,
desc: 'the other title',
},
string: {
type: 'String',
desc: 'a string attribute',
Expand Down Expand Up @@ -170,7 +185,18 @@
s = setter
Class.new do
def get(_context)
[]
[
{
name: 'foo',
name2: 'bar',
title: 'foo-bar',
},
{
name: 'foo2',
name2: 'bar2',
title: 'foo2-bar2',
},
]
end

attr_reader :last_changes
Expand Down Expand Up @@ -198,7 +224,7 @@ def get(_context)
# See spec/acceptance/metaparam_spec.rb for more in-depth testing with a full puppet apply
let(:catalog) { instance_double('Puppet::Resource::Catalog', 'catalog') }
let(:instance) do
type.new(name: 'somename', ensure: 'present', boolean: true, integer: 15, float: 1.23,
type.new(name: 'somename', name2: 'othername', ensure: 'present', boolean: true, integer: 15, float: 1.23,
variant_pattern: '0x1234ABCD', url: 'http://www.google.com', sensitive: Puppet::Pops::Types::PSensitiveType::Sensitive.new('a password'),
string_array: %w[a b c],
variant_array: 'not_an_array', array_of_arrays: [%w[a b c], %w[d e f]],
Expand All @@ -225,7 +251,7 @@ def get(_context)
end

it 'can serialize to json' do
expect({ 'resources' => [instance.to_resource] }.to_json).to eq '{"resources":[{"somename":{"ensure":"absent","boolean_param":false,"integer_param":99,"float_param":3.21,"ensure_param":"present","variant_pattern_param":"1234ABCD","url_param":"http://www.puppet.com","string_array_param":"d","e":"f","string_param":"default value"}}]}' # rubocop:disable Metrics/LineLength
expect({ 'resources' => [instance.to_resource] }.to_json).to eq '{"resources":[{"somename":{"name":"somename","name2":"othername","ensure":"absent","boolean_param":false,"integer_param":99,"float_param":3.21,"ensure_param":"present","variant_pattern_param":"1234ABCD","url_param":"http://www.puppet.com","string_array_param":"d","e":"f","string_param":"default value"}}]}' # rubocop:disable Metrics/LineLength
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/puppet/resource_api/glue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,20 @@
it { expect(instance.to_hash).to eq(namevarname: 'title', attr: 'value', attr_ro: 'fixed') }
end
end

describe Puppet::ResourceApi::MonkeyHash do
it { expect(described_class.ancestors).to include Hash }

describe '#<=>(other)' do
subject(:value) { described_class[b: 'b', c: 'c'] }

it { expect(value <=> 'string').to eq(-1) }
# Avoid this test on jruby 1.7, where it is hitting a implementation inconsistency and `'string' <=> value` returns `nil`
it('compares to a string', j17_exclude: true) { expect('string' <=> value).to eq 1 }
it { expect(value <=> described_class[b: 'b', c: 'c']).to eq 0 }
it { expect(value <=> described_class[d: 'd']).to eq(-1) }
it { expect(value <=> described_class[a: 'a']).to eq 1 }
it { expect(value <=> described_class[b: 'a', c: 'c']).to eq 1 }
end
end
end
Loading

0 comments on commit 7ab1d9e

Please sign in to comment.