【问题标题】:rspec-puppet unit test for define type using resourcerspec-puppet 单元测试,用于使用资源定义类型
【发布时间】:2017-08-15 14:16:52
【问题描述】:

我编写了一个用户定义类型,它使用 wget 下载文件并存储在 /root 中。我已经使用 exec 类型来完成此操作。该代码在 puppet apply 中运行良好,现在当我尝试为其编写 rspec 测试时,我遇到了问题并收到失败消息。 Please find the site.pp, download.pp, init_spec.rb file.

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

ma​​nifest/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 测试文件

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 执行错误显示信息

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

【问题讨论】:

    标签: puppet rspec-puppet


    【解决方案1】:

    看来你走对了。

    这是您在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 中的内容。

    否则,它看起来不错。

    【讨论】:

    • 代码按照建议的更改工作。谢谢亚历克斯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多