Ask Your Question
0

"<" symbol not working in rspec-puppet

asked 2016-02-24 05:54:04 -0600

Chutki gravatar image

Hi,

I have below code for sysctl module

$shmx_val = hiera_hash('sysctl::shmmax')
create_resources ('shmmax_value',$tempx)

define shmmax_value ($shmx_val) {
    augeas { 'shmmax':
      incl    => '/etc/sysctl.conf',
      lens    => 'Sysctl.lns',
      onlyif  => "get kernel.shmmax < '${::shmx_val}'",
      changes => "set kernel.shmmax '${::shmx_val}'"

below is the rspec-puppet test-case for this

context 'iapplication kernel parameter' do
    let(:facts) {{ :memorysize_mb => '5647' }}

    let(:hiera_config) { 'spec/fixtures/hiera.yaml' }
    hiera = Hiera.new({ :config => 'spec/fixtures/hiera.yaml' })

    shmx_val = hiera.lookup('shmmax',nil,nil)

    it { should contain_augeas('shmmax').with(
      'incl'    => '/etc/sysctl.conf',
      'lens'    => 'Sysctl.lns',
      'onlyif'  => 'get kernel.shmmax < shmx_val',
      'changes' => 'set kernel.shmmax shmx_val'
    )}
  end

when I run this test case getting below error

Failure/Error: )}
       expected that the catalogue would contain Augeas[shmmax] with onlyif set to "get kernel.shmmax < shmx_val" 
but it is set to "get kernel.shmx_val < ''", and parameter changes set to "set kernel.shmmax shmx_val" but it is set to "set kernel.shmmax ''"
       Diff:

       @@ -1,4 +1,4 @@
       -get kernel.shmmax < shmx_val'
       +get kernel.shmmax < ''

       -set kernel.shmmax shmx_val'
       +set kernel.shmmax ''

I tried below options to pass the test case

'onlyif'  => 'get kernel.shmmax' < shmx_val,
'changes' => 'set kernel.shmmax' shmx_val

'onlyif'  => 'get kernel.shmmax <' shmx_val,
'changes' => 'set kernel.shmmax' shmx_val

'onlyif'  => '"get kernel.shmmax" < shmx_val',
'changes' => 'set kernel.shmmax' shmx_val

but none of the options worked.

Please help me how I can write rspec-puppet for this augeas resources.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-02-24 08:28:22 -0600

updated 2016-02-29 07:01:50 -0600

These lines here, what are they telling you:

Failure/Error: )}
       expected ...
       Diff:

       @@ -1,4 +1,4 @@
       -get kernel.shmmax < shmx_val'
       +get kernel.shmmax < ''

A list of issues with this code:

  1. In the first two lines you have shown us:

    $shmx_val = hiera_hash('sysctl::shmmax')
    create_resources ('shmmax_value',$tempx)
    

    Since $shmx_val is apparently supposed to be an integer, why is the hiera_hash() function used to read it in? This makes no sense.

  2. In line 2 you call create_resources and pass in a variable $tempx. Where did this $tempx come from?
  3. In your defined type, you call a top scope variable ${::shmx_val}. Why? I suspect you mean $shmx_val not $::shmx_val.
  4. Whereas in your manifest you look up hiera_hash('sysctl::shmmax'), in your rspec test you say shmx_val = hiera.lookup('shmmax',nil,nil). Which one is correct? Does Hiera contain a key shmmax or does it contain a key sysctl::shmmax? Try running grep -r ^shmmax hieradata/ and grep -r ^sysctl::shmmax hieradata/ to find out.
  5. When you say in your rspec test:

    'onlyif'  => 'get kernel.shmmax < shmx_val',
    'changes' => 'set kernel.shmmax shmx_val',
    

    You really want to interpolate shmx_val don't you? Try:

    'onlyif'  => "get kernel.shmmax < #{shmx_val}",
    'changes' => "set kernel.shmmax #{shmx_val}",
    
edit flag offensive delete link more

Comments

My understanding is, we should not pass any valu after < '' its is null.

Chutki gravatar imageChutki ( 2016-02-26 07:21:59 -0600 )edit

It's telling you the difference between what it expected, and what it received. Expected shmx_val; received an empty string. So what's wrong?

Alex Harvey gravatar imageAlex Harvey ( 2016-02-26 08:21:54 -0600 )edit

I changed the value to ("'onlyif' => "get kernel.shmmax < ''", 'changes' => "set kernel.shmmax ''"") my test case passed. But how it worked when I passed empty string in test file when the actual manifest has value "(onlyif => "get kernel.shmmax < '${::shmx_val}'" )

Chutki gravatar imageChutki ( 2016-02-29 01:22:50 -0600 )edit

That's obviously not the solution. WHY did you get an empty string in the first place? Which line in the code sets the value of shmx_val? What is that line doing? What should it be doing?

Alex Harvey gravatar imageAlex Harvey ( 2016-02-29 02:03:04 -0600 )edit

Ok, I have updated my answer with a more comprehensive list of issues I see in this code.

Alex Harvey gravatar imageAlex Harvey ( 2016-02-29 02:28:22 -0600 )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-02-24 05:54:04 -0600

Seen: 127 times

Last updated: Feb 29 '16