Ask Your Question
0

Is it possible to clear catalog cache in rspec-puppet

asked 2016-04-06 05:07:58 -0500

Chutki gravatar image

Hi,

I have below manifests

class module_name {

  if str2bool("$::is_virtual") == true {

    $p1 = 'package-name'


    if $::operatingsystemmajrelease < '7' {
         package {["${p1}-pae"]: ensure => installed }
        }
        else {
          package {$p1: ensure => installed }
        }
      }
    }
}

rspec-puppet test is,

require 'spec_helper'

describe 'module_name' do


  it { should contain_class('module_name') }

    osver = '5'

   if osver < '7'

     p1 = 'package-name'

     it { should contain_package("#{p1}-pae").with_ensure('installed')}
  end

  if osver == '7'

    p1 = 'package-name'

    it { should contain_package(p1).with_ensure('installed')}
  end
end

The above test case worked with out any issues. But if I change the value to osver = '7', getting below error

Failure/Error: it { should contain_package('package-name').with_ensure('installed')}
       expected that the catalogue would contain Package[package-name]

I know the cause of the error, because rspec-puppet will only re-compile the catalog there are changes in facts or variable. But it won't be satisfied because the catalog was already compiled for a previous test, so its not accepting the changed value test case.

so, is there any option to clear the previously compiled test case catalog and re-compile the catalog if we cahnge any "if condition" or facts.

I know about mock function, but it works only for custom function and hiera. Do we have any other alternate for my test case.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-04-06 06:19:18 -0500

The way to "clear" it is simply begin a new describe or context block.

All you need here, however, is to specify your facts:

require 'spec_helper'

describe 'module_name' do
  context 'os release 7' do
    let(:facts) {{ :operatingsystemmajrelease => '7' }}
    it { should contain_package('package-name-pae').with_ensure('installed')}
  end

  context 'os release earlier than 7' do
    let(:facts) {{ :operatingsystemmajrelease => '5' }}
    it { should contain_package('package-name').with_ensure('installed')}
  end
end
edit flag offensive delete link more

Comments

getting error. Failure/Error: it { should contain_package('package-name').with_ensure('installed')} expected that the catalogue would contain Package(package-name)

Chutki gravatar imageChutki ( 2016-04-06 06:29:45 -0500 )edit

So why don't you dump the catalog like I showed you and see what's wrong? :)

Alex Harvey gravatar imageAlex Harvey ( 2016-04-06 08:43:43 -0500 )edit

You should also have mocked $::is_virtual somewhere too don't forget.

Alex Harvey gravatar imageAlex Harvey ( 2016-04-06 08:45:27 -0500 )edit

I tried require 'spec_helper' describe 'module_name' do it { File.write( 'module_name.json', PSON.pretty_generate(catalogue) )}end module_name.json file created and /opt/puppet/bin/rake spec /opt/puppet/bin/ruby -S rspec spec/classes/init_spec.rb --color . Finished in 0.06311

Chutki gravatar imageChutki ( 2016-04-06 22:17:35 -0500 )edit

I have completed one more module rspec-puppet test case with "if condition", but it's working without any issues when I change hiera value from "on" to "off" or from "off" to "on". http://pastebin.com/aLLwJ3pv

Chutki gravatar imageChutki ( 2016-04-07 00:28:55 -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-04-06 05:07:58 -0500

Seen: 212 times

Last updated: Apr 06 '16