【问题标题】:ChefSpec and for loops with dynamic attributesChefSpec 和具有动态属性的 for 循环
【发布时间】:2017-12-11 14:50:48
【问题描述】:

我有一些非常非正统的 Chef 代码,我需要为其编写单元测试,但我无法找到正确的方法。

不幸的是,由于环境文件的结构,您在下面看到的所有这些 for 循环都是必需的。环境文件无法解决,因为上面已经写了几十本说明书

代码基本上是这样的:

# Redis Sentinel information
node['os_setup']['zones'].each do |_zone|
  _zone['nodes'].each do |_node|
    if _node['nodename'] =~ /mpredis/i #is it a redis node?
      node['db']['redis_sentinels'] << _node['hostname']
    end
  end
end

# generate /etc/hosts entries for Redis Sentinels
node['db']['redis_sentinels'].each do |_hostname|
  # add entry for INTERSITE network
  hostsfile_entry findIPfrom_host(_hostname, 'REDIS') do
    hostname "#{_hostname}.#{node['domain']}"
    aliases [_hostname]
    unique true
    comment "appended by recipe[#{cookbook_name}::#{recipe_name}]"
    action :create
  end

  # add entry for TRAFFIC network
  hostsfile_entry findIPfrom_host(_hostname, 'TRAFFIC') do
    hostname "#{_hostname}db.#{node['domain']}"
    aliases ["#{_hostname}db"]
    unique true
    comment "appended by recipe[#{cookbook_name}::#{recipe_name}]"
    action :create
  end
end

findIPfrom_host() 函数位于另一本食谱的库中,如下所示:

def getIPfrom_host(hostname, interface)
  ip_addr = nil
  node['os_setup']['zones'].each do |_zone|
    _zone['nodes'].each do |_node|
      if _node['hostname'] == hostname
        _node['interfaces'].each do |_nic|
          if _nic['device'] == node['interface_definitions'][interface.upcase]
            ip_addr = _nic['ipaddress']
          end
        end
      end
    end
  end
  return ip_addr
end

我不得不承认我完全坚持这一点。我在 ChefSpec 文档或示例中找不到任何帮助。

这里有没有人更了解这方面的知识?

谢谢!

【问题讨论】:

    标签: ruby unit-testing loops chef-infra chefspec


    【解决方案1】:

    没有什么特别的,只需设置一些节点属性的runner,然后检查正确的hostsfile_entry资源(it { expect(chef_run).to create_hostsfile_entry('asdf') }等)。

    【讨论】:

      猜你喜欢
      • 2014-02-10
      • 2013-04-23
      • 2023-04-10
      • 2018-01-03
      • 2012-05-16
      • 2017-09-14
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      相关资源
      最近更新 更多