Search code examples
puppetrspec-puppet

rspec-puppet unit test for define type using resource


I have written a user define type which downloads a file using wget and stores in /root. I have used exec type to accomplish this. the code is working well with puppet apply, now when I am trying to write rspec test for the same, i am facing issues and getting failure messages. Please find the site.pp, download.pp, init_spec.rb file.

I am new to rspec and puppet and unable to find a solution for this, kindly help with suggestion on how to write the rspec test for this define type.

manifest/site.pp

$link='https://rpm.nodesource.com/pub_8.x/el/7/x86_64/'
$rpm = 'nodejs-8.0.0-1nodesource.el7.centos.x86_64.rpm'

node_js::download{'execute wget':
                comm=>'/usr/bin/wget',
                url=>"${link}${rpm}",
                path=>'/root',
            }

modules/node_js/manifests/download.pp

define node_js::download($comm=undef,
                         $url=undef,
                         $path=undef){

$validate= "nodejs-8.0.0-1nodesource.el7.centos.x86_64.rpm"

    exec { 'execute wget':
    command => "${comm} ${url}",
        cwd => "${path}",
     unless => "/usr/bin/ls  ${path} | grep ${validate}",

    }
}

rspec test file

modules/node_js/spec/defines/init_spec.rb

require 'spec_helper'
describe 'node_js::download', :type => 'define' do

  let(:title) { 'node_js::download' }


  it {
    is_expected.to contain_exec('/usr/bin/wget h*tps://rpm.nodesource.com/pub_8.x/el/7/x86_64/nodejs-8.0.0-1nodesource.el7.centos.x86_64.rpm').with({
         'cwd' => '/root/' ,
    })
  }

end

rspec execution error display message

[root@puppet node_js]# rspec spec/defines/init_spec.rb
F

Failures:

  1) node_js::download should contain Exec[/usr/bin/wget https://rpm.nodesource.com/pub_8.x/el/7/x86_64/nodejs-8.0.0-1nodesource.el7.centos.x86_64.rpm] with cwd => "/root/"
should contain Exec[/usr/bin/wget h*tps://rpm.nodesource.com/pub_8.x/el/7/x86_64/nodejs-8.0.0-1nodesource.el7.centos.x86_64.rpm] with cwd => "/root/"
     Failure/Error:
       is_expected.to contain_exec('/usr/bin/wget h*tps://rpm.nodesource.com/pub_8.x/el/7/x86_64/nodejs-8.0.0-1nodesource.el7.centos.x86_64.rpm').with({
         'cwd' => '/root/' ,
       })

       expected that the catalogue would contain Exec[/usr/bin/wget https://rpm.nodesource.com/pub_8.x/el/7/x86_64/nodejs-8.0.0-1nodesource.el7.centos.x86_64.rpm]
     # ./spec/defines/init_spec.rb:8:in `block (2 levels) in <top (required)>'

Finished in 0.69964 seconds (files took 8.81 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/defines/init_spec.rb:7 # node_js::download should contain Exec[/usr/bin/wget ht*ps://rpm.nodesource.com/pub_8.x/el/7/x86_64/nodejs-8.0.0-1nodesource.el7.centos.x86_64.rpm] with cwd => "/root/"

[root@puppet node_js]#

Solution

  • It looks like you are on the right track.

    Here is what you should have in init_spec.rb:

    require 'spec_helper'
    
    describe 'node_js::download', :type => 'define' do
      let(:title) { 'node_js::download' }
      let(:params) do
        {
          :comm => '/usr/bin/wget',
          :path => '/root/',
          :url  => 'https://rpm.nodesource.com/pub_8.x/el/7/x86_64/nodejs-8.0.0-1nodesource.el7.centos.x86_64.rpm',
        }
      end
    
      it {
        is_expected.to contain_exec('execute wget').with({
          'command' => '/usr/bin/wget https://rpm.nodesource.com/pub_8.x/el/7/x86_64/nodejs-8.0.0-1nodesource.el7.centos.x86_64.rpm',
          'cwd'     => '/root/',
        })
      }
    end
    

    You have correctly set the title of the defined-type-under-test using let(:title) { ... } but you have not set the input parameters. I added the input parameters that you apparently intended.

    Second point is you expected the Exec resource to have a title containing the command, whereas the actual title is going to be execute wget, per what you have in download.pp.

    Otherwise, it looks good.