Ask Your Question

rspec-puppet - how to check array contents of ressources

asked 2017-02-16 08:14:55 -0600

diLLec gravatar image

Hi there,

is there a way to use the contain_* matchers on ressource parameters? I have a define, which accepts an array which is passed to augeas

define mymodule::mydef (array $raw_changes) {
  <code to mangle $raw_changes into $changes>
augeas { $config_file:
    context => $context,
    changes => $changes,
    lens => $lens,
    incl => $config_file

Now I would like to write a rspec test which checks if there is an augeas ressource by that name and whether it holds the changes I've specified to the define. I simply could check if all my specified changes have made it into the augeas ressource, but I think that this is not very elegant.

it { contain_augeas('/etc/ssh/sshd_config').with_changes(['set HostKey = /etc/ssh/ssh_host_key', 'set Port = 23'])}

I'd rather like to do something like this:

ressource_augeas('/etc/ssh/sshd_config').contains_changes('set HostKey = /etc/ssh/ssh_host_key')

Is that possible?

Greetings, diLLec

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-02-17 07:01:14 -0600

DarylW gravatar image

updated 2017-02-17 07:01:56 -0600

I ran into this same problem and wrote a blog post about it -

Basically, the problem is that the parameters aren't directly exposed to the matchers, so you have to jump down into the pure include matcher from the RSpec Expectations expect(param_array).to include('value to match in array')

Here's a full example -

require 'spec_helper'

describe 'hosttest' do
  context 'with default values for all parameters' do
    context 'With foo.local host pre_condition' do
      let(:pre_condition) {"
        host { 'foo.local':
          ip => '',
          host_aliases => [ 'foo1.local', 'foo2.local' ],
      # This style works, but it requires the exact and full array for comparison
      it { should contain_host('foo.local').with_host_aliases( ['foo1.local', 'foo2.local'] ) }
      # This works, but it feels really awkard to do
      it {
        host_aliases = catalogue.resource('host', 'foo.local').send(:parameters)[:host_aliases]
        expect(host_aliases).to include('foo1.local')
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-02-16 05:50:30 -0600

Seen: 397 times

Last updated: Feb 17 '17