Ask Your Question
0

getting error when pass hiera in rspec-puppet

asked 2016-02-15 05:44:00 -0500

Chutki gravatar image

updated 2016-02-15 22:58:44 -0500

Hi,

I have below code

service { ntpd :
    ensure    => stopped,
    enable    => false,
    hasstatus => true,
}

Rspec test case for the manifests below

 3  describe 'module' do
  4
  5   context 'testing module' do
  6     
  7
  8     it { should contain_class('module') }
  9
 10     it { should contain_package('module').with_ensure('latest')}
 11
 12     let(:hiera_config) { 'spec/fixtures/hiera.yaml' }
 13     hiera = Hiera.new({ :config => 'spec/fixtures/hiera.yaml' })
 14
 15     var_arr = hiera.lookup('temp::services',nil,nil)
 16
 17     it { should contain_service(var_arr).with(
 18       'ensure'    => 'stopped',
 19       'enable'    => 'false',
 20       'hasstatus' => 'true'
 21     )}
 22    end
 23 end

below is the hiera file tempa.yaml

---
temp::services:
  - 'ntpd'

And hiera.yaml

---
:backends:
  - yaml
:yaml:
  :datadir: './spec/fixtures/hieradata'
:hierarchy:
  - 'tempa'

when I run this test case getting

 1 ) module_name with defaults for all parameters should contain Service[["ntpd"]] with ensure => "stopped", enable    => "false" and hasstatus => "true"
         Failure/Error: )}
           expected that the catalogue would contain Service[["ntpd"]]
         # ./spec/classes/init_spec.rb:21:in `block (3 levels) in <top (required)>'
edit retag flag offensive close merge delete

Comments

Please update so that you have copied the code in here correctly. E.g. I see a trailing "end" that doesn't belong there. Also make clear which line is line 21.

Alex Harvey gravatar imageAlex Harvey ( 2016-02-15 06:52:48 -0500 )edit

Updated the test case with line numbers

Chutki gravatar imageChutki ( 2016-02-15 22:59:01 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-02-15 23:37:40 -0500

updated 2016-02-16 00:39:16 -0500

So this line returns an array right:

var_arr = hiera.lookup('temp::services',nil,nil)

It's not obvious why you are doing a Hiera lookup there at all. What's wrong with:

describe 'module' do
  context 'testing module' do
    it { should contain_class('module') }
    it { should contain_package('module').with_ensure('latest')}
    it { should contain_service('ntpd').with(
      'ensure'    => 'stopped',
      'enable'    => 'false',
      'hasstatus' => 'true'
    )}
  end
end

If there's a valid reason to use an array:

describe 'module' do
 context 'testing module' do
   it { should contain_class('module') } 
   it { should contain_package('module').with_ensure('latest')}

   let(:hiera_config) { 'spec/fixtures/hiera.yaml' } 
   hiera = Hiera.new({ :config => 'spec/fixtures/hiera.yaml' })
   service = hiera.lookup('temp::services',nil,nil)[0] 
   it { should contain_service(service).with(
     'ensure'    => 'stopped',
     'enable'    => 'false',
     'hasstatus' => 'true'
   )}
  end
end

And if you need to loop through the array:

describe 'module' do
 context 'testing module' do
   it { should contain_class('module') } 
   it { should contain_package('module').with_ensure('latest')}

   let(:hiera_config) { 'spec/fixtures/hiera.yaml' } 
   hiera = Hiera.new({ :config => 'spec/fixtures/hiera.yaml' })
   services = hiera.lookup('temp::services',nil,nil)
   services.each do |service|
     it { should contain_service(service).with(
       'ensure'    => 'stopped',
       'enable'    => 'false',
       'hasstatus' => 'true'
     )}
    end
  end
edit flag offensive delete link more

Comments

I don't have single service, I have more that 20 services in tempa.yaml thats why I use hiera lookup.

Chutki gravatar imageChutki ( 2016-02-16 00:14:40 -0500 )edit

Then you just need to address the elements of the array properly.

Alex Harvey gravatar imageAlex Harvey ( 2016-02-16 00:31:59 -0500 )edit

I updated the code. Haven't tested it.

Alex Harvey gravatar imageAlex Harvey ( 2016-02-16 00:36:35 -0500 )edit

Of course you probably also want to loop through that array. :)

Alex Harvey gravatar imageAlex Harvey ( 2016-02-16 00:37:24 -0500 )edit

Updated again.

Alex Harvey gravatar imageAlex Harvey ( 2016-02-16 00:40:10 -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

Stats

Asked: 2016-02-15 05:44:00 -0500

Seen: 63 times

Last updated: Feb 16 '16