我有一个类,它看起来像
class addon {
case $::operatingsystem {
'windows': {
Dsc_xfirewall {
dsc_ensure => 'Present',
}
}
'RedHat': {
}
default: { warning "OS : ${::operatingsystem} is not (yet) supported" }
}
}我希望我的测试看起来像
describe 'addon', :type => :class do
os = 'windows'
let(:facts) {{:operatingsystem => os}}
describe os do
it {
is_expected.to contain_class('addon').with(
{
:Dsc_xfirewall => {
:dsc_ensure => 'Present',
}
}
)
}
end
end目录编译正确,为清楚起见,删除了is_expected.to编译。然而,我似乎不能让它工作:如果我尝试dsc_xfirewall相同的故事,如果尝试,Dsc_xfirewall是空的
contains_dsc_xfirewall我得到一个错误,dsc_firewall不是一个有效的定义。有没有人知道如何更好地组织我的测试?在有人指出如果目录编译正确,我就不需要这个测试之前,我知道;这只是一个更复杂的东西的简化版本。
因此,我的问题是:要检查类是否包含dsc_xfirewall以及是否正确设置了所有参数,测试应该是什么样的?
发布于 2017-06-24 21:36:56
明显的问题是,您的清单没有声明任何实际的资源,而您的Rspec似乎期望清单实际上声明了任何资源。
下面的代码如下:
Dsc_xfirewall {
dsc_ensure => 'Present',
}为自定义类型dsc_xfirewall声明资源默认值(ref)。我的猜测是Present中的大写P也是一个拼写错误。
我还注意到,您将let(:facts)语句放错了位置,并且您打开了另一个describe块,我认为您应该在其中使用context。
我写了一些代码来说明清单和Rspec代码应该是什么样子:
(我做了一些修改,这样我就可以很容易地在我的Mac上编译它。)
class foo {
case $::operatingsystem {
'Darwin': {
file { '/tmp/foo':
ensure => file,
}
}
'RedHat': {
}
default: { fail("OS : ${::operatingsystem} is not (yet) supported") }
}
}Rspec:
describe 'foo', :type => :class do
context 'Darwin' do
let(:facts) {{:operatingsystem => 'Darwin'}}
it {
is_expected.to contain_class('foo')
}
it {
is_expected.to contain_file('/tmp/foo').with(
{
:ensure => 'file',
}
)
}
# Write out the catalog for debugging purposes.
it { File.write('myclass.json', PSON.pretty_generate(catalogue)) }
end
end显然,您应该使用contain_dsc_xfirewall而不是contain_file。
https://stackoverflow.com/questions/44735263
复制相似问题