rspec-使用资源定义类型的人偶单元测试
rspec-puppet unit test for define type using resource
我编写了一个用户定义类型,它使用 wget 下载文件并将其存储在 /root 中。我已经使用 exec 类型来完成这个。该代码与 puppet apply 配合使用效果很好,现在当我尝试为其编写 rspec 测试时,我遇到了问题并收到失败消息。 Please find the site.pp, download.pp, init_spec.rb file.
我是 rspec 和 puppet 的新手,无法为此找到解决方案,请提供有关如何为此定义类型编写 rspec 测试的建议。
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 测试文件
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]#
看来您的方向是正确的。
这是您在 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) { ... }
正确设置了 defined-type-under-test 的标题,但尚未设置输入参数。我添加了您显然想要的输入参数。
第二点是您希望 Exec 资源具有包含命令的标题,而实际标题将是 execute wget
,根据您在 download.pp
.[=15= 中的内容]
否则,看起来不错
我编写了一个用户定义类型,它使用 wget 下载文件并将其存储在 /root 中。我已经使用 exec 类型来完成这个。该代码与 puppet apply 配合使用效果很好,现在当我尝试为其编写 rspec 测试时,我遇到了问题并收到失败消息。 Please find the site.pp, download.pp, init_spec.rb file.
我是 rspec 和 puppet 的新手,无法为此找到解决方案,请提供有关如何为此定义类型编写 rspec 测试的建议。
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 测试文件
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]#
看来您的方向是正确的。
这是您在 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) { ... }
正确设置了 defined-type-under-test 的标题,但尚未设置输入参数。我添加了您显然想要的输入参数。
第二点是您希望 Exec 资源具有包含命令的标题,而实际标题将是 execute wget
,根据您在 download.pp
.[=15= 中的内容]
否则,看起来不错