Project

General

Profile

« Previous | Next » 

Revision 570fb4cb

Added by Thomas McKay over 8 years ago

fixes #11766 - differentiate between puppet facts and those from plugins

+ add test

View differences:

app/services/fact_importer.rb
attr_reader :host, :facts
def delete_removed_facts
to_delete = host.fact_values.joins(:fact_name).where('fact_names.name NOT IN (?)', facts.keys)
to_delete = host.fact_values.joins(:fact_name).where("fact_names.type = '#{fact_name_class}' AND fact_names.name NOT IN (?)", facts.keys)
# N+1 DELETE SQL, but this would allow us to use callbacks (e.g. auditing) when deleting.
deleted = to_delete.destroy_all
@counters[:deleted] = deleted.size
......
end
def db_facts
@db_facts ||= host.fact_values.includes(:fact_name).index_by(&:name)
@db_facts ||= host.fact_values.includes(:fact_name).where("fact_names.type = '#{fact_name_class}'").index_by(&:name)
end
end
test/factories/facts_related.rb
sequence(:value) {|n| "value#{n}" }
host
end
factory :fact_name_other, :class => FactName do
type 'FactNameOther'
sequence(:name) {|n| "fact#{n}" }
end
end
test/unit/puppet_fact_importer_test.rb
assert_equal 1, importer.counters[:added]
end
test "importer retains 'other' facts" do
assert_equal '2.6.9', value('kernelversion')
FactoryGirl.create(:fact_value, :value => 'othervalue',:host => @host,
:fact_name => FactoryGirl.create(:fact_name_other, :name => 'otherfact'))
import('ipaddress' => '10.0.19.5', 'uptime' => '1 picosecond')
assert_equal 'othervalue', value('otherfact')
assert_nil value('kernelversion')
assert_equal '10.0.19.5', value('ipaddress')
assert_equal '1 picosecond', value('uptime')
assert_equal 1, importer.counters[:deleted]
assert_equal 1, importer.counters[:updated]
assert_equal 1, importer.counters[:added]
end
def import(facts)
@importer = PuppetFactImporter.new(@host, facts)
importer.import!

Also available in: Unified diff