foreman_rh_cloud 15.0.1 → 15.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/api/v2/advisor_engine/advisor_engine_controller.rb +1 -73
  3. data/app/controllers/foreman_inventory_upload/accounts_controller.rb +29 -14
  4. data/app/controllers/insights_cloud/ui_requests_controller.rb +8 -2
  5. data/app/services/foreman_rh_cloud/insights_api_forwarder.rb +93 -79
  6. data/config/package-lock.json.plugin +7160 -21310
  7. data/config/routes.rb +0 -4
  8. data/lib/foreman_inventory_upload/async/generate_host_report.rb +6 -1
  9. data/lib/foreman_inventory_upload/async/host_inventory_report_job.rb +10 -0
  10. data/lib/foreman_inventory_upload/async/queue_for_upload_job.rb +1 -0
  11. data/lib/foreman_inventory_upload/async/upload_report_direct_job.rb +16 -2
  12. data/lib/foreman_inventory_upload/generators/archived_report.rb +4 -4
  13. data/lib/foreman_inventory_upload/generators/slice.rb +55 -12
  14. data/lib/foreman_inventory_upload.rb +69 -0
  15. data/lib/foreman_rh_cloud/plugin.rb +24 -11
  16. data/lib/foreman_rh_cloud/version.rb +1 -1
  17. data/package.json +6 -13
  18. data/test/controllers/insights_cloud/api/advisor_engine_controller_test.rb +4 -15
  19. data/test/controllers/insights_cloud/ui_requests_controller_test.rb +70 -3
  20. data/test/jobs/generate_host_report_test.rb +1 -1
  21. data/test/jobs/host_inventory_report_job_test.rb +72 -0
  22. data/test/jobs/queue_for_upload_job_test.rb +4 -1
  23. data/test/jobs/upload_report_direct_job_test.rb +28 -7
  24. data/test/unit/archived_report_generator_test.rb +21 -0
  25. data/test/unit/foreman_inventory_upload_host_report_path_test.rb +111 -0
  26. data/test/unit/rh_cloud_permissions_test.rb +0 -2
  27. data/test/unit/services/foreman_rh_cloud/insights_api_forwarder_test.rb +189 -356
  28. data/test/unit/slice_generator_test.rb +28 -0
  29. data/webpack/ForemanInventoryUpload/Components/Dashboard/Dashboard.js +1 -0
  30. data/webpack/ForemanInventoryUpload/Components/TaskProgress/TaskProgress.js +8 -2
  31. data/webpack/HostsIndexExtensions/VulnerabilityAnalysisActions.js +1 -1
  32. data/webpack/test_setup.js +18 -0
  33. metadata +3 -5
  34. data/app/controllers/foreman_inventory_upload/api/tasks_controller.rb +0 -110
  35. data/app/services/foreman_rh_cloud/hits_uploader.rb +0 -50
  36. data/test/unit/services/foreman_rh_cloud/hits_uploader_test.rb +0 -131
@@ -34,12 +34,15 @@ class QueueForUploadJobTest < ActiveSupport::TestCase
34
34
  test 'run method processes file and moves it to uploads folder' do
35
35
  ForemanInventoryUpload::Async::QueueForUploadJob.any_instance.stubs(:plan_upload_report)
36
36
 
37
+ enqueued = File.join(uploads_folder, report_file)
38
+ ForemanInventoryUpload.expects(:log_inventory_report_saved).with(anything, enqueued)
39
+
37
40
  action = create_and_plan_action(ForemanInventoryUpload::Async::QueueForUploadJob, base_folder, report_file, organization.id)
38
41
  run_action(action)
39
42
 
40
43
  # Verify the file was moved
41
44
  refute File.exist?(report_path), "Original file should be moved"
42
- assert File.exist?(File.join(uploads_folder, report_file)), "File should exist in uploads folder"
45
+ assert File.exist?(enqueued), "File should exist in uploads folder"
43
46
  end
44
47
 
45
48
  test 'creates necessary folders' do
@@ -35,6 +35,13 @@ class UploadReportDirectJobTest < ActiveSupport::TestCase
35
35
  ForemanRhCloud.unstub(:with_iop_smart_proxy?)
36
36
  end
37
37
 
38
+ # plan_action leaves output as a Dynflow OutputReference; try_execute needs a writable Hash.
39
+ def with_writable_output(action)
40
+ writable = ActiveSupport::HashWithIndifferentAccess.new
41
+ action.stubs(:output).returns(writable)
42
+ writable
43
+ end
44
+
38
45
  test 'plan sets input correctly' do
39
46
  action = create_action(ForemanInventoryUpload::Async::UploadReportDirectJob)
40
47
  action.expects(:action_subject).with(@organization)
@@ -220,6 +227,8 @@ class UploadReportDirectJobTest < ActiveSupport::TestCase
220
227
  action.expects(:action_subject).with(@organization)
221
228
  plan_action(action, @filename, @organization.id)
222
229
 
230
+ with_writable_output(action)
231
+
223
232
  # Should raise the error (handled by Dynflow retry mechanism)
224
233
  assert_raises(RestClient::InternalServerError) do
225
234
  action.send(:try_execute)
@@ -235,6 +244,8 @@ class UploadReportDirectJobTest < ActiveSupport::TestCase
235
244
  action.expects(:action_subject).with(@organization)
236
245
  plan_action(action, @filename, @organization.id)
237
246
 
247
+ with_writable_output(action)
248
+
238
249
  # Should raise the error (handled by Dynflow retry mechanism via ExponentialBackoff)
239
250
  assert_raises(RestClient::Exceptions::Timeout) do
240
251
  action.send(:try_execute)
@@ -289,11 +300,14 @@ class UploadReportDirectJobTest < ActiveSupport::TestCase
289
300
  action.expects(:action_subject).with(@organization)
290
301
  plan_action(action, test_file, @organization.id)
291
302
 
292
- # Execute the action
303
+ ForemanInventoryUpload.expects(:log_inventory_report_saved).with(anything, test_file)
304
+
305
+ output = with_writable_output(action)
293
306
  action.send(:try_execute)
294
307
 
295
308
  # Verify file still exists (not moved or deleted)
296
309
  assert File.exist?(test_file), "File should remain when upload is aborted"
310
+ refute output[:uploaded]
297
311
  ensure
298
312
  FileUtils.rm_f(test_file) if File.exist?(test_file)
299
313
  end
@@ -313,11 +327,14 @@ class UploadReportDirectJobTest < ActiveSupport::TestCase
313
327
  action.expects(:action_subject).with(@organization)
314
328
  plan_action(action, test_file, @organization.id)
315
329
 
316
- # Execute the action
330
+ ForemanInventoryUpload.expects(:log_inventory_report_saved).with(anything, test_file)
331
+
332
+ output = with_writable_output(action)
317
333
  action.send(:try_execute)
318
334
 
319
- # Verify file still exists
335
+ # Verify file still exists and upload was not recorded as completed
320
336
  assert File.exist?(test_file), "File should remain when connection is disabled"
337
+ refute output[:uploaded]
321
338
  ensure
322
339
  FileUtils.rm_f(test_file) if File.exist?(test_file)
323
340
  end
@@ -342,18 +359,20 @@ class UploadReportDirectJobTest < ActiveSupport::TestCase
342
359
  # Stub upload_file to avoid actual HTTP upload
343
360
  action.stubs(:upload_file).returns(nil)
344
361
 
345
- # Expect a warning when overwriting the existing done file
362
+ # Expect a warning when overwriting the existing done file, then a save path log
346
363
  action.expects(:logger).at_least_once.returns(mock_logger = mock('logger'))
347
364
  mock_logger.expects(:warn).with(regexp_matches(/already exists.*overwriting/i))
348
365
  mock_logger.stubs(:debug)
366
+ ForemanInventoryUpload.expects(:log_inventory_report_saved).with(mock_logger, done_file_path)
349
367
 
350
368
  plan_action(action, source_file, @organization.id)
351
369
 
352
- # Execute the action (which will call move_to_done_folder)
370
+ output = with_writable_output(action)
353
371
  action.send(:try_execute)
354
372
 
355
373
  # Done file should now contain the new report content
356
374
  assert_equal new_report_content, File.read(done_file_path)
375
+ assert output[:uploaded]
357
376
  ensure
358
377
  FileUtils.rm_f(source_file) if defined?(source_file) && source_file
359
378
  FileUtils.rm_f(done_file_path) if defined?(done_file_path) && done_file_path
@@ -410,7 +429,8 @@ class UploadReportDirectJobTest < ActiveSupport::TestCase
410
429
  action.expects(:action_subject).with(@organization)
411
430
  plan_action(action, nil, @organization.id)
412
431
 
413
- # Execute the action
432
+ with_writable_output(action)
433
+ ForemanInventoryUpload.stubs(:log_inventory_report_saved)
414
434
  action.send(:try_execute)
415
435
 
416
436
  # Verify it is marked as done
@@ -424,7 +444,8 @@ class UploadReportDirectJobTest < ActiveSupport::TestCase
424
444
  action.expects(:action_subject).with(@organization)
425
445
  plan_action(action, nil, @organization.id)
426
446
 
427
- # Execute the action
447
+ with_writable_output(action)
448
+ ForemanInventoryUpload.stubs(:log_inventory_report_saved)
428
449
  action.send(:try_execute)
429
450
 
430
451
  # Verify it is marked as done
@@ -60,4 +60,25 @@ class ArchivedReportGeneratorTest < ActiveSupport::TestCase
60
60
  assert_equal "#{tmpdir}/test.tar.gz", files.first
61
61
  end
62
62
  end
63
+
64
+ test 'raises when tar archive fails' do
65
+ batches = Host.where(id: @host.id).in_batches
66
+ test_org = FactoryBot.create(:organization)
67
+
68
+ ForemanInventoryUpload::Generators::Queries.expects(:for_org).with(test_org.id, hosts_query: '').returns(batches)
69
+
70
+ status = stub(success?: false, to_s: 'pid 1 exit 1')
71
+ wait_thr = stub(value: status)
72
+ Open3.expects(:popen2e).yields(nil, StringIO.new('tar failed'), wait_thr)
73
+
74
+ Dir.mktmpdir do |tmpdir|
75
+ target = File.join(tmpdir, 'test.tar.xz')
76
+ generator = ForemanInventoryUpload::Generators::ArchivedReport.new(target, Logger.new(File::NULL))
77
+
78
+ error = assert_raises(RuntimeError) do
79
+ generator.render(organization: test_org.id)
80
+ end
81
+ assert_match(/Tar command failed/, error.message)
82
+ end
83
+ end
63
84
  end
@@ -0,0 +1,111 @@
1
+ require 'test_plugin_helper'
2
+
3
+ class ForemanInventoryUploadHostReportPathTest < ActiveSupport::TestCase
4
+ test 'inventory_report_location returns blank input unchanged' do
5
+ assert_nil ForemanInventoryUpload.inventory_report_location(nil)
6
+ assert_equal '', ForemanInventoryUpload.inventory_report_location('')
7
+ end
8
+
9
+ test 'inventory_report_location uses filesystem path in test/development' do
10
+ Dir.mktmpdir do |tmpdir|
11
+ report = File.join(tmpdir, 'report_for_1.tar.xz')
12
+ FileUtils.touch(report)
13
+
14
+ assert_equal File.realpath(report), ForemanInventoryUpload.inventory_report_location(report)
15
+ end
16
+ end
17
+
18
+ test 'inventory_report_location describes volume and relative path outside development' do
19
+ Dir.mktmpdir do |tmpdir|
20
+ ForemanInventoryUpload.stubs(:base_folder).returns(File.join(tmpdir, 'red_hat_inventory'))
21
+ FileUtils.mkdir_p(ForemanInventoryUpload.base_folder)
22
+ report = File.join(ForemanInventoryUpload.base_folder, 'uploads', 'report_for_1.tar.xz')
23
+ FileUtils.mkdir_p(File.dirname(report))
24
+ FileUtils.touch(report)
25
+
26
+ ForemanInventoryUpload.stubs(:filesystem_report_path_logging?).returns(false)
27
+ old_mountpoint = ENV.delete('FOREMAN_DATA_RUN_MOUNTPOINT')
28
+
29
+ begin
30
+ location = ForemanInventoryUpload.inventory_report_location(report)
31
+
32
+ assert_includes location, "podman volume 'foreman-data-run'"
33
+ assert_includes location, "relative path 'red_hat_inventory/uploads/report_for_1.tar.xz'"
34
+ assert_includes location, "sudo podman volume inspect --format '{{.Mountpoint}}' foreman-data-run"
35
+ ensure
36
+ ENV['FOREMAN_DATA_RUN_MOUNTPOINT'] = old_mountpoint if old_mountpoint
37
+ end
38
+ end
39
+ end
40
+
41
+ test 'inventory_report_location uses path relative to volume root for siblings of inventory dir' do
42
+ Dir.mktmpdir do |tmpdir|
43
+ ForemanInventoryUpload.stubs(:base_folder).returns(File.join(tmpdir, 'red_hat_inventory'))
44
+ FileUtils.mkdir_p(ForemanInventoryUpload.base_folder)
45
+ custom = File.join(tmpdir, 'custom', 'report.tar.xz')
46
+ FileUtils.mkdir_p(File.dirname(custom))
47
+ FileUtils.touch(custom)
48
+
49
+ ForemanInventoryUpload.stubs(:filesystem_report_path_logging?).returns(false)
50
+ old_mountpoint = ENV.delete('FOREMAN_DATA_RUN_MOUNTPOINT')
51
+
52
+ begin
53
+ location = ForemanInventoryUpload.inventory_report_location(custom)
54
+ assert_includes location, "relative path 'custom/report.tar.xz'"
55
+ refute_includes location, 'red_hat_inventory/report.tar.xz'
56
+ ensure
57
+ ENV['FOREMAN_DATA_RUN_MOUNTPOINT'] = old_mountpoint if old_mountpoint
58
+ end
59
+ end
60
+ end
61
+
62
+ test 'inventory_report_location identifies paths outside the volume' do
63
+ Dir.mktmpdir do |inventory_parent|
64
+ Dir.mktmpdir do |other|
65
+ ForemanInventoryUpload.stubs(:base_folder).returns(File.join(inventory_parent, 'red_hat_inventory'))
66
+ FileUtils.mkdir_p(ForemanInventoryUpload.base_folder)
67
+ outside = File.join(other, 'report.tar.xz')
68
+ FileUtils.touch(outside)
69
+
70
+ ForemanInventoryUpload.stubs(:filesystem_report_path_logging?).returns(false)
71
+ old_mountpoint = ENV.delete('FOREMAN_DATA_RUN_MOUNTPOINT')
72
+
73
+ begin
74
+ location = ForemanInventoryUpload.inventory_report_location(outside)
75
+ assert_includes location, File.realpath(outside)
76
+ assert_includes location, "outside podman volume 'foreman-data-run'"
77
+ refute_includes location, 'red_hat_inventory/report.tar.xz'
78
+ ensure
79
+ ENV['FOREMAN_DATA_RUN_MOUNTPOINT'] = old_mountpoint if old_mountpoint
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ test 'inventory_report_location uses FOREMAN_DATA_RUN_MOUNTPOINT when set' do
86
+ Dir.mktmpdir do |tmpdir|
87
+ ForemanInventoryUpload.stubs(:base_folder).returns(File.join(tmpdir, 'red_hat_inventory'))
88
+ FileUtils.mkdir_p(ForemanInventoryUpload.base_folder)
89
+ report = File.join(ForemanInventoryUpload.base_folder, 'generated_reports', 'report_for_1.tar.xz')
90
+ FileUtils.mkdir_p(File.dirname(report))
91
+ FileUtils.touch(report)
92
+
93
+ old_mountpoint = ENV['FOREMAN_DATA_RUN_MOUNTPOINT']
94
+ ENV['FOREMAN_DATA_RUN_MOUNTPOINT'] = '/var/lib/containers/storage/volumes/foreman-data-run/_data'
95
+ begin
96
+ ForemanInventoryUpload.stubs(:filesystem_report_path_logging?).returns(false)
97
+ location = ForemanInventoryUpload.inventory_report_location(report)
98
+ assert_equal(
99
+ '/var/lib/containers/storage/volumes/foreman-data-run/_data/red_hat_inventory/generated_reports/report_for_1.tar.xz',
100
+ location
101
+ )
102
+ ensure
103
+ if old_mountpoint
104
+ ENV['FOREMAN_DATA_RUN_MOUNTPOINT'] = old_mountpoint
105
+ else
106
+ ENV.delete('FOREMAN_DATA_RUN_MOUNTPOINT')
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -9,8 +9,6 @@ class RhCloudPermissionsTest < ActiveSupport::TestCase
9
9
  [
10
10
  'insights_cloud/api/machine_telemetries/forward_request',
11
11
  'insights_cloud/api/machine_telemetries/branch_info',
12
- 'api/v2/rh_cloud/foreman_inventory_upload/api/tasks/current',
13
- 'api/v2/rh_cloud/foreman_inventory_upload/api/tasks/history',
14
12
  ],
15
13
  skip_patterns: [/^(?!foreman_inventory_upload|insights_cloud|.*rh_cloud).*/]
16
14
  ) # include only plugin paths