1

Have some stuck with using variable in rspec. Here is my params.pp

case $::osfamily{
  Debian: {
    $ssh_daemon = "ssh"
  }
  Redhat: {
    $ssh_daemon = "sshd"
  }

In an rspec test I need to use the variable $ssh_daemon like this:

it { should contain_service("${ssh_daemon}").with(
  :ensure => 'running',
  :enable => 'true',
)}

Here is my ssh.pp file

service { "${ssh_daemon}":
  ensure => running,
  enable => true,
}

How can I write this variable ($ssh_daemon) to get this test to work?

Peter Souter
  • 5,110
  • 1
  • 33
  • 62
Andrey
  • 13
  • 3

1 Answers1

1

You can do this by mocking Facter output.

Something like:

context 'service on debian' do
  let(:facts) { { :osfamily => 'Debian' } }
  it { should contain_service("ssh") }
end

context 'service on redhat' do
  let(:facts) { { :osfamily => 'RedHat' } }
  it { should contain_service("sshd") }
end
Peter Souter
  • 5,110
  • 1
  • 33
  • 62
  • 1
    IOW, your test needs to anticipate the variable values to prod for sensible results. Or to take another angle - your test makes sure that the variable actually *will* take the correct value. – Felix Frank Jun 22 '15 at 08:43
  • Thanks for help, using mocking facter is the right answer and it work properly. – Andrey Jun 22 '15 at 21:13