foreman_rh_cloud 15.0.0 → 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.
- checksums.yaml +4 -4
- data/app/controllers/api/v2/advisor_engine/advisor_engine_controller.rb +1 -73
- data/app/controllers/foreman_inventory_upload/accounts_controller.rb +29 -14
- data/app/controllers/insights_cloud/ui_requests_controller.rb +19 -2
- data/app/services/foreman_rh_cloud/insights_api_forwarder.rb +93 -79
- data/config/package-lock.json.plugin +7160 -21310
- data/config/routes.rb +0 -4
- data/lib/foreman_inventory_upload/async/generate_host_report.rb +6 -1
- data/lib/foreman_inventory_upload/async/host_inventory_report_job.rb +10 -0
- data/lib/foreman_inventory_upload/async/queue_for_upload_job.rb +1 -0
- data/lib/foreman_inventory_upload/async/upload_report_direct_job.rb +16 -2
- data/lib/foreman_inventory_upload/generators/archived_report.rb +4 -4
- data/lib/foreman_inventory_upload/generators/slice.rb +55 -12
- data/lib/foreman_inventory_upload.rb +69 -0
- data/lib/foreman_rh_cloud/plugin.rb +24 -11
- data/lib/foreman_rh_cloud/version.rb +1 -1
- data/package.json +6 -13
- data/test/controllers/insights_cloud/api/advisor_engine_controller_test.rb +4 -15
- data/test/controllers/insights_cloud/ui_requests_controller_test.rb +217 -5
- data/test/jobs/generate_host_report_test.rb +1 -1
- data/test/jobs/host_inventory_report_job_test.rb +72 -0
- data/test/jobs/queue_for_upload_job_test.rb +4 -1
- data/test/jobs/upload_report_direct_job_test.rb +28 -7
- data/test/unit/archived_report_generator_test.rb +21 -0
- data/test/unit/foreman_inventory_upload_host_report_path_test.rb +111 -0
- data/test/unit/rh_cloud_permissions_test.rb +0 -2
- data/test/unit/services/foreman_rh_cloud/insights_api_forwarder_test.rb +189 -356
- data/test/unit/slice_generator_test.rb +28 -0
- data/webpack/ForemanInventoryUpload/Components/Dashboard/Dashboard.js +1 -0
- data/webpack/ForemanInventoryUpload/Components/TaskProgress/TaskProgress.js +8 -2
- data/webpack/ForemanInventoryUpload/SubscriptionsPageExtension/InventoryAutoUpload/InventoryAutoUpload.js +3 -1
- data/webpack/ForemanInventoryUpload/SubscriptionsPageExtension/InventoryAutoUpload/__tests__/InventoryAutoUpload.test.js +23 -0
- data/webpack/HostsIndexExtensions/VulnerabilityAnalysisActions.js +1 -1
- data/webpack/test_setup.js +18 -0
- metadata +3 -6
- data/app/controllers/foreman_inventory_upload/api/tasks_controller.rb +0 -110
- data/app/services/foreman_rh_cloud/hits_uploader.rb +0 -50
- data/lib/tasks/hybrid_cloud.rake +0 -132
- data/test/unit/services/foreman_rh_cloud/hits_uploader_test.rb +0 -131
|
@@ -3,8 +3,6 @@ require 'rest-client'
|
|
|
3
3
|
|
|
4
4
|
module InsightsCloud
|
|
5
5
|
class UIRequestsControllerTest < ActionController::TestCase
|
|
6
|
-
include KatelloCVEHelper
|
|
7
|
-
|
|
8
6
|
setup do
|
|
9
7
|
FactoryBot.create(:common_parameter, name: InsightsCloud.enable_client_param, key_type: 'boolean', value: true)
|
|
10
8
|
end
|
|
@@ -13,19 +11,19 @@ module InsightsCloud
|
|
|
13
11
|
setup do
|
|
14
12
|
@org = FactoryBot.create(:organization)
|
|
15
13
|
@loc = FactoryBot.create(:location)
|
|
16
|
-
@host = FactoryBot.create(:host, :with_subscription, :organization => @org)
|
|
14
|
+
@host = FactoryBot.create(:host, :with_subscription, :organization => @org, :location => @loc)
|
|
17
15
|
@facet = InsightsFacet.create(host: @host, uuid: @host.subscription_facet.uuid)
|
|
18
16
|
end
|
|
19
17
|
|
|
20
18
|
test "should redirect to host details page by id" do
|
|
21
|
-
get :translate_insights_host, params: { "uuid" => @host.subscription_facet.uuid }, session:
|
|
19
|
+
get :translate_insights_host, params: { "uuid" => @host.subscription_facet.uuid }, session: set_session_user
|
|
22
20
|
assert_equal 302, @response.status
|
|
23
21
|
assert @response.redirect?
|
|
24
22
|
assert_equal "http://test.host/new/hosts/#{@host.id}", @response.redirect_url
|
|
25
23
|
end
|
|
26
24
|
|
|
27
25
|
test "should redirect to not-found page" do
|
|
28
|
-
get :translate_insights_host, params: { "uuid" => "foo" }, session:
|
|
26
|
+
get :translate_insights_host, params: { "uuid" => "foo" }, session: set_session_user
|
|
29
27
|
assert_equal 302, @response.status
|
|
30
28
|
assert @response.redirect?
|
|
31
29
|
assert_equal "http://test.host/page-not-found", @response.redirect_url
|
|
@@ -222,6 +220,220 @@ module InsightsCloud
|
|
|
222
220
|
end
|
|
223
221
|
end
|
|
224
222
|
|
|
223
|
+
context '#forward_request organization/location scoping' do
|
|
224
|
+
setup do
|
|
225
|
+
as_admin do
|
|
226
|
+
@org = FactoryBot.create(:organization)
|
|
227
|
+
@loc = FactoryBot.create(:location)
|
|
228
|
+
@other_org = FactoryBot.create(:organization)
|
|
229
|
+
@other_loc = FactoryBot.create(:location)
|
|
230
|
+
@restricted_user = FactoryBot.create(:user, :organizations => [@org], :locations => [@loc])
|
|
231
|
+
end
|
|
232
|
+
setup_user('view', 'insights_hits', nil, @restricted_user)
|
|
233
|
+
setup_user('view', 'vulnerability', nil, @restricted_user)
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
test 'resolves organization_id/location_id params into the scoped org/location and forwards the request' do
|
|
237
|
+
net_http_resp = Net::HTTPResponse.new(1.0, 200, 'OK')
|
|
238
|
+
http_req = RestClient::Request.new(:method => 'GET', :url => 'http://test.theforeman.org')
|
|
239
|
+
res = RestClient::Response.create('Cloud response body', net_http_resp, http_req)
|
|
240
|
+
|
|
241
|
+
::ForemanRhCloud::InsightsApiForwarder.any_instance.expects(:forward_request).with do |_req, _path, _controller_name, user, organization, location|
|
|
242
|
+
user == @restricted_user && organization == @org && location == @loc
|
|
243
|
+
end.returns(res)
|
|
244
|
+
|
|
245
|
+
get :forward_request,
|
|
246
|
+
params: {
|
|
247
|
+
:format => :json,
|
|
248
|
+
:controller => 'vulnerabilities',
|
|
249
|
+
:path => 'api/vulnerability/v1/cves',
|
|
250
|
+
:organization_id => @org.id,
|
|
251
|
+
:location_id => @loc.id,
|
|
252
|
+
},
|
|
253
|
+
session: set_session_user(@restricted_user)
|
|
254
|
+
|
|
255
|
+
assert_response :success
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
test "rejects forward_request when organization_id belongs to an org the user can't access" do
|
|
259
|
+
::ForemanRhCloud::InsightsApiForwarder.any_instance.expects(:forward_request).never
|
|
260
|
+
|
|
261
|
+
get :forward_request,
|
|
262
|
+
params: {
|
|
263
|
+
:format => :json,
|
|
264
|
+
:controller => 'vulnerabilities',
|
|
265
|
+
:path => 'api/vulnerability/v1/cves',
|
|
266
|
+
:organization_id => @other_org.id,
|
|
267
|
+
},
|
|
268
|
+
session: set_session_user(@restricted_user)
|
|
269
|
+
|
|
270
|
+
assert_equal 404, @response.status
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
test "rejects forward_request when location_id belongs to a location the user can't access" do
|
|
274
|
+
::ForemanRhCloud::InsightsApiForwarder.any_instance.expects(:forward_request).never
|
|
275
|
+
|
|
276
|
+
get :forward_request,
|
|
277
|
+
params: {
|
|
278
|
+
:format => :json,
|
|
279
|
+
:controller => 'vulnerabilities',
|
|
280
|
+
:path => 'api/vulnerability/v1/cves',
|
|
281
|
+
:organization_id => @org.id,
|
|
282
|
+
:location_id => @other_loc.id,
|
|
283
|
+
},
|
|
284
|
+
session: set_session_user(@restricted_user)
|
|
285
|
+
|
|
286
|
+
assert_equal 404, @response.status
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
context "#forward_request CSRF protection" do
|
|
291
|
+
setup do
|
|
292
|
+
ActionController::Base.allow_forgery_protection = true
|
|
293
|
+
as_admin do
|
|
294
|
+
@org = FactoryBot.create(:organization)
|
|
295
|
+
@loc = FactoryBot.create(:location)
|
|
296
|
+
end
|
|
297
|
+
User.current = nil
|
|
298
|
+
@forwarder = ForemanRhCloud::InsightsApiForwarder.any_instance
|
|
299
|
+
@forwarder.stubs(:forward_request).returns(successful_cloud_response)
|
|
300
|
+
@controller.stubs(:authorized).returns(true)
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
teardown do
|
|
304
|
+
ActionController::Base.allow_forgery_protection = false
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
test 'rejects a browser session without a CSRF token' do
|
|
308
|
+
@forwarder.expects(:forward_request).never
|
|
309
|
+
|
|
310
|
+
assert_raises(ActionController::InvalidAuthenticityToken) do
|
|
311
|
+
request_with(:post, session: set_session)
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
test 'rejects a browser session with an invalid CSRF token' do
|
|
316
|
+
@forwarder.expects(:forward_request).never
|
|
317
|
+
|
|
318
|
+
assert_raises(ActionController::InvalidAuthenticityToken) do
|
|
319
|
+
request_with(:post, session: set_session, headers: { 'X-CSRF-Token' => 'invalid' })
|
|
320
|
+
end
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
test 'forwards a browser session with a valid CSRF token' do
|
|
324
|
+
token = @controller.send(:form_authenticity_token)
|
|
325
|
+
|
|
326
|
+
request_with(:post, params: { authenticity_token: token }, session: set_session,
|
|
327
|
+
headers: { 'X-CSRF-Token' => token })
|
|
328
|
+
|
|
329
|
+
assert_response :success
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
test 'forwards valid basic credentials without a session or CSRF token' do
|
|
333
|
+
set_basic_auth(users(:admin), 'secret')
|
|
334
|
+
|
|
335
|
+
request_with(:post)
|
|
336
|
+
|
|
337
|
+
assert_response :success
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
test 'forwards valid personal access token without a session or CSRF token' do
|
|
341
|
+
user = users(:admin)
|
|
342
|
+
token_value = as_admin do
|
|
343
|
+
token = FactoryBot.create(:personal_access_token, user: user)
|
|
344
|
+
value = token.generate_token
|
|
345
|
+
token.save!
|
|
346
|
+
value
|
|
347
|
+
end
|
|
348
|
+
set_basic_auth(user, token_value)
|
|
349
|
+
|
|
350
|
+
request_with(:post)
|
|
351
|
+
|
|
352
|
+
assert_response :success
|
|
353
|
+
assert_equal user.id, session[:user]
|
|
354
|
+
assert session[:api_authenticated_session]
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
test 'rejects invalid credentials without a session or CSRF token' do
|
|
358
|
+
@forwarder.expects(:forward_request).never
|
|
359
|
+
set_basic_auth('admin', 'invalid')
|
|
360
|
+
|
|
361
|
+
request_with(:post)
|
|
362
|
+
|
|
363
|
+
assert_response :unauthorized
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
test 'rejects a browser session with authorization and without a CSRF token' do
|
|
367
|
+
@forwarder.expects(:forward_request).never
|
|
368
|
+
set_basic_auth(users(:admin), 'secret')
|
|
369
|
+
|
|
370
|
+
assert_raises(ActionController::InvalidAuthenticityToken) do
|
|
371
|
+
request_with(:post, session: set_session)
|
|
372
|
+
end
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
test 'rejects a browser session with authorization and an invalid CSRF token' do
|
|
376
|
+
@forwarder.expects(:forward_request).never
|
|
377
|
+
set_basic_auth(users(:admin), 'secret')
|
|
378
|
+
|
|
379
|
+
assert_raises(ActionController::InvalidAuthenticityToken) do
|
|
380
|
+
request_with(:post, session: set_session, headers: { 'X-CSRF-Token' => 'invalid' })
|
|
381
|
+
end
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
test 'rejects DELETE from a browser session without a CSRF token' do
|
|
385
|
+
@forwarder.expects(:forward_request).never
|
|
386
|
+
|
|
387
|
+
assert_raises(ActionController::InvalidAuthenticityToken) do
|
|
388
|
+
request_with(:delete, session: set_session)
|
|
389
|
+
end
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
test 'forwards DELETE with valid basic credentials without a session or CSRF token' do
|
|
393
|
+
set_basic_auth(users(:admin), 'secret')
|
|
394
|
+
|
|
395
|
+
request_with(:delete)
|
|
396
|
+
|
|
397
|
+
assert_response :success
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
test 'forwards DELETE with a valid personal access token without a session or CSRF token' do
|
|
401
|
+
user = users(:admin)
|
|
402
|
+
token_value = as_admin do
|
|
403
|
+
token = FactoryBot.create(:personal_access_token, user: user)
|
|
404
|
+
value = token.generate_token
|
|
405
|
+
token.save!
|
|
406
|
+
value
|
|
407
|
+
end
|
|
408
|
+
set_basic_auth(user, token_value)
|
|
409
|
+
|
|
410
|
+
request_with(:delete)
|
|
411
|
+
|
|
412
|
+
assert_response :success
|
|
413
|
+
assert_equal user.id, session[:user]
|
|
414
|
+
assert session[:api_authenticated_session]
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
private
|
|
418
|
+
|
|
419
|
+
def request_with(method, params: {}, session: nil, headers: {})
|
|
420
|
+
headers.each { |name, value| @request.headers[name] = value }
|
|
421
|
+
public_send(method, :forward_request,
|
|
422
|
+
params: forward_request_params.merge(params), session: session)
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
def forward_request_params
|
|
426
|
+
{ controller: 'vulnerabilities', path: 'api/vulnerability/v1/cves', organization_id: @org.id }
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
def successful_cloud_response
|
|
430
|
+
request = RestClient::Request.new(method: 'POST', url: 'http://test.theforeman.org')
|
|
431
|
+
response = Net::HTTPResponse.new(1.0, 200, 'OK')
|
|
432
|
+
response['content-type'] = 'application/json'
|
|
433
|
+
RestClient::Response.create('{}', response, request)
|
|
434
|
+
end
|
|
435
|
+
end
|
|
436
|
+
|
|
225
437
|
def set_session
|
|
226
438
|
set_session_user.merge(
|
|
227
439
|
organization_id: @org.id,
|
|
@@ -44,7 +44,7 @@ class GenerateHostReportTest < ActiveSupport::TestCase
|
|
|
44
44
|
)
|
|
45
45
|
action = run_action(action)
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
assert_equal "Generated report for organization id #{organization.id}", action.output[:result]
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
test 'generates report with filter' do
|
|
@@ -307,4 +307,76 @@ class HostInventoryReportJobTest < ActiveSupport::TestCase
|
|
|
307
307
|
FileUtils.rm_f(done_path)
|
|
308
308
|
FileUtils.rm_f(uploads_path)
|
|
309
309
|
end
|
|
310
|
+
|
|
311
|
+
def stub_upload_action(uploaded:)
|
|
312
|
+
upload_action = mock('upload_action')
|
|
313
|
+
upload_action.stubs(:output).returns(uploaded: uploaded)
|
|
314
|
+
upload_action
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
test 'uploaded? is false when upload was not requested' do
|
|
318
|
+
action = create_action(ForemanInventoryUpload::Async::HostInventoryReportJob)
|
|
319
|
+
action.expects(:action_subject).with(organization)
|
|
320
|
+
plan_action(action, base_folder, organization.id, hosts_filter, false)
|
|
321
|
+
action.stubs(:phase).returns(Dynflow::Action::Present)
|
|
322
|
+
|
|
323
|
+
refute action.uploaded?
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
test 'uploaded? reads output[:uploaded] from this report UploadReportDirectJob' do
|
|
327
|
+
action = create_action(ForemanInventoryUpload::Async::HostInventoryReportJob)
|
|
328
|
+
action.expects(:action_subject).with(organization)
|
|
329
|
+
plan_action(action, base_folder, organization.id, hosts_filter, true)
|
|
330
|
+
action.stubs(:phase).returns(Dynflow::Action::Present)
|
|
331
|
+
|
|
332
|
+
upload_action = stub_upload_action(uploaded: true)
|
|
333
|
+
action.stubs(:all_planned_actions)
|
|
334
|
+
.with(ForemanInventoryUpload::Async::UploadReportDirectJob)
|
|
335
|
+
.returns([upload_action])
|
|
336
|
+
|
|
337
|
+
assert action.uploaded?
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
test 'uploaded? remains true when a later step failed after upload completed' do
|
|
341
|
+
action = create_action(ForemanInventoryUpload::Async::HostInventoryReportJob)
|
|
342
|
+
action.expects(:action_subject).with(organization)
|
|
343
|
+
plan_action(action, base_folder, organization.id, hosts_filter, true)
|
|
344
|
+
action.stubs(:phase).returns(Dynflow::Action::Present)
|
|
345
|
+
|
|
346
|
+
# Upload finished successfully; a later sibling (e.g. CreateMissingInsightsFacets) failing
|
|
347
|
+
# must not change the recorded upload result for this report.
|
|
348
|
+
upload_action = stub_upload_action(uploaded: true)
|
|
349
|
+
action.stubs(:all_planned_actions)
|
|
350
|
+
.with(ForemanInventoryUpload::Async::UploadReportDirectJob)
|
|
351
|
+
.returns([upload_action])
|
|
352
|
+
|
|
353
|
+
assert action.uploaded?
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
test 'uploaded? only considers UploadReportDirectJob planned by this report job' do
|
|
357
|
+
other_organization = FactoryBot.create(:organization)
|
|
358
|
+
|
|
359
|
+
this_action = create_action(ForemanInventoryUpload::Async::HostInventoryReportJob)
|
|
360
|
+
this_action.expects(:action_subject).with(organization)
|
|
361
|
+
plan_action(this_action, base_folder, organization.id, hosts_filter, true)
|
|
362
|
+
this_action.stubs(:phase).returns(Dynflow::Action::Present)
|
|
363
|
+
|
|
364
|
+
other_action = create_action(ForemanInventoryUpload::Async::HostInventoryReportJob)
|
|
365
|
+
other_action.expects(:action_subject).with(other_organization)
|
|
366
|
+
plan_action(other_action, base_folder, other_organization.id, hosts_filter, true)
|
|
367
|
+
other_action.stubs(:phase).returns(Dynflow::Action::Present)
|
|
368
|
+
|
|
369
|
+
# Each report job only sees its own planned upload action via all_planned_actions.
|
|
370
|
+
this_upload = stub_upload_action(uploaded: false)
|
|
371
|
+
other_upload = stub_upload_action(uploaded: true)
|
|
372
|
+
this_action.stubs(:all_planned_actions)
|
|
373
|
+
.with(ForemanInventoryUpload::Async::UploadReportDirectJob)
|
|
374
|
+
.returns([this_upload])
|
|
375
|
+
other_action.stubs(:all_planned_actions)
|
|
376
|
+
.with(ForemanInventoryUpload::Async::UploadReportDirectJob)
|
|
377
|
+
.returns([other_upload])
|
|
378
|
+
|
|
379
|
+
refute this_action.uploaded?
|
|
380
|
+
assert other_action.uploaded?
|
|
381
|
+
end
|
|
310
382
|
end
|
|
@@ -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?(
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|