foreman_rh_cloud 5.0.35 → 5.0.38

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_controller.rb +0 -7
  4. data/app/controllers/foreman_inventory_upload/uploads_settings_controller.rb +0 -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 +1 -1
  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/cloud_connector_announce_task.rb +44 -0
  28. data/lib/insights_cloud/async/connector_playbook_execution_reporter_task.rb +7 -1
  29. data/lib/insights_cloud/async/insights_full_sync.rb +40 -24
  30. data/lib/insights_cloud/async/insights_resolutions_sync.rb +10 -13
  31. data/lib/insights_cloud/async/insights_rules_sync.rb +27 -19
  32. data/lib/insights_cloud/async/insights_scheduled_sync.rb +1 -1
  33. data/lib/insights_cloud.rb +4 -8
  34. data/lib/inventory_sync/async/host_result.rb +3 -2
  35. data/lib/inventory_sync/async/inventory_full_sync.rb +3 -3
  36. data/lib/inventory_sync/async/inventory_hosts_sync.rb +11 -10
  37. data/lib/inventory_sync/async/inventory_self_host_sync.rb +5 -6
  38. data/lib/inventory_sync/async/query_inventory_job.rb +40 -12
  39. data/lib/tasks/insights.rake +1 -1
  40. data/package.json +6 -6
  41. data/test/controllers/insights_cloud/api/cloud_request_controller_test.rb +0 -1
  42. data/test/controllers/inventory_upload/cloud_status_controller_test.rb +0 -6
  43. data/test/jobs/cloud_connector_announce_task_test.rb +125 -0
  44. data/test/jobs/insights_full_sync_test.rb +9 -5
  45. data/test/jobs/insights_resolutions_sync_test.rb +7 -1
  46. data/test/jobs/insights_rules_sync_test.rb +8 -4
  47. data/test/jobs/inventory_full_sync_test.rb +16 -5
  48. data/test/jobs/inventory_hosts_sync_test.rb +18 -4
  49. data/test/jobs/inventory_self_host_sync_test.rb +6 -1
  50. data/test/unit/services/foreman_rh_cloud/cloud_request_forwarder_test.rb +0 -1
  51. data/test/unit/services/foreman_rh_cloud/cloud_status_service_test.rb +1 -14
  52. data/test/unit/services/foreman_rh_cloud/template_renderer_helper_test.rb +5 -1
  53. data/test/unit/services/foreman_rh_cloud/url_remediations_retriever_test.rb +3 -1
  54. data/webpack/ForemanInventoryUpload/Components/InventorySettings/InventorySettingsSelectors.js +0 -2
  55. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/CloudPingModal/index.js +1 -10
  56. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/SyncButton.js +3 -12
  57. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/__tests__/SyncButton.test.js +1 -1
  58. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/__tests__/__snapshots__/SyncButton.test.js.snap +0 -4
  59. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/index.js +0 -2
  60. data/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableSelectors.js +0 -3
  61. data/webpack/InsightsCloudSync/Components/InsightsTable/Pagination.js +16 -0
  62. data/webpack/InsightsCloudSync/Components/InsightsTable/__tests__/InsightsTableSelectors.test.js +0 -2
  63. data/webpack/InsightsCloudSync/Components/InsightsTable/__tests__/__snapshots__/InsightsTableSelectors.test.js.snap +0 -2
  64. data/webpack/InsightsCloudSync/Components/InsightsTable/__tests__/fixtures.js +0 -1
  65. data/webpack/InsightsCloudSync/InsightsCloudSync.js +1 -17
  66. data/webpack/InsightsCloudSync/InsightsCloudSync.test.js +0 -7
  67. data/webpack/InsightsCloudSync/InsightsCloudSyncConstants.js +0 -4
  68. data/webpack/InsightsCloudSync/InsightsCloudSyncHelpers.js +0 -7
  69. data/webpack/InsightsCloudSync/__snapshots__/InsightsCloudSync.test.js.snap +0 -9
  70. data/webpack/InsightsCloudSync/index.js +1 -5
  71. metadata +7 -9
  72. data/app/services/foreman_rh_cloud/cloud_auth.rb +0 -44
  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,7 +16,7 @@ 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
@@ -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.35",
3
+ "version": "5.0.38",
4
4
  "description": "Inventory Upload =============",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,14 +22,14 @@
22
22
  "url": "http://projects.theforeman.org/projects/foreman_rh_cloud/issues"
23
23
  },
24
24
  "peerDependencies": {
25
- "@theforeman/vendor": "~8.16.0"
25
+ "@theforeman/vendor": ">=8.16.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@babel/core": "~7.7.0",
29
- "@theforeman/builder": "~8.16.0",
30
- "@theforeman/stories": "~8.16.0",
31
- "@theforeman/test": "~8.16.0",
32
- "@theforeman/eslint-plugin-foreman": "~8.16.0",
29
+ "@theforeman/builder": ">=8.16.0",
30
+ "@theforeman/stories": ">=8.16.0",
31
+ "@theforeman/test": ">=8.16.0",
32
+ "@theforeman/eslint-plugin-foreman": ">=8.16.0",
33
33
  "babel-eslint": "~10.0.0",
34
34
  "eslint": "~6.7.2",
35
35
  "eslint-plugin-spellcheck": "~0.0.17",
@@ -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!
@@ -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']
@@ -0,0 +1,125 @@
1
+ require 'json'
2
+ require 'test_plugin_helper'
3
+ require 'foreman_tasks/test_helpers'
4
+ require "#{ForemanTasks::Engine.root}/test/support/dummy_dynflow_action"
5
+
6
+ class CloudConnectorAnnounceTaskTest < ActiveSupport::TestCase
7
+ include ForemanTasks::TestHelpers::WithInThreadExecutor
8
+
9
+ setup do
10
+ RemoteExecutionFeature.register(
11
+ :ansible_configure_cloud_connector,
12
+ N_('Configure Cloud Connector on given hosts'),
13
+ :description => N_('Configure Cloud Connector on given hosts'),
14
+ :proxy_selector_override => ::RemoteExecutionProxySelector::INTERNAL_PROXY
15
+ )
16
+
17
+ @job_invocation = generate_job_invocation(:ansible_configure_cloud_connector)
18
+
19
+ # reset connector feature ID cache
20
+ InsightsCloud::Async::CloudConnectorAnnounceTask.instance_variable_set(:@connector_feature_id, nil)
21
+ end
22
+
23
+ test 'It executes cloud presence announcer' do
24
+ ForemanRhCloud::CloudPresence.any_instance.expects(:announce_to_sources).times(Organization.unscoped.count)
25
+
26
+ ForemanTasks.sync_task(InsightsCloud::Async::CloudConnectorAnnounceTask, @job_invocation)
27
+ end
28
+
29
+ private
30
+
31
+ def generate_job_invocation(feature_name)
32
+ job_template = FactoryBot.build(
33
+ :job_template,
34
+ :template => 'BLEH'
35
+ )
36
+ feature = RemoteExecutionFeature.feature!(feature_name).id
37
+
38
+ job_invocation = FactoryBot.create(
39
+ :job_invocation,
40
+ remote_execution_feature_id: feature,
41
+ task_id: FactoryBot.create(:dynflow_task).id
42
+ )
43
+
44
+ job_template.template_inputs << playbook_url_input = FactoryBot.build(:template_input,
45
+ :name => 'playbook_url',
46
+ :input_type => 'user',
47
+ :required => true)
48
+ job_template.template_inputs << report_url_input = FactoryBot.build(:template_input,
49
+ :name => 'report_url',
50
+ :input_type => 'user',
51
+ :required => true)
52
+ job_template.template_inputs << correlation_id_input = FactoryBot.build(:template_input,
53
+ :name => 'correlation_id',
54
+ :input_type => 'user',
55
+ :required => true)
56
+ job_template.template_inputs << report_interval_input = FactoryBot.build(:template_input,
57
+ :name => 'report_interval',
58
+ :input_type => 'user',
59
+ :required => true)
60
+
61
+ template_invocation = FactoryBot.build(:template_invocation,
62
+ :template => job_template,
63
+ :job_invocation => job_invocation
64
+ )
65
+
66
+ template_invocation.input_values << FactoryBot.create(
67
+ :template_invocation_input_value,
68
+ :template_invocation => template_invocation,
69
+ :template_input => playbook_url_input,
70
+ :value => 'http://example.com/TEST_PLAYBOOK'
71
+ )
72
+ template_invocation.input_values << FactoryBot.create(
73
+ :template_invocation_input_value,
74
+ :template_invocation => template_invocation,
75
+ :template_input => report_url_input,
76
+ :value => 'http://example.com/TEST_REPORT'
77
+ )
78
+ template_invocation.input_values << FactoryBot.create(
79
+ :template_invocation_input_value,
80
+ :template_invocation => template_invocation,
81
+ :template_input => correlation_id_input,
82
+ :value => 'TEST_CORRELATION'
83
+ )
84
+ template_invocation.input_values << FactoryBot.create(
85
+ :template_invocation_input_value,
86
+ :template_invocation => template_invocation,
87
+ :template_input => report_interval_input,
88
+ :value => '1'
89
+ )
90
+
91
+ @host1 = FactoryBot.create(:host, :with_insights_hits, name: 'host1')
92
+ @host1.insights.uuid = 'TEST_UUID1'
93
+ @host1.insights.save!
94
+ @host2 = FactoryBot.create(:host, :with_insights_hits, name: 'host2')
95
+ @host2.insights.uuid = 'TEST_UUID2'
96
+ @host2.insights.save!
97
+
98
+ targeting = FactoryBot.create(:targeting, hosts: [@host1, @host2])
99
+ job_invocation.targeting = targeting
100
+ job_invocation.save!
101
+
102
+ job_invocation.template_invocations << FactoryBot.create(
103
+ :template_invocation,
104
+ run_host_job_task: FactoryBot.create(:dynflow_task),
105
+ host_id: @host1.id
106
+ )
107
+ job_invocation.template_invocations << FactoryBot.create(
108
+ :template_invocation,
109
+ run_host_job_task: FactoryBot.create(:dynflow_task),
110
+ host_id: @host2.id
111
+ )
112
+
113
+ fake_output = (1..5).map do |i|
114
+ { 'timestamp' => (Time.now - (5 - i)).to_f, 'output' => "#{i}\n" }
115
+ end
116
+ Support::DummyDynflowAction.any_instance.stubs(:live_output).returns(fake_output)
117
+ Support::DummyDynflowAction.any_instance.stubs(:exit_status).returns(0)
118
+
119
+ job_invocation
120
+ end
121
+
122
+ def read_jsonl(jsonl)
123
+ jsonl.lines.map { |l| JSON.parse(l) }
124
+ end
125
+ end
@@ -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