我开发了我的木偶模块,在以下 gem 文件上1.8.7
运行了 ruby 版本bundle install
,并且 rspec 测试成功运行(木偶运行也是如此):
source 'https://rubygems.org'
if puppetversion = ENV['PUPPET_GEM_VERSION']
gem 'puppet', puppetversion, :require => false
else
gem 'puppet', '3.7.5'
end
gem 'metadata-json-lint'
gem 'puppetlabs_spec_helper', '>= 0.1.0'
gem 'puppet-lint', '>= 1.0.0'
gem 'facter', '>= 1.7.0'
gem 'rspec-puppet-facts'
# rspec must be v2 for ruby 1.8.7
if RUBY_VERSION >= '1.8.7' and RUBY_VERSION < '1.9'
gem 'rspec', '~> 2.0'
end
但是,当我在具有 ruby 的 CI 服务器bundle install
上执行相同操作时,我遇到了以下 rspec 失败:Gemfile
1.9.3
常见的错误是:
Puppet::Error:
Failed to parse template silex/var/config/settings.json.erb:
Filepath: /var/tmp/silex/spec/fixtures/modules/silex/templates/var/config/settings.json.erb
Line: 4
Detail: undefined method `each_with_index' for "default":String
使用该each_with_index
方法的模板:
{
"elasticsearch.settings": {
"connections": [
<% @elastic_hosts.each_with_index do |host,i| -%>
{"host":"<%= host['ip'] %>", "port": <%= host['port'] %> }<%= ',' if i < (@elastic_hosts.size - 1) -%>
<% end -%>
],
"index": "<%= @elastic_index %>"
}
},
"cache.lifetime": <%= @elastic_cache_lifetime %>,
"environment": "dev",
"trusted": [
<% @trusted_hosts.each_with_index do |host,i| -%>
{"host":"<%= host['host'] %>"}<%= ',' if i < (@trusted_hosts.size - 1) -%>
<% end -%>
],
"homepage.settings": {
"hero.count": 20,
"list.count": 20
}
}
我不明白为什么使用不同版本的红宝石会失败?我也尝试过 ruby 2.1.1
,结果相同。结果仅在使用 ruby 的 CI 服务器上成功1.8.7
(与我用于开发模块的版本相同)。
更新 1
似乎因为elastic_hosts
是一个哈希映射,而我有一个字符串它失败了。我确实与trusted_hosts
(另一个哈希图)一起解决了这个问题,这导致大多数测试通过但错误仍然存在。
注意:该错误与上面突出显示的常见错误相同。
我还用完整的内容更新了上面的模板文件。
如果您查看我的配置规范文件,我没有default
在任何地方指定为字符串,但它告诉我这是错误..
使固定
怀疑我的 init 规范类设置了默认值,并添加了 hiera 查找elastic_hosts
并trusted_hosts
修复了该问题。
更新 2
此行为在 version1.8.7
和 version >之间是可重现的1.9.3
。它似乎与如何处理哈希有关。