Ask Your Question
0

"undefined method `parameters' for nil:NilClass" error in rspec-puppet

asked 2016-05-02 09:10:43 -0500

Chutki gravatar image

Hi,

I have below manifests for firewall module

class mod_firewalld {
firewalld::service { 'tsm':
  short           => "storage manag",
  description     => "backup",
  ports           => [
                    {
                    port      => "4335_7542",
                    protocol  => "tcp",
                    },
                    {
                    port      => "4353_0120",
                    protocol  => "tcp",
                    },
                    {
                    port      => "2345_4343",
                    protocol  => "tcp",
                    },],
}->
firewalld::service { 'scom':
  short           => "SCOM",
  description     => "back scom port",
  ports           => [
                    {
                      port     => "22",
                      protocol  => "tcp",
                    },
                    {
                      port     => "5676",
                      protocol  => "tcp",
                    },],
}

Below is the defined class

define firewalld::service(
  $short = '',
  $description = '',
  $ports = [],
  $modules = [],
  $destination = {},
) {



  file { "/etc/services/${name}.xml":
    content => template('tem_name/service.xml.erb'),
    owner   => root,
    group   => root,
    mode    => '0644',
    require => Package['firewalld'],
    notify  => Service['firewalld'],
  }
}

I tried below rspec-puppet

describe 'firewalld::service', :type => 'define' do

  let(:title) { 'undefined' }
   firewalld_srvcs = {
    'tsm' => {
      'short'       => 'storage manag',
      'description' => 'backup',
      'ports'       => [{
                    'port'      => "4335_7542",
                    'protocol'  => "tcp",
                    },
                    {
                   'port'     => "4353_0120",
                   'protocol' => "tcp",
                    },
                    {
                   'port'     => "2345_4343",
                   'protocol' => "tcp",
                    },],
      'modules'     => [],
      'destination' => {},
    },
   'test2' => {
    short           => "SCOM",
    description     => "back scom port",
    ports           => [
                    {
                      port     => "22",
                      protocol  => "tcp",
                    },
                    {
                      port     => "5676",
                      protocol  => "tcp",

           },],
   }
 firewalld_srvcs.each do|title,value|
    context title do
      let(:title) { title }

      let(:params) {{
        'short'          => value['short'],
        'description'    => value['description'],
        'ports'          => value['ports'],
      }}

      it { should contain_file("etc/services/#{title}.xml").with(
        'owner'   => 'root',
        'group'   => 'root',
        'mode'    => '0644',
        'require' => 'Package[firewalld]',
        'notify'  => 'Service[firewalld]',
      )}
    end
  end
  it 'should generate valid content for tsm.xml' do
    content = catalogue.resource('file', 'etc/services/tsm.xml').send(:parameters)[:content]
    content.should match('<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>storage manag</short>
  <description>backup</description>
  <port port="4335_7542" protocol="tcp"/>
  <port port="4353_0120" protocol="tcp"/>
  <port port="2345_4343" protocol="tcp"/>
</service>')
  end
end

but when I run test case it failed with below error message

Failure/Error: content = catalogue.resource('file', '/etc/services/tsm.xml').send(:parameters)[:content]
     NoMethodError:
       undefined method `parameters' for nil:NilClass
     # ./spec/defines/firewalld_def_spec.rb:180:in `block (2 levels) in <top (required)>'
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-05-02 18:40:48 -0500

WhatsARanjit gravatar image

You could test for content like any other attribute instead of using the catalogue.resource method.

it { should contain_file("etc/services/tsm.xml").with(
  'content' => '<?xml version="1.0" encoding="utf-8"?>\nblahblahblah',
)}
edit flag offensive delete link more

Comments

Yes I tried this but still getting Same error

Chutki gravatar imageChutki ( 2016-05-02 19:36:46 -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

2 followers

Stats

Asked: 2016-05-02 09:10:43 -0500

Seen: 138 times

Last updated: May 02 '16