Revision 4547f3b4
Added by Marek Hulán about 5 years ago
app/models/host/base.rb | ||
---|---|---|
has_one :subnet, :through => :primary_interface
|
||
has_one :subnet6, :through => :primary_interface
|
||
has_one :kernel_release, -> { joins(:fact_name).where({ 'fact_names.name' => KERNEL_RELEASE_FACTS }).order('fact_names.type') }, :class_name => '::FactValue', :foreign_key => 'host_id'
|
||
has_one :uptime_fact, -> { joins(:fact_name).where({ 'fact_names.name' => UPTIME_FACTS }).order('fact_names.type') }, :class_name => '::FactValue', :foreign_key => 'host_id'
|
||
has_one :uptime_fact, -> { joins(:fact_name).where({ 'fact_names.name' => UPTIME_FACTS }).order('fact_values.updated_at DESC') }, :class_name => '::FactValue', :foreign_key => 'host_id'
|
||
accepts_nested_attributes_for :interfaces, :allow_destroy => true
|
||
|
||
belongs_to :location
|
test/models/host_test.rb | ||
---|---|---|
chef_uptime_fact = FactoryBot.create(:fact_name, name: 'uptime_seconds', :type => 'FactName::Chef')
|
||
unrelated_fact = FactoryBot.create(:fact_name, name: 'os')
|
||
puppet_fact = FactoryBot.create(:fact_name, name: 'system_uptime::seconds')
|
||
FactoryBot.create(:fact_value, fact_name: ansible_uptime_fact, host: host, :value => 123)
|
||
FactoryBot.create(:fact_value, fact_name: chef_uptime_fact, host: host, :value => 222)
|
||
ansible_fact = FactoryBot.create(:fact_value, fact_name: ansible_uptime_fact, host: host, :value => 123)
|
||
cf = FactoryBot.create(:fact_value, fact_name: chef_uptime_fact, host: host, :value => 222)
|
||
cf.update_attribute :updated_at, cf.updated_at + 1.second
|
||
FactoryBot.create(:fact_value, fact_name: unrelated_fact, host: host, :value => 'Fedora 29')
|
||
assert_equal 123, host.uptime_seconds
|
||
FactoryBot.create(:fact_value, fact_name: puppet_fact, host: host, :value => 456)
|
||
assert_equal 222, host.uptime_seconds
|
||
pf = FactoryBot.create(:fact_value, fact_name: puppet_fact, host: host, :value => 456)
|
||
pf.update_attribute :updated_at, cf.updated_at + 2.seconds
|
||
host.reload
|
||
assert_equal 456, host.uptime_seconds
|
||
ansible_fact.update :value => 789, :updated_at => cf.updated_at + 3.seconds
|
||
host.reload
|
||
assert_equal 789, host.uptime_seconds
|
||
end
|
||
end
|
||
|
Also available in: Unified diff
Refs #25974 - sort uptime facts by freshness