File tree Expand file tree Collapse file tree 3 files changed +48
-2
lines changed 
lib/octocatalog-diff/facts Expand file tree Collapse file tree 3 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -14,8 +14,19 @@ class JSON
1414      # @return [Hash] Facts 
1515      def  self . fact_retriever ( options  =  { } ,  node  =  '' ) 
1616        facts  =  ::JSON . parse ( options . fetch ( :fact_file_string ) ) 
17-         node  =  facts . fetch ( 'fqdn' ,  'unknown.node' )  if  node . empty? 
18-         {  'name'  =>  node ,  'values'  =>  facts  } 
17+ 
18+         if  facts . keys . include? ( 'name' )  && facts . keys . include? ( 'values' )  && facts [ 'values' ] . is_a? ( Hash ) 
19+           # If you saved the output of something like 
20+           # `puppet facts find $(hostname)` the structure will already be a 
21+           # {'name' => <fqdn>, 'values' => <hash of facts>}. We do nothing 
22+           # here because we don't want to double-encode. 
23+         else 
24+           facts  =  {  'name'  =>  node ,  'values'  =>  facts  } 
25+         end 
26+ 
27+         facts [ 'name' ]  =  node  unless  node . empty? 
28+         facts [ 'name' ]  =  facts [ 'values' ] . fetch ( 'fqdn' ,  'unknown.node' )  if  facts [ 'name' ] . empty? 
29+         facts 
1930      end 
2031    end 
2132  end 
Original file line number Diff line number Diff line change 1+ {
2+   "name" :" rspec-node.abc.github.net" 
3+   "values" :{
4+     "apt_update_last_success" :1458162123 ,
5+     "architecture" :" amd64" 
6+     "datacenter" :" xyz" 
7+     "fqdn" :" rspec-node.xyz.github.net" 
8+     "_timestamp" :" 2014-12-02 14:56:20 -0600" 
9+   }
10+ }
Original file line number Diff line number Diff line change 3939      expect ( result [ 'name' ] ) . to  eq ( 'override.node' ) 
4040      expect ( result [ 'values' ] [ 'fqdn' ] ) . to  eq ( 'rspec-node.xyz.github.net' ) 
4141    end 
42+ 
43+     it  'should fill in a missing name from hash with name/values'  do 
44+       fact_file  =  OctocatalogDiff ::Spec . fixture_path ( 'facts/encoded-facts.json' ) 
45+       data  =  JSON . parse ( File . read ( fact_file ) ) 
46+       data [ 'name' ]  =  '' 
47+       options  =  { 
48+         fact_file_string : JSON . generate ( data ) 
49+       } 
50+       result  =  OctocatalogDiff ::Facts ::JSON . fact_retriever ( options ) 
51+       expect ( result ) . to  be_a_kind_of ( Hash ) 
52+       expect ( result [ 'name' ] ) . to  eq ( 'rspec-node.xyz.github.net' ) 
53+       expect ( result [ 'values' ] [ 'fqdn' ] ) . to  eq ( 'rspec-node.xyz.github.net' ) 
54+     end 
55+ 
56+     it  'should not double-encode hash already containing name and values'  do 
57+       fact_file  =  OctocatalogDiff ::Spec . fixture_path ( 'facts/encoded-facts.json' ) 
58+       options  =  { 
59+         fact_file_string : File . read ( fact_file ) 
60+       } 
61+       result  =  OctocatalogDiff ::Facts ::JSON . fact_retriever ( options ) 
62+       expect ( result ) . to  be_a_kind_of ( Hash ) 
63+       expect ( result . keys ) . to  match_array ( %w( name  values ) ) 
64+       expect ( result [ 'name' ] ) . to  eq ( 'rspec-node.abc.github.net' ) 
65+       expect ( result [ 'values' ] [ 'fqdn' ] ) . to  eq ( 'rspec-node.xyz.github.net' ) 
66+     end 
4267  end 
4368end 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments