Ask Your Question
0

verify_contents catalog error in rspec-puppet

asked 2016-03-03 05:21:03 -0500

Chutki gravatar image

Hi,

I tried to use verify_contents function to test content of config file in rspec-puppet

it { should contain_file('/etc/resolv.conf').with(
      'ensure'  => 'present',
      'mode'    => '0640',
      'group'   => 'root',
      'owner'   => 'root',
      verify_contents(catalog, '/etc/resolv.conf',[
        'search server.com'
    ])
  )}

but I got below error

`block (2 levels) in <top (required)>': undefined local variable or method `catalog' for #<Class:0x0000000261c860> (NameError)
        from /opt/puppet/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:246:in `module_eval'

is this any rspec version issue.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-03-04 00:17:49 -0500

updated 2016-07-11 09:24:48 -0500

The method verify_contents is an undocumented feature provided by the puppetlabs_spec_helper gem, and my recommendation would be not to use this feature here.

You can see some example code that uses verify_contents here and you can see the method's source code here.

If you really need to use this feature, you could do this (note: you must use the UK spelling for catalogue):

it {
  is_expected.to contain_file('/etc/resolv.conf').with(
    'ensure'  => 'present',
    'mode'    => '0640',
    'group'   => 'root',
    'owner'   => 'root',
  )
  verify_contents(catalogue, '/etc/resolv.conf', ['search server.com'])
}

However it is simpler just to do this:

it {
  is_expected.to contain_file('/etc/resolv.conf').with(
    'ensure'  => 'present',
    'mode'    => '0640',
    'group'   => 'root',
    'owner'   => 'root',
    'content' => "search server.com\n",
  )
}

As you have mentioned in the comments it's also possible to do this:

it {
  is_expected.to contain_file('/etc/resolv.conf').with(
    'ensure'  => 'present',
    'mode'    => '0640',
    'group'   => 'root',
    'owner'   => 'root',
  )
  content = catalogue.resource('file', '/etc/resolv.conf').send(:parameters)[:content]
  expect(content).to match 'search server.com'
}
edit flag offensive delete link more

Comments

Thanks for the update. I am already using content = catalogue.resource('file', /opt/puppet/test.txt').send(:parameters)[:content] content.should match(' content')

Chutki gravatar imageChutki ( 2016-07-11 05:57:54 -0500 )edit

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

Stats

Asked: 2016-03-03 05:21:03 -0500

Seen: 203 times

Last updated: Jul 11 '16