Skip to content

Commit

Permalink
Fixes #38009 - content-override with deselect
Browse files Browse the repository at this point in the history
For host repository-sets with structured apt enabled setting
content-overrides using bulk-action:
de-selecting deb-repos after using select-all has no effect.
  • Loading branch information
m-bucher committed Nov 13, 2024
1 parent c32d9ac commit ae3d07f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 15 deletions.
30 changes: 16 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,22 @@ 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])

Check failure on line 255 in app/controllers/katello/api/v2/host_subscriptions_controller.rb

View workflow job for this annotation

GitHub Actions / Rubocop / Rubocop

Lint/UselessAssignment: Useless assignment to variable - `content_labels`.
.pluck(:label)

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

@content_overrides = content_labels.map do |label|
{ content_label: label,
Expand Down
32 changes: 32 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,38 @@ 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
# enable structured_apt
Foreman.settings.set_user_value('deb_enable_structured_apt', true)
# 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)

# Get content_id and label of first product of host
products = ::Katello::Content.joins(:product_contents)
.where("#{Katello::ProductContent.table_name}.product_id": host.organization.products.subscribable.enabled)
in_env_id = products.pluck(:content_id)[2]

# Create fake product with content_id and stub ProductContentFinder
content = katello_contents(:deb_content_v1)
pc = [FactoryBot.build(:katello_product_content, content: content, content_id: in_env_id)]
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
18 changes: 17 additions & 1 deletion test/fixtures/models/katello_contents.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
deb_content_v1:
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_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"

some_content:
name: Fedora
cp_content_id: 1
Expand All @@ -12,4 +28,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"
10 changes: 10 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,13 @@ rhel_content:
content_id: <%= ActiveRecord::FixtureSet.identify(:rhel_content) %>
product_id: <%= ActiveRecord::FixtureSet.identify(:redhat) %>
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
2 changes: 2 additions & 0 deletions test/fixtures/models/katello_repositories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ debian_10_dev_library_view:
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) %>
content_id: <%= ActiveRecord::FixtureSet.identify(:deb_content_v2) %>

debian_10_amd64_dev:
library_instance: debian_10_amd64
Expand All @@ -47,6 +48,7 @@ debian_10_amd64_dev:
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) %>
content_id: <%= ActiveRecord::FixtureSet.identify(:deb_content_v1) %>
distribution_arch: "amd64"
distribution_version: "10.1"
distribution_family: "Debian"
Expand Down

0 comments on commit ae3d07f

Please sign in to comment.