问题描述
我有以下(简化的)设置:
mymod/manifests/as/myressource.pp:
define mymod::as::myressource (
Integer $abc,) {
notice('mymod::as::myressource start ...')
$maxheap3 = '3G'
notice("maxheap3 = ${maxheap3}")
}
mymod/manifests/init.pp:
class mymod(
Optional[String] $maxheap,) {
notice("${title} wird installiert...")
mymod::as::myressource {'no.1':
abc => 35
}
mymod::as::myressource {'no.2':
abc => 70
}
}
mymod/spec/classes/sometest.rb:
describe 'mymod' do
on_supported_os.each do |os,os_facts|
context "on #{os}" do
let(:facts) { os_facts }
let(:params) { {
} }
it { is_expected.to compile }
it { is_expected.not_to contain_mymod__as__myressource('no.3') }
it { is_expected.to contain_mymod__as__myressource('no.1').with({
:abc => 35,# :maxheap3 => '3G'
}) }
end
end
end
测试没有错误,但如果我用 maxheap3 取消注释该行,它会失败并告诉我: “预计目录将包含 Mymod::As::Myressource[no.1],maxheap3 设置为“3G”,但设置为 nil”
为什么我可以以这种方式检查参数的值,但不能检查局部变量?我该怎么做才能在测试中检查 $maxheap3 的值?
解决方法
为什么我可以通过这种方式检查参数的值,而不是局部变量?
使用 Rspec puppet 进行单元测试测试目录的内容。该目录包含类和资源参数,但不包含有关局部变量的信息。
如何检查测试中 $maxheap3 的值?
你不能测试局部变量,至少不能直接测试,你不应该想要这样做。它们是一个实现细节。但是,您可以测试它们对目录中显示的类和资源的影响。这可能采取以下形式:声明了多少或哪些给定类型的资源、声明了哪些类、类和资源参数采用什么值等。
在您的示例中,$maxheap3
实例中的 mymod::as::myressource
值对目录根本没有影响,因此您无法通过 Rspec 测试对其进行测试。但那又怎样?对catalog没有影响就是不影响Puppet配置目标节点的方式,所以在Rspec_puppet的层面上没有关系。