Ask Your Question

if statement error in rspec-puppet

asked 2016-03-03 05:13:09 -0600

Chutki gravatar image


I have boolean value in hiera variable.

$bol     = hiera('bolvalue', false)

if str2bool($bol) {
    package { ['pckg1','pckg2']: ensure => latest }
   package { ['pckg1','pckg2']: ensure => absent }

hiera file


bolvalue: 'true'

I tried if statment in rspec-puppet

 [pckg1','pckg2'].each do|pckg|
            ['true','false'].each do|bool|
              if bool == 'true'
                it { should contain_package(pckg).with_ensure('latest') }
             it { should contain_package(pckg).with_ensure('absent') }

but I am getting below error

Failure/Error: it { should contain_package(pckg).with_ensure('absent') }
       expected that the catalogue would contain Package[pckg1] with ensure set to "absent" but it is set to "latest"
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-03-03 06:44:27 -0600

updated 2016-03-03 06:46:37 -0600

The catalog under test was compiled before this code you've shown us executes. It already contains, or does not contain, the two packages pckg1 and pckg2, so the logic is invalid. As far as I know, a catalog is compiled for each describe/context block. It sounds like you want to test the behaviour when the hiera data changes. I think your best option is to refactor your manifest so that $bol is an input (parameter) to the class you're testing.

edit flag offensive delete link more


I think , we can implement "if" statement in rspec-puppet. pls refer the below url Here they use osrelease in "if" statment

Chutki gravatar imageChutki ( 2016-03-09 07:21:08 -0600 )edit

You miss the point. In the SELinux module, a new catalog is compiled for each value of osrelease, due to placement of the describe block. But in your code, you firstly compile a catalog, then you ask if the package ensure => latest, and then if the same package ensure => absent. Can't be both!

Alex Harvey gravatar imageAlex Harvey ( 2016-03-09 07:46:40 -0600 )edit

But of course you can have if statements in rspec-puppet. But what you are saying with the if statement doesn't make sense. The logic is invalid.

Alex Harvey gravatar imageAlex Harvey ( 2016-03-09 07:49:29 -0600 )edit

I am bit confused with your comments, how rspec-puppet compile a catalog. My understanding is, rspec just check the already compiled catalog.

Chutki gravatar imageChutki ( 2016-03-10 05:29:42 -0600 )edit

TBH, it's not well documented. I'd have to read the code to fully understand what it does. Whether it compiles one catalog in memory and modifies that, or if it compiles a new one for a new describe/params block, I'm not certain. Your logic, though, can imagine a new catalog for each describe+input.

Alex Harvey gravatar imageAlex Harvey ( 2016-03-10 06:30:00 -0600 )edit

answered 2016-03-03 22:44:30 -0600

DarylW gravatar image

I agree with Alex above that you should refactor to pass your hiera data as a parameter, but it is possible to test for different values of hiera data, but it won't be as direct as above..

You can add a hiera.yaml file to be read in for testing, but the way to test multiple values in the hiera data would be to make several %{::clientcert} specific .yaml files, and test each condition with a different 'host' in your test fixture.

edit flag offensive delete link more


Chukti's issue isn't about accessing the Hiera data; his issue is that the Hiera function call is always going to return true or false depending on the test Hiera data, but is not a function of the class's input parameters. Actually, to test this, you would need to mock the Hiera function.

Alex Harvey gravatar imageAlex Harvey ( 2016-03-03 22:58:03 -0600 )edit
Alex Harvey gravatar imageAlex Harvey ( 2016-03-03 23:04:27 -0600 )edit

Thanks Alex for your response, but I couldn't access the link

Chutki gravatar imageChutki ( 2016-03-04 01:13:11 -0600 )edit

I don't know what's wrong, if you type that link into your browser directly it works. Strange, if I click on the link some additional characters get added.

Alex Harvey gravatar imageAlex Harvey ( 2016-03-04 01:37:16 -0600 )edit

I was trying to imply that if you had two test different sets of hiera data, you COULD contrive a test with different 'nodes' set up in your test, node 'TRUEbolvalue' { } node 'FALSEbolvalue' { } You could use %{::clientcert} to 'pick' the correct hiera config for that test. ugly, but possible.

DarylW gravatar imageDarylW ( 2016-03-04 09:15:48 -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


Asked: 2016-03-03 05:13:09 -0600

Seen: 458 times

Last updated: Mar 03 '16