Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #38009 - content-override with deselect #11216

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions app/controllers/katello/api/v2/host_subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,20 +242,18 @@ def action_permission
def find_content_overrides
if !params.dig(:content_overrides_search, :search).nil?

content_labels = ::Katello::Content.joins(:product_contents)
.where("#{Katello::ProductContent.table_name}.product_id": @host.organization.products.subscribable.enabled)
.search_for(params[:content_overrides_search][:search])
.pluck(:label)

if Foreman::Cast.to_bool(params.dig(:content_overrides_search, :limit_to_env))
env_content = ProductContentFinder.new(
:match_subscription => false,
:match_environment => true,
:consumable => @host.subscription_facet
).product_content
env_content_labels = ::Katello::Content.find(env_content.pluck(:content_id)).pluck(:label)
content_labels &= env_content_labels
end
content = ::Katello::Content.joins(:product_contents)
.where("#{Katello::ProductContent.table_name}.product_id": @host.organization.products.subscribable.enabled)

env_content = ProductContentFinder.new(
:match_subscription => false,
:match_environment => Foreman::Cast.to_bool(params.dig(:content_overrides_search, :limit_to_env)),
:consumable => @host.subscription_facet
).product_content
content = content.where(id: env_content.pluck(:content_id))

content_labels = content.search_for(params[:content_overrides_search][:search])
.pluck(:label)

@content_overrides = content_labels.map do |label|
{ content_label: label,
Expand Down
25 changes: 25 additions & 0 deletions test/controllers/api/v2/host_subscriptions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,31 @@ def test_find_content_overrides_with_empty_string_search_limited_to_environment
assert_equal(label, result[0][:content_label])
end

def test_find_content_overrides_with_inverse_search_limited_to_environment_with_structured_apt
# Create Host with "deb" as content
content_view = katello_content_views(:library_dev_view)
library = katello_environments(:library)
activation_key = katello_activation_keys(:library_dev_staging_view_key)
host_collection = katello_host_collections(:simple_host_collection)
activation_key.host_collections << host_collection

host = FactoryBot.create(:host, :with_content, :with_subscription, :content_view => content_view,
:lifecycle_environment => library, :organization => content_view.organization)

# Create fake product with content_id and stub ProductContentFinder
content = katello_contents(:deb_content_v1)
pc = [FactoryBot.build(:katello_product_content, content: content)]
ProductContentFinder.any_instance.stubs(:product_content).returns(pc)

controller = ::Katello::Api::V2::HostSubscriptionsController.new
controller.params = { :host_id => host.id, :content_overrides_search => { :search => "cp_content_id !^ (#{content.cp_content_id})", :limit_to_env => true} }
controller.instance_variable_set(:@host, host)
controller.send(:find_content_overrides)

result = controller.instance_variable_get(:@content_overrides)
assert_equal(0, result.length)
end

def test_content_override_bulk
content_overrides = [{:content_label => 'some-content', :value => 1}]
expected_content_labels = content_overrides.map { |co| co[:content_label] }
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/models/katello_content_view_repositories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ library_view_debian_10_amd64:
content_view_id: <%= ActiveRecord::FixtureSet.identify(:library_view) %>
repository_id: <%= ActiveRecord::FixtureSet.identify(:debian_10_amd64) %>

library_view_acme_debian_10_amd64:
content_view_id: <%= ActiveRecord::FixtureSet.identify(:acme_default) %>
repository_id: <%= ActiveRecord::FixtureSet.identify(:debian_10_amd64_dev) %>

library_view_rhel_6_x86_64:
content_view_id: <%= ActiveRecord::FixtureSet.identify(:acme_default) %>
repository_id: <%= ActiveRecord::FixtureSet.identify(:rhel_6_x86_64) %>
Expand Down
34 changes: 33 additions & 1 deletion test/fixtures/models/katello_contents.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
deb_content:
name: debian
label: debian
cp_content_id: 110
organization_id: <%= ActiveRecord::FixtureSet.identify(:empty_organization) %>
content_url: /custom/Debian_12/Debian_12_amd64_main/?comp=main&rel=bookworm
content_type: "deb"

deb_content_v1:
name: debian
label: debian
cp_content_id: 111
organization_id: <%= ActiveRecord::FixtureSet.identify(:empty_organization) %>
content_url: /custom/Debian_12/Debian_12_amd64_main/?comp=main&rel=bookworm
content_type: "deb"

deb_content_v2:
name: debian
label: debian
cp_content_id: 112
organization_id: <%= ActiveRecord::FixtureSet.identify(:empty_organization) %>
content_url: /custom/Debian_12/Debian_12_amd64_main/?comp=main&rel=bookworm
content_type: "deb"

deb_content_v3:
name: debian
label: debian
cp_content_id: 113
organization_id: <%= ActiveRecord::FixtureSet.identify(:empty_organization) %>
content_url: /custom/Debian_12/Debian_10_amd64_main/?comp=main&rel=buster
content_type: "deb"

some_content:
name: Fedora
cp_content_id: 1
Expand All @@ -12,4 +44,4 @@ rhel_content:
cp_content_id: 69
organization_id: <%= ActiveRecord::FixtureSet.identify(:empty_organization) %>
content_url: /content/dist/rhel/server/$releasever/$basearch/os
content_type: "yum"
content_type: "yum"
6 changes: 5 additions & 1 deletion test/fixtures/models/katello_pool_products.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ basic_redhat:
pool_id: <%= ActiveRecord::FixtureSet.identify(:pool_one) %>
product_id: <%= ActiveRecord::FixtureSet.identify(:redhat) %>

basic_debian:
pool_id: <%= ActiveRecord::FixtureSet.identify(:pool_one) %>
product_id: <%= ActiveRecord::FixtureSet.identify(:debian) %>

other_sub_fedora:
pool_id: <%= ActiveRecord::FixtureSet.identify(:pool_two) %>
product_id: <%= ActiveRecord::FixtureSet.identify(:fedora) %>
product_id: <%= ActiveRecord::FixtureSet.identify(:fedora) %>
20 changes: 20 additions & 0 deletions test/fixtures/models/katello_product_contents.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,23 @@ rhel_content:
content_id: <%= ActiveRecord::FixtureSet.identify(:rhel_content) %>
product_id: <%= ActiveRecord::FixtureSet.identify(:redhat) %>
enabled: false

debian_content:
content_id: <%= ActiveRecord::FixtureSet.identify(:deb_content) %>
product_id: <%= ActiveRecord::FixtureSet.identify(:debian) %>
enabled: false

debian_content_v1:
content_id: <%= ActiveRecord::FixtureSet.identify(:deb_content_v1) %>
product_id: <%= ActiveRecord::FixtureSet.identify(:debian) %>
enabled: false

debian_content_v2:
content_id: <%= ActiveRecord::FixtureSet.identify(:deb_content_v2) %>
product_id: <%= ActiveRecord::FixtureSet.identify(:debian) %>
enabled: false

debian_content_v3:
content_id: <%= ActiveRecord::FixtureSet.identify(:deb_content_v3) %>
product_id: <%= ActiveRecord::FixtureSet.identify(:debian) %>
enabled: false
12 changes: 12 additions & 0 deletions test/fixtures/models/katello_repositories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ debian_9_amd64:
debian_10_amd64:
root_id: <%= ActiveRecord::FixtureSet.identify(:debian_10_amd64_root) %>
pulp_id: Debian_10
content_id: 110
relative_path: 'ACME_Corporation/library/debian_10_label'
environment_id: <%= ActiveRecord::FixtureSet.identify(:library) %>
content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:library_default_version) %>
Expand All @@ -36,6 +37,7 @@ debian_10_dev_library_view:
library_instance: debian_10_amd64
root_id: <%= ActiveRecord::FixtureSet.identify(:debian_10_amd64_root) %>
pulp_id: debian_10_dev_library_view
content_id: 112
relative_path: 'ACME_Corporation/dev/debian_10_library_library_view_label'
environment_id: <%= ActiveRecord::FixtureSet.identify(:dev) %>
content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:library_view_version_2) %>
Expand All @@ -44,6 +46,7 @@ debian_10_amd64_dev:
library_instance: debian_10_amd64
root_id: <%= ActiveRecord::FixtureSet.identify(:debian_10_amd64_root) %>
pulp_id: 12
content_id: 111
relative_path: 'ACME_Corporation/dev/debian_10_dev_label'
environment_id: <%= ActiveRecord::FixtureSet.identify(:dev) %>
content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:library_dev_view_version) %>
Expand Down Expand Up @@ -310,6 +313,15 @@ busybox_view2:
environment_id:
content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:library_view_version_2) %>

debian_10_amd64_composite_view_version_1:
library_instance: debian_10_amd64
root_id: <%= ActiveRecord::FixtureSet.identify(:debian_10_amd64_root) %>
pulp_id: 8_deb_composite_version1
content_id: 113
relative_path: 'ACME_Corporation/library/composite/debian_10_label'
environment_id: <%= ActiveRecord::FixtureSet.identify(:library) %>
content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:composite_view_version_1) %>

rhel_6_x86_64_composite_view_version_1:
library_instance: rhel_6_x86_64
root_id: <%= ActiveRecord::FixtureSet.identify(:rhel_6_x86_64_root) %>
Expand Down
Loading