foreman_rh_cloud 4.0.22 → 4.0.25

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/api/v2/rh_cloud/inventory_controller.rb +50 -0
  3. data/app/controllers/concerns/inventory_upload/report_actions.rb +26 -0
  4. data/app/controllers/concerns/inventory_upload/task_actions.rb +25 -0
  5. data/app/controllers/foreman_inventory_upload/reports_controller.rb +3 -1
  6. data/app/controllers/foreman_inventory_upload/tasks_controller.rb +5 -13
  7. data/app/controllers/foreman_inventory_upload/uploads_controller.rb +4 -4
  8. data/app/controllers/insights_cloud/api/machine_telemetries_controller.rb +6 -3
  9. data/app/controllers/insights_cloud/hits_controller.rb +7 -3
  10. data/app/helpers/foreman_insights_host_helper.rb +19 -0
  11. data/app/models/insights_client_report_status.rb +11 -22
  12. data/app/services/foreman_rh_cloud/cloud_auth.rb +4 -0
  13. data/app/services/foreman_rh_cloud/cloud_request_forwarder.rb +0 -8
  14. data/app/services/foreman_rh_cloud/insights_status_cleaner.rb +17 -0
  15. data/app/services/foreman_rh_cloud/remediations_retriever.rb +5 -0
  16. data/config/package-lock.json.plugin +30931 -0
  17. data/config/routes.rb +19 -0
  18. data/db/migrate/20210720000001_remove_old_insights_statuses.foreman_rh_cloud.rb +6 -0
  19. data/lib/foreman_inventory_upload.rb +9 -1
  20. data/lib/foreman_inventory_upload/generators/queries.rb +1 -0
  21. data/lib/foreman_inventory_upload/generators/slice.rb +1 -0
  22. data/lib/foreman_inventory_upload/generators/tags.rb +3 -1
  23. data/lib/foreman_rh_cloud/engine.rb +17 -10
  24. data/lib/foreman_rh_cloud/version.rb +1 -1
  25. data/lib/insights_cloud/async/insights_client_status_aging.rb +23 -0
  26. data/lib/insights_cloud/async/insights_full_sync.rb +5 -0
  27. data/lib/insights_cloud/async/insights_resolutions_sync.rb +9 -0
  28. data/lib/insights_cloud/async/insights_rules_sync.rb +5 -0
  29. data/lib/inventory_sync/async/host_result.rb +4 -0
  30. data/lib/inventory_sync/async/inventory_full_sync.rb +5 -0
  31. data/lib/inventory_sync/async/inventory_hosts_sync.rb +21 -2
  32. data/lib/inventory_sync/async/inventory_scheduled_sync.rb +4 -0
  33. data/lib/inventory_sync/async/inventory_self_host_sync.rb +39 -0
  34. data/lib/inventory_sync/async/query_inventory_job.rb +5 -1
  35. data/lib/tasks/insights.rake +15 -0
  36. data/lib/tasks/rh_cloud_inventory.rake +2 -1
  37. data/package.json +1 -1
  38. data/test/controllers/insights_cloud/api/machine_telemetries_controller_test.rb +41 -0
  39. data/test/controllers/inventory_upload/api/inventory_controller_test.rb +53 -0
  40. data/test/jobs/insights_client_status_aging_test.rb +33 -0
  41. data/test/jobs/insights_full_sync_test.rb +1 -0
  42. data/test/jobs/insights_resolutions_sync_test.rb +1 -0
  43. data/test/jobs/insights_rules_sync_test.rb +1 -0
  44. data/test/jobs/inventory_full_sync_test.rb +10 -0
  45. data/test/jobs/inventory_hosts_sync_test.rb +268 -0
  46. data/test/jobs/inventory_scheduled_sync_test.rb +22 -0
  47. data/test/jobs/inventory_self_host_sync_test.rb +104 -0
  48. data/test/models/insights_client_report_status_test.rb +70 -72
  49. data/test/test_plugin_helper.rb +2 -0
  50. data/test/unit/services/foreman_rh_cloud/cloud_request_forwarder_test.rb +3 -3
  51. data/test/unit/services/foreman_rh_cloud/insights_status_cleaner_test.rb +31 -0
  52. data/test/unit/services/foreman_rh_cloud/template_renderer_helper_test.rb +1 -0
  53. data/test/unit/slice_generator_test.rb +15 -0
  54. data/test/unit/tags_generator_test.rb +41 -0
  55. data/webpack/ForemanInventoryUpload/Components/AccountList/Components/EmptyState/EmptyState.js +1 -1
  56. data/webpack/ForemanInventoryUpload/Components/AccountList/Components/EmptyState/__tests__/__snapshots__/EmptyState.test.js.snap +1 -2
  57. data/webpack/ForemanInventoryUpload/Components/FileDownload/FileDownload.js +3 -1
  58. data/webpack/ForemanInventoryUpload/Components/FileDownload/__tests__/__snapshots__/FileDownload.test.js.snap +2 -1
  59. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/InventoryFilter.js +1 -2
  60. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/InventoryFilterConstants.js +3 -1
  61. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/PageDescription/PageDescription.js +12 -1
  62. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/PageDescription/__tests__/__snapshots__/PageDescription.test.js.snap +11 -0
  63. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/components/Modal.js +1 -1
  64. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/SyncButton/components/Toast.js +2 -2
  65. data/webpack/ForemanInventoryUpload/Components/StatusChart/StatusChart.js +4 -3
  66. data/webpack/ForemanInventoryUpload/SubscriptionsPageExtension/InventoryAutoUpload/InventoryAutoUpload.js +3 -1
  67. data/webpack/InsightsCloudSync/Components/InsightsSyncSwitcher/InsightsSyncSwitcher.js +1 -1
  68. data/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTable.js +2 -1
  69. data/webpack/InsightsCloudSync/Components/InsightsTable/InsightsTableConstants.js +14 -0
  70. data/webpack/InsightsCloudSync/Components/InsightsTable/SelectAllAlert.js +1 -1
  71. data/webpack/InsightsCloudSync/Components/InsightsTable/__tests__/__snapshots__/InsightsTable.test.js.snap +2 -4
  72. data/webpack/InsightsCloudSync/Components/RemediationModal/RemediationModal.scss +14 -0
  73. data/webpack/InsightsHostDetailsTab/InsightsTab.js +3 -2
  74. data/webpack/InsightsHostDetailsTab/InsightsTab.scss +4 -4
  75. data/webpack/InsightsHostDetailsTab/components/ListItem/ListItem.js +9 -7
  76. metadata +24 -4
  77. data/app/subscribers/foreman_rh_cloud/insights_subscriber.rb +0 -9
@@ -0,0 +1,53 @@
1
+ require 'test_plugin_helper'
2
+
3
+ module InventoryUpload::Api
4
+ class InventoryControllerTest < ActionController::TestCase
5
+ tests Api::V2::RhCloud::InventoryController
6
+
7
+ setup do
8
+ @test_org = FactoryBot.create(:organization)
9
+ end
10
+
11
+ test 'Starts report generation' do
12
+ Api::V2::RhCloud::InventoryController.any_instance
13
+ .expects(:start_report_generation)
14
+ .with(@test_org.id.to_s)
15
+
16
+ post :generate_report, params: { organization_id: @test_org.id }
17
+
18
+ assert_response :success
19
+ end
20
+
21
+ test 'Starts inventory sync action' do
22
+ test_task = FactoryBot.create(:some_task)
23
+
24
+ Api::V2::RhCloud::InventoryController.any_instance
25
+ .expects(:start_inventory_sync)
26
+ .with() { |actual_org| @test_org.id == actual_org.id }
27
+ .returns(test_task)
28
+
29
+ post :sync_inventory_status, params: { organization_id: @test_org.id }
30
+
31
+ assert_response :success
32
+
33
+ assert_not_nil(actual_task = @response.parsed_body['task'])
34
+ assert_equal test_task.id.to_s, actual_task['id']
35
+ end
36
+
37
+ test 'Starts cloud connector configuration job' do
38
+ test_job = FactoryBot.create(:job_invocation)
39
+
40
+ ForemanRhCloud::CloudConnector.any_instance
41
+ .expects(:install)
42
+ .returns(test_job)
43
+
44
+ post :enable_cloud_connector
45
+
46
+ assert_response :success
47
+
48
+ actual_job = @response.parsed_body
49
+
50
+ assert_equal test_job.id, actual_job['id']
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_plugin_helper'
2
+ require 'foreman_tasks/test_helpers'
3
+
4
+ class InsightsClientStatusAgingTest < ActiveSupport::TestCase
5
+ include ForemanTasks::TestHelpers::WithInThreadExecutor
6
+
7
+ setup do
8
+ User.current = User.find_by(login: 'secret_admin')
9
+
10
+ @host1 = FactoryBot.create(:host)
11
+ @host2 = FactoryBot.create(:host)
12
+ @host3 = FactoryBot.create(:host)
13
+ @host4 = FactoryBot.create(:host)
14
+
15
+ @hosts = [@host1, @host2, @host3, @host4]
16
+ end
17
+
18
+ test 'stale statuses should change' do
19
+ InsightsClientReportStatus.find_or_initialize_by(host_id: @host1.id).update(status: InsightsClientReportStatus::REPORTING, reported_at: Time.now - InsightsClientReportStatus::REPORT_INTERVAL + 1.day)
20
+ InsightsClientReportStatus.find_or_initialize_by(host_id: @host2.id).update(status: InsightsClientReportStatus::NO_REPORT, reported_at: Time.now - InsightsClientReportStatus::REPORT_INTERVAL + 1.day)
21
+ InsightsClientReportStatus.find_or_initialize_by(host_id: @host3.id).update(status: InsightsClientReportStatus::REPORTING, reported_at: Time.now - InsightsClientReportStatus::REPORT_INTERVAL - 1.day)
22
+ InsightsClientReportStatus.find_or_initialize_by(host_id: @host4.id).update(status: InsightsClientReportStatus::NO_REPORT, reported_at: Time.now - InsightsClientReportStatus::REPORT_INTERVAL - 1.day)
23
+
24
+ ForemanTasks.sync_task(InsightsCloud::Async::InsightsClientStatusAging)
25
+
26
+ @hosts.each(&:reload)
27
+
28
+ assert_equal InsightsClientReportStatus::REPORTING, @host1.get_status(InsightsClientReportStatus).status
29
+ assert_equal InsightsClientReportStatus::NO_REPORT, @host2.get_status(InsightsClientReportStatus).status
30
+ assert_equal InsightsClientReportStatus::NO_REPORT, @host3.get_status(InsightsClientReportStatus).status
31
+ assert_equal InsightsClientReportStatus::NO_REPORT, @host4.get_status(InsightsClientReportStatus).status
32
+ end
33
+ end
@@ -7,6 +7,7 @@ class InsightsFullSyncTest < ActiveSupport::TestCase
7
7
  setup do
8
8
  InsightsCloud::Async::InsightsFullSync.any_instance.stubs(:plan_rules_sync)
9
9
  InsightsCloud::Async::InsightsFullSync.any_instance.stubs(:plan_notifications)
10
+ FactoryBot.create(:setting, name: 'rh_cloud_token', value: 'MOCK_TOKEN')
10
11
 
11
12
  uuid1 = 'accdf444-5628-451d-bf3e-cf909ad72756'
12
13
  @host1 = FactoryBot.create(:host, :managed, name: 'host1')
@@ -63,6 +63,7 @@ class InsightsResolutionsSyncTest < ActiveSupport::TestCase
63
63
  }
64
64
 
65
65
  @rule = FactoryBot.create(:insights_rule, rule_id: 'network_tcp_connection_hang|NETWORK_TCP_CONNECTION_HANG_WARN')
66
+ FactoryBot.create(:setting, name: 'rh_cloud_token', value: 'MOCK_TOKEN')
66
67
  end
67
68
 
68
69
  test 'Resolutions data is replaced with data from cloud' do
@@ -112,6 +112,7 @@ 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
+ FactoryBot.create(:setting, name: 'rh_cloud_token', value: 'MOCK_TOKEN')
115
116
  end
116
117
 
117
118
  test 'Hits data is replaced with data from cloud' do
@@ -242,6 +242,7 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
242
242
  end
243
243
 
244
244
  test 'Host status should be SYNC for inventory hosts' do
245
+ FactoryBot.create(:setting, name: 'rh_cloud_token', value: 'TEST TOKEN')
245
246
  InventorySync::Async::InventoryFullSync.any_instance.expects(:query_inventory).returns(@inventory)
246
247
 
247
248
  ForemanTasks.sync_task(InventorySync::Async::InventoryFullSync, @host2.organization)
@@ -253,6 +254,7 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
253
254
  end
254
255
 
255
256
  test 'Host status should be DISCONNECT for hosts that are not returned from cloud' do
257
+ FactoryBot.create(:setting, name: 'rh_cloud_token', value: 'TEST TOKEN')
256
258
  InventorySync::Async::InventoryFullSync.any_instance.expects(:query_inventory).returns(@inventory)
257
259
  FactoryBot.create(:fact_value, fact_name: fact_names['virt::uuid'], value: '1234', host: @host2)
258
260
 
@@ -261,4 +263,12 @@ class InventoryFullSyncTest < ActiveSupport::TestCase
261
263
 
262
264
  assert_equal InventorySync::InventoryStatus::DISCONNECT, InventorySync::InventoryStatus.where(host_id: @host1.id).first.status
263
265
  end
266
+
267
+ test 'Task should be aborted if token is not present' do
268
+ FactoryBot.create(:setting, name: 'rh_cloud_token', value: '')
269
+
270
+ InventorySync::Async::InventoryFullSync.any_instance.expects(:plan_self).never
271
+
272
+ ForemanTasks.sync_task(InventorySync::Async::InventoryFullSync, @host1.organization)
273
+ end
264
274
  end
@@ -0,0 +1,268 @@
1
+ require 'test_plugin_helper'
2
+ require 'foreman_tasks/test_helpers'
3
+
4
+ class InventoryHostsSyncTest < ActiveSupport::TestCase
5
+ include ForemanTasks::TestHelpers::WithInThreadExecutor
6
+
7
+ setup do
8
+ User.current = User.find_by(login: 'secret_admin')
9
+ FactoryBot.create(:setting, name: 'rh_cloud_token', value: 'MOCK_TOKEN')
10
+
11
+ env = FactoryBot.create(:katello_k_t_environment)
12
+ cv = env.content_views << FactoryBot.create(:katello_content_view, organization: env.organization)
13
+
14
+ # this host would pass our plugin queries, so it could be uploaded to the cloud.
15
+ @host1 = FactoryBot.create(
16
+ :host,
17
+ :with_subscription,
18
+ :with_content,
19
+ content_view: cv.first,
20
+ lifecycle_environment: env,
21
+ organization: env.organization
22
+ )
23
+
24
+ pool = FactoryBot.create(:katello_pool, account_number: '1234', cp_id: 1)
25
+
26
+ @host1.subscription_facet.pools << pool
27
+
28
+ # this host would pass our plugin queries, so it could be uploaded to the cloud.
29
+ @host2 = FactoryBot.create(
30
+ :host,
31
+ :with_subscription,
32
+ :with_content,
33
+ content_view: cv.first,
34
+ lifecycle_environment: env,
35
+ organization: env.organization
36
+ )
37
+
38
+ @host2.subscription_facet.pools << pool
39
+ @host2_inventory_id = '4536bf5c-ff03-4154-a8c9-32ff4b40e40c'
40
+
41
+ ForemanInventoryUpload::Generators::Queries.instance_variable_set(:@fact_names, nil)
42
+
43
+ inventory_json = <<-INVENTORY_JSON
44
+ {
45
+ "total": 3,
46
+ "count": 3,
47
+ "page": 1,
48
+ "per_page": 3,
49
+ "results": [
50
+ {
51
+ "insights_id": "72d29d75-dbbf-4121-9566-2f581ab77f36",
52
+ "rhel_machine_id": null,
53
+ "subscription_manager_id": "#{@host2.subscription_facet.uuid}",
54
+ "satellite_id": "bb72bf95-0a19-4090-8009-f0d8c68aca61",
55
+ "bios_uuid": "b48a7e5f-cb50-4029-a75e-366bf43db641",
56
+ "ip_addresses": [
57
+ "192.168.122.56"
58
+ ],
59
+ "fqdn": "#{@host2.fqdn}",
60
+ "mac_addresses": [
61
+ "52:54:00:aa:12:12",
62
+ "00:00:00:00:00:00"
63
+ ],
64
+ "external_id": null,
65
+ "id": "#{@host2_inventory_id}",
66
+ "account": "1460290",
67
+ "display_name": "insights-rh7.example.com",
68
+ "ansible_host": null,
69
+ "facts": [
70
+ {
71
+ "namespace": "satellite",
72
+ "facts": {
73
+ "virtual_host_name": "virt-who-nobody.home-1",
74
+ "satellite_instance_id": "fc4d0cb0-a0b0-421e-b096-b028319b8e47",
75
+ "is_simple_content_access": false,
76
+ "distribution_version": "7.3",
77
+ "satellite_version": "6.8.4",
78
+ "organization_id": 1,
79
+ "is_hostname_obfuscated": false,
80
+ "virtual_host_uuid": "a90e6294-4766-420a-8dc0-3ec5b96d60ec"
81
+ }
82
+ },
83
+ {
84
+ "namespace": "yupana",
85
+ "facts": {
86
+ "report_platform_id": "d37afa50-08ce-4efb-a0e5-759c2a016661",
87
+ "report_slice_id": "5bf791d7-5e30-4a3c-929a-11dd9fa6eb72",
88
+ "source": "Satellite",
89
+ "yupana_host_id": "e85958b6-58db-4cfd-aeb6-01ee81bc0f43",
90
+ "account": "1460290"
91
+ }
92
+ }
93
+ ],
94
+ "reporter": "puptoo",
95
+ "stale_timestamp": "2021-03-19T07:57:42.466399+00:00",
96
+ "stale_warning_timestamp": "2021-03-26T07:57:42.466399+00:00",
97
+ "culled_timestamp": "2021-04-02T07:57:42.466399+00:00",
98
+ "created": "2021-02-08T14:36:03.613880+00:00",
99
+ "updated": "2021-03-18T02:57:42.535250+00:00"
100
+ },
101
+ {
102
+ "insights_id": "e0dc5144-d78e-43ed-97be-a7a21d1b6946",
103
+ "rhel_machine_id": null,
104
+ "subscription_manager_id": "0f97ee19-6862-4900-aea4-f121c8754776",
105
+ "satellite_id": "0f97ee19-6862-4900-aea4-f121c8754776",
106
+ "bios_uuid": "6a0b199a-8225-4ade-ae44-3b18cfc84a01",
107
+ "ip_addresses": [
108
+ "192.168.122.136"
109
+ ],
110
+ "fqdn": "#{@host1.fqdn}",
111
+ "mac_addresses": [
112
+ "52:54:00:02:d1:2a",
113
+ "00:00:00:00:00:00"
114
+ ],
115
+ "external_id": null,
116
+ "id": "3255d080-e6c1-44e2-8d72-b044b9a38d8f",
117
+ "account": "1460290",
118
+ "display_name": "insights-rh8.example.com",
119
+ "ansible_host": null,
120
+ "facts": [
121
+ {
122
+ "namespace": "satellite",
123
+ "facts": {
124
+ "virtual_host_name": "virt-who-nobody.home-1",
125
+ "satellite_instance_id": "fc4d0cb0-a0b0-421e-b096-b028319b8e47",
126
+ "is_simple_content_access": false,
127
+ "distribution_version": "8.3",
128
+ "satellite_version": "6.8.4",
129
+ "organization_id": 1,
130
+ "is_hostname_obfuscated": false,
131
+ "virtual_host_uuid": "a90e6294-4766-420a-8dc0-3ec5b96d60ec"
132
+ }
133
+ },
134
+ {
135
+ "namespace": "yupana",
136
+ "facts": {
137
+ "report_platform_id": "d37afa50-08ce-4efb-a0e5-759c2a016661",
138
+ "report_slice_id": "5bf791d7-5e30-4a3c-929a-11dd9fa6eb72",
139
+ "source": "Satellite",
140
+ "yupana_host_id": "78c62486-0ac4-406c-a4c0-3a1f81112a2d",
141
+ "account": "1460290"
142
+ }
143
+ }
144
+ ],
145
+ "reporter": "puptoo",
146
+ "stale_timestamp": "2021-03-19T06:05:12.092136+00:00",
147
+ "stale_warning_timestamp": "2021-03-26T06:05:12.092136+00:00",
148
+ "culled_timestamp": "2021-04-02T06:05:12.092136+00:00",
149
+ "created": "2021-02-08T13:22:50.555671+00:00",
150
+ "updated": "2021-03-18T01:05:12.131847+00:00"
151
+ },
152
+ {
153
+ "insights_id": "b533848e-465f-4f1a-9b2b-b71cb2d5239d",
154
+ "rhel_machine_id": null,
155
+ "subscription_manager_id": "d29bde40-348e-437c-8acf-8fa98320fc1b",
156
+ "satellite_id": "d29bde40-348e-437c-8acf-8fa98320fc1b",
157
+ "bios_uuid": "3cd5d972-cfb5-451a-8314-fd2f56629d7c",
158
+ "ip_addresses": [
159
+ "172.16.5.39",
160
+ "fd6e:2298:736e::857",
161
+ "fd6e:2298:736e:0:2c66:6101:9cc6:2b23"
162
+ ],
163
+ "fqdn": "rhel8-demo.oss-lab.net",
164
+ "mac_addresses": [
165
+ "6e:66:a6:fe:fc:07",
166
+ "00:00:00:00:00:00"
167
+ ],
168
+ "external_id": null,
169
+ "id": "59ab38db-c25b-4fc7-bfb2-c8eb01d865a9",
170
+ "account": "1460290",
171
+ "display_name": "rhel8-demo.oss-lab.net",
172
+ "ansible_host": null,
173
+ "facts": [
174
+ {
175
+ "namespace": "satellite",
176
+ "facts": {
177
+ "satellite_instance_id": "fb3643d8-9030-487b-b95c-684783806ffd",
178
+ "system_purpose_sla": "",
179
+ "is_simple_content_access": false,
180
+ "distribution_version": "8.3",
181
+ "satellite_version": "6.8.1",
182
+ "organization_id": 1,
183
+ "system_purpose_role": "",
184
+ "system_purpose_usage": "",
185
+ "is_hostname_obfuscated": false
186
+ }
187
+ },
188
+ {
189
+ "namespace": "yupana",
190
+ "facts": {
191
+ "report_platform_id": "fa8b924c-51ee-479d-97d2-b4623cf1d4aa",
192
+ "report_slice_id": "0b49103f-6471-4ade-ad74-a51537bc5691",
193
+ "source": "Satellite",
194
+ "yupana_host_id": "30e43340-12fb-445d-b23f-faaf5cbc2092",
195
+ "account": "1460290"
196
+ }
197
+ }
198
+ ],
199
+ "reporter": "puptoo",
200
+ "stale_timestamp": "2021-03-19T05:55:23.700781+00:00",
201
+ "stale_warning_timestamp": "2021-03-26T05:55:23.700781+00:00",
202
+ "culled_timestamp": "2021-04-02T05:55:23.700781+00:00",
203
+ "created": "2021-01-13T20:05:51.059012+00:00",
204
+ "updated": "2021-03-18T00:55:23.739162+00:00"
205
+ }
206
+ ]
207
+ }
208
+ INVENTORY_JSON
209
+ @inventory = JSON.parse(inventory_json)
210
+ end
211
+
212
+ def interesting_facts
213
+ [
214
+ 'dmi::system::uuid',
215
+ 'virt::uuid',
216
+ 'cpu::cpu(s)',
217
+ 'cpu::cpu_socket(s)',
218
+ 'cpu::core(s)_per_socket',
219
+ 'memory::memtotal',
220
+ 'dmi::bios::vendor',
221
+ 'dmi::bios::version',
222
+ 'dmi::bios::relase_date',
223
+ 'uname::release',
224
+ 'lscpu::flags',
225
+ 'distribution::name',
226
+ 'distribution::version',
227
+ 'distribution::id',
228
+ 'virt::is_guest',
229
+ 'dmi::system::manufacturer',
230
+ 'dmi::system::product_name',
231
+ 'dmi::chassis::asset_tag',
232
+ 'insights_client::obfuscate_hostname_enabled',
233
+ 'insights_client::hostname',
234
+ ]
235
+ end
236
+
237
+ def fact_names
238
+ @fact_names ||= Hash[
239
+ interesting_facts.map do |fact|
240
+ [fact, FactoryBot.create(:fact_name, name: fact, type: 'Katello::RhsmFactName')]
241
+ end
242
+ ]
243
+ end
244
+
245
+ test 'Inventory should sync UUID for existing Insights Facets' do
246
+ InventorySync::Async::InventoryHostsSync.any_instance.expects(:query_inventory).returns(@inventory)
247
+ InventorySync::Async::InventoryHostsSync.any_instance.expects(:plan_self_host_sync)
248
+
249
+ @host2.build_insights.save
250
+
251
+ ForemanTasks.sync_task(InventorySync::Async::InventoryHostsSync)
252
+
253
+ @host2.reload
254
+
255
+ assert_equal @host2_inventory_id, @host2.insights.uuid
256
+ end
257
+
258
+ test 'Inventory should sync UUID for new Insights Facets' do
259
+ InventorySync::Async::InventoryHostsSync.any_instance.expects(:query_inventory).returns(@inventory)
260
+ InventorySync::Async::InventoryHostsSync.any_instance.expects(:plan_self_host_sync)
261
+
262
+ ForemanTasks.sync_task(InventorySync::Async::InventoryHostsSync)
263
+
264
+ @host2.reload
265
+
266
+ assert_equal @host2_inventory_id, @host2.insights.uuid
267
+ end
268
+ end
@@ -0,0 +1,22 @@
1
+ require 'test_plugin_helper'
2
+ require 'foreman_tasks/test_helpers'
3
+
4
+ class InventoryScheduledSyncTest < ActiveSupport::TestCase
5
+ include ForemanTasks::TestHelpers::WithInThreadExecutor
6
+
7
+ test 'Schedules an execution if auto upload is enabled' do
8
+ FactoryBot.create(:setting, :name => 'allow_auto_inventory_upload', :settings_type => "boolean", :category => "Setting::RhCloud", :default => false, :value => true)
9
+
10
+ InventorySync::Async::InventoryScheduledSync.any_instance.expects(:plan_org_sync).times(Organization.unscoped.count)
11
+
12
+ ForemanTasks.sync_task(InventorySync::Async::InventoryScheduledSync)
13
+ end
14
+
15
+ test 'Skips execution if auto upload is disabled' do
16
+ FactoryBot.create(:setting, :name => 'allow_auto_inventory_upload', :settings_type => "boolean", :category => "Setting::RhCloud", :default => false, :value => false)
17
+
18
+ InventorySync::Async::InventoryScheduledSync.any_instance.expects(:plan_org_sync).never
19
+
20
+ ForemanTasks.sync_task(InventorySync::Async::InventoryScheduledSync)
21
+ end
22
+ end
@@ -0,0 +1,104 @@
1
+ require 'test_plugin_helper'
2
+ require 'foreman_tasks/test_helpers'
3
+
4
+ class InventorySelfHostSyncTest < ActiveSupport::TestCase
5
+ include ForemanTasks::TestHelpers::WithInThreadExecutor
6
+
7
+ setup do
8
+ User.current = User.find_by(login: 'secret_admin')
9
+ FactoryBot.create(:setting, name: 'rh_cloud_token', value: 'MOCK_TOKEN')
10
+
11
+ # this host would pass our plugin queries, so it could be uploaded to the cloud.
12
+ @host1 = FactoryBot.create(:host)
13
+ @host1_inventory_id = '3255d080-e6c1-44e2-8d72-b044b9a38d8f'
14
+
15
+ ForemanInventoryUpload::Generators::Queries.instance_variable_set(:@fact_names, nil)
16
+
17
+ ForemanRhCloud.stubs(:foreman_host).returns(@host1)
18
+
19
+ inventory_json = <<-INVENTORY_JSON
20
+ {
21
+ "total": 1,
22
+ "count": 1,
23
+ "page": 1,
24
+ "per_page": 1,
25
+ "results": [
26
+ {
27
+ "insights_id": "e0dc5144-d78e-43ed-97be-a7a21d1b6946",
28
+ "rhel_machine_id": null,
29
+ "subscription_manager_id": "0f97ee19-6862-4900-aea4-f121c8754776",
30
+ "satellite_id": "0f97ee19-6862-4900-aea4-f121c8754776",
31
+ "bios_uuid": "6a0b199a-8225-4ade-ae44-3b18cfc84a01",
32
+ "ip_addresses": [
33
+ "192.168.122.136"
34
+ ],
35
+ "fqdn": "#{@host1.fqdn}",
36
+ "mac_addresses": [
37
+ "52:54:00:02:d1:2a",
38
+ "00:00:00:00:00:00"
39
+ ],
40
+ "external_id": null,
41
+ "id": "#{@host1_inventory_id}",
42
+ "account": "1460290",
43
+ "display_name": "insights-rh8.example.com",
44
+ "ansible_host": null,
45
+ "facts": [
46
+ {
47
+ "namespace": "satellite",
48
+ "facts": {
49
+ "virtual_host_name": "virt-who-nobody.home-1",
50
+ "satellite_instance_id": "fc4d0cb0-a0b0-421e-b096-b028319b8e47",
51
+ "is_simple_content_access": false,
52
+ "distribution_version": "8.3",
53
+ "satellite_version": "6.8.4",
54
+ "organization_id": 1,
55
+ "is_hostname_obfuscated": false,
56
+ "virtual_host_uuid": "a90e6294-4766-420a-8dc0-3ec5b96d60ec"
57
+ }
58
+ },
59
+ {
60
+ "namespace": "yupana",
61
+ "facts": {
62
+ "report_platform_id": "d37afa50-08ce-4efb-a0e5-759c2a016661",
63
+ "report_slice_id": "5bf791d7-5e30-4a3c-929a-11dd9fa6eb72",
64
+ "source": "Satellite",
65
+ "yupana_host_id": "78c62486-0ac4-406c-a4c0-3a1f81112a2d",
66
+ "account": "1460290"
67
+ }
68
+ }
69
+ ],
70
+ "reporter": "puptoo",
71
+ "stale_timestamp": "2021-03-19T06:05:12.092136+00:00",
72
+ "stale_warning_timestamp": "2021-03-26T06:05:12.092136+00:00",
73
+ "culled_timestamp": "2021-04-02T06:05:12.092136+00:00",
74
+ "created": "2021-02-08T13:22:50.555671+00:00",
75
+ "updated": "2021-03-18T01:05:12.131847+00:00"
76
+ }
77
+ ]
78
+ }
79
+ INVENTORY_JSON
80
+ @inventory = JSON.parse(inventory_json)
81
+ end
82
+
83
+ test 'Inventory should sync UUID for existing Insights Facets' do
84
+ InventorySync::Async::InventorySelfHostSync.any_instance.expects(:query_inventory).returns(@inventory)
85
+
86
+ @host1.build_insights.save
87
+
88
+ ForemanTasks.sync_task(InventorySync::Async::InventorySelfHostSync)
89
+
90
+ @host1.reload
91
+
92
+ assert_equal @host1_inventory_id, @host1.insights.uuid
93
+ end
94
+
95
+ test 'Inventory should sync UUID for new Insights Facets' do
96
+ InventorySync::Async::InventorySelfHostSync.any_instance.expects(:query_inventory).returns(@inventory)
97
+
98
+ ForemanTasks.sync_task(InventorySync::Async::InventorySelfHostSync)
99
+
100
+ @host1.reload
101
+
102
+ assert_equal @host1_inventory_id, @host1.insights.uuid
103
+ end
104
+ end