Search code examples
rubyrspecpuppetrspec-puppet

Using external variable in rspec-puppet test


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?


Solution

  • 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