如何测试将木偶模板应用于给定测试参数的结果 [英] how to test the result of applying a puppet template to given test parameters

查看:67
本文介绍了如何测试将木偶模板应用于给定测试参数的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下木偶模板文件solr.json.erb:

I have the following puppet template file solr.json.erb:

{
  "servers" : [ {
    "port" : "<%= jmx_port %>",
    "host" : "localhost",

    "queries" : [
      <% @markets.each do |market| -%>
    {
      "outputWriters" : [ {
        "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter",
      } ],
      "obj" : "solr/market_<%= market %>:type=queryResultCache,id=org.apache.solr.search.LRUCache",
      "attr" : [ "hits","hitratio"]
    },
    <% end -%>
    ],
    "numQueryThreads" : 2
  } ]
}

,并且我想在将其应用于人偶之前测试将模板应用于某些测试参数的结果。

and I want to test the result of applying the template to some test parameters before executing this in puppet.

我该怎么做?

之前,我尝试过使用这样的脚本my_script.ruby

before, I tried with a script like this, my_script.ruby

require 'erb'
require 'ostruct'
namespace = OpenStruct.new(:jmx_port => 9200, :markets=> ['CH', 'FR'])
template = File.open("solr.json.erb", "rb").read;
puts ERB.new(template).result(namespace.instance_eval { binding })

但这没有解决,因为OpenStruct没有实例变量,所以我不能使用 @markets

but it didn't work out, because OpenStruct does not have instance variables, so I cannot use @markets.

文档中提到您可以使用以下命令检查语法:
http://docs.puppetlabs.com/guides/templating.html

the documentation mentions that you can check the syntax with this command: http://docs.puppetlabs.com/guides/templating.html

erb -P -x -T '-' mytemplate.erb | ruby -c

但这不是我要的。
i要求获取对模板应用一些测试参数(jmx_port = 9200,markets = ['CH','FR'])的结果。

but that's not what i am asking. i am asking to get the result of applying some test parameters (jmx_port=9200, markets=['CH', 'FR']) to the template.

我该怎么办?

推荐答案

我认为您不需要openstruct东西。这对我有用:

I dont think you need the openstruct stuff. This works for me:

require 'erb'
#Test Variables
jmx_port = 9200
@markets = ['CH', 'FR']

temp = File.open("testerb.erb", "rb").read;
renderer = ERB.new(temp)
puts output = renderer.result()

尽管我确实不得不稍微修改一下模板:

Though I did have to alter your template a fraction:

我已将-从您模板中的-%> 。这些阻止了它的编译,因为它们应该与<%=

I've removed the - from the -%> you had in your template. These prevented it from compiling, as they're supposed to be paired with <%=

{
  "servers" : [ {
    "port" : "<%= jmx_port %>",
    "host" : "localhost",

    "queries" : [
      <% @markets.each do |market| %>
    {
      "outputWriters" : [ {
        "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter",
      } ],
      "obj" : "solr/market_<%= market %>:type=queryResultCache,id=org.apache.solr.search.LRUCache",
      "attr" : [ "hits","hitratio"]
    },
    <% end %>
    ],
    "numQueryThreads" : 2
  } ]
}

这篇关于如何测试将木偶模板应用于给定测试参数的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆