rspec-puppet单元测试用于使用资源定义类型

时间:2017-08-15 14:16:52

标签: puppet rspec-puppet

我编写了一个用户定义类型,它使用wget下载文件并存储在/ root中。我用exec类型来完成这个。这个代码适用于puppet apply,现在当我尝试编写相同的rspec测试时,我面临问题并获得失败消息。 Please find the site.pp, download.pp, init_spec.rb file.

我是rspec和puppet的新手,无法为此找到解决方案,请提供有关如何为此定义类型编写rspec测试的建议。

清单/ 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',
            }

模块/ node_js /表现/ 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测试文件

模块/ node_js /规格/定义/ 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执行错误显示消息

[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]#

1 个答案:

答案 0 :(得分:2)

看起来你走在正确的轨道上。

以下是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

您已使用let(:title) { ... }正确设置了已定义的测试类型的标题,但您尚未设置输入参数。我添加了您显然想要的输入参数。

第二点是你希望Exec资源有一个包含命令的标题,而实际标题将是execute wget,符合download.pp中的标题。

否则,它看起来不错。