Skip to content

Commit

Permalink
Fixes #37043 - Inheritance overrides host facets
Browse files Browse the repository at this point in the history
  • Loading branch information
ananace committed Feb 14, 2024
1 parent c109691 commit 915be0a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
6 changes: 3 additions & 3 deletions app/models/concerns/facets/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ module ClassMethods
# Change attributes that will be sent to an facet based on inherited values from the hostgroup.
def inherited_attributes(hostgroup, facet_attributes)
_, facet_config = Facets.find_facet_by_class(self, :host)
return facet_attributes unless facet_config.has_hostgroup_configuration? && hostgroup

if facet_config.has_hostgroup_configuration? && hostgroup
facet_attributes = hostgroup.inherited_facet_attributes(facet_config)
hostgroup.inherited_facet_attributes(facet_config).merge(facet_attributes || {}) do |key, left, right|
facet_attributes.include?(key) ? right : left
end
facet_attributes
end

# Use this method to populate host's fields based on fact values exposed by the importer.
Expand Down
36 changes: 36 additions & 0 deletions test/unit/facet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,41 @@ def my_attribute=(val)

assert_equal({'to_inherit' => 'val1'}, actual['hostgroup_facet_attributes'])
end

test 'hostgroup facet does not override attributes from host' do
Facets.register :hostgroup_facet do
configure_host TestFacet
configure_hostgroup TestHostgroupFacet
end

host = Host::Managed.new
hostgroup = FactoryBot.create(:hostgroup, :hostgroup_facet => TestHostgroupFacet.new)

TestHostgroupFacet.stubs(:attributes_to_inherit).returns([])
TestHostgroupFacet.inherit_attributes :to_inherit
TestHostgroupFacet.any_instance.stubs(:attributes).returns({:to_inherit => 'val1'})

actual = host.apply_inherited_attributes('hostgroup' => hostgroup, 'hostgroup_facet_attributes' => { 'to_inherit' => 'val3' })

assert_equal({'to_inherit' => 'val3'}, actual['hostgroup_facet_attributes'])
end

test 'hostgroup facet does not override nil attributes from host' do
Facets.register :hostgroup_facet do
configure_host TestFacet
configure_hostgroup TestHostgroupFacet
end

host = Host::Managed.new
hostgroup = FactoryBot.create(:hostgroup, :hostgroup_facet => TestHostgroupFacet.new)

TestHostgroupFacet.stubs(:attributes_to_inherit).returns([])
TestHostgroupFacet.inherit_attributes :to_inherit
TestHostgroupFacet.any_instance.stubs(:attributes).returns({:to_inherit => 'val1'})

actual = host.apply_inherited_attributes('hostgroup' => hostgroup, 'hostgroup_facet_attributes' => { 'to_inherit' => nil })

assert_equal({'to_inherit' => nil}, actual['hostgroup_facet_attributes'])
end
end
end

0 comments on commit 915be0a

Please sign in to comment.