Skip to content

Commit

Permalink
Fixes #38117 - Refresh smart proxy sync history when proxy is updated (
Browse files Browse the repository at this point in the history
  • Loading branch information
sjha4 authored Dec 23, 2024
1 parent 7a9771d commit fb6cca7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/models/katello/concerns/smart_proxy_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/models/concerns/smart_proxy_extensions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" =>
Expand Down Expand Up @@ -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" =>
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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!
Expand Down

0 comments on commit fb6cca7

Please sign in to comment.