Skip to content

Commit

Permalink
Fixes #35863 - Stop using #hosts.delete_all with KTEnvironments (#10392)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylenz committed Dec 16, 2022
1 parent 3920e5a commit fa2e548
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
5 changes: 2 additions & 3 deletions app/lib/actions/katello/content_view/remove.rb
Expand Up @@ -71,17 +71,16 @@ def plan(content_view, options)
content_view_history_ids: cv_histories.map { |history| history.id })

if organization_destroy
destroy_hosts_and_hostgroups(content_view: content_view)
destroy_host_and_hostgroup_associations(content_view: content_view)
end
end
end

def destroy_hosts_and_hostgroups(content_view:)
def destroy_host_and_hostgroup_associations(content_view:)
content_view.hostgroups.destroy_all
host_ids = content_view.hosts.ids
::Katello::Host::ContentFacet.where(:host_id => host_ids).destroy_all
::Katello::Host::SubscriptionFacet.where(:host_id => host_ids).destroy_all
::Host::Managed.where(:id => host_ids).destroy_all
end

def check_version_deletion(versions, cv_envs)
Expand Down
10 changes: 8 additions & 2 deletions app/lib/actions/katello/environment/destroy.rb
Expand Up @@ -24,14 +24,20 @@ def plan(env, options = {})
end

if organization_destroy
env.hostgroups.clear
env.hosts.clear
delete_host_and_hostgroup_associations(environment: env)
end

plan_self
end
end

def delete_host_and_hostgroup_associations(environment:)
environment.hostgroups.delete_all
host_ids = environment.hosts.ids
::Katello::Host::ContentFacet.where(:host_id => host_ids).delete_all
::Katello::Host::SubscriptionFacet.where(:host_id => host_ids).delete_all
end

def humanized_name
_("Delete Lifecycle Environment")
end
Expand Down
21 changes: 21 additions & 0 deletions test/actions/katello/environment_test.rb
Expand Up @@ -31,4 +31,25 @@ class DestroyTest < TestBase
assert_action_planned_with(action, ::Actions::Katello::ContentView::Remove, content_view, :content_view_environments => [cve], :skip_repo_destroy => false, :organization_destroy => false)
end
end

class DestroyWithOrganizationDestroyTest < TestBase
let(:action_class) { ::Actions::Katello::Environment::Destroy }
let(:action) { create_action action_class }

let(:environment) { stub }

it 'plans' do
stub_remote_user
content_view = stub
cve = mock(:content_view => content_view)
action.stubs(:action_subject).with(environment)
environment.expects(:content_view_environments).returns([cve])
environment.expects(:deletable?).returns(true)
environment.expects(:hostgroups).returns(::Hostgroup.none)
environment.expects(:hosts).returns(::Host.none)
environment.expects(:hosts=).never
plan_action(action, environment, :organization_destroy => true)
assert_action_planned_with(action, ::Actions::Katello::ContentView::Remove, content_view, :content_view_environments => [cve], :skip_repo_destroy => false, :organization_destroy => true)
end
end
end

0 comments on commit fa2e548

Please sign in to comment.