ChefSpec and for loops with dynamic attributes

问题内容:

I have some very unorthodox Chef code for which I need to write a unit test, and I cannot figure out a way to correctly do it.

Unfortunately because of the structure of the environment file all these for loops you see below are needed. The environment file cannot be addressed as there are already dozens of cookbooks written on it

The code basically looks like this:

# 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

The findIPfrom_host() function is found in a library from a different cookbook, and it looks like this:

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

I have to admit I am completely stuck on this. Nothing I could find in the ChefSpec documentation or examples helps.

Is anyone here more knowledgeable in this area?

Thanks!

问题评论:

原文地址:

https://stackoverflow.com/questions/47755474/chefspec-and-for-loops-with-dynamic-attributes

添加评论

友情链接:蝴蝶教程