我有一个关于如何为同一资源设置多个测试以及更改不同测试的参数的问题。这个例子被最小化,以反映问题。
TheGood:我运行了第一个单元测试,其中参数是资源的一个直接参数:mymod/舱单/as/myresSource3.pp:
define mymod::as::myressource3 (
Optional[String] $maxheap = undef,
) {
$effective_maxheap = ($maxheap =~ NotUndef) ? {
true => $maxheap,
false => "${::facts['memory']['system']['total_bytes'] * 3 / 4 / 1024 / 1024}M",
}
notice("effective_maxheap = ${effective_maxheap}")
mymod::as::myressource2 {"myres3_myres2-${title}":
maxheap => $effective_maxheap,
}
}mymod/spec/定义/as/myresSource3_spec.rb:
require 'spec_helper'
describe 'mymod::as::myressource3' do
let(:title) { 'as_myressource3_test' }
on_supported_os.each do |os, os_facts|
context "no maxheap on #{os}" do
let(:facts) { os_facts }
let(:params) { {
} }
it { is_expected.to compile }
it { is_expected.to contain_mymod__as__myressource2("myres3_myres2-as_myressource3_test").with({
:maxheap => /^[0-9]+M$/,
})}
context "with maxheap on #{os}" do
let(:params) do
super().merge('maxheap' => '3G')
end
it { is_expected.to compile }
it { is_expected.to contain_mymod__as__myressource2("myres3_myres2-as_myressource3_test").with({
:maxheap => '3G',
})}
end
end
end
end如前所述,这很好,我可以为第二个测试更改参数max堆的值。
TheBad:但是在不同的情况下,另一个资源是使用来自外部类的“全局”变量。使用与“TheGood”中相同的方法,我无法在第二个上下文中更改参数。
mymod/舱单/as/myressource.pp.as:
define mymod::as::myressource (
) {
$effective_maxheap = ($::mymod::maxheap =~ NotUndef) ? {
true => $::mymod::maxheap,
false => "${::facts['memory']['system']['total_bytes'] * 3 / 4 / 1024 / 1024}M",
}
notice("effective_maxheap = ${effective_maxheap}")
mymod::as::myressource2 {"myres_myres2-${title}":
maxheap => $effective_maxheap,
}
}注意$::mymod::maxheap的使用!
mymod/舱单/init.pp:
class mymod(
Optional[String] $maxheap = undef,
) {
notice("${title} start maxheap=${maxheap} ...")
mymod::as::myressource {'whatever': }
notice("${title} end ...")
}mymod/spec/定义/as/myressource_spec.rb:
require 'spec_helper'
describe 'mymod::as::myressource' do
let(:title) { 'as_myressource_test' }
on_supported_os.each do |os, os_facts|
context "no maxheap on #{os}" do
let(:facts) { os_facts }
let(:pre_condition) do
"
class { 'mymod':
}
"
end
it { is_expected.to compile }
it { is_expected.to contain_mymod__as__myressource2("myres_myres2-as_myressource_test").with({
:maxheap => /^[0-9]+M$/,
})}
context "with maxheap on #{os}" do
let(:params) do
super().merge('::mymod::maxheap' => '3G')
end
it { is_expected.to compile }
it { is_expected.to contain_mymod__as__myressource2("myres_myres2-as_myressource_test").with({
:maxheap => '3G'
})}
end
end
end
end这给我们带来了以下的启示:
1) mymod::as::myressource no maxheap on sles-12-x86_64 with maxheap on sles-12-x86_64 is expected to compile into a catalogue without dependency cycles
Failure/Error: super().merge('::mymod::maxheap' => '3G')
NoMethodError:
super: no superclass method `params' for #<RSpec::ExampleGroups::MymodAsMyressource::NoMaxheapOnSles12X8664::WithMaxheapOnSles12X8664:0x000000000781c5c0>
Did you mean? param_str
# /opt/puppetlabs/pdk/share/cache/ruby/2.5.0/gems/rspec-puppet-2.7.8/lib/rspec-puppet/matchers/dynamic_matchers.rb:7:in `method_missing'
# ./spec/defines/as/myressource_spec.rb:23:in `block (5 levels) in <top (required)>'
# /opt/puppetlabs/pdk/share/cache/ruby/2.5.0/gems/rspec-puppet-2.7.8/lib/rspec-puppet/support.rb:149:in `test_manifest'
# /opt/puppetlabs/pdk/share/cache/ruby/2.5.0/gems/rspec-puppet-2.7.8/lib/rspec-puppet/support.rb:21:in `build_code'
# /opt/puppetlabs/pdk/share/cache/ruby/2.5.0/gems/rspec-puppet-2.7.8/lib/rspec-puppet/support.rb:91:in `block in load_catalogue'
# /opt/puppetlabs/pdk/share/cache/ruby/2.5.0/gems/rspec-puppet-2.7.8/lib/rspec-puppet/support.rb:376:in `with_vardir'
# /opt/puppetlabs/pdk/share/cache/ruby/2.5.0/gems/rspec-puppet-2.7.8/lib/rspec-puppet/support.rb:83:in `load_catalogue'
# /opt/puppetlabs/pdk/share/cache/ruby/2.5.0/gems/rspec-puppet-2.7.8/lib/rspec-puppet/example/define_example_group.rb:7:in `catalogue'
# /opt/puppetlabs/pdk/share/cache/ruby/2.5.0/gems/rspec-puppet-2.7.8/lib/rspec-puppet/support.rb:12:in `block in subject'
# /opt/puppetlabs/pdk/share/cache/ruby/2.5.0/gems/rspec-puppet-2.7.8/lib/rspec-puppet/matchers/compile.rb:23:in `matches?'
# ./spec/defines/as/myressource_spec.rb:25:in `block (5 levels) in <top (required)>'如何更改::mymod::max堆进行第二次测试?或者这个例子不是很好的木偶风格?
发布于 2021-02-10 11:06:26
我认为有两种方法可以解决这个问题。首先,您有以下内容:
define mymod::as::myressource (
) {
$effective_maxheap = ($::mymod::maxheap =~ NotUndef) ? {}
}当您使用mymod类时,我将显式地包含它。
define mymod::as::myressource (
) {
include mymod
$effective_maxheap = ($::mymod::maxheap =~ NotUndef) ? {}
}这意味着当您的rspec测试运行mymod时,将自动包含其中的所有代码,并且您不需要在rspec中模拟任何东西。
如果这是不可能的或不理想的,那么您可以使用pre_condition来为您初始化这个类。同样,这将导致mymod中的所有内容在运行规范测试时也会被编译。
describe 'mymod::as::myressource' do
let(:title) { 'as_myressource_test' }
on_supported_os.each do |os, os_facts|
context "no maxheap on #{os}" do
let(:facts) { os_facts }
let(:pre_condition) { "class {'mymod': $maxheap = undef" }
end
end
end如果在mymod中编译代码是不可取的,而且您真的想要模拟max堆变量,则可以执行以下操作
describe 'mymod::as::myressource' do
let(:title) { 'as_myressource_test' }
on_supported_os.each do |os, os_facts|
context "no maxheap on #{os}" do
let(:facts) { os_facts }
let(:pre_condition) { "class mymod { $maxheap = undef }" }
end
end
endhttps://stackoverflow.com/questions/66127715
复制相似问题