Last active
September 7, 2016 14:34
-
-
Save armiller/63a2f0d042d607ef585b95a27703e2c3 to your computer and use it in GitHub Desktop.
Recursive test for node attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'chef/sugar' | |
def loop_hash(hash, keys = [], &block) | |
hash.each do |k, v| | |
if v.is_a?(Hash) | |
loop_hash(v, keys.push(k), &block) | |
keys.pop | |
elsif block | |
p = keys << k | |
yield(p.clone, v) | |
keys.pop | |
end | |
end | |
end | |
# In the tests | |
test = { | |
'nagios' => { | |
'foo' => { | |
'bar' => true | |
} | |
} | |
} | |
# k: array of parent keys | |
# v: value of attribute | |
loop_hash(test) do |k, v| | |
it "Sets attribute #{k.join('.')} to #{v}" do | |
expect(chef_run.node.deep_fetch(*k)).to eq(v) | |
end | |
end | |
# You don't have to use top level attributes | |
test = { | |
'foo' => { | |
'bar' => 'test' | |
} | |
} | |
loop_hash(test) do |k, v| | |
it "sets attributes #{k.join('.')} to #{v}" do | |
expect(chef_run.node.deep_fetch('nagios', *k)).to eq(v) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment