diff --git a/app/models/katello/concerns/smart_proxy_extensions.rb b/app/models/katello/concerns/smart_proxy_extensions.rb index 3761f4110e6..abbbc998191 100644 --- a/app/models/katello/concerns/smart_proxy_extensions.rb +++ b/app/models/katello/concerns/smart_proxy_extensions.rb @@ -33,6 +33,7 @@ def refresh before_create :associate_default_locations before_create :associate_lifecycle_environments before_validation :set_default_download_policy + after_update :refresh_smart_proxy_sync_histories lazy_accessor :pulp_repositories, :initializer => lambda { |_s| pulp_node.extensions.repository.retrieve_all } @@ -471,6 +472,9 @@ def add_lifecycle_environment(environment) end def remove_lifecycle_environment(environment) + smart_proxy_helper = ::Katello::SmartProxyHelper.new(self) + repos = smart_proxy_helper.repositories_available_to_capsule(environment) + smart_proxy_helper.clear_smart_proxy_sync_histories(repos) unless repos.empty? self.lifecycle_environments.find(environment.id) unless self.lifecycle_environments.destroy(environment) fail _("Could not remove the lifecycle environment from the smart proxy") @@ -479,6 +483,16 @@ def remove_lifecycle_environment(environment) raise _("Lifecycle environment was not attached to the smart proxy; therefore, no changes were made.") end + def refresh_smart_proxy_sync_histories + smart_proxy_helper = ::Katello::SmartProxyHelper.new(self) + repos = smart_proxy_helper.repositories_available_to_capsule.select(:id) + if repos.size == 0 + self.smart_proxy_sync_histories.delete_all + else + self.smart_proxy_sync_histories.where.not(repository_id: repos).delete_all + end + end + def available_lifecycle_environments(organization_id = nil) scope = Katello::KTEnvironment.not_in_capsule(self) scope = scope.where(organization_id: organization_id) if organization_id diff --git a/test/models/concerns/smart_proxy_extensions_test.rb b/test/models/concerns/smart_proxy_extensions_test.rb index 107d0da2243..7888dc51fb9 100644 --- a/test/models/concerns/smart_proxy_extensions_test.rb +++ b/test/models/concerns/smart_proxy_extensions_test.rb @@ -144,6 +144,7 @@ def test_update_global_content_counts ostree_repo, deb_repo, python_repo, cvv_container_repo] @proxy.lifecycle_environments << [container_repo.environment, cvv_container_repo.environment] ::Katello::SmartProxyHelper.any_instance.expects(:repositories_available_to_capsule).once.returns(repos) + @proxy.expects(:refresh_smart_proxy_sync_histories).returns(true) @proxy.update_content_counts! counts = @proxy.content_counts expected_counts = { "content_view_versions" => @@ -247,6 +248,7 @@ def test_update_environment_content_counts .with(container_repo.environment, nil) .once .returns(repos) + @proxy.expects(:refresh_smart_proxy_sync_histories).returns(true) @proxy.update_content_counts!(environment: container_repo.environment) counts = @proxy.content_counts expected_counts = { "content_view_versions" => @@ -277,6 +279,7 @@ def test_update_content_view_counts .with(nil, cvv_container_repo.content_view_version.content_view) .once .returns(repos) + @proxy.expects(:refresh_smart_proxy_sync_histories).returns(true) @proxy.update_content_counts!(environment: nil, content_view: cvv_container_repo.content_view_version.content_view, repository: nil) @@ -303,6 +306,7 @@ def test_update_content_view_counts def test_update_repository_counts cvv_container_repo = setup_cvv_container_repo + @proxy.expects(:refresh_smart_proxy_sync_histories).returns(true) @proxy.lifecycle_environments << cvv_container_repo.environment @proxy.update_content_counts!(repository: cvv_container_repo) counts = @proxy.content_counts @@ -326,6 +330,19 @@ def test_update_repository_counts assert_equal expected_counts, counts end + def test_refresh_sync_history + file_repo = katello_repositories(:pulp3_file_1) + file_repo.update library_instance_id: file_repo.id + repos = [file_repo] + @proxy.lifecycle_environments = [file_repo.environment] + ::Katello::SmartProxyHelper.any_instance.expects(:repositories_available_to_capsule) + .returns(repos) + file_repo.create_smart_proxy_sync_history(@proxy) + assert_equal 1, @proxy.smart_proxy_sync_histories.count + @proxy.remove_lifecycle_environment(file_repo.environment) + assert_equal 0, @proxy.smart_proxy_sync_histories.count + end + def test_sets_default_download_policy Setting[:default_proxy_download_policy] = ::Katello::RootRepository::DOWNLOAD_ON_DEMAND @proxy.save!