Skip to content

Commit aa201bc

Browse files
committed
SAT-35237 - Keep report in generated folder for disconnected users
* Report was being moved regardless of the setting so added logger statements and to return out of the task if the disconnected setting is on. * Added unit tests for queue_for_upload_job since we didn't have any and added some for the change I made (cherry picked from commit e4177f9)
1 parent 0cb6c25 commit aa201bc

File tree

5 files changed

+85
-16
lines changed

5 files changed

+85
-16
lines changed

lib/foreman_inventory_upload/async/generate_report_job.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ def plan(base_folder, organization_id, disconnected, hosts_filter = nil)
1414
hosts_filter: hosts_filter
1515
)
1616

17-
plan_action(
18-
QueueForUploadJob,
19-
base_folder,
20-
ForemanInventoryUpload.facts_archive_name(organization_id, hosts_filter),
21-
organization_id,
22-
disconnected
23-
)
17+
unless content_disconnected?(disconnected)
18+
plan_action(
19+
QueueForUploadJob,
20+
base_folder,
21+
ForemanInventoryUpload.facts_archive_name(organization_id, hosts_filter),
22+
organization_id
23+
)
24+
end
2425
end
2526
end
2627

@@ -40,6 +41,10 @@ def env
4041
)
4142
end
4243

44+
def content_disconnected?(disconnected)
45+
disconnected || !Setting[:subscription_connection_enabled]
46+
end
47+
4348
def base_folder
4449
input[:base_folder]
4550
end

lib/foreman_inventory_upload/async/queue_for_upload_job.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module ForemanInventoryUpload
22
module Async
33
class QueueForUploadJob < ::Actions::EntryAction
4-
def plan(base_folder, report_file, organization_id, disconnected)
4+
def plan(base_folder, report_file, organization_id)
55
enqueue_task = plan_self(base_folder: base_folder, report_file: report_file)
6-
plan_upload_report(enqueue_task.output[:enqueued_file_name], organization_id, disconnected)
6+
plan_upload_report(enqueue_task.output[:enqueued_file_name], organization_id)
77
end
88

99
def run
@@ -59,8 +59,8 @@ def report_file
5959
input[:report_file]
6060
end
6161

62-
def plan_upload_report(enqueued_file_name, organization_id, disconnected)
63-
plan_action(UploadReportJob, enqueued_file_name, organization_id, disconnected)
62+
def plan_upload_report(enqueued_file_name, organization_id)
63+
plan_action(UploadReportJob, enqueued_file_name, organization_id)
6464
end
6565
end
6666
end

lib/foreman_inventory_upload/async/upload_report_job.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ def self.output_label(label)
77
"upload_for_#{label}"
88
end
99

10-
def plan(filename, organization_id, disconnected = false)
10+
def plan(filename, organization_id)
1111
label = UploadReportJob.output_label(organization_id)
12-
super(label, filename: filename, organization_id: organization_id, disconnected: disconnected)
12+
super(label, filename: filename, organization_id: organization_id)
1313
end
1414

1515
def try_execute
1616
if content_disconnected?
1717
progress_output do |progress_output|
18-
progress_output.write_line('Upload canceled because connection to Insights is not enabled or the --no-upload option was passed.')
18+
progress_output.write_line("Report was not moved and upload was canceled because connection to Insights is not enabled. Report location: #{filename}.")
1919
progress_output.status = "Task aborted, exit 1"
2020
done!
2121
end
@@ -89,7 +89,7 @@ def organization
8989
end
9090

9191
def content_disconnected?
92-
input[:disconnected] || !Setting[:subscription_connection_enabled]
92+
!Setting[:subscription_connection_enabled]
9393
end
9494
end
9595
end
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require 'test_plugin_helper'
2+
require 'foreman_tasks/test_helpers'
3+
4+
class QueueForUploadJobTest < ActiveSupport::TestCase
5+
include ForemanTasks::TestHelpers::WithInThreadExecutor
6+
include FolderIsolation
7+
8+
let(:organization) { FactoryBot.create(:organization) }
9+
let(:base_folder) { @tmpdir }
10+
let(:report_file) { 'test_report.tar.xz' }
11+
let(:report_path) { File.join(base_folder, report_file) }
12+
let(:uploads_folder) { ForemanInventoryUpload.uploads_folder }
13+
subject { ForemanTasks.sync_task(ForemanInventoryUpload::Async::QueueForUploadJob, base_folder, report_file, organization.id) }
14+
15+
setup do
16+
# Stub the script template source
17+
script_source = File.join(ForemanRhCloud::Engine.root, 'lib/foreman_inventory_upload/scripts/uploader.sh.erb')
18+
File.stubs(:read).with(script_source).returns('#!/bin/bash\necho "Test script"')
19+
20+
# Stub template rendering
21+
Foreman::Renderer.stubs(:render).returns('#!/bin/bash\necho "Rendered script"')
22+
23+
# Stub additional settings that are accessed
24+
Setting.stubs(:[]).with(:content_default_http_proxy).returns(nil)
25+
Setting.stubs(:[]).with(:http_proxy).returns(nil)
26+
Setting.stubs(:[]).with("foreman_tasks_sync_task_timeout").returns(120)
27+
FileUtils.touch(report_path)
28+
end
29+
30+
teardown do
31+
FileUtils.rm_rf(uploads_folder) if Dir.exist?(uploads_folder)
32+
end
33+
34+
test 'plan method sets up the job correctly and calls plan_upload_report' do
35+
# Mock plan_upload_report to verify it's called
36+
ForemanInventoryUpload::Async::QueueForUploadJob.any_instance.expects(:plan_upload_report).once
37+
38+
assert_equal 'success', subject.result
39+
end
40+
41+
test 'run method processes file and moves it to uploads folder' do
42+
ForemanInventoryUpload::Async::QueueForUploadJob.any_instance.stubs(:plan_upload_report)
43+
44+
assert_equal 'success', subject.result
45+
46+
# Verify the file was moved
47+
refute File.exist?(report_path), "Original file should be moved"
48+
assert File.exist?(File.join(uploads_folder, report_file)), "File should exist in uploads folder"
49+
end
50+
51+
test 'creates necessary folders and scripts' do
52+
ForemanInventoryUpload::Async::QueueForUploadJob.any_instance.stubs(:plan_upload_report)
53+
54+
assert_equal 'success', subject.result
55+
56+
# Verify the uploads folder was created
57+
assert Dir.exist?(uploads_folder), "Uploads folder should be created"
58+
59+
# Verify the script file was created
60+
script_path = File.join(uploads_folder, ForemanInventoryUpload.upload_script_file)
61+
assert File.exist?(script_path), "Upload script should be created"
62+
end
63+
end

test/jobs/upload_report_job_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class UploadReportJobTest < ActiveSupport::TestCase
1818

1919
label = ForemanInventoryUpload::Async::UploadReportJob.output_label(organization.id)
2020
progress_output = ForemanInventoryUpload::Async::ProgressOutput.get(label)
21-
assert_match(/Upload canceled/, progress_output.full_output)
21+
assert_match(/upload was canceled because connection to Insights is not enabled/, progress_output.full_output)
22+
assert_match(/Report location:/, progress_output.full_output)
2223
assert_match(/exit 1/, progress_output.status)
2324
end
2425

0 commit comments

Comments
 (0)