From 501e42883d2a2db014640fdcf824a21bb2541e34 Mon Sep 17 00:00:00 2001 From: Brad Buckingham Date: Wed, 19 Aug 2015 15:11:21 -0400 Subject: [PATCH] fixes #11436 - update repo destroy action to check if repo is deletable, in plan phase This small commit is to ensure that if a user attempts to delete a repository (e.g. as a result of 'hammer repository-set disable') that the request is immediately denied in the action plan phase; otherwise, other backend actions (e.g. pulp/candlepin) will get executed on a repo that is not deletable. --- app/lib/actions/katello/repository/destroy.rb | 7 ++++++ app/models/katello/repository.rb | 24 +++++++++---------- test/actions/katello/repository_test.rb | 11 ++++++++- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/app/lib/actions/katello/repository/destroy.rb b/app/lib/actions/katello/repository/destroy.rb index 754aacbc2d1..c2ef0b6d045 100644 --- a/app/lib/actions/katello/repository/destroy.rb +++ b/app/lib/actions/katello/repository/destroy.rb @@ -12,6 +12,13 @@ def plan(repository, options = {}) skip_environment_update = options.fetch(:organization_destroy, false) action_subject(repository) + + if !planned_destroy && !repository.assert_deletable + # The repository is going to be deleted in finalize, but it cannot be deleted. + # Stop now and inform the user. + fail repository.errors.messages.values.join("\n") + end + plan_action(ContentViewPuppetModule::Destroy, repository) if repository.puppet? plan_action(Pulp::Repository::Destroy, pulp_id: repository.pulp_id) plan_action(Product::ContentDestroy, repository) diff --git a/app/models/katello/repository.rb b/app/models/katello/repository.rb index 34f57d9b59b..d3027423d53 100644 --- a/app/models/katello/repository.rb +++ b/app/models/katello/repository.rb @@ -490,18 +490,6 @@ def remove_db_units(units) end end - protected - - def removable_unit_association - if yum? - self.rpms - elsif docker? - self.docker_images - else - fail "Content type not supported for removal" - end - end - def assert_deletable if self.environment.try(:library?) && self.content_view.default? if self.environment.organization.being_deleted? @@ -519,6 +507,18 @@ def assert_deletable end end + protected + + def removable_unit_association + if yum? + self.rpms + elsif docker? + self.docker_images + else + fail "Content type not supported for removal" + end + end + def downcase_pulp_id # Docker doesn't support uppercase letters in repository names. Since the pulp_id # is currently being used for the name, it will be downcased for this content type. diff --git a/test/actions/katello/repository_test.rb b/test/actions/katello/repository_test.rb index 11618fe8c2e..36e3c6c3574 100644 --- a/test/actions/katello/repository_test.rb +++ b/test/actions/katello/repository_test.rb @@ -49,15 +49,24 @@ class DestroyTest < TestBase let(:action_class) { ::Actions::Katello::Repository::Destroy } let(:pulp_action_class) { ::Actions::Pulp::Repository::Destroy } let(:unpublished_repository) { katello_repositories(:fedora_17_unpublished) } + let(:published_repository) { katello_repositories(:rhel_6_x86_64) } it 'plans' do - action = create_action action_class + action = create_action action_class action.stubs(:action_subject).with(unpublished_repository) action.expects(:plan_self) plan_action action, unpublished_repository assert_action_planed_with action, pulp_action_class, pulp_id: unpublished_repository.pulp_id assert_action_planed_with action, ::Actions::Katello::Product::ContentDestroy, unpublished_repository end + + it "plan fails if repository is not deletable" do + action = create_action action_class + action.stubs(:action_subject).with(published_repository) + assert_raises(RuntimeError) do + plan_action action, published_repository + end + end end class DyscoverTest < TestBase