foreman_rh_cloud 1.0.4.1 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/foreman_inventory_upload/accounts_controller.rb +1 -0
  3. data/app/controllers/foreman_inventory_upload/uploads_controller.rb +14 -7
  4. data/app/models/setting/rh_cloud.rb +13 -0
  5. data/config/routes.rb +1 -0
  6. data/lib/foreman_inventory_upload.rb +12 -0
  7. data/lib/foreman_inventory_upload/async/async_helpers.rb +13 -0
  8. data/lib/foreman_inventory_upload/async/generate_all_reports_job.rb +8 -0
  9. data/lib/foreman_inventory_upload/async/shell_process.rb +3 -1
  10. data/lib/foreman_inventory_upload/generators/archived_report.rb +6 -3
  11. data/lib/foreman_inventory_upload/generators/fact_helpers.rb +13 -0
  12. data/lib/foreman_inventory_upload/generators/queries.rb +2 -0
  13. data/lib/foreman_inventory_upload/generators/slice.rb +30 -10
  14. data/lib/foreman_rh_cloud/engine.rb +4 -0
  15. data/lib/foreman_rh_cloud/version.rb +1 -1
  16. data/test/controllers/accounts_controller_test.rb +8 -3
  17. data/test/controllers/uploads_controller_test.rb +13 -0
  18. data/test/unit/archived_report_generator_test.rb +1 -0
  19. data/test/unit/fact_helpers_test.rb +29 -0
  20. data/test/unit/slice_generator_test.rb +109 -12
  21. data/test/unit/slice_generator_test.rb.orig +280 -0
  22. data/webpack/ForemanInventoryUpload/Components/AccountList/AccountList.fixtures.js +2 -0
  23. data/webpack/ForemanInventoryUpload/Components/AccountList/AccountListActions.js +2 -1
  24. data/webpack/ForemanInventoryUpload/Components/AccountList/AccountListReducer.js +8 -0
  25. data/webpack/ForemanInventoryUpload/Components/AccountList/AccountListSelectors.js +2 -0
  26. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/AccountListReducer.test.js +11 -0
  27. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/AccountListSelectors.test.js +9 -1
  28. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/__snapshots__/AccountListReducer.test.js.snap +10 -0
  29. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/__snapshots__/AccountListSelectors.test.js.snap +5 -2
  30. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/AutoUploadSwitcher.fixtures.js +1 -0
  31. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/AutoUploadSwitcher.js +32 -0
  32. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/AutoUploadSwitcherActions.js +27 -0
  33. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/AutoUploadSwitcherConstants.js +2 -0
  34. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/__tests__/AutoUploadSwitcher.test.js +14 -0
  35. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/__tests__/AutoUploadSwitcherActions.test.js +14 -0
  36. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/__tests__/__snapshots__/AutoUploadSwitcher.test.js.snap +38 -0
  37. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/__tests__/__snapshots__/AutoUploadSwitcherActions.test.js.snap +14 -0
  38. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/autoUploadSwitcher.scss +3 -0
  39. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/index.js +17 -0
  40. data/webpack/ForemanInventoryUpload/Components/Dashboard/__tests__/DashboardIntegration.test.js +2 -1
  41. data/webpack/ForemanInventoryUpload/Components/PageHeader/PageHeader.js +21 -0
  42. data/webpack/ForemanInventoryUpload/Components/PageHeader/__tests__/PageHeader.test.js +13 -0
  43. data/webpack/ForemanInventoryUpload/Components/PageHeader/__tests__/__snapshots__/PageHeader.test.js.snap +33 -0
  44. data/webpack/ForemanInventoryUpload/Components/PageHeader/index.js +1 -0
  45. data/webpack/ForemanInventoryUpload/Components/ScheduledRun/ScheduledRun.fixtures.js +1 -0
  46. data/webpack/ForemanInventoryUpload/Components/ScheduledRun/ScheduledRun.js +4 -2
  47. data/webpack/ForemanInventoryUpload/Components/ScheduledRun/__tests__/ScheduledRun.test.js +0 -1
  48. data/webpack/ForemanInventoryUpload/Components/ScheduledRun/__tests__/__snapshots__/ScheduledRun.test.js.snap +0 -2
  49. data/webpack/ForemanInventoryUpload/Components/ScheduledRun/index.js +12 -1
  50. data/webpack/ForemanInventoryUpload/Components/TabBody/__tests__/__snapshots__/TabBody.test.js.snap +1 -1
  51. data/webpack/ForemanInventoryUpload/ForemanInventoryUpload.js +5 -4
  52. data/webpack/ForemanInventoryUpload/__snapshots__/ForemanInventoryUpload.test.js.snap +6 -5
  53. metadata +22 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5c1a5b84d72bf881ad6d2b9569113edefae817ca2f86058d80b8e96d07662e8
4
- data.tar.gz: 745f0cc63d929faf0811310c5d9430e919a5f4ba6e127e1a5e32314311cd36e2
3
+ metadata.gz: 58cd71785dedf35f475e09a442c930ba6e703c03a0dcf292a6786efa4b57d6c5
4
+ data.tar.gz: 0f87e840488f7381b2797012175d5be8a5cb1eae89febbea1c246e46fd2b8fcf
5
5
  SHA512:
6
- metadata.gz: 60501f4a0518539698b2480512e08266c8f9379779e3b344e65f48ec47629a3b364d936f650c14eae8d41988f16b933abfe7975e8181d049744b3fc2a24a38ec
7
- data.tar.gz: 75f29d922dbb485f0c675d18847b62d448126f02240b5ff2ba022d6d79e0f1fc08cc3fb352044d9400782b86ad29a908e7a6f96eb2b47c58b8d5be9a3a5a11fe
6
+ metadata.gz: c11c234bfbf1349223411b242317987a972aeb343fdeb4dfcc7fe4de8207b111c1bd2c4a5f10b86410e967a4d466b51fa6060670c220129e811f6c0af8b7cbea
7
+ data.tar.gz: 49d3ca3734f72c765c756c691cad4b66a33491e5772348f63a4358741a11c223348aa2cccd4d96537a2b03ee85c07e364eebbb223a2276972e3b1c17b150d41d
@@ -25,6 +25,7 @@ module ForemanInventoryUpload
25
25
  ]
26
26
 
27
27
  render json: {
28
+ autoUploadEnabled: Setting[:allow_auto_inventory_upload],
28
29
  accounts: accounts,
29
30
  }, status: :ok
30
31
  end
@@ -11,14 +11,10 @@ module ForemanInventoryUpload
11
11
 
12
12
  def download_file
13
13
  filename = ForemanInventoryUpload.facts_archive_name(params[:organization_id])
14
- path = Rails.root.join(ForemanInventoryUpload.uploads_folder, filename)
15
- unless File.exist? path
16
- return throw_flash_error(
17
- "Path doesn't exist: #{path}"
18
- )
19
- end
14
+ files = Dir["{#{ForemanInventoryUpload.uploads_file_path(filename)},#{ForemanInventoryUpload.done_file_path(filename)}}"]
20
15
 
21
- send_file path, disposition: 'attachment', filename: filename
16
+ return send_file files.first, disposition: 'attachment', filename: filename unless files.empty?
17
+ throw_flash_error "File doesn't exist"
22
18
  end
23
19
 
24
20
  def throw_flash_error(message)
@@ -27,5 +23,16 @@ module ForemanInventoryUpload
27
23
  :error_msg => message
28
24
  )
29
25
  end
26
+
27
+ def auto_upload
28
+ Setting[:allow_auto_inventory_upload] = auto_upload_params
29
+ render json: {
30
+ autoUploadEnabled: Setting[:allow_auto_inventory_upload],
31
+ }
32
+ end
33
+
34
+ def auto_upload_params
35
+ ActiveModel::Type::Boolean.new.cast(params.require(:value))
36
+ end
30
37
  end
31
38
  end
@@ -0,0 +1,13 @@
1
+ class Setting::RhCloud < Setting
2
+ def self.default_settings
3
+ return unless ActiveRecord::Base.connection.table_exists?('settings')
4
+ return unless super
5
+ [
6
+ set('allow_auto_inventory_upload', N_('Allow automatic upload of the host inventory to the Red Hat cloud'), true),
7
+ ]
8
+ end
9
+
10
+ def self.humanized_category
11
+ N_('RH Cloud')
12
+ end
13
+ end
@@ -6,5 +6,6 @@ Rails.application.routes.draw do
6
6
  get ':organization_id/uploads/last', to: 'uploads#last', constraints: { organization_id: %r{[^\/]+} }
7
7
  get ':organization_id/uploads/file', to: 'uploads#download_file', constraints: { organization_id: %r{[^\/]+} }
8
8
  get 'accounts', to: 'accounts#index'
9
+ post 'auto_upload', to: 'uploads#auto_upload'
9
10
  end
10
11
  end
@@ -18,6 +18,18 @@ module ForemanInventoryUpload
18
18
  )
19
19
  end
20
20
 
21
+ def self.uploads_file_path(filename)
22
+ File.join(ForemanInventoryUpload.uploads_folder, filename)
23
+ end
24
+
25
+ def self.done_folder
26
+ File.join(ForemanInventoryUpload.uploads_folder, 'done/')
27
+ end
28
+
29
+ def self.done_file_path(filename)
30
+ File.join(ForemanInventoryUpload.done_folder, filename)
31
+ end
32
+
21
33
  def self.generated_reports_folder
22
34
  @generated_reports_folder ||= ensure_folder(
23
35
  File.join(
@@ -0,0 +1,13 @@
1
+ module ForemanInventoryUpload
2
+ module Async
3
+ module AsyncHelpers
4
+ extend ActiveSupport::Concern
5
+
6
+ def hash_to_s(hash)
7
+ hash.each_with_object({}) do |(k, v), a|
8
+ a[k.to_s] = v.to_s
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -2,6 +2,14 @@ module ForemanInventoryUpload
2
2
  module Async
3
3
  class GenerateAllReportsJob < ::ApplicationJob
4
4
  def perform
5
+ unless Setting[:allow_auto_inventory_upload]
6
+ logger.debug(
7
+ 'The scheduled process is disabled due to the "allow_auto_inventory_upload"
8
+ setting being set to false.'
9
+ )
10
+ return
11
+ end
12
+
5
13
  organizations = Organization.unscoped.all
6
14
 
7
15
  organizations.map do |organization|
@@ -3,11 +3,13 @@ require 'open3'
3
3
  module ForemanInventoryUpload
4
4
  module Async
5
5
  class ShellProcess < ::ApplicationJob
6
+ include AsyncHelpers
7
+
6
8
  def perform(instance_label)
7
9
  klass_name = self.class.name
8
10
  logger.debug("Starting #{klass_name} with label #{instance_label}")
9
11
  progress_output = ProgressOutput.register(instance_label)
10
- Open3.popen2e(env, command) do |_stdin, stdout_stderr, wait_thread|
12
+ Open3.popen2e(hash_to_s(env), command) do |_stdin, stdout_stderr, wait_thread|
11
13
  progress_output.status = "Running in pid #{wait_thread.pid}"
12
14
 
13
15
  stdout_stderr.each do |out_line|
@@ -16,9 +16,9 @@ module ForemanInventoryUpload
16
16
  first = true
17
17
  host_batches.each do |hosts_batch|
18
18
  slice_id = Foreman.uuid
19
- hosts_count = hosts_batch.count
20
- @logger.info "Adding slice #{slice_id} with #{hosts_count} hosts"
21
- generate_slice(tmpdir, slice_id, hosts_batch)
19
+ @logger.info "Adding slice #{slice_id}"
20
+ hosts_count = generate_slice(tmpdir, slice_id, hosts_batch)
21
+ @logger.info "slice #{slice_id} was created with #{hosts_count} hosts"
22
22
  inner_generator.add_slice(slice_id, hosts_count, first)
23
23
  first = false
24
24
  end
@@ -43,10 +43,13 @@ module ForemanInventoryUpload
43
43
  private
44
44
 
45
45
  def generate_slice(tmpdir, slice_id, hosts_batch)
46
+ hosts_count = 0
46
47
  File.open(File.join(tmpdir, "#{slice_id}.json"), 'w') do |slice_out|
47
48
  slice_generator = ForemanInventoryUpload::Generators::Slice.new(hosts_batch, slice_out, slice_id)
48
49
  slice_generator.render
50
+ hosts_count = slice_generator.hosts_count
49
51
  end
52
+ hosts_count
50
53
  end
51
54
  end
52
55
  end
@@ -13,6 +13,19 @@ module ForemanInventoryUpload
13
13
  def kilobytes_to_bytes(kilobytes)
14
14
  kilobytes * 1024
15
15
  end
16
+
17
+ def account_id(organization)
18
+ @organization_accounts ||= {}
19
+ @organization_accounts[organization.id] ||= organization.pools.where.not(account_number: nil).pluck(:account_number).first
20
+ end
21
+
22
+ def golden_ticket?(organization)
23
+ result = organization.try(:golden_ticket?)
24
+ result = organization.content_access_mode == 'org_environment' if result.nil?
25
+
26
+ @organization_golden_tickets ||= {}
27
+ @organization_golden_tickets[organization.id] ||= result
28
+ end
16
29
  end
17
30
  end
18
31
  end
@@ -18,6 +18,8 @@ module ForemanInventoryUpload
18
18
  'uname::release',
19
19
  'lscpu::flags',
20
20
  'distribution::version',
21
+ 'distribution::id',
22
+ 'virt::is_guest',
21
23
  ]).pluck(:name, :id)
22
24
  ]
23
25
  end
@@ -4,11 +4,13 @@ module ForemanInventoryUpload
4
4
  include FactHelpers
5
5
 
6
6
  attr_accessor :slice_id
7
+ attr_reader :hosts_count
7
8
 
8
9
  def initialize(hosts, output = [], slice_id = Foreman.uuid)
9
10
  @stream = JsonStream.new(output)
10
11
  @hosts = hosts
11
12
  @slice_id = slice_id
13
+ @hosts_count = 0
12
14
  end
13
15
 
14
16
  def render
@@ -24,9 +26,12 @@ module ForemanInventoryUpload
24
26
  @stream.array_field('hosts', :last) do
25
27
  first = true
26
28
  hosts_batch.each do |host|
27
- next unless host&.subscription_facet&.pools&.first
29
+ next unless host&.subscription_facet
28
30
  @stream.comma unless first
29
- first = false if report_host(host)
31
+ if report_host(host)
32
+ first = false
33
+ @hosts_count += 1
34
+ end
30
35
  end
31
36
  end
32
37
  end
@@ -34,11 +39,10 @@ module ForemanInventoryUpload
34
39
 
35
40
  def report_host(host)
36
41
  @stream.object do
37
- @stream.simple_field('display_name', host.name)
38
42
  @stream.simple_field('fqdn', host.fqdn)
39
- @stream.simple_field('account', host.subscription_facet.pools.first.account_number.to_s)
40
- @stream.simple_field('subscription_manager_id', host.subscription_facet.uuid)
41
- @stream.simple_field('satellite_id', host.subscription_facet.uuid)
43
+ @stream.simple_field('account', account_id(host.organization).to_s)
44
+ @stream.simple_field('subscription_manager_id', host.subscription_facet&.uuid)
45
+ @stream.simple_field('satellite_id', host.subscription_facet&.uuid)
42
46
  @stream.simple_field('bios_uuid', fact_value(host, 'dmi::system::uuid'))
43
47
  @stream.simple_field('vm_uuid', fact_value(host, 'virt::uuid'))
44
48
  @stream.array_field('ip_addresses') do
@@ -105,12 +109,23 @@ module ForemanInventoryUpload
105
109
  end.join(', '))
106
110
  end
107
111
  end
108
- @stream.simple_field('os_release', fact_value(host, 'distribution::name'))
112
+ @stream.simple_field(
113
+ 'os_release',
114
+ os_release_value(
115
+ name: fact_value(host, 'distribution::name'),
116
+ version: fact_value(host, 'distribution::version'),
117
+ codename: fact_value(host, 'distribution::id')
118
+ )
119
+ )
109
120
  @stream.simple_field('os_kernel_version', fact_value(host, 'uname::release'))
110
121
  @stream.simple_field('arch', host.architecture&.name)
111
122
  @stream.simple_field('subscription_status', host.subscription_status_label)
112
123
  @stream.simple_field('katello_agent_running', host.content_facet&.katello_agent_installed?)
113
124
  @stream.simple_field('satellite_managed', true)
125
+ @stream.simple_field(
126
+ 'infrastructure_type',
127
+ ActiveModel::Type::Boolean.new.cast(fact_value(host, 'virt::is_guest')) ? 'virtual' : 'physical'
128
+ )
114
129
  unless (installed_products = host.subscription_facet&.installed_products).empty?
115
130
  @stream.array_field('installed_products') do
116
131
  @stream.raw(installed_products.map do |product|
@@ -131,17 +146,22 @@ module ForemanInventoryUpload
131
146
  end
132
147
 
133
148
  def report_satellite_facts(host)
134
- @stream.simple_field('virtual_host_name', host.subscription_facet.hypervisor_host&.name)
135
- @stream.simple_field('virtual_host_uuid', host.subscription_facet.hypervisor_host&.subscription_facet&.uuid)
149
+ @stream.simple_field('virtual_host_name', host.subscription_facet&.hypervisor_host&.name)
150
+ @stream.simple_field('virtual_host_uuid', host.subscription_facet&.hypervisor_host&.subscription_facet&.uuid)
136
151
  if defined?(ForemanThemeSatellite)
137
152
  @stream.simple_field('satellite_version', ForemanThemeSatellite::SATELLITE_VERSION)
138
153
  end
139
154
  @stream.simple_field('system_purpose_usage', host.subscription_facet.purpose_usage)
140
155
  @stream.simple_field('system_purpose_role', host.subscription_facet.purpose_role)
141
156
  @stream.simple_field('distribution_version', fact_value(host, 'distribution::version'))
142
- @stream.simple_field('satellite_instance_id', Foreman.respond_to?(:instance_id) ? Foreman.instance_id : nil)
157
+ @stream.simple_field('satellite_instance_id', Foreman.try(:instance_id))
158
+ @stream.simple_field('is_simple_content_access', golden_ticket?(host.organization))
143
159
  @stream.simple_field('organization_id', host.organization_id, :last)
144
160
  end
161
+
162
+ def os_release_value(name:, version:, codename:)
163
+ "#{name} #{version} (#{codename})"
164
+ end
145
165
  end
146
166
  end
147
167
  end
@@ -5,6 +5,10 @@ module ForemanRhCloud
5
5
  class Engine < ::Rails::Engine
6
6
  engine_name 'foreman_rh_cloud'
7
7
 
8
+ initializer 'foreman_rh_cloud.load_default_settings', :before => :load_config_initializers do
9
+ require_dependency File.expand_path('../../app/models/setting/rh_cloud.rb', __dir__)
10
+ end
11
+
8
12
  config.autoload_paths += Dir["#{config.root}/app/controllers/concerns"]
9
13
  config.autoload_paths += Dir["#{config.root}/app/helpers/concerns"]
10
14
  config.autoload_paths += Dir["#{config.root}/app/models/concerns"]
@@ -1,3 +1,3 @@
1
1
  module ForemanRhCloud
2
- VERSION = '1.0.4.1'.freeze
2
+ VERSION = '1.0.6'.freeze
3
3
  end
@@ -14,12 +14,17 @@ class AccountsControllerTest < ActionController::TestCase
14
14
  upload_label = ForemanInventoryUpload::Async::UploadReportJob.output_label(test_org.id)
15
15
  upload_output = ForemanInventoryUpload::Async::ProgressOutput.register(upload_label)
16
16
  upload_output.status = 'upload_status_test'
17
+ FactoryBot.create(:setting, :name => 'allow_auto_inventory_upload', :value => true)
18
+ assert_equal true, Setting[:allow_auto_inventory_upload]
17
19
 
18
20
  get :index, session: set_session_user
19
21
 
20
22
  assert_response :success
21
- actual = JSON.parse(response.body)['accounts'][test_org.id.to_s]
22
- assert_equal 'generate_status_test', actual['generate_report_status']
23
- assert_equal 'upload_status_test', actual['upload_report_status']
23
+ actual = JSON.parse(response.body)
24
+ actual_account_statuses = actual['accounts'][test_org.id.to_s]
25
+ assert_equal 'generate_status_test', actual_account_statuses['generate_report_status']
26
+ assert_equal 'upload_status_test', actual_account_statuses['upload_report_status']
27
+
28
+ assert_equal true, actual['autoUploadEnabled']
24
29
  end
25
30
  end
@@ -18,4 +18,17 @@ class UploadsControllerTest < ActionController::TestCase
18
18
  actual = JSON.parse(response.body)
19
19
  assert_equal 'test output', actual['output']
20
20
  end
21
+
22
+ test 'should update allow_auto_inventory_upload setting' do
23
+ FactoryBot.create(:setting, :name => 'allow_auto_inventory_upload', :settings_type => "boolean", :category => "Setting::RhCloud", :default => false, :value => false)
24
+
25
+ assert_equal false, Setting[:allow_auto_inventory_upload]
26
+
27
+ post :auto_upload, params: { value: true }, session: set_session_user
28
+
29
+ assert_response :success
30
+ actual = JSON.parse(response.body)
31
+ assert_equal true, actual['autoUploadEnabled']
32
+ assert_equal true, Setting[:allow_auto_inventory_upload]
33
+ end
21
34
  end
@@ -49,6 +49,7 @@ class ArchivedReportGeneratorTest < ActiveSupport::TestCase
49
49
  test_org = FactoryBot.create(:organization)
50
50
 
51
51
  ForemanInventoryUpload::Generators::Queries.expects(:for_org).with(test_org.id).returns(batches)
52
+ ForemanInventoryUpload::Generators::Slice.any_instance.stubs(:golden_ticket?).returns(false)
52
53
  Dir.mktmpdir do |tmpdir|
53
54
  target = File.join(tmpdir, 'test.tar.gz')
54
55
  generator = ForemanInventoryUpload::Generators::ArchivedReport.new(target, Logger.new(STDOUT))
@@ -0,0 +1,29 @@
1
+ require 'test_plugin_helper'
2
+
3
+ class FactHelpersTest < ActiveSupport::TestCase
4
+ class FactsHelpersTestStub
5
+ include ForemanInventoryUpload::Generators::FactHelpers
6
+ end
7
+
8
+ setup do
9
+ @instance = FactsHelpersTestStub.new
10
+
11
+ @org = FactoryBot.create(:organization)
12
+ end
13
+
14
+ test 'golden_ticket uses golden_ticket method when defined' do
15
+ @org.expects(:golden_ticket?).returns(true)
16
+
17
+ actual = @instance.golden_ticket?(@org)
18
+
19
+ assert actual
20
+ end
21
+
22
+ test 'golden_ticket uses content_access_mode method when golden_ticket not defined' do
23
+ @org.expects(:content_access_mode).returns('org_environment')
24
+
25
+ actual = @instance.golden_ticket?(@org)
26
+
27
+ assert actual
28
+ end
29
+ end
@@ -16,7 +16,9 @@ class ReportGeneratorTest < ActiveSupport::TestCase
16
16
  organization: env.organization
17
17
  )
18
18
 
19
- @host.subscription_facet.pools << FactoryBot.create(:katello_pool, account_number: '1234', cp_id: 1)
19
+ @host.organization.pools << FactoryBot.create(:katello_pool, account_number: '1234', cp_id: 1)
20
+
21
+ ForemanInventoryUpload::Generators::Queries.instance_variable_set(:@fact_names, nil)
20
22
  end
21
23
 
22
24
  def interesting_facts
@@ -30,9 +32,12 @@ class ReportGeneratorTest < ActiveSupport::TestCase
30
32
  'dmi::bios::vendor',
31
33
  'dmi::bios::version',
32
34
  'dmi::bios::relase_date',
33
- 'distribution::name',
34
35
  'uname::release',
35
36
  'lscpu::flags',
37
+ 'distribution::name',
38
+ 'distribution::version',
39
+ 'distribution::id',
40
+ 'virt::is_guest',
36
41
  ]
37
42
  end
38
43
 
@@ -46,22 +51,22 @@ class ReportGeneratorTest < ActiveSupport::TestCase
46
51
 
47
52
  test 'generates a report for a single host' do
48
53
  batch = Host.where(id: @host.id).in_batches.first
49
- generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], 'slice_123')
54
+ generator = create_generator(batch)
50
55
 
51
56
  json_str = generator.render
52
57
  actual = JSON.parse(json_str.join("\n"))
53
58
 
54
59
  assert_equal 'slice_123', actual['report_slice_id']
55
60
  assert_not_nil(actual_host = actual['hosts'].first)
56
- assert_equal @host.name, actual_host['display_name']
57
61
  assert_equal @host.fqdn, actual_host['fqdn']
58
62
  assert_equal '1234', actual_host['account']
63
+ assert_equal 1, generator.hosts_count
59
64
  end
60
65
 
61
66
  test 'generates a report with satellite facts' do
62
67
  Foreman.expects(:instance_id).twice.returns('satellite-id')
63
68
  batch = Host.where(id: @host.id).in_batches.first
64
- generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], 'slice-123')
69
+ generator = create_generator(batch)
65
70
 
66
71
  json_str = generator.render
67
72
  actual = JSON.parse(json_str.join("\n"))
@@ -102,14 +107,13 @@ class ReportGeneratorTest < ActiveSupport::TestCase
102
107
  @host.save!
103
108
 
104
109
  batch = Host.where(id: @host.id).in_batches.first
105
- generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], 'slice_123')
110
+ generator = create_generator(batch)
106
111
 
107
112
  json_str = generator.render
108
113
  actual = JSON.parse(json_str.join("\n"))
109
114
 
110
115
  assert_equal 'slice_123', actual['report_slice_id']
111
116
  assert_not_nil(actual_host = actual['hosts'].first)
112
- assert_equal @host.name, actual_host['display_name']
113
117
  assert_equal @host.fqdn, actual_host['fqdn']
114
118
  assert_not_nil(host_facts = actual_host['facts']&.first)
115
119
  assert_equal 'satellite', host_facts['namespace']
@@ -124,14 +128,13 @@ class ReportGeneratorTest < ActiveSupport::TestCase
124
128
  @host.subscription_facet.save!
125
129
 
126
130
  batch = Host.where(id: @host.id).in_batches.first
127
- generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], 'slice_123')
131
+ generator = create_generator(batch)
128
132
 
129
133
  json_str = generator.render
130
134
  actual = JSON.parse(json_str.join("\n"))
131
135
 
132
136
  assert_equal 'slice_123', actual['report_slice_id']
133
137
  assert_not_nil(actual_host = actual['hosts'].first)
134
- assert_equal @host.name, actual_host['display_name']
135
138
  assert_equal @host.fqdn, actual_host['fqdn']
136
139
  assert_not_nil(host_facts = actual_host['facts']&.first)
137
140
  assert_equal 'satellite', host_facts['namespace']
@@ -140,6 +143,22 @@ class ReportGeneratorTest < ActiveSupport::TestCase
140
143
  assert_equal 'test_role', fact_values['system_purpose_role']
141
144
  end
142
145
 
146
+ test 'generates a report for a golden ticket' do
147
+ batch = Host.where(id: @host.id).in_batches.first
148
+ generator = create_generator(batch) do |generator|
149
+ generator.stubs(:golden_ticket?).returns(true)
150
+ end
151
+
152
+ json_str = generator.render
153
+ actual = JSON.parse(json_str.join("\n"))
154
+
155
+ assert_equal 'slice_123', actual['report_slice_id']
156
+ assert_not_nil(actual_host = actual['hosts'].first)
157
+ assert_equal @host.fqdn, actual_host['fqdn']
158
+ assert_equal '1234', actual_host['account']
159
+ assert_equal 1, generator.hosts_count
160
+ end
161
+
143
162
  test 'skips hosts without subscription' do
144
163
  a_host = FactoryBot.create(
145
164
  :host,
@@ -148,23 +167,23 @@ class ReportGeneratorTest < ActiveSupport::TestCase
148
167
 
149
168
  # make a_host last
150
169
  batch = Host.where(id: [@host.id, a_host.id]).order(:name).in_batches.first
151
- generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], 'slice_123')
170
+ generator = create_generator(batch)
152
171
 
153
172
  json_str = generator.render
154
173
  actual = JSON.parse(json_str.join("\n"))
155
174
 
156
175
  assert_equal 'slice_123', actual['report_slice_id']
157
176
  assert_not_nil(actual_host = actual['hosts'].first)
158
- assert_equal @host.name, actual_host['display_name']
159
177
  assert_equal @host.fqdn, actual_host['fqdn']
160
178
  assert_equal '1234', actual_host['account']
179
+ assert_equal 1, generator.hosts_count
161
180
  end
162
181
 
163
182
  test 'shows system_memory_bytes in bytes' do
164
183
  FactoryBot.create(:fact_value, fact_name: fact_names['memory::memtotal'], value: '1', host: @host)
165
184
 
166
185
  batch = Host.where(id: @host.id).in_batches.first
167
- generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], 'slice_123')
186
+ generator = create_generator(batch)
168
187
 
169
188
  json_str = generator.render
170
189
  actual = JSON.parse(json_str.join("\n"))
@@ -174,4 +193,82 @@ class ReportGeneratorTest < ActiveSupport::TestCase
174
193
  assert_not_nil(actual_profile = actual_host['system_profile'])
175
194
  assert_equal 1024, actual_profile['system_memory_bytes']
176
195
  end
196
+
197
+ test 'reports an account for hosts with multiple pools' do
198
+ first_pool = @host.organization.pools.first
199
+ second_pool = FactoryBot.create(:katello_pool, account_number: nil, cp_id: 2)
200
+ new_org = FactoryBot.create(:organization, pools: [first_pool, second_pool])
201
+ @host.organization = new_org
202
+ @host.save!
203
+
204
+ batch = Host.where(id: @host.id).in_batches.first
205
+ generator = create_generator(batch)
206
+
207
+ json_str = generator.render
208
+ actual = JSON.parse(json_str.join("\n"))
209
+
210
+ assert_equal 'slice_123', actual['report_slice_id']
211
+ assert_not_nil(actual_host = actual['hosts'].first)
212
+ assert_not_nil(actual_host['account'])
213
+ assert_not_empty(actual_host['account'])
214
+ end
215
+
216
+ test 'Generates os_release with version and id' do
217
+ FactoryBot.create(:fact_value, fact_name: fact_names['distribution::name'], value: 'Red Hat Test Linux', host: @host)
218
+ FactoryBot.create(:fact_value, fact_name: fact_names['distribution::version'], value: '7.1', host: @host)
219
+ FactoryBot.create(:fact_value, fact_name: fact_names['distribution::id'], value: 'TestId', host: @host)
220
+
221
+ batch = Host.where(id: @host.id).in_batches.first
222
+ generator = create_generator(batch)
223
+
224
+ json_str = generator.render
225
+ actual = JSON.parse(json_str.join("\n"))
226
+
227
+ assert_equal 'slice_123', actual['report_slice_id']
228
+ assert_not_nil(actual_host = actual['hosts'].first)
229
+ assert_not_nil(actual_profile = actual_host['system_profile'])
230
+ assert_equal 'Red Hat Test Linux 7.1 (TestId)', actual_profile['os_release']
231
+ end
232
+
233
+ test 'sets infrastructure_type to "virtual" based on virt.is_guest fact' do
234
+ FactoryBot.create(:fact_value, fact_name: fact_names['virt::is_guest'], value: true, host: @host)
235
+
236
+ batch = Host.where(id: @host.id).in_batches.first
237
+ generator = create_generator(batch)
238
+
239
+ json_str = generator.render
240
+ actual = JSON.parse(json_str.join("\n"))
241
+
242
+ assert_equal 'slice_123', actual['report_slice_id']
243
+ assert_not_nil(actual_host = actual['hosts'].first)
244
+ assert_not_nil(actual_profile = actual_host['system_profile'])
245
+ assert_equal 'virtual', actual_profile['infrastructure_type']
246
+ end
247
+
248
+ test 'sets infrastructure_type to "physical" based on virt.is_guest fact' do
249
+ FactoryBot.create(:fact_value, fact_name: fact_names['virt::is_guest'], value: false, host: @host)
250
+
251
+ batch = Host.where(id: @host.id).in_batches.first
252
+ generator = create_generator(batch)
253
+
254
+ json_str = generator.render
255
+ actual = JSON.parse(json_str.join("\n"))
256
+
257
+ assert_equal 'slice_123', actual['report_slice_id']
258
+ assert_not_nil(actual_host = actual['hosts'].first)
259
+ assert_not_nil(actual_profile = actual_host['system_profile'])
260
+ assert_equal 'physical', actual_profile['infrastructure_type']
261
+ end
262
+
263
+ private
264
+
265
+ def create_generator(batch, name = 'slice_123')
266
+ generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], name)
267
+ if block_given?
268
+ yield(generator)
269
+ else
270
+ generator.stubs(:golden_ticket?).returns(false)
271
+ end
272
+ generator
273
+ end
177
274
  end