我正在尝试测试一种类型,我有一个问题:表驱动的测试是否可能?我有一个可以传递哈希的课程,我想用不同的哈希内容进行检查,所以我希望我的测试看起来像
require_relative '../../../spec_helper'
require_relative '../../isamjunction_mocks'
describe 'addon::isam::junctionmember::config', :type => :class do
let(:pre_condition) {[$mock_isam_junction_member.render]}
$junctionmember_hashes.each do |member|
let(:params) {{:data_isamjunctionmember => member}}
describe member[:name] do
it {
compare = member.dup
compare.delete(:isam_servername)
is_expected.to contain_class('isam_junction_member').with(
compare
)
}
end
end
end
当我运行它时,我看起来像是木偶目录是用我的最后一系列测试哈希汇编而成的,因为我看到,当我的第一个测试哈希与目录中的第一个测试哈希相提并论时,我会感到失败(看起来像目录与我的第二个也是最后一次哈希(他们的测试成功(一起编译。
在rspec-puppet中,目录何时会编译?我可以强制重新编译吗?是否有更好,更惯用的方式?
$mock_isam_junction_member = MockResource.new 'isam_junction_member', {
:params => {
:reverseproxy_id => nil,
:junction_point => nil,
:junction_type => nil,
:server_port => nil,
:virtual_hostname => nil,
:virtual_https_hostname => nil,
:server_dn => nil,
:query_contents => nil,
:stateful_junction => nil,
:case_sensitive_url => nil,
:windows_style_url => nil,
:https_port => nil,
:http_port => nil,
:proxy_hostname => nil,
:proxy_port => nil,
:sms_environment => nil,
:vhost_label => nil,
:admin_rest_url => nil,
:admin_ssl => nil,
:admin_verify_ssl => nil,
:admin_rest_username => nil,
:admin_rest_password => nil
}
}
$junctionmember_hashes = [
{
:isam_servername => '145.70.150.207',
:reverseproxy_id => 'reverseproxy_id',
:name => 'http.backendserver.internalcorp.net',
:junction_point => 'a-virtual-junction-point',
:junction_type => 'tcp',
:server_port => 1234,
:virtual_hostname => 'afund.pensions.nl',
:virtual_https_hostname => nil,
:server_dn => 'distinguished.afund.pensions.nl',
:query_contents => 'query_contents.exe',
:stateful_junction => true,
:case_sensitive_url => 'no',
:windows_style_url => 'yes',
:https_port => 443,
:http_port => 80,
:proxy_hostname => 'abc''',
:proxy_port => 5678,
:sms_environment => 'ghi',
:vhost_label => 'xyz',
}, {
:isam_servername => '145.70.150.207',
:reverseproxy_id => 'reverseproxy_id',
:name => 'https.backendserver.internalcorp.net',
:junction_point => 'a-virtual-junction-point',
:junction_type => 'ssl',
:server_port => 5678,
:virtual_hostname => 'afund.pensions.nl',
:server_dn => 'distinguished.afund.pensions.nl',
:query_contents => 'query_contents.exe',
:stateful_junction => true,
:case_sensitive_url => 'no',
:windows_style_url => 'yes',
:https_port => 443,
:sms_environment => 'abc',
:vhost_label => 'def',
}
]
这是代码
class addon::isam::junctionmember::config (
$data_isamjunctionmember = {}
) {
$appliance_servername = $data_isamjunctionmember['isam_servername']
$data_isamappliance = hiera('drdc_base', undef)['isamappliances'][$appliance_servername]
$port = $data_isamjunctionmember['server_port']
$protocol = $data_isamjunctionmember['server_protocol']
$server_hostname = $data_isamjunctionmember['server_hostname']
dsc_xfirewall {
downcase("${appliance_servername}_IsamJunctionMember"):
dsc_name => 'DRDC-IsamJunctionMember',
dsc_displayname => 'DRDC ISAM Junction Member',
dsc_localport => sprintf('%d', $port),
dsc_protocol => $protocol,
dsc_profile => ['Domain', 'Private', 'Public'],
dsc_description => "Firewall rule to allow the ISAM server to forward traffic to a backend server. [${protocol}://${server_hostname}:${port}]"
}
class { 'isam_junction_member':
reverseproxy_id => $data_isamjunctionmember['reverseproxy_id'],
name => $server_hostname,
junction_point => $data_isamjunctionmember['junction_point'],
junction_type => $data_isamjunctionmember['junction_type'],
server_port => $port,
virtual_hostname => $data_isamjunctionmember['virtual_hostname'],
virtual_https_hostname => $data_isamjunctionmember['virtual_https_hostname'],
server_dn => $data_isamjunctionmember['server_dn'],
query_contents => $data_isamjunctionmember['query_contents'],
stateful_junction => $data_isamjunctionmember['stateful_junction'],
case_sensitive_url => $data_isamjunctionmember['case_sensitive_url'],
windows_style_url => $data_isamjunctionmember['windows_style_url'],
https_port => $data_isamjunctionmember['https_port'],
http_port => $data_isamjunctionmember['http_port'],
proxy_hostname => $data_isamjunctionmember['proxy_hostname'],
proxy_port => $data_isamjunctionmember['proxy_port'],
sms_environment => $data_isamjunctionmember['sms_environment'],
vhost_label => $data_isamjunctionmember['vhost_label'],
admin_rest_url => "https://${appliance_servername}",
admin_ssl => $data_isamappliance['admin_ssl'],
admin_verify_ssl => $data_isamappliance['admin_ssl_verify'],
admin_rest_username => $data_isamappliance['admin_rest_username'],
admin_rest_password => $data_isamappliance['admin_rest_password']
}
}
感谢@Alexharvey的建议,我找到了答案。很高兴与我的少年英雄之一一起被救出。
我稍微重新组织了代码,现在它可以按预期工作。诀窍似乎是每次迭代都需要在描述中完成完整的初始化。测试的所有内容都在同一范围内是有道理的,但是ATM我不完全理解其工作原理的细节(即进行目录汇编的方式以及何时进行(。
require_relative '../../../spec_helper'
require_relative '../../isamjunction_mocks'
describe 'addon::isam::junctionmember::config', :type => :class do
$junctionmember_hashes.each do |member|
describe member[:name] do
let(:pre_condition) {
[
$mock_isam_junction_member.render
]
}
let(:facts) {{:operatingsystem => 'RedHat'}}
let(:params) {{:data_isamjunctionmember => keys_to_s(member)}}
it {
compare = member.dup
compare.delete(:isam_servername)
compare.delete(:name)
puts compare
is_expected.to contain_class('isam_junction_member').with(
compare
)
}
end
end
end