Ask Your Question
0

how to use inherit vaiable from other class in rspec

asked 2016-03-10 06:53:30 -0500

Chutki gravatar image

updated 2016-03-10 06:55:23 -0500

Hi

I have below manifests

class module::script1 inherits module::script2 {

service { 'foo':
  ensure    => $::ensure,
  enable    => $::enable,
}}

class module::script2 {
$ensure = hiera("ensure::$::operatingsystemmajrelease")
 $enable = hiera("enable::$::operatingsystemmajrelease")
}

I wrote below rspec-puppet test case

describe 'module::script1' do
   let(:facts) {{ :operatingsystemmajrelease => '7' }}

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

     enb = hiera.lookup('ensure::7',nil,nil)
    val = hiera.lookup('enable::7',nil,nil)

    it { should contain_service('foo').with(
      'ensure'    => enb,
      'enable'    => val,
 )}
end
end

I get below error when I test

1) puppet::client puppet client test case should contain Service[foo] with ensure => "enabled", enable => "true"
     Failure/Error: )}
       expected that the catalogue would contain Service[foo] with ensure set to "enabled" but it is set to nil, and parameter enable set to "true"
 but it is set to nil

 Diff:
       @@ -1,4 +1 @@
       -enabled
       -
       -true

My doubt is, how we can pass "$::ensure" and "$::enable" in rspec-puppet.

edit retag flag offensive close merge delete

Comments

I think considering that you're asking rspec-puppet questions continually here, the least you can do is take the time to mark your questions as resolved if they are resolved.

Alex Harvey gravatar imageAlex Harvey ( 2016-03-10 08:38:36 -0500 )edit

I am already doing it. If I don't mark any questions as resolved then it means still I am working on it

Chutki gravatar imageChutki ( 2016-03-10 11:19:28 -0500 )edit

any update on this? I referred the url http://terrarum.net/blog/puppet-testing-part-1.html (refer $basedir from blog ) for the problem what issue I posted here. The blog updated, we need to hard code the value, if we inherits variable from other class. I also used the same but why I got nil error.

Chutki gravatar imageChutki ( 2016-03-15 05:57:12 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-03-15 19:52:35 -0500

updated 2016-03-16 04:28:33 -0500

The manifests don't make any sense and won't do anything unless there are custom facts $::ensure and $::enable.

I would ask the person who wrote the code if this was their intention:

class module::script1 inherits module::script2 {
  service { 'foo':
    ensure => $::module::script2::ensure,
    enable => $::module::script2::enable,
  }
}

class module::script2 {
  $ensure = hiera("ensure::$::operatingsystemmajrelease")
  $enable = hiera("enable::$::operatingsystemmajrelease")
}

Until the author of this code confirms their intention, it's impossible to write tests for it.

I tested using:

class module::script1 inherits module::script2 {
  if $::ensure == undef {
    notify { "ensure is set to undef": }
  }
  if $::enable == undef {
    notify { "enable is set to undef": }
  }
  service { 'foo':
    ensure => $::ensure,
    enable => $::enable,
  }
}

class module::script2 {
  $ensure = absent
  $enable = false
}

include module::script1

and

[root@centos-72-x64 ~]# puppet apply /tmp/foo.pp                                                                                                                                                          
Notice: Compiled catalog for centos-72-x64.wg.dir.telstra.com in environment production in 0.05 seconds
Notice: ensure is set to undef
Notice: /Stage[main]/Module::Script1/Notify[ensure is set to undef]/message: defined 'message' as 'ensure is set to undef'
Notice: enable is set to undef
Notice: /Stage[main]/Module::Script1/Notify[enable is set to undef]/message: defined 'message' as 'enable is set to undef'
Notice: Applied catalog in 0.03 seconds

That's showing that unless there are custom facts defined, the code simply declares a Service['foo'] with no parameters.

edit flag offensive delete link more

Comments

these two classes are separate one, I didn't write the two classes under the same manifests.

Chutki gravatar imageChutki ( 2016-03-15 23:17:43 -0500 )edit

So are you saying that your team has to write rspec-puppet tests but another team writes the manifests? I'm not sure what you're meaning is.

Alex Harvey gravatar imageAlex Harvey ( 2016-03-15 23:59:08 -0500 )edit

yes, manifests was written by someone> I just write rspec-puppet test case. Also, the "class module::script2" and "class module::script1 inherits module::script2" are separate manifest not in same manifests.

Chutki gravatar imageChutki ( 2016-03-16 02:45:27 -0500 )edit

Yes, I would expect they will be in separate files, but "manifests" in Puppet just means all the manifest files that are compiled into a single catalog. Can you confirm that you copied the files in accurately? This code makes no sense at all.

Alex Harvey gravatar imageAlex Harvey ( 2016-03-16 04:17:12 -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-03-10 06:53:30 -0500

Seen: 275 times

Last updated: Mar 16 '16