Skip to content

Commit

Permalink
Refs #37552 - add container push CV publish tests and limit PublishCo…
Browse files Browse the repository at this point in the history
…ntainerRespositories to container repos
  • Loading branch information
ianballou committed Jun 28, 2024
1 parent c301503 commit 35a6448
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,12 @@ def check_blob_push_container(props)
end

def create_container_repo_if_needed
unless @product.organization.library.registry_unauthenticated_pull
unless @product.syncable?
return render_podman_error(
'DENIED',
_("Requested access to '#{@container_name}' is denied")
)
end
unless @product.syncable?
return render_podman_error(
'DENIED',
_("Requested access to '#{@container_name}' is denied"),
:not_found
)
end

if get_root_repo_from_product(@product, @container_name).empty?
Expand Down Expand Up @@ -371,9 +370,6 @@ def blob_push_cleanup
latest_version_href = push_repo_api_response&.latest_version_href
pulp_repo_href = push_repo_api_response&.pulp_href

distribution_api_response = pulp_api.container_push_distribution_for_repository(pulp_repo_href)
pulp_distribution_href = distribution_api_response&.pulp_href

if latest_version_href.empty? || pulp_repo_href.empty?
return render_podman_error(
"BLOB_UPLOAD_UNKNOWN",
Expand All @@ -382,6 +378,9 @@ def blob_push_cleanup
)
end

distribution_api_response = pulp_api.container_push_distribution_for_repository(pulp_repo_href)
pulp_distribution_href = distribution_api_response&.pulp_href

if pulp_distribution_href.empty?
return render_podman_error(
"BLOB_UPLOAD_UNKNOWN",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PublishContainerRepositories < Actions::EntryAction
end

def plan(env)
repositories = env.repositories
repositories = env.repositories.docker_type
action_subject(env)
concurrence do
repositories.each do |repository|
Expand Down
20 changes: 20 additions & 0 deletions test/actions/katello/environment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ class TestBase < ActiveSupport::TestCase
end
end

class PublishContainerRepositoriesTest < TestBase
let(:action_class) { ::Actions::Katello::Environment::PublishContainerRepositories }
let(:action) { create_action action_class }

let(:environment) { stub }

it 'does not plan for container push library repos' do
container_push_repo = ::Katello::RootRepository.find_by(name: 'busybox').library_instance
container_push_repo.root.update(is_container_push: true)
environment.stubs(:repositories).returns([container_push_repo])
container_push_repo.expects(:set_container_repository_name).never
container_push_repo.expects(:clear_smart_proxy_sync_histories).never
action.stubs(:action_subject).with(environment)

plan_action(action, environment)
refute_action_planned(action, ::Actions::Katello::Repository::InstanceUpdate)
refute_action_planned(action, ::Actions::Katello::Repository::CapsuleSync)
end
end

class DestroyTest < TestBase
let(:action_class) { ::Actions::Katello::Environment::Destroy }
let(:action) { create_action action_class }
Expand Down
141 changes: 100 additions & 41 deletions test/controllers/api/registry/registry_proxies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -949,14 +949,15 @@ def setup_permissions
refute @controller.check_blob_push_container(props)
end

it 'creates container repo when needed' do
it 'creates a container repo if authorized' do
container_name = "foo"
container_push_name = "default_org/test/foo"
container_push_name_format = "label"
mock_root_repo = mock('root_repository')
mock_root_repositories = mock('root_repositories')
mock_root_repositories.stubs(:where).with(label: container_name).returns([])
mock_product = mock('Product')
mock_product.expects(:syncable?).returns(true)
mock_product.stubs(:root_repositories).returns(mock_root_repositories)
mock_product.expects(:add_repo).with(
name: container_name,
Expand All @@ -980,12 +981,30 @@ def setup_permissions
assert @controller.create_container_repo_if_needed
end

it 'does not create container repo when unauthorized' do
container_name = "foo"
container_push_name = "default_org/test/foo"
container_push_name_format = "label"
mock_root_repositories = mock('root_repositories')
mock_root_repositories.stubs(:where).with(label: container_name).returns([])
mock_product = mock('Product')
mock_product.expects(:syncable?).returns(false)
mock_product.stubs(:root_repositories).returns(mock_root_repositories)
@controller.instance_variable_set(:@product, mock_product)
@controller.instance_variable_set(:@container_name, container_name)
@controller.instance_variable_set(:@container_path_input, container_push_name)
@controller.instance_variable_set(:@container_push_name_format, container_push_name_format)
expect_render_podman_error("DENIED", :not_found)
refute @controller.create_container_repo_if_needed
end

it 'does not create container repo if it already exists' do
container_name = "foo"
mock_root_repo = mock('root_repository')
mock_root_repositories = mock('root_repositories')
mock_root_repositories.stubs(:where).with(label: container_name).returns([mock_root_repo])
mock_product = mock('Product')
mock_product.expects(:syncable?).returns(true)
mock_product.stubs(:root_repositories).returns(mock_root_repositories)
mock_product.expects(:add_repo).never
@controller.instance_variable_set(:@product, mock_product)
Expand All @@ -998,8 +1017,10 @@ def setup_permissions
container_name = "foo"
container_push_name = "default_org/test/foo"
latest_version_href = "asdfghjk"
pulp_href = "qwertyui"
pulp_repo_href = "repo_href"
pulp_distribution_href = "distribution_href"
root_id = 8_675_309
instance_id = ::Katello::RootRepository.find_by(name: 'busybox').library_instance.id
content_view_id = 2

# mock the product, root repo, instance repo, content view
Expand All @@ -1010,6 +1031,7 @@ def setup_permissions
mock_instance_repo.stubs(:root_id).returns(root_id)
mock_instance_repo.stubs(:content_view).returns(mock_content_view)
mock_instance_repo.expects(:index_content)
mock_instance_repo.expects(:id).returns(instance_id)
mock_root_repo = mock('root_repository')
mock_root_repo.stubs(:library_instance).returns(mock_instance_repo)
mock_root_repositories = mock('root_repositories')
Expand All @@ -1019,28 +1041,31 @@ def setup_permissions
mock_product.expects(:add_repo).never

# mock the pulp api endpoint
mock_api_response_results = mock('mock_api_response_results')
mock_api_response_results.stubs(:latest_version_href).returns(latest_version_href)
mock_api_response_results.stubs(:pulp_href).returns(pulp_href)
mock_api_response = mock('mock_api_response')
mock_api_response.stubs(:results).returns([mock_api_response_results])
mock_container_push_api = mock('container_push_api')
mock_container_push_api.expects(:list).with(name: container_push_name).returns(mock_api_response)
mock_repo_api = mock('repo_api')
mock_repo_api.stubs(:container_push_api).returns(mock_container_push_api)
mock_push_repo_api_response_results = mock('mock_push_repo_api_response_results')
mock_push_repo_api_response_results.stubs(:latest_version_href).returns(latest_version_href)
mock_push_repo_api_response_results.stubs(:pulp_href).returns(pulp_repo_href)
mock_distribution_api_response_results = mock('mock_distribution_api_response_results')
mock_distribution_api_response_results.stubs(:pulp_href).returns(pulp_distribution_href)

mock_pulp_api = mock('pulp_api')
mock_pulp_api.expects(:container_push_repo_for_name).with(container_push_name).returns(mock_push_repo_api_response_results)
mock_pulp_api.expects(:container_push_distribution_for_repository).with(pulp_repo_href).returns(mock_distribution_api_response_results)

# mock the repository reference
mock_repo_reference = mock('repo_reference')
mock_repo_reference.expects(:create!)

# set up pulp stubs
mock_pulp_primary = mock('pulp_primary')
mock_backend_service = mock('backend_service')
SmartProxy.stubs(:pulp_primary).returns(mock_pulp_primary)
::Katello::Pulp3::Repository.expects(:api).with(mock_pulp_primary, ::Katello::Repository::DOCKER_TYPE).returns(mock_repo_api)
#::Katello::Pulp3::Repository.expects(:api).with(mock_pulp_primary, ::Katello::Repository::DOCKER_TYPE).returns(mock_repo_api)
mock_instance_repo.expects(:backend_service).with(mock_pulp_primary).returns(mock_backend_service)
mock_backend_service.expects(:api).returns(mock_pulp_api)
::Katello::Pulp3::RepositoryReference.stubs(:where).with(
root_repository_id: root_id,
content_view_id: content_view_id,
repository_href: pulp_href
repository_href: pulp_repo_href
).returns(mock_repo_reference)

# set up the controller
Expand Down Expand Up @@ -1081,9 +1106,39 @@ def setup_permissions
refute @controller.blob_push_cleanup
end

it 'rejects missing api response on content indexing' do
it 'rejects missing repo api response on content indexing' do
container_name = "foo"
container_push_name = "default_org/test/foo"

mock_instance_repo = mock('library_instance')
mock_root_repo = mock('root_repository')
mock_root_repo.stubs(:library_instance).returns(mock_instance_repo)
mock_root_repositories = mock('root_repositories')
mock_root_repositories.stubs(:where).with(label: container_name).returns([mock_root_repo])
mock_product = mock('Product')
mock_product.stubs(:root_repositories).returns(mock_root_repositories)

mock_pulp_api = mock('pulp_api')
mock_pulp_api.expects(:container_push_repo_for_name).with(container_push_name).returns(nil)

mock_pulp_primary = mock('pulp_primary')
SmartProxy.stubs(:pulp_primary).returns(mock_pulp_primary)
mock_backend_service = mock('backend_service')
mock_instance_repo.expects(:backend_service).with(mock_pulp_primary).returns(mock_backend_service)
mock_backend_service.expects(:api).returns(mock_pulp_api)

@controller.instance_variable_set(:@product, mock_product)
@controller.instance_variable_set(:@container_name, container_name)
@controller.instance_variable_set(:@container_path_input, container_push_name)
expect_render_podman_error("BLOB_UPLOAD_UNKNOWN", :not_found)
refute @controller.blob_push_cleanup
end

it 'rejects missing pulp_distribution_href on content indexing' do
container_name = "foo"
container_push_name = "default_org/test/foo"
pulp_repo_href = "repo_href"
latest_version_href = "latest_version_href"

mock_instance_repo = mock('library_instance')
mock_root_repo = mock('root_repository')
Expand All @@ -1093,14 +1148,21 @@ def setup_permissions
mock_product = mock('Product')
mock_product.stubs(:root_repositories).returns(mock_root_repositories)

mock_container_push_api = mock('container_push_api')
mock_container_push_api.expects(:list).with(name: container_push_name).returns(nil)
mock_repo_api = mock('repo_api')
mock_repo_api.stubs(:container_push_api).returns(mock_container_push_api)
mock_push_repo_api_response_results = mock('mock_push_repo_api_response_results')
mock_push_repo_api_response_results.stubs(:latest_version_href).returns(latest_version_href)
mock_push_repo_api_response_results.stubs(:pulp_href).returns(pulp_repo_href)
mock_distribution_api_response_results = mock('mock_distribution_api_response_results')
mock_distribution_api_response_results.stubs(:pulp_href).returns(nil)

mock_pulp_api = mock('pulp_api')
mock_pulp_api.expects(:container_push_repo_for_name).with(container_push_name).returns(mock_push_repo_api_response_results)
mock_pulp_api.expects(:container_push_distribution_for_repository).with(pulp_repo_href).returns(mock_distribution_api_response_results)

mock_pulp_primary = mock('pulp_primary')
SmartProxy.stubs(:pulp_primary).returns(mock_pulp_primary)
::Katello::Pulp3::Repository.expects(:api).with(mock_pulp_primary, ::Katello::Repository::DOCKER_TYPE).returns(mock_repo_api)
mock_backend_service = mock('backend_service')
mock_instance_repo.expects(:backend_service).with(mock_pulp_primary).returns(mock_backend_service)
mock_backend_service.expects(:api).returns(mock_pulp_api)

@controller.instance_variable_set(:@product, mock_product)
@controller.instance_variable_set(:@container_name, container_name)
Expand All @@ -1112,7 +1174,7 @@ def setup_permissions
it 'rejects missing latest_version_href on content indexing' do
container_name = "foo"
container_push_name = "default_org/test/foo"
pulp_href = "qwertyui"
pulp_repo_href = "repo_href"

mock_instance_repo = mock('library_instance')
mock_root_repo = mock('root_repository')
Expand All @@ -1122,19 +1184,18 @@ def setup_permissions
mock_product = mock('Product')
mock_product.stubs(:root_repositories).returns(mock_root_repositories)

mock_api_response_results = mock('mock_api_response_results')
mock_api_response_results.stubs(:latest_version_href).returns(nil)
mock_api_response_results.stubs(:pulp_href).returns(pulp_href)
mock_api_response = mock('mock_api_response')
mock_api_response.stubs(:results).returns([mock_api_response_results])
mock_container_push_api = mock('container_push_api')
mock_container_push_api.expects(:list).with(name: container_push_name).returns(mock_api_response)
mock_repo_api = mock('repo_api')
mock_repo_api.stubs(:container_push_api).returns(mock_container_push_api)
mock_push_repo_api_response_results = mock('mock_push_repo_api_response_results')
mock_push_repo_api_response_results.stubs(:latest_version_href).returns(nil)
mock_push_repo_api_response_results.stubs(:pulp_href).returns(pulp_repo_href)

mock_pulp_api = mock('pulp_api')
mock_pulp_api.expects(:container_push_repo_for_name).with(container_push_name).returns(mock_push_repo_api_response_results)

mock_pulp_primary = mock('pulp_primary')
SmartProxy.stubs(:pulp_primary).returns(mock_pulp_primary)
::Katello::Pulp3::Repository.expects(:api).with(mock_pulp_primary, ::Katello::Repository::DOCKER_TYPE).returns(mock_repo_api)
mock_backend_service = mock('backend_service')
mock_instance_repo.expects(:backend_service).with(mock_pulp_primary).returns(mock_backend_service)
mock_backend_service.expects(:api).returns(mock_pulp_api)

@controller.instance_variable_set(:@product, mock_product)
@controller.instance_variable_set(:@container_name, container_name)
Expand All @@ -1155,21 +1216,19 @@ def setup_permissions
mock_root_repositories.stubs(:where).with(label: container_name).returns([mock_root_repo])
mock_product = mock('Product')
mock_product.stubs(:root_repositories).returns(mock_root_repositories)
mock_product.expects(:add_repo).never

mock_api_response_results = mock('mock_api_response_results')
mock_api_response_results.stubs(:latest_version_href).returns(latest_version_href)
mock_api_response_results.stubs(:pulp_href).returns(nil)
mock_api_response = mock('mock_api_response')
mock_api_response.stubs(:results).returns([mock_api_response_results])
mock_container_push_api = mock('container_push_api')
mock_container_push_api.expects(:list).with(name: container_push_name).returns(mock_api_response)
mock_repo_api = mock('repo_api')
mock_repo_api.stubs(:container_push_api).returns(mock_container_push_api)
mock_push_repo_api_response_results = mock('mock_push_repo_api_response_results')
mock_push_repo_api_response_results.stubs(:latest_version_href).returns(latest_version_href)
mock_push_repo_api_response_results.stubs(:pulp_href).returns(nil)

mock_pulp_api = mock('pulp_api')
mock_pulp_api.expects(:container_push_repo_for_name).with(container_push_name).returns(mock_push_repo_api_response_results)

mock_pulp_primary = mock('pulp_primary')
SmartProxy.stubs(:pulp_primary).returns(mock_pulp_primary)
::Katello::Pulp3::Repository.expects(:api).with(mock_pulp_primary, ::Katello::Repository::DOCKER_TYPE).returns(mock_repo_api)
mock_backend_service = mock('backend_service')
mock_instance_repo.expects(:backend_service).with(mock_pulp_primary).returns(mock_backend_service)
mock_backend_service.expects(:api).returns(mock_pulp_api)

@controller.instance_variable_set(:@product, mock_product)
@controller.instance_variable_set(:@container_name, container_name)
Expand Down
7 changes: 4 additions & 3 deletions test/models/smart_proxy_sync_history_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ def test_clear_history_on_smart_proxy

def test_clear_history_on_publish_repositories
User.current = users(:admin)
@repo.create_smart_proxy_sync_history(proxy_with_pulp)
busybox = katello_repositories(:busybox)
busybox.create_smart_proxy_sync_history(proxy_with_pulp)
library = katello_environments(:library)
library.expects(:repositories).returns([@repo])
library.expects(:repositories).returns(::Katello::Repository.where(id: busybox.id))
::Actions::Katello::Environment::PublishContainerRepositories.any_instance.expects(:plan_action).twice
::ForemanTasks.sync_task(::Actions::Katello::Environment::PublishContainerRepositories, library)
assert_equal @repo.smart_proxy_sync_histories.count, 0
assert_equal busybox.smart_proxy_sync_histories.count, 0
end
end
end

0 comments on commit 35a6448

Please sign in to comment.