foreman_rh_cloud 4.0.26 → 4.0.31

Sign up to get free protection for your applications and to get access to all the features.
@@ -34,6 +34,7 @@ module ForemanInventoryUpload
34
34
 
35
35
  def self.for_slice(base)
36
36
  base
37
+ .search_for("not params.#{InsightsCloud.enable_client_param} = f")
37
38
  .joins(:subscription_facet)
38
39
  .preload(
39
40
  :interfaces,
@@ -13,15 +13,15 @@ module ForemanInventoryUpload
13
13
  organizations +
14
14
  content_data +
15
15
  satellite_server_data
16
- ).reject { |key, value| value.empty? }
16
+ ).reject { |key, value| value.empty? }.map { |key, value| [key, truncated_value(value)] }
17
17
  end
18
18
 
19
19
  def generate_parameters
20
20
  return [] unless Setting[:include_parameter_tags]
21
21
 
22
- (@host.host_inherited_params_objects || [])
23
- .map { |item| [item.name, item.value] }
22
+ (@host.host_params || {})
24
23
  .select { |_name, value| value.present? || value.is_a?(FalseClass) }
24
+ .map { |key, value| [key, truncated_value(value)] }
25
25
  end
26
26
 
27
27
  private
@@ -58,6 +58,12 @@ module ForemanInventoryUpload
58
58
  ['organization_id', @host.organization_id.to_s],
59
59
  ]
60
60
  end
61
+
62
+ def truncated_value(value)
63
+ return 'Original value exceeds 250 characters' if value.to_s.length > 250
64
+
65
+ value
66
+ end
61
67
  end
62
68
  end
63
69
  end
@@ -1,3 +1,3 @@
1
1
  module ForemanRhCloud
2
- VERSION = '4.0.26'.freeze
2
+ VERSION = '4.0.31'.freeze
3
3
  end
@@ -98,7 +98,18 @@ module ForemanRhCloud
98
98
 
99
99
  # For testing purposes we can override the default hostname with an environment variable SATELLITE_RH_CLOUD_FOREMAN_HOST
100
100
  def self.foreman_host
101
- @foreman_host ||= ::Host.unscoped.friendly.find(ENV['SATELLITE_RH_CLOUD_FOREMAN_HOST'] || ::SmartProxy.default_capsule.name)
101
+ @foreman_host ||= begin
102
+ fullname = foreman_host_name
103
+ ::Host.unscoped.friendly.find(fullname)
104
+ rescue ActiveRecord::RecordNotFound
105
+ # fullname didn't work. Let's try shortname
106
+ shortname = /(?<shortname>[^\.]*)\.?.*/.match(fullname)[:shortname]
107
+ ::Host.unscoped.friendly.find(shortname)
108
+ end
109
+ end
110
+
111
+ def self.foreman_host_name
112
+ ENV['SATELLITE_RH_CLOUD_FOREMAN_HOST'] || ::SmartProxy.default_capsule.name
102
113
  end
103
114
 
104
115
  def self.legacy_insights_ca
@@ -17,7 +17,6 @@ module InventorySync
17
17
  @sub_ids.map do |sub_id|
18
18
  host_id = host_id(sub_id)
19
19
  if host_id
20
- touched << host_id
21
20
  {
22
21
  host_id: host_id,
23
22
  status: InventorySync::InventoryStatus::SYNC,
@@ -28,10 +27,6 @@ module InventorySync
28
27
  end.compact
29
28
  end
30
29
 
31
- def touched
32
- @touched ||= []
33
- end
34
-
35
30
  def host_id(sub_id)
36
31
  hosts[sub_id]
37
32
  end
@@ -14,11 +14,7 @@ module InventorySync
14
14
  end
15
15
 
16
16
  def setup_statuses
17
- @subscribed_hosts_ids = Set.new(
18
- ForemanInventoryUpload::Generators::Queries.for_slice(
19
- Host.unscoped.where(organization: input[:organization_id])
20
- ).pluck(:id)
21
- )
17
+ @subscribed_hosts_ids = Set.new(affected_host_ids)
22
18
 
23
19
  InventorySync::InventoryStatus.transaction do
24
20
  InventorySync::InventoryStatus.where(host_id: @subscribed_hosts_ids).delete_all
@@ -35,8 +31,10 @@ module InventorySync
35
31
  def update_statuses_batch
36
32
  results = yield
37
33
 
38
- update_hosts_status(results.status_hashes, results.touched)
39
- host_statuses[:sync] += results.touched.size
34
+ existing_hosts = results.status_hashes.select { |hash| @subscribed_hosts_ids.include?(hash[:host_id]) }
35
+
36
+ update_hosts_status(existing_hosts)
37
+ host_statuses[:sync] += existing_hosts.size
40
38
  end
41
39
 
42
40
  def rescue_strategy_for_self
@@ -45,9 +43,10 @@ module InventorySync
45
43
 
46
44
  private
47
45
 
48
- def update_hosts_status(status_hashes, touched)
46
+ def update_hosts_status(status_hashes)
49
47
  InventorySync::InventoryStatus.create(status_hashes)
50
- @subscribed_hosts_ids.subtract(touched)
48
+ updated_ids = status_hashes.map { |hash| hash[:host_id] }
49
+ @subscribed_hosts_ids.subtract(updated_ids)
51
50
  end
52
51
 
53
52
  def add_missing_hosts_statuses(hosts_ids)
@@ -68,6 +67,12 @@ module InventorySync
68
67
  disconnect: 0,
69
68
  }
70
69
  end
70
+
71
+ def affected_host_ids
72
+ ForemanInventoryUpload::Generators::Queries.for_slice(
73
+ Host.unscoped.where(organization: input[:organization_id])
74
+ ).pluck(:id)
75
+ end
71
76
  end
72
77
  end
73
78
  end
@@ -35,18 +35,14 @@ module InventorySync
35
35
  private
36
36
 
37
37
  def add_missing_insights_facets(uuids_hash)
38
- existing_facets = InsightsFacet.where(host_id: uuids_hash.keys).pluck(:host_id, :uuid)
39
- missing_facets = uuids_hash.except(*existing_facets.map(&:first)).map do |host_id, uuid|
38
+ all_facets = uuids_hash.map do |host_id, uuid|
40
39
  {
41
40
  host_id: host_id,
42
41
  uuid: uuid,
43
42
  }
44
43
  end
45
- InsightsFacet.create(missing_facets)
46
44
 
47
- existing_facets.select { |host_id, uuid| uuid.empty? }.each do |host_id, _uuid|
48
- InsightsFacet.where(host_id: host_id).update_all(uuid: uuids_hash[host_id])
49
- end
45
+ InsightsFacet.upsert_all(all_facets, unique_by: :host_id) unless all_facets.empty?
50
46
  end
51
47
 
52
48
  def plan_self_host_sync
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foreman_rh_cloud",
3
- "version": "4.0.26",
3
+ "version": "4.0.31",
4
4
  "description": "Inventory Upload =============",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -54,10 +54,24 @@ FactoryBot.define do
54
54
  end
55
55
  end
56
56
 
57
+ FactoryBot.define do
58
+ factory :katello_subscription, :class => Katello::Subscription do
59
+ end
60
+ end
61
+
57
62
  FactoryBot.define do
58
63
  factory :katello_pool, :class => Katello::Pool do
59
64
  active { true }
60
65
  end_date { Date.today + 1.year }
66
+ cp_id { 1 }
67
+
68
+ association :organization, :factory => :katello_organization
69
+
70
+ after(:build) do |pool, _evaluator|
71
+ pool.subscription.organization = pool.organization
72
+ end
73
+
74
+ association :subscription, :factory => :katello_subscription
61
75
  end
62
76
  end
63
77
 
@@ -37,6 +37,18 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
37
37
  @host2.subscription_facet.pools << pool
38
38
  @host2_inventory_id = '4536bf5c-ff03-4154-a8c9-32ff4b40e40c'
39
39
 
40
+ # this host would pass our plugin queries, so it could be uploaded to the cloud.
41
+ @host3 = FactoryBot.create(
42
+ :host,
43
+ :with_subscription,
44
+ :with_content,
45
+ content_view: cv.first,
46
+ lifecycle_environment: env,
47
+ organization: env.organization
48
+ )
49
+
50
+ @host3.subscription_facet.pools << pool
51
+
40
52
  ForemanInventoryUpload::Generators::Queries.instance_variable_set(:@fact_names, nil)
41
53
 
42
54
  inventory_json = <<-INVENTORY_JSON
@@ -151,7 +163,7 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
151
163
  {
152
164
  "insights_id": "b533848e-465f-4f1a-9b2b-b71cb2d5239d",
153
165
  "rhel_machine_id": null,
154
- "subscription_manager_id": "d29bde40-348e-437c-8acf-8fa98320fc1b",
166
+ "subscription_manager_id": "#{@host3.subscription_facet.uuid}",
155
167
  "satellite_id": "d29bde40-348e-437c-8acf-8fa98320fc1b",
156
168
  "bios_uuid": "3cd5d972-cfb5-451a-8314-fd2f56629d7c",
157
169
  "ip_addresses": [
@@ -159,7 +171,7 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
159
171
  "fd6e:2298:736e::857",
160
172
  "fd6e:2298:736e:0:2c66:6101:9cc6:2b23"
161
173
  ],
162
- "fqdn": "rhel8-demo.oss-lab.net",
174
+ "fqdn": "#{@host3.fqdn}",
163
175
  "mac_addresses": [
164
176
  "6e:66:a6:fe:fc:07",
165
177
  "00:00:00:00:00:00"
@@ -271,4 +283,18 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
271
283
 
272
284
  ForemanTasks.sync_task(InventorySync::Async::InventoryFullSync, @host1.organization)
273
285
  end
286
+
287
+ test 'Should skip hosts that are not returned in query' do
288
+ assert_nil InventorySync::InventoryStatus.where(host_id: @host3.id).first
289
+
290
+ Setting[:rh_cloud_token] = 'TEST TOKEN'
291
+ InventorySync::Async::InventoryFullSync.any_instance.expects(:query_inventory).returns(@inventory)
292
+ InventorySync::Async::InventoryFullSync.any_instance.expects(:affected_host_ids).returns([@host1.id, @host2.id])
293
+ FactoryBot.create(:fact_value, fact_name: fact_names['virt::uuid'], value: '1234', host: @host2)
294
+
295
+ ForemanTasks.sync_task(InventorySync::Async::InventoryFullSync, @host1.organization)
296
+ @host2.reload
297
+
298
+ assert_nil InventorySync::InventoryStatus.where(host_id: @host3.id).first
299
+ end
274
300
  end
@@ -265,4 +265,19 @@ class InventoryHostsSyncTest < ActiveSupport::TestCase
265
265
 
266
266
  assert_equal @host2_inventory_id, @host2.insights.uuid
267
267
  end
268
+
269
+ test 'Inventory should sync empty facets list' do
270
+ empty_inventory = @inventory.deep_clone
271
+ empty_inventory['results'] = []
272
+ InventorySync::Async::InventoryHostsSync.any_instance.expects(:query_inventory).returns(empty_inventory)
273
+ InventorySync::Async::InventoryHostsSync.any_instance.expects(:plan_self_host_sync)
274
+
275
+ assert_nil @host2.insights
276
+
277
+ ForemanTasks.sync_task(InventorySync::Async::InventoryHostsSync)
278
+
279
+ @host2.reload
280
+
281
+ assert_nil @host2.insights
282
+ end
268
283
  end
@@ -0,0 +1,28 @@
1
+ require 'test_plugin_helper'
2
+
3
+ class ForemanRhCloudSelfHostTest < ActiveSupport::TestCase
4
+ setup do
5
+ # reset cached value
6
+ ForemanRhCloud.instance_variable_set(:@foreman_host, nil)
7
+ end
8
+
9
+ test 'finds host by fullname' do
10
+ @domain
11
+ @host = FactoryBot.create(:host, :managed)
12
+ ForemanRhCloud.expects(:foreman_host_name).returns(@host.name)
13
+
14
+ actual = ForemanRhCloud.foreman_host
15
+
16
+ assert_not_nil actual
17
+ end
18
+
19
+ test 'finds host by shortname' do
20
+ @host = FactoryBot.create(:host, :managed)
21
+ Host.where(name: @host.name).update_all(name: @host.shortname)
22
+ ForemanRhCloud.expects(:foreman_host_name).returns(@host.name)
23
+
24
+ actual = ForemanRhCloud.foreman_host
25
+
26
+ assert_not_nil actual
27
+ end
28
+ end
@@ -133,4 +133,23 @@ class CloudRequestForwarderTest < ActiveSupport::TestCase
133
133
  assert_equal 'GET', actual[:method]
134
134
  assert_equal params, actual[:headers][:params]
135
135
  end
136
+
137
+ test 'should forward content type correctly' do
138
+ user_agent = { :foo => :bar }
139
+ params = { :page => 5, :per_page => 42 }
140
+ ForemanRhCloud::BranchInfo.any_instance.expects(:core_app_name).returns('test_app')
141
+ ForemanRhCloud::BranchInfo.any_instance.expects(:core_app_version).returns('test_ver')
142
+
143
+ req = ActionDispatch::Request.new(
144
+ 'REQUEST_URI' => '/foo/bar',
145
+ 'REQUEST_METHOD' => 'GET',
146
+ 'HTTP_USER_AGENT' => user_agent,
147
+ 'rack.input' => ::Puma::NullIO.new,
148
+ 'action_dispatch.request.query_parameters' => params
149
+ )
150
+
151
+ actual = @forwarder.prepare_request_opts(req, 'TEST PAYLOAD', params, generate_certs_hash)
152
+
153
+ assert_match /text\/html/, actual[:headers][:content_type]
154
+ end
136
155
  end
@@ -22,7 +22,13 @@ class SliceGeneratorTest < ActiveSupport::TestCase
22
22
  location: location
23
23
  )
24
24
 
25
- @host.organization.pools << FactoryBot.create(:katello_pool, account_number: '1234', cp_id: 1)
25
+ @host.organization.pools << FactoryBot.create(
26
+ :katello_pool,
27
+ account_number: '1234',
28
+ cp_id: 1,
29
+ organization: env.organization,
30
+ subscription: FactoryBot.create(:katello_subscription, organization_id: env.organization.id)
31
+ )
26
32
  @host.interfaces.first.identifier = 'test_nic1'
27
33
  @host.save!
28
34
 
@@ -441,6 +447,18 @@ class SliceGeneratorTest < ActiveSupport::TestCase
441
447
  assert_equal 1, generator.hosts_count
442
448
  end
443
449
 
450
+ test 'excludes hosts with host_registration_insights set to false' do
451
+ @host.host_parameters << HostParameter.create(
452
+ name: 'host_registration_insights',
453
+ value: "false",
454
+ parameter_type: 'boolean'
455
+ )
456
+
457
+ count = ForemanInventoryUpload::Generators::Queries.for_org(@host.organization_id).count
458
+
459
+ assert_equal 0, count
460
+ end
461
+
444
462
  test 'shows system_memory_bytes in bytes' do
445
463
  FactoryBot.create(:fact_value, fact_name: fact_names['memory::memtotal'], value: '1', host: @host)
446
464
 
@@ -457,9 +475,23 @@ class SliceGeneratorTest < ActiveSupport::TestCase
457
475
  end
458
476
 
459
477
  test 'reports an account for hosts with multiple pools' do
460
- first_pool = @host.organization.pools.first
461
- second_pool = FactoryBot.create(:katello_pool, account_number: nil, cp_id: 2)
462
- new_org = FactoryBot.create(:organization, pools: [first_pool, second_pool])
478
+ new_org = FactoryBot.create(:organization)
479
+ first_pool = FactoryBot.create(
480
+ :katello_pool,
481
+ account_number: '5678',
482
+ cp_id: 2,
483
+ organization: new_org,
484
+ subscription: FactoryBot.create(:katello_subscription, organization_id: new_org.id)
485
+ )
486
+ second_pool = FactoryBot.create(
487
+ :katello_pool,
488
+ account_number: '9012',
489
+ cp_id: 3,
490
+ organization: new_org,
491
+ subscription: FactoryBot.create(:katello_subscription, organization_id: new_org.id)
492
+ )
493
+ new_org.pools << first_pool
494
+ new_org.pools << second_pool
463
495
 
464
496
  another_host = FactoryBot.create(
465
497
  :host,
@@ -65,14 +65,14 @@ class TagsGeneratorTest < ActiveSupport::TestCase
65
65
  test 'generates parameter tags' do
66
66
  FactoryBot.create(:setting, :name => 'include_parameter_tags', :settings_type => "boolean", :category => "Setting::RhCloud", :default => false, :value => true)
67
67
 
68
- @host.stubs(:host_inherited_params_objects).returns(
69
- [
70
- OpenStruct.new(name: 'bool_param', value: true),
71
- OpenStruct.new(name: 'false_param', value: false),
72
- OpenStruct.new(name: 'int_param', value: 1),
73
- OpenStruct.new(name: 'empty_param', value: nil),
74
- OpenStruct.new(name: 'empty_str_param', value: ''),
75
- ]
68
+ @host.stubs(:host_params).returns(
69
+ {
70
+ 'bool_param' => true,
71
+ 'false_param' => false,
72
+ 'int_param' => 1,
73
+ 'empty_param' => nil,
74
+ 'empty_str_param' => '',
75
+ }
76
76
  )
77
77
 
78
78
  generator = create_generator
@@ -87,14 +87,14 @@ class TagsGeneratorTest < ActiveSupport::TestCase
87
87
  test 'skips parameter tags if include_parameter_tags setting is off' do
88
88
  FactoryBot.create(:setting, :name => 'include_parameter_tags', :settings_type => "boolean", :category => "Setting::RhCloud", :default => false, :value => false)
89
89
 
90
- @host.stubs(:host_inherited_params_objects).returns(
91
- [
92
- OpenStruct.new(name: 'bool_param', value: true),
93
- OpenStruct.new(name: 'false_param', value: false),
94
- OpenStruct.new(name: 'int_param', value: 1),
95
- OpenStruct.new(name: 'empty_param', value: nil),
96
- OpenStruct.new(name: 'empty_str_param', value: ''),
97
- ]
90
+ @host.stubs(:host_params).returns(
91
+ {
92
+ 'bool_param' => true,
93
+ 'false_param' => false,
94
+ 'int_param' => 1,
95
+ 'empty_param' => nil,
96
+ 'empty_str_param' => '',
97
+ }
98
98
  )
99
99
 
100
100
  generator = create_generator
@@ -103,6 +103,21 @@ class TagsGeneratorTest < ActiveSupport::TestCase
103
103
  assert_equal 0, actual.count
104
104
  end
105
105
 
106
+ test 'truncates parameter tags' do
107
+ Setting[:include_parameter_tags] = true
108
+
109
+ @host.stubs(:host_params).returns(
110
+ {
111
+ 'str_param' => 'a' * 251,
112
+ }
113
+ )
114
+
115
+ generator = create_generator
116
+ actual = Hash[generator.generate_parameters]
117
+
118
+ assert_equal 'Original value exceeds 250 characters', actual['str_param']
119
+ end
120
+
106
121
  private
107
122
 
108
123
  def create_generator
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_rh_cloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.26
4
+ version: 4.0.31
5
5
  platform: ruby
6
6
  authors:
7
7
  - Foreman Red Hat Cloud team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-23 00:00:00.000000000 Z
11
+ date: 2022-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: katello
@@ -174,6 +174,7 @@ files:
174
174
  - app/views/layouts/foreman_rh_cloud/application.html.erb
175
175
  - config/Gemfile.lock.gh_test
176
176
  - config/database.yml.example
177
+ - config/package-lock.json
177
178
  - config/package-lock.json.gh_test
178
179
  - config/package-lock.json.plugin
179
180
  - config/rh_cert-api_chain.pem
@@ -255,6 +256,7 @@ files:
255
256
  - test/test_plugin_helper.rb
256
257
  - test/unit/archived_report_generator_test.rb
257
258
  - test/unit/fact_helpers_test.rb
259
+ - test/unit/foreman_rh_cloud_self_host_test.rb
258
260
  - test/unit/insights_facet_test.rb
259
261
  - test/unit/metadata_generator_test.rb
260
262
  - test/unit/rh_cloud_http_proxy_test.rb
@@ -685,6 +687,7 @@ test_files:
685
687
  - test/test_plugin_helper.rb
686
688
  - test/unit/archived_report_generator_test.rb
687
689
  - test/unit/fact_helpers_test.rb
690
+ - test/unit/foreman_rh_cloud_self_host_test.rb
688
691
  - test/unit/insights_facet_test.rb
689
692
  - test/unit/metadata_generator_test.rb
690
693
  - test/unit/rh_cloud_http_proxy_test.rb