2

我编写了一个用户定义类型,它使用 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/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 测试文件

模块/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]#
4

1 回答 1

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.

否则,它看起来不错。

于 2017-08-15T15:07:16.337 回答