Skip to content

Commit

Permalink
Merge branch 'master' into kpaulisse-ignore-comparison-tag
Browse files Browse the repository at this point in the history
  • Loading branch information
ahayworth authored Feb 16, 2021
2 parents e96ebd5 + 6701b4f commit 62da538
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/octocatalog-diff/facts/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ class JSON
# @return [Hash] Facts
def self.fact_retriever(options = {}, node = '')
facts = ::JSON.parse(options.fetch(:fact_file_string))
node = facts.fetch('fqdn', 'unknown.node') if node.empty?
{ 'name' => node, 'values' => facts }

if facts.keys.include?('name') && facts.keys.include?('values') && facts['values'].is_a?(Hash)
# If you saved the output of something like
# `puppet facts find $(hostname)` the structure will already be a
# {'name' => <fqdn>, 'values' => <hash of facts>}. We do nothing
# here because we don't want to double-encode.
else
facts = { 'name' => node, 'values' => facts }
end

facts['name'] = node unless node.empty?
facts['name'] = facts['values'].fetch('fqdn', 'unknown.node') if facts['name'].empty?
facts
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/octocatalog-diff/fixtures/facts/encoded-facts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name":"rspec-node.abc.github.net",
"values":{
"apt_update_last_success":1458162123,
"architecture":"amd64",
"datacenter":"xyz",
"fqdn":"rspec-node.xyz.github.net",
"_timestamp":"2014-12-02 14:56:20 -0600"
}
}
25 changes: 25 additions & 0 deletions spec/octocatalog-diff/tests/facts/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,30 @@
expect(result['name']).to eq('override.node')
expect(result['values']['fqdn']).to eq('rspec-node.xyz.github.net')
end

it 'should fill in a missing name from hash with name/values' do
fact_file = OctocatalogDiff::Spec.fixture_path('facts/encoded-facts.json')
data = JSON.parse(File.read(fact_file))
data['name'] = ''
options = {
fact_file_string: JSON.generate(data)
}
result = OctocatalogDiff::Facts::JSON.fact_retriever(options)
expect(result).to be_a_kind_of(Hash)
expect(result['name']).to eq('rspec-node.xyz.github.net')
expect(result['values']['fqdn']).to eq('rspec-node.xyz.github.net')
end

it 'should not double-encode hash already containing name and values' do
fact_file = OctocatalogDiff::Spec.fixture_path('facts/encoded-facts.json')
options = {
fact_file_string: File.read(fact_file)
}
result = OctocatalogDiff::Facts::JSON.fact_retriever(options)
expect(result).to be_a_kind_of(Hash)
expect(result.keys).to match_array(%w(name values))
expect(result['name']).to eq('rspec-node.abc.github.net')
expect(result['values']['fqdn']).to eq('rspec-node.xyz.github.net')
end
end
end

0 comments on commit 62da538

Please sign in to comment.