Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #37179 - Clone hostgroup facets when cloning hostgroup #10055

Merged
merged 1 commit into from
Mar 10, 2024

Conversation

jeremylenz
Copy link
Contributor

When cloning a hostgroup, its facets were not being cloned. Because of this, the hostgroup form would be pre-filled with some attributes, but not all.

This change clones all registered facets when cloning a hostgroup, enabling improvements in plugins such as Katello/katello#10894.

@jeremylenz jeremylenz changed the title Clone hostgroup facets when cloning hostgroup Fixes #37179 - Clone hostgroup facets when cloning hostgroup Feb 19, 2024
@ehelms
Copy link
Member

ehelms commented Feb 19, 2024

[test katello]

@jeremylenz
Copy link
Contributor Author

[test katello]

@jeremylenz
Copy link
Contributor Author

[test integration]

@jeremylenz
Copy link
Contributor Author

changed back to @hostgroup.clone per @parthaa

Copy link
Contributor

@parthaa parthaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well!

@parthaa
Copy link
Contributor

parthaa commented Feb 28, 2024

can you add this unit test

diff --git a/test/controllers/hostgroups_controller_test.rb b/test/controllers/hostgroups_controller_test.rb
index 697729e0e..2fd26b7da 100644
--- a/test/controllers/hostgroups_controller_test.rb
+++ b/test/controllers/hostgroups_controller_test.rb
@@ -38,6 +38,24 @@ class HostgroupsControllerTest < ActionController::TestCase
     assert_template 'new'
   end
 
+  def test_copies_facets
+    hg = hostgroups(:common)
+    hg.stubs(:facet_definitions).returns([OpenStruct.new(name: :mock_facet)])
+    stubbed_facet = OpenStruct.new(attribute_one:"one", two: "two")
+    hg.instance_eval {self.class.attr_accessor :mock_facet}
+    hg.mock_facet = stubbed_facet
+
+    @controller.stubs(:find_resource)
+    @controller.instance_variable_set(:@hostgroup, hg)
+    get :clone, params: { :id => hg.id }, session: set_session_user
+    assert_template 'new'
+    cloned_hg = @controller.instance_variable_get(:@hostgroup)
+    assert_nil cloned_hg.id
+    assert_nil cloned_hg.name
+    assert_equal stubbed_facet, cloned_hg.mock_facet
+    refute_equal stubbed_facet.object_id, cloned_hg.mock_facet.object_id
+  end
+
   def test_edit
     get :edit, params: { :id => hostgroups(:common) }, session: set_session_user
     assert_template 'edit

@jeremylenz
Copy link
Contributor Author

Added test 👍

load_vars_for_ajax
# Clone all its facets
@hostgroup.facet_definitions.map(&:name).each do |facet_name|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you don't need to do the legwork manually, you can use the SelectiveClone to do the work for you, similar to the host object:

include_in_clone facet_config.name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you @ShimShtein . Sounds like the entire PR can be replaced with

diff --git a/app/models/concerns/facets/hostgroup_extensions.rb b/app/models/concerns/facets/hostgroup_extensions.rb
index ba05649f2..4259504e0 100644
--- a/app/models/concerns/facets/hostgroup_extensions.rb
+++ b/app/models/concerns/facets/hostgroup_extensions.rb
@@ -6,7 +6,9 @@ module Facets
     include Facets::ModelExtensionsBase
 
     included do
-      configure_facet(:hostgroup, :hostgroup, :hostgroup_id)
+      configure_facet(:hostgroup, :hostgroup, :hostgroup_id) do |facet_config|
+        include_in_clone facet_config.name
+      end
     end
 
     def hostgroup_ancestry_cache

@jeremylenz
Copy link
Contributor Author

@ShimShtein updated 👍

@@ -38,6 +38,23 @@ def test_clone
assert_template 'new'
end

def test_copies_facets
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test will belong with all facet behavior tests:

context 'hostgroup behavior' do

This test file already has the proper setup.

@jeremylenz
Copy link
Contributor Author

@ShimShtein Moved and adjusted the test.

all this time I didn't realize we were running Hostgroup#clone, not Rails' clone. thanks!

@jeremylenz
Copy link
Contributor Author

Side note: this appears to still need Katello/katello#10894 in order to work; otherwise I still don't see content view / LCE populated when I clone.

@ShimShtein
Copy link
Member

Merging. Thanks @jeremylenz !

@ShimShtein ShimShtein merged commit a488cd2 into theforeman:develop Mar 10, 2024
40 checks passed
@evgeni
Copy link
Member

evgeni commented Mar 11, 2024

I'm afraid this broke foreman_puppet, which fails to boot now:

2024-03-10T22:18:19 [W|app|] ForemanPuppet: skipping engine hook (undefined method `include_in_clone' for Hostgroup (call 'Hostgroup.connection' to establish a connection):Class
 | Did you mean?  include_root_in_json)
 | /usr/share/gems/gems/activerecord-6.1.7.7/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
 | /usr/share/foreman/app/models/concerns/facets/hostgroup_extensions.rb:10:in `block (2 levels) in <module:HostgroupExtensions>'
 | /usr/share/foreman/app/models/concerns/facets/model_extensions_base.rb:57:in `register_facet_relation_for_type'
 | /usr/share/foreman/app/models/concerns/facets/model_extensions_base.rb:22:in `block in configure_facet'
 | /usr/share/foreman/app/models/concerns/facets/model_extensions_base.rb:21:in `each'
 | /usr/share/foreman/app/models/concerns/facets/model_extensions_base.rb:21:in `configure_facet'
 | /usr/share/foreman/app/models/concerns/facets/hostgroup_extensions.rb:9:in `block in <module:HostgroupExtensions>'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/concern.rb:136:in `class_eval'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/concern.rb:136:in `append_features'
 | /usr/share/foreman/app/models/hostgroup.rb:13:in `include'
 | /usr/share/foreman/app/models/hostgroup.rb:13:in `<class:Hostgroup>'
 | /usr/share/foreman/app/models/hostgroup.rb:1:in `<top (required)>'
 | /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:83:in `require'
 | /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:83:in `require'
 | /usr/share/gems/gems/zeitwerk-2.6.13/lib/zeitwerk/kernel.rb:34:in `require'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/dependencies.rb:332:in `block in require'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/dependencies.rb:299:in `load_dependency'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/dependencies.rb:332:in `require'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/dependencies.rb:424:in `block in require_or_load'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/dependencies.rb:39:in `block in load_interlock'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/dependencies/interlock.rb:13:in `loading'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/dependencies.rb:39:in `load_interlock'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/dependencies.rb:402:in `require_or_load'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/dependencies.rb:558:in `load_missing_constant'
 | /usr/share/gems/gems/activesupport-6.1.7.7/lib/active_support/dependencies.rb:213:in `const_missing'
 | /usr/share/gems/gems/foreman_puppet-6.2.0/lib/foreman_puppet/engine.rb:37:in `block in <class:Engine>'

@evgeni
Copy link
Member

evgeni commented Mar 11, 2024

#10084

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants