foreman_rh_cloud 5.0.34 → 5.0.37

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/api/v2/rh_cloud/cloud_request_controller.rb +1 -1
  3. data/app/controllers/foreman_inventory_upload/uploads_settings_controller.rb +0 -1
  4. data/app/controllers/insights_cloud/api/machine_telemetries_controller.rb +7 -1
  5. data/app/controllers/insights_cloud/hits_controller.rb +0 -1
  6. data/app/controllers/insights_cloud/settings_controller.rb +0 -8
  7. data/app/controllers/insights_cloud/tasks_controller.rb +1 -1
  8. data/app/models/insights_facet.rb +1 -0
  9. data/app/models/insights_hit.rb +1 -0
  10. data/app/models/insights_rule.rb +2 -0
  11. data/app/models/setting/rh_cloud.rb +1 -2
  12. data/app/services/foreman_rh_cloud/cert_auth.rb +22 -0
  13. data/app/services/foreman_rh_cloud/cloud_connector.rb +10 -1
  14. data/app/services/foreman_rh_cloud/cloud_ping_service.rb +0 -27
  15. data/app/services/foreman_rh_cloud/hit_remediations_retriever.rb +4 -0
  16. data/app/services/foreman_rh_cloud/remediations_retriever.rb +7 -3
  17. data/app/services/foreman_rh_cloud/template_renderer_helper.rb +4 -3
  18. data/app/services/foreman_rh_cloud/url_remediations_retriever.rb +6 -1
  19. data/app/views/job_templates/cloud_connector.erb +6 -0
  20. data/app/views/job_templates/rh_cloud_download_playbook.erb +5 -1
  21. data/config/routes.rb +0 -1
  22. data/db/migrate/20220321000001_add_unique_to_insights_rules.foreman_rh_cloud.rb +13 -0
  23. data/lib/foreman_inventory_upload.rb +1 -1
  24. data/lib/foreman_rh_cloud/engine.rb +14 -11
  25. data/lib/foreman_rh_cloud/version.rb +1 -1
  26. data/lib/foreman_rh_cloud.rb +0 -5
  27. data/lib/insights_cloud/async/connector_playbook_execution_reporter_task.rb +7 -1
  28. data/lib/insights_cloud/async/insights_full_sync.rb +40 -24
  29. data/lib/insights_cloud/async/insights_resolutions_sync.rb +10 -13
  30. data/lib/insights_cloud/async/insights_rules_sync.rb +27 -19
  31. data/lib/insights_cloud/async/insights_scheduled_sync.rb +5 -1
  32. data/lib/insights_cloud.rb +4 -8
  33. data/lib/inventory_sync/async/host_result.rb +3 -2
  34. data/lib/inventory_sync/async/inventory_full_sync.rb +3 -3
  35. data/lib/inventory_sync/async/inventory_hosts_sync.rb +11 -10
  36. data/lib/inventory_sync/async/inventory_self_host_sync.rb +5 -6
  37. data/lib/inventory_sync/async/query_inventory_job.rb +40 -12
  38. data/lib/tasks/insights.rake +1 -1
  39. data/package.json +1 -1
  40. data/test/controllers/insights_cloud/api/cloud_request_controller_test.rb +0 -1
  41. data/test/controllers/insights_cloud/api/machine_telemetries_controller_test.rb +16 -0
  42. data/test/controllers/inventory_upload/cloud_status_controller_test.rb +0 -6
  43. data/test/jobs/insights_full_sync_test.rb +9 -5
  44. data/test/jobs/insights_resolutions_sync_test.rb +7 -1
  45. data/test/jobs/insights_rules_sync_test.rb +8 -4
  46. data/test/jobs/inventory_full_sync_test.rb +16 -5
  47. data/test/jobs/inventory_hosts_sync_test.rb +18 -4
  48. data/test/jobs/inventory_self_host_sync_test.rb +6 -1
  49. data/test/unit/services/foreman_rh_cloud/cloud_request_forwarder_test.rb +12 -8
  50. data/test/unit/services/foreman_rh_cloud/cloud_status_service_test.rb +1 -14
  51. data/test/unit/services/foreman_rh_cloud/template_renderer_helper_test.rb +5 -1
  52. data/test/unit/services/foreman_rh_cloud/url_remediations_retriever_test.rb +3 -1
  53. data/webpack/ForemanInventoryUpload/Components/InventorySettings/InventorySettingsSelectors.js +0 -2
  54. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/CloudPingModal/index.js +1 -10
  55. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/SyncButton.js +3 -12
  56. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/__tests__/SyncButton.test.js +1 -1
  57. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/__tests__/__snapshots__/SyncButton.test.js.snap +0 -4
  58. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/index.js +0 -2
  59. data/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableSelectors.js +0 -3
  60. data/webpack/InsightsCloudSync/Components/InsightsTable/Pagination.js +16 -0
  61. data/webpack/InsightsCloudSync/Components/InsightsTable/__tests__/InsightsTableSelectors.test.js +0 -2
  62. data/webpack/InsightsCloudSync/Components/InsightsTable/__tests__/__snapshots__/InsightsTableSelectors.test.js.snap +0 -2
  63. data/webpack/InsightsCloudSync/Components/InsightsTable/__tests__/fixtures.js +0 -1
  64. data/webpack/InsightsCloudSync/InsightsCloudSync.js +1 -17
  65. data/webpack/InsightsCloudSync/InsightsCloudSync.test.js +0 -7
  66. data/webpack/InsightsCloudSync/InsightsCloudSyncConstants.js +0 -4
  67. data/webpack/InsightsCloudSync/InsightsCloudSyncHelpers.js +0 -7
  68. data/webpack/InsightsCloudSync/__snapshots__/InsightsCloudSync.test.js.snap +0 -9
  69. data/webpack/InsightsCloudSync/index.js +1 -5
  70. metadata +11 -17
  71. data/app/services/foreman_rh_cloud/cloud_auth.rb +0 -44
  72. data/config/package-lock.json +0 -41822
  73. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/CloudPingModal/index.scss +0 -5
  74. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/components/Modal.js +0 -63
  75. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/components/modal.scss +0 -20
  76. data/webpack/InsightsCloudSync/Components/NoTokenEmptyState.js +0 -79
  77. data/webpack/InsightsCloudSync/Components/__tests__/NoTokenEmptyState.test.js +0 -19
  78. data/webpack/InsightsCloudSync/Components/__tests__/__snapshots__/NoTokenEmptyState.test.js.snap +0 -225
@@ -3,18 +3,13 @@ require 'rest-client'
3
3
  module InsightsCloud
4
4
  module Async
5
5
  class InsightsRulesSync < ::Actions::EntryAction
6
- include ::ForemanRhCloud::CloudAuth
7
-
8
- def plan
9
- unless cloud_auth_available?
10
- logger.debug('Cloud authentication is not available, skipping rules sync')
11
- return
12
- end
6
+ include ::ForemanRhCloud::CertAuth
13
7
 
8
+ def plan(organizations)
14
9
  # since the tasks are not connected, we need to force sequence execution here
15
10
  # to make sure we don't run resolutions until we synced all our rules
16
11
  sequence do
17
- plan_self
12
+ plan_self(organization_ids: organizations.map(&:id))
18
13
  plan_resolutions
19
14
  end
20
15
  end
@@ -26,16 +21,20 @@ module InsightsCloud
26
21
  def run
27
22
  offset = 0
28
23
  InsightsRule.transaction do
29
- InsightsRule.delete_all
30
- loop do
31
- api_response = query_insights_rules(offset)
32
- results = RulesResult.new(api_response)
33
- logger.debug("Downloaded #{offset + results.count} of #{results.total}")
34
- write_rules_page(results.rules)
35
- offset += results.count
36
- output[:rules_count] = results.total
37
- break if offset >= results.total
24
+ organizations.each do |organization|
25
+ loop do
26
+ api_response = query_insights_rules(offset, organization)
27
+ results = RulesResult.new(api_response)
28
+ logger.debug("Downloaded #{offset + results.count} of #{results.total}")
29
+ write_rules_page(results.rules)
30
+ offset += results.count
31
+ output[:rules_count] = results.total
32
+ break if offset >= results.total
33
+ end
38
34
  end
35
+
36
+ # Remove all rules that do not have hits associated with them
37
+ cleanup_rules
39
38
  end
40
39
  end
41
40
 
@@ -45,8 +44,9 @@ module InsightsCloud
45
44
 
46
45
  private
47
46
 
48
- def query_insights_rules(offset)
47
+ def query_insights_rules(offset, organization)
49
48
  rules_response = execute_cloud_request(
49
+ organization: organization,
50
50
  method: :get,
51
51
  url: InsightsCloud.rules_url(offset: offset)
52
52
  )
@@ -57,7 +57,7 @@ module InsightsCloud
57
57
  def write_rules_page(rules)
58
58
  rules_attributes = rules.map { |rule| to_rule_hash(rule) }
59
59
 
60
- InsightsRule.create(rules_attributes)
60
+ InsightsRule.upsert_all(rules_attributes, unique_by: :rule_id) unless rules_attributes.empty?
61
61
  end
62
62
 
63
63
  def to_rule_hash(rule_hash)
@@ -76,9 +76,17 @@ module InsightsCloud
76
76
  }
77
77
  end
78
78
 
79
+ def cleanup_rules
80
+ InsightsRule.left_outer_joins(:hits).where(insights_hits: { id: nil }).delete_all
81
+ end
82
+
79
83
  def rescue_strategy_for_self
80
84
  Dynflow::Action::Rescue::Fail
81
85
  end
86
+
87
+ def organizations
88
+ @organizations ||= Organization.where(id: input[:organization_ids])
89
+ end
82
90
  end
83
91
  end
84
92
  end
@@ -16,12 +16,16 @@ module InsightsCloud
16
16
  end
17
17
 
18
18
  def plan_full_sync
19
- plan_action InsightsFullSync
19
+ plan_action(InsightsFullSync, Organization.unscoped.all)
20
20
  end
21
21
 
22
22
  def rescue_strategy_for_self
23
23
  Dynflow::Action::Rescue::Fail
24
24
  end
25
+
26
+ def logger
27
+ action_logger
28
+ end
25
29
  end
26
30
  end
27
31
  end
@@ -3,26 +3,22 @@ module InsightsCloud
3
3
  ADVISOR = 'advisor'
4
4
  VULNERABILITY = 'vulnerability'
5
5
  PATCH = 'patch'
6
-
7
- def self.system_url(subsystem, host_uuid)
8
- ForemanRhCloud.base_url + "/insights/#{subsystem}/systems/#{host_uuid}"
9
- end
10
6
  end
11
7
 
12
8
  def self.hits_export_url
13
- ForemanRhCloud.base_url + '/api/insights/v1/export/hits/'
9
+ ForemanRhCloud.cert_base_url + '/api/insights/v1/export/hits/'
14
10
  end
15
11
 
16
12
  def self.rules_url(limit: ForemanRhCloud.query_limit, offset: 0)
17
- ForemanRhCloud.base_url + "/api/insights/v1/rule/?impacting=true&rule_status=enabled&has_playbook=true&limit=#{limit}&offset=#{offset}"
13
+ ForemanRhCloud.cert_base_url + "/api/insights/v1/rule/?impacting=true&rule_status=enabled&has_playbook=true&limit=#{limit}&offset=#{offset}"
18
14
  end
19
15
 
20
16
  def self.resolutions_url
21
- ForemanRhCloud.base_url + '/api/remediations/v1/resolutions'
17
+ ForemanRhCloud.cert_base_url + '/api/remediations/v1/resolutions'
22
18
  end
23
19
 
24
20
  def self.playbook_url
25
- ForemanRhCloud.base_url + '/api/remediations/v1/playbook'
21
+ ForemanRhCloud.cert_base_url + '/api/remediations/v1/playbook'
26
22
  end
27
23
 
28
24
  def self.remediation_rule_id(rule_id)
@@ -1,9 +1,10 @@
1
1
  module InventorySync
2
2
  module Async
3
3
  class HostResult
4
- attr_reader :uuid_by_fqdn
4
+ attr_reader :uuid_by_fqdn, :organization
5
5
 
6
- def initialize(result)
6
+ def initialize(result, organization)
7
+ @organization = organization
7
8
  @total = result['total']
8
9
  @count = result['count']
9
10
  @page = result['page']
@@ -5,12 +5,12 @@ module InventorySync
5
5
  set_callback :step, :around, :update_statuses_batch
6
6
 
7
7
  def plan(organization)
8
- unless cloud_auth_available?
8
+ unless cert_auth_available?(organization)
9
9
  logger.debug('Cloud authentication is not available, skipping inventory hosts sync')
10
10
  return
11
11
  end
12
12
 
13
- plan_self(organization_id: organization.id)
13
+ super(organization)
14
14
  end
15
15
 
16
16
  def setup_statuses
@@ -70,7 +70,7 @@ module InventorySync
70
70
 
71
71
  def affected_host_ids
72
72
  ForemanInventoryUpload::Generators::Queries.for_slice(
73
- Host.unscoped.where(organization: input[:organization_id])
73
+ Host.unscoped.where(organization: organizations)
74
74
  ).pluck(:id)
75
75
  end
76
76
  end
@@ -4,14 +4,9 @@ module InventorySync
4
4
  set_callback :iteration, :around, :setup_facet_transaction
5
5
  set_callback :step, :around, :create_facets
6
6
 
7
- def plan
8
- unless cloud_auth_available?
9
- logger.debug('Cloud authentication is not available, skipping inventory hosts sync')
10
- return
11
- end
12
-
7
+ def plan(organizations)
13
8
  # by default the tasks will be executed concurrently
14
- plan_self
9
+ super(organizations)
15
10
  plan_self_host_sync
16
11
  end
17
12
 
@@ -24,7 +19,7 @@ module InventorySync
24
19
  def create_facets
25
20
  # get the results from the event
26
21
  results = yield
27
- add_missing_insights_facets(results.host_uuids)
22
+ add_missing_insights_facets(results.organization, results.host_uuids)
28
23
  results
29
24
  end
30
25
 
@@ -34,8 +29,10 @@ module InventorySync
34
29
 
35
30
  private
36
31
 
37
- def add_missing_insights_facets(uuids_hash)
38
- all_facets = uuids_hash.map do |host_id, uuid|
32
+ def add_missing_insights_facets(organization, uuids_hash)
33
+ # Filter out hosts that belong to different organization (although they are visible by the query)
34
+ unrelated_hosts = Host.where.not(organization_id: organization.id).where(id: uuids_hash.keys).pluck(:id)
35
+ all_facets = uuids_hash.except(unrelated_hosts).map do |host_id, uuid|
39
36
  {
40
37
  host_id: host_id,
41
38
  uuid: uuid,
@@ -48,6 +45,10 @@ module InventorySync
48
45
  def plan_self_host_sync
49
46
  plan_action InventorySync::Async::InventorySelfHostSync
50
47
  end
48
+
49
+ def action_name
50
+ 'inventory hosts sync'
51
+ end
51
52
  end
52
53
  end
53
54
  end
@@ -4,12 +4,7 @@ module InventorySync
4
4
  set_callback :step, :around, :create_facets
5
5
 
6
6
  def plan
7
- unless cloud_auth_available?
8
- logger.debug('Cloud authentication is not available, skipping self host sync')
9
- return
10
- end
11
-
12
- plan_self
7
+ super(ForemanRhCloud.foreman_host.organization)
13
8
  end
14
9
 
15
10
  def create_facets
@@ -38,6 +33,10 @@ module InventorySync
38
33
  def request_url
39
34
  ForemanInventoryUpload.inventory_self_url
40
35
  end
36
+
37
+ def action_name
38
+ 'self host sync'
39
+ end
41
40
  end
42
41
  end
43
42
  end
@@ -4,32 +4,52 @@ module InventorySync
4
4
  module Async
5
5
  class QueryInventoryJob < ::Actions::EntryAction
6
6
  include ActiveSupport::Callbacks
7
- include ::ForemanRhCloud::CloudAuth
7
+ include ::ForemanRhCloud::CertAuth
8
8
 
9
9
  define_callbacks :iteration, :step
10
10
 
11
+ def plan(organizations, **params)
12
+ actual_params = params.merge(
13
+ {
14
+ organization_ids: Array(organizations).map(&:id),
15
+ }
16
+ )
17
+
18
+ plan_self(actual_params)
19
+ end
20
+
11
21
  def run
12
22
  run_callbacks :iteration do
13
- page = 1
14
- loop do
15
- api_response = query_inventory(page)
16
- results = HostResult.new(api_response)
17
- logger.debug("Downloaded cloud inventory data: #{results.percentage}%")
18
-
19
- run_callbacks :step do
20
- results
23
+ organizations.each do |organization|
24
+ if !cert_auth_available?(organization) || organization.manifest_expired?
25
+ logger.debug("Subscription manifest not available, skipping #{action_name} for organization #{organization.name}")
26
+ next
21
27
  end
22
28
 
23
- page += 1
24
- break if results.last?
29
+ logger.debug("Executing #{action_name} for organization #{organization.name}")
30
+
31
+ page = 1
32
+ loop do
33
+ api_response = query_inventory(organization, page)
34
+ results = HostResult.new(api_response, organization)
35
+ logger.debug("Downloaded cloud inventory data: #{results.percentage}%")
36
+
37
+ run_callbacks :step do
38
+ results
39
+ end
40
+
41
+ page += 1
42
+ break if results.last?
43
+ end
25
44
  end
26
45
  end
27
46
  end
28
47
 
29
48
  private
30
49
 
31
- def query_inventory(page = 1)
50
+ def query_inventory(organization, page = 1)
32
51
  hosts_inventory_response = execute_cloud_request(
52
+ organization: organization,
33
53
  method: :get,
34
54
  url: request_url,
35
55
  headers: {
@@ -54,6 +74,14 @@ module InventorySync
54
74
  def rescue_strategy_for_self
55
75
  Dynflow::Action::Rescue::Fail
56
76
  end
77
+
78
+ def organizations
79
+ @organizations ||= Organization.where(id: input[:organization_ids])
80
+ end
81
+
82
+ def action_name
83
+ 'inventory query'
84
+ end
57
85
  end
58
86
  end
59
87
  end
@@ -1,7 +1,7 @@
1
1
  namespace :rh_cloud_insights do
2
2
  desc "Synchronize Insights hosts hits"
3
3
  task sync: [:environment, 'dynflow:client'] do
4
- ForemanTasks.sync_task(InsightsCloud::Async::InsightsFullSync)
4
+ ForemanTasks.sync_task(InsightsCloud::Async::InsightsFullSync, Organization.unscoped.all)
5
5
  puts "Synchronized Insights hosts hits data"
6
6
  end
7
7
 
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foreman_rh_cloud",
3
- "version": "5.0.34",
3
+ "version": "5.0.37",
4
4
  "description": "Inventory Upload =============",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -19,7 +19,6 @@ module InsightsCloud::Api
19
19
  end
20
20
 
21
21
  test 'Starts playbook run for correct directive' do
22
- Setting[:rh_cloud_token] = 'MOCK_TOKEN'
23
22
  host1 = FactoryBot.create(:host, :with_insights_hits)
24
23
  host1.insights.uuid = 'TEST_UUID1'
25
24
  host1.insights.save!
@@ -60,6 +60,18 @@ module InsightsCloud::Api
60
60
  assert_equal x_rh_insights_request_id, @response.headers['x_rh_insights_request_id']
61
61
  end
62
62
 
63
+ test "should set etag header to response from cloud" do
64
+ etag = '12345'
65
+ req = RestClient::Request.new(:method => 'GET', :url => 'http://test.theforeman.org', :headers => { "If-None-Match": etag})
66
+ net_http_resp = Net::HTTPResponse.new(1.0, 200, "OK")
67
+ net_http_resp[Rack::ETAG] = etag
68
+ res = RestClient::Response.create(@body, net_http_resp, req)
69
+ ::ForemanRhCloud::CloudRequestForwarder.any_instance.stubs(:forward_request).returns(res)
70
+
71
+ get :forward_request, params: { "path" => "static/v1/release/insights-core.egg" }
72
+ assert_equal etag, @response.headers[Rack::ETAG]
73
+ end
74
+
63
75
  test "should handle failed authentication to cloud" do
64
76
  net_http_resp = Net::HTTPResponse.new(1.0, 401, "Unauthorized")
65
77
  res = RestClient::Response.create(@body, net_http_resp, @http_req)
@@ -116,6 +128,10 @@ module InsightsCloud::Api
116
128
  test 'should get branch info' do
117
129
  get :branch_info
118
130
 
131
+ res = JSON.parse(@response.body)
132
+
133
+ assert_not_equal 0, res['labels'].find { |l| l['key'] == 'hostgroup' }.count
134
+
119
135
  assert_response :success
120
136
  end
121
137
 
@@ -10,10 +10,6 @@ class CloudStatusControllerTest < ActionController::TestCase
10
10
  User.stubs(:current).returns(user)
11
11
  user.stubs(:my_organizations).returns(organizations)
12
12
 
13
- ForemanRhCloud::CloudPingService::TokenPing.any_instance.expects(:execute_cloud_request).returns(
14
- RestClient::Response.new('TEST RESPONSE')
15
- )
16
-
17
13
  setup_certs_expectation do
18
14
  ForemanRhCloud::CloudPingService::CertPing.any_instance.expects(:candlepin_id_cert).with { |actual| actual.id == organizations[0].id }
19
15
  end
@@ -36,8 +32,6 @@ class CloudStatusControllerTest < ActionController::TestCase
36
32
  assert_response :success
37
33
  actual = JSON.parse(response.body)
38
34
  assert_not_nil (actual_ping = actual['ping'])
39
- assert actual_ping['token_auth']['success']
40
- assert_nil actual_ping['token_auth']['error']
41
35
  assert actual_ping['cert_auth'][0]['success']
42
36
  assert_nil actual_ping['cert_auth'][0]['error']
43
37
  assert actual_ping['cert_auth'][1]['success']
@@ -3,11 +3,15 @@ require 'foreman_tasks/test_helpers'
3
3
 
4
4
  class InsightsFullSyncTest < ActiveSupport::TestCase
5
5
  include ForemanTasks::TestHelpers::WithInThreadExecutor
6
+ include MockCerts
6
7
 
7
8
  setup do
8
9
  InsightsCloud::Async::InsightsFullSync.any_instance.stubs(:plan_rules_sync)
9
10
  InsightsCloud::Async::InsightsFullSync.any_instance.stubs(:plan_notifications)
10
- Setting[:rh_cloud_token] = 'MOCK_TOKEN'
11
+
12
+ setup_certs_expectation do
13
+ InsightsCloud::Async::InsightsFullSync.any_instance.stubs(:candlepin_id_cert)
14
+ end
11
15
 
12
16
  uuid1 = 'accdf444-5628-451d-bf3e-cf909ad72756'
13
17
  @host1 = FactoryBot.create(:host, :managed, name: 'host1')
@@ -66,7 +70,7 @@ class InsightsFullSyncTest < ActiveSupport::TestCase
66
70
  InsightsCloud::Async::InsightsFullSync.any_instance.expects(:plan_hosts_sync)
67
71
  InsightsCloud::Async::InsightsFullSync.any_instance.expects(:plan_rules_sync)
68
72
  InsightsCloud::Async::InsightsFullSync.any_instance.expects(:plan_notifications)
69
- ForemanTasks.sync_task(InsightsCloud::Async::InsightsFullSync)
73
+ ForemanTasks.sync_task(InsightsCloud::Async::InsightsFullSync, [@host1.organization, @host2.organization])
70
74
 
71
75
  @host1.reload
72
76
  @host2.reload
@@ -80,9 +84,9 @@ class InsightsFullSyncTest < ActiveSupport::TestCase
80
84
 
81
85
  InsightsCloud::Async::InsightsFullSync.any_instance.stubs(:plan_hosts_sync)
82
86
 
83
- ForemanTasks.sync_task(InsightsCloud::Async::InsightsFullSync)
87
+ ForemanTasks.sync_task(InsightsCloud::Async::InsightsFullSync, [@host1.organization, @host2.organization])
84
88
  # Invoke again
85
- ForemanTasks.sync_task(InsightsCloud::Async::InsightsFullSync)
89
+ ForemanTasks.sync_task(InsightsCloud::Async::InsightsFullSync, [@host1.organization, @host2.organization])
86
90
 
87
91
  @host1.reload
88
92
  @host2.reload
@@ -114,7 +118,7 @@ class InsightsFullSyncTest < ActiveSupport::TestCase
114
118
  InsightsCloud::Async::InsightsFullSync.any_instance.stubs(:plan_hosts_sync)
115
119
  InsightsCloud::Async::InsightsFullSync.any_instance.expects(:query_insights_hits).returns(hits)
116
120
 
117
- ForemanTasks.sync_task(InsightsCloud::Async::InsightsFullSync)
121
+ ForemanTasks.sync_task(InsightsCloud::Async::InsightsFullSync, [@host1.organization, @host2.organization])
118
122
 
119
123
  @host1.reload
120
124
  @host2.reload
@@ -3,6 +3,7 @@ require 'foreman_tasks/test_helpers'
3
3
 
4
4
  class InsightsResolutionsSyncTest < ActiveSupport::TestCase
5
5
  include ForemanTasks::TestHelpers::WithInThreadExecutor
6
+ include MockCerts
6
7
 
7
8
  setup do
8
9
  @resolutions = {
@@ -63,7 +64,12 @@ class InsightsResolutionsSyncTest < ActiveSupport::TestCase
63
64
  }
64
65
 
65
66
  @rule = FactoryBot.create(:insights_rule, rule_id: 'network_tcp_connection_hang|NETWORK_TCP_CONNECTION_HANG_WARN')
66
- Setting[:rh_cloud_token] = 'MOCK_TOKEN'
67
+
68
+ setup_certs_expectation do
69
+ InsightsCloud::Async::InsightsResolutionsSync.any_instance.stubs(:candlepin_id_cert)
70
+ end
71
+
72
+ Organization.any_instance.stubs(:manifest_expired?).returns(false)
67
73
  end
68
74
 
69
75
  test 'Resolutions data is replaced with data from cloud' do
@@ -112,13 +112,14 @@ class InsightsRulesSyncTest < ActiveSupport::TestCase
112
112
  @hit = FactoryBot.create(:insights_hit, host_id: @host.id)
113
113
 
114
114
  InsightsCloud::Async::InsightsRulesSync.any_instance.stubs(:plan_resolutions)
115
- Setting[:rh_cloud_token] = 'MOCK_TOKEN'
116
115
  end
117
116
 
118
117
  test 'Hits data is replaced with data from cloud' do
119
- InsightsCloud::Async::InsightsRulesSync.any_instance.expects(:query_insights_rules).returns(@rules)
118
+ InsightsCloud::Async::InsightsRulesSync.any_instance.expects(:query_insights_rules).returns(@rules).times(Organization.count)
119
+ # do not cleanup unused rules for tests
120
+ InsightsCloud::Async::InsightsRulesSync.any_instance.stubs(:cleanup_rules)
120
121
 
121
- ForemanTasks.sync_task(InsightsCloud::Async::InsightsRulesSync)
122
+ ForemanTasks.sync_task(InsightsCloud::Async::InsightsRulesSync, Organization.all)
122
123
  @hit.reload
123
124
 
124
125
  assert_equal 2, InsightsRule.all.count
@@ -197,7 +198,10 @@ class InsightsRulesSyncTest < ActiveSupport::TestCase
197
198
  InsightsCloud::Async::InsightsRulesSync.any_instance.
198
199
  stubs(:query_insights_rules).returns(@rules).then.returns(@last_rule)
199
200
 
200
- ForemanTasks.sync_task(InsightsCloud::Async::InsightsRulesSync)
201
+ # do not cleanup unused rules for tests
202
+ InsightsCloud::Async::InsightsRulesSync.any_instance.stubs(:cleanup_rules)
203
+
204
+ ForemanTasks.sync_task(InsightsCloud::Async::InsightsRulesSync, Organization.all)
201
205
 
202
206
  assert_equal 3, InsightsRule.all.count
203
207
  end
@@ -3,10 +3,14 @@ require 'foreman_tasks/test_helpers'
3
3
 
4
4
  class InventoryFullSyncTest < ActiveSupport::TestCase
5
5
  include ForemanTasks::TestHelpers::WithInThreadExecutor
6
+ include MockCerts
6
7
 
7
8
  setup do
8
9
  User.current = User.find_by(login: 'secret_admin')
9
10
 
11
+ InventorySync::Async::InventoryFullSync.any_instance.stubs(:plan_self_host_sync)
12
+ Organization.any_instance.stubs(:manifest_expired?).returns(false)
13
+
10
14
  env = FactoryBot.create(:katello_k_t_environment)
11
15
  cv = env.content_views << FactoryBot.create(:katello_content_view, organization: env.organization)
12
16
 
@@ -254,7 +258,10 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
254
258
  end
255
259
 
256
260
  test 'Host status should be SYNC for inventory hosts' do
257
- Setting[:rh_cloud_token] = 'TEST TOKEN'
261
+ setup_certs_expectation do
262
+ InventorySync::Async::InventoryFullSync.any_instance.stubs(:candlepin_id_cert)
263
+ end
264
+
258
265
  InventorySync::Async::InventoryFullSync.any_instance.expects(:query_inventory).returns(@inventory)
259
266
 
260
267
  ForemanTasks.sync_task(InventorySync::Async::InventoryFullSync, @host2.organization)
@@ -266,7 +273,9 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
266
273
  end
267
274
 
268
275
  test 'Host status should be DISCONNECT for hosts that are not returned from cloud' do
269
- Setting[:rh_cloud_token] = 'TEST TOKEN'
276
+ setup_certs_expectation do
277
+ InventorySync::Async::InventoryFullSync.any_instance.stubs(:candlepin_id_cert)
278
+ end
270
279
  InventorySync::Async::InventoryFullSync.any_instance.expects(:query_inventory).returns(@inventory)
271
280
  FactoryBot.create(:fact_value, fact_name: fact_names['virt::uuid'], value: '1234', host: @host2)
272
281
 
@@ -276,8 +285,8 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
276
285
  assert_equal InventorySync::InventoryStatus::DISCONNECT, InventorySync::InventoryStatus.where(host_id: @host1.id).first.status
277
286
  end
278
287
 
279
- test 'Task should be aborted if token is not present' do
280
- Setting[:rh_cloud_token] = ''
288
+ test 'Task should be aborted if manifest is not present' do
289
+ InventorySync::Async::InventoryFullSync.any_instance.expects(:upstream_owner).returns(nil)
281
290
 
282
291
  InventorySync::Async::InventoryFullSync.any_instance.expects(:plan_self).never
283
292
 
@@ -287,7 +296,9 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
287
296
  test 'Should skip hosts that are not returned in query' do
288
297
  assert_nil InventorySync::InventoryStatus.where(host_id: @host3.id).first
289
298
 
290
- Setting[:rh_cloud_token] = 'TEST TOKEN'
299
+ setup_certs_expectation do
300
+ InventorySync::Async::InventoryFullSync.any_instance.stubs(:candlepin_id_cert)
301
+ end
291
302
  InventorySync::Async::InventoryFullSync.any_instance.expects(:query_inventory).returns(@inventory)
292
303
  InventorySync::Async::InventoryFullSync.any_instance.expects(:affected_host_ids).returns([@host1.id, @host2.id])
293
304
  FactoryBot.create(:fact_value, fact_name: fact_names['virt::uuid'], value: '1234', host: @host2)
@@ -3,10 +3,10 @@ require 'foreman_tasks/test_helpers'
3
3
 
4
4
  class InventoryHostsSyncTest < ActiveSupport::TestCase
5
5
  include ForemanTasks::TestHelpers::WithInThreadExecutor
6
+ include MockCerts
6
7
 
7
8
  setup do
8
9
  User.current = User.find_by(login: 'secret_admin')
9
- Setting[:rh_cloud_token] = 'MOCK_TOKEN'
10
10
 
11
11
  env = FactoryBot.create(:katello_k_t_environment)
12
12
  cv = env.content_views << FactoryBot.create(:katello_content_view, organization: env.organization)
@@ -40,6 +40,8 @@ class InventoryHostsSyncTest < ActiveSupport::TestCase
40
40
 
41
41
  ForemanInventoryUpload::Generators::Queries.instance_variable_set(:@fact_names, nil)
42
42
 
43
+ Organization.any_instance.stubs(:manifest_expired?).returns(false)
44
+
43
45
  inventory_json = <<-INVENTORY_JSON
44
46
  {
45
47
  "total": 3,
@@ -246,9 +248,13 @@ class InventoryHostsSyncTest < ActiveSupport::TestCase
246
248
  InventorySync::Async::InventoryHostsSync.any_instance.expects(:query_inventory).returns(@inventory)
247
249
  InventorySync::Async::InventoryHostsSync.any_instance.expects(:plan_self_host_sync)
248
250
 
251
+ setup_certs_expectation do
252
+ InventorySync::Async::InventoryHostsSync.any_instance.stubs(:candlepin_id_cert)
253
+ end
254
+
249
255
  @host2.build_insights.save
250
256
 
251
- ForemanTasks.sync_task(InventorySync::Async::InventoryHostsSync)
257
+ ForemanTasks.sync_task(InventorySync::Async::InventoryHostsSync, [@host1.organization, @host2.organization])
252
258
 
253
259
  @host2.reload
254
260
 
@@ -259,7 +265,11 @@ class InventoryHostsSyncTest < ActiveSupport::TestCase
259
265
  InventorySync::Async::InventoryHostsSync.any_instance.expects(:query_inventory).returns(@inventory)
260
266
  InventorySync::Async::InventoryHostsSync.any_instance.expects(:plan_self_host_sync)
261
267
 
262
- ForemanTasks.sync_task(InventorySync::Async::InventoryHostsSync)
268
+ setup_certs_expectation do
269
+ InventorySync::Async::InventoryHostsSync.any_instance.stubs(:candlepin_id_cert)
270
+ end
271
+
272
+ ForemanTasks.sync_task(InventorySync::Async::InventoryHostsSync, [@host1.organization, @host2.organization])
263
273
 
264
274
  @host2.reload
265
275
 
@@ -272,9 +282,13 @@ class InventoryHostsSyncTest < ActiveSupport::TestCase
272
282
  InventorySync::Async::InventoryHostsSync.any_instance.expects(:query_inventory).returns(empty_inventory)
273
283
  InventorySync::Async::InventoryHostsSync.any_instance.expects(:plan_self_host_sync)
274
284
 
285
+ setup_certs_expectation do
286
+ InventorySync::Async::InventoryHostsSync.any_instance.stubs(:candlepin_id_cert)
287
+ end
288
+
275
289
  assert_nil @host2.insights
276
290
 
277
- ForemanTasks.sync_task(InventorySync::Async::InventoryHostsSync)
291
+ ForemanTasks.sync_task(InventorySync::Async::InventoryHostsSync, [@host1.organization, @host2.organization])
278
292
 
279
293
  @host2.reload
280
294
 
@@ -3,10 +3,15 @@ require 'foreman_tasks/test_helpers'
3
3
 
4
4
  class InventorySelfHostSyncTest < ActiveSupport::TestCase
5
5
  include ForemanTasks::TestHelpers::WithInThreadExecutor
6
+ include MockCerts
6
7
 
7
8
  setup do
8
9
  User.current = User.find_by(login: 'secret_admin')
9
- Setting[:rh_cloud_token] = 'MOCK_TOKEN'
10
+
11
+ setup_certs_expectation do
12
+ InventorySync::Async::InventorySelfHostSync.any_instance.stubs(:candlepin_id_cert)
13
+ end
14
+ Organization.any_instance.stubs(:manifest_expired?).returns(false)
10
15
 
11
16
  # this host would pass our plugin queries, so it could be uploaded to the cloud.
12
17
  @host1 = FactoryBot.create(:host)