foreman_rh_cloud 1.0.4.1 → 1.0.9

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 (172) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/foreman_inventory_upload/accounts_controller.rb +4 -6
  3. data/app/controllers/foreman_inventory_upload/uploads_controller.rb +33 -7
  4. data/app/controllers/foreman_rh_cloud/react_controller.rb +11 -0
  5. data/app/controllers/insights_cloud/tasks_controller.rb +8 -0
  6. data/app/helpers/foreman_inventory_upload_host_helper.rb +11 -0
  7. data/app/models/insights_facet.rb +3 -0
  8. data/app/models/insights_hit.rb +6 -0
  9. data/app/models/setting/rh_cloud.rb +15 -0
  10. data/app/overrides/hosts_list.rb +13 -0
  11. data/app/views/foreman_rh_cloud/react/insights_cloud.html.erb +6 -0
  12. data/app/views/foreman_rh_cloud/react/inventory_upload.html.erb +1 -0
  13. data/app/views/{foreman_inventory_upload/layouts/react.html.erb → layouts/foreman_rh_cloud/application.html.erb} +2 -2
  14. data/config/routes.rb +12 -1
  15. data/db/migrate/20191215104806_create_insights_hits.foreman_inventory_upload.rb +14 -0
  16. data/db/migrate/20191216062008_create_insights_facets.foreman_inventory_upload.rb +7 -0
  17. data/lib/foreman_inventory_upload.rb +12 -0
  18. data/lib/foreman_inventory_upload/async/async_helpers.rb +13 -0
  19. data/lib/foreman_inventory_upload/async/generate_all_reports_job.rb +8 -0
  20. data/lib/foreman_inventory_upload/async/shell_process.rb +3 -1
  21. data/lib/foreman_inventory_upload/generators/archived_report.rb +6 -3
  22. data/lib/foreman_inventory_upload/generators/fact_helpers.rb +56 -0
  23. data/lib/foreman_inventory_upload/generators/json_stream.rb +10 -0
  24. data/lib/foreman_inventory_upload/generators/queries.rb +7 -0
  25. data/lib/foreman_inventory_upload/generators/slice.rb +47 -21
  26. data/lib/foreman_inventory_upload/notifications/manifest_import_success_notification_override.rb +19 -0
  27. data/lib/foreman_rh_cloud/engine.rb +33 -5
  28. data/lib/foreman_rh_cloud/version.rb +1 -1
  29. data/lib/insights_cloud.rb +15 -0
  30. data/lib/insights_cloud/async/insights_full_sync.rb +96 -0
  31. data/test/controllers/accounts_controller_test.rb +8 -3
  32. data/test/controllers/uploads_controller_test.rb +13 -0
  33. data/test/jobs/insights_full_sync_test.rb +92 -0
  34. data/test/unit/archived_report_generator_test.rb +1 -0
  35. data/test/unit/fact_helpers_test.rb +29 -0
  36. data/test/unit/slice_generator_test.rb +274 -12
  37. data/webpack/ForemanInventoryUpload/Components/AccountList/AccountList.fixtures.js +14 -2
  38. data/webpack/ForemanInventoryUpload/Components/AccountList/AccountList.js +13 -10
  39. data/webpack/ForemanInventoryUpload/Components/AccountList/AccountListActions.js +6 -3
  40. data/webpack/ForemanInventoryUpload/Components/AccountList/AccountListHelper.js +10 -0
  41. data/webpack/ForemanInventoryUpload/Components/AccountList/AccountListReducer.js +16 -0
  42. data/webpack/ForemanInventoryUpload/Components/AccountList/AccountListSelectors.js +10 -1
  43. data/webpack/ForemanInventoryUpload/Components/AccountList/Components/EmptyResults/EmptyResults.js +15 -0
  44. data/webpack/ForemanInventoryUpload/Components/AccountList/Components/EmptyResults/__tests__/EmptyResults.test.js +13 -0
  45. data/webpack/ForemanInventoryUpload/Components/AccountList/Components/EmptyResults/__tests__/__snapshots__/EmptyResults.test.js.snap +18 -0
  46. data/webpack/ForemanInventoryUpload/Components/AccountList/Components/EmptyResults/emptyResults.scss +7 -0
  47. data/webpack/ForemanInventoryUpload/Components/AccountList/Components/EmptyResults/index.js +1 -0
  48. data/webpack/ForemanInventoryUpload/Components/AccountList/Components/ListItem/ListItem.js +1 -4
  49. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/AccountList.test.js +1 -0
  50. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/AccountListActions.test.js +8 -1
  51. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/AccountListHelper.test.js +12 -0
  52. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/AccountListIntegration.test.js +1 -1
  53. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/AccountListReducer.test.js +22 -0
  54. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/AccountListSelectors.test.js +20 -8
  55. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/__snapshots__/AccountList.test.js.snap +2 -3
  56. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/__snapshots__/AccountListActions.test.js.snap +20 -2
  57. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/__snapshots__/AccountListReducer.test.js.snap +20 -0
  58. data/webpack/ForemanInventoryUpload/Components/AccountList/__tests__/__snapshots__/AccountListSelectors.test.js.snap +8 -2
  59. data/webpack/ForemanInventoryUpload/Components/AccountList/index.js +2 -0
  60. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/AutoUploadSwitcher.fixtures.js +1 -0
  61. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/AutoUploadSwitcher.js +32 -0
  62. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/AutoUploadSwitcherActions.js +30 -0
  63. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/AutoUploadSwitcherConstants.js +2 -0
  64. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/__tests__/AutoUploadSwitcher.test.js +14 -0
  65. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/__tests__/AutoUploadSwitcherActions.test.js +14 -0
  66. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/__tests__/__snapshots__/AutoUploadSwitcher.test.js.snap +38 -0
  67. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/__tests__/__snapshots__/AutoUploadSwitcherActions.test.js.snap +14 -0
  68. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/autoUploadSwitcher.scss +3 -0
  69. data/webpack/ForemanInventoryUpload/Components/AutoUploadSwitcher/index.js +17 -0
  70. data/webpack/ForemanInventoryUpload/Components/Dashboard/DashboardActions.js +4 -3
  71. data/webpack/ForemanInventoryUpload/Components/Dashboard/DashboardSelectors.js +1 -1
  72. data/webpack/ForemanInventoryUpload/Components/Dashboard/__tests__/DashboardActions.test.js +2 -3
  73. data/webpack/ForemanInventoryUpload/Components/Dashboard/__tests__/DashboardIntegration.test.js +2 -1
  74. data/webpack/ForemanInventoryUpload/Components/Dashboard/__tests__/DashboardSelectors.test.js +3 -2
  75. data/webpack/ForemanInventoryUpload/Components/HostObfuscationSwitcher/HostObfuscationSwitcher.fixtures.js +1 -0
  76. data/webpack/ForemanInventoryUpload/Components/HostObfuscationSwitcher/HostObfuscationSwitcher.js +29 -0
  77. data/webpack/ForemanInventoryUpload/Components/HostObfuscationSwitcher/HostObfuscationSwitcherActions.js +30 -0
  78. data/webpack/ForemanInventoryUpload/Components/HostObfuscationSwitcher/HostObfuscationSwitcherConstants.js +3 -0
  79. data/webpack/ForemanInventoryUpload/Components/HostObfuscationSwitcher/__tests__/HostObfuscationSwitcher.test.js +14 -0
  80. data/webpack/ForemanInventoryUpload/Components/HostObfuscationSwitcher/__tests__/HostObfuscationSwitcherActions.test.js +14 -0
  81. data/webpack/ForemanInventoryUpload/Components/HostObfuscationSwitcher/__tests__/__snapshots__/HostObfuscationSwitcher.test.js.snap +38 -0
  82. data/webpack/ForemanInventoryUpload/Components/HostObfuscationSwitcher/__tests__/__snapshots__/HostObfuscationSwitcherActions.test.js.snap +14 -0
  83. data/webpack/ForemanInventoryUpload/Components/HostObfuscationSwitcher/hostObfuscationSwitcher.scss +0 -0
  84. data/webpack/ForemanInventoryUpload/Components/HostObfuscationSwitcher/index.js +20 -0
  85. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/Components/ClearButton/ClearButton.js +26 -0
  86. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/Components/ClearButton/index.js +1 -0
  87. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/InventoryFilter.fixtures.js +2 -0
  88. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/InventoryFilter.js +39 -0
  89. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/InventoryFilterActions.js +16 -0
  90. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/InventoryFilterConstants.js +3 -0
  91. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/InventoryFilterReducer.js +36 -0
  92. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/InventoryFilterSelectors.js +7 -0
  93. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/__tests__/InventoryFilter.test.js +14 -0
  94. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/__tests__/InventoryFilterActions.test.js +14 -0
  95. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/__tests__/InventoryFilterReducer.test.js +35 -0
  96. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/__tests__/InventoryFilterSelectors.test.js +21 -0
  97. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/__tests__/__snapshots__/InventoryFilter.test.js.snap +25 -0
  98. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/__tests__/__snapshots__/InventoryFilterActions.test.js.snap +17 -0
  99. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/__tests__/__snapshots__/InventoryFilterReducer.test.js.snap +25 -0
  100. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/__tests__/__snapshots__/InventoryFilterSelectors.test.js.snap +9 -0
  101. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/__tests__/__snapshots__/integration.test.js.snap +31 -0
  102. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/__tests__/integration.test.js +18 -0
  103. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/index.js +17 -0
  104. data/webpack/ForemanInventoryUpload/Components/InventoryFilter/inventoryFilter.scss +28 -0
  105. data/webpack/ForemanInventoryUpload/Components/InventorySettings/InventorySettings.js +15 -0
  106. data/webpack/ForemanInventoryUpload/Components/InventorySettings/__tests__/InventorySettings.test.js +13 -0
  107. data/webpack/ForemanInventoryUpload/Components/InventorySettings/__tests__/__snapshots__/InventorySettings.test.js.snap +13 -0
  108. data/webpack/ForemanInventoryUpload/Components/InventorySettings/index.js +1 -0
  109. data/webpack/ForemanInventoryUpload/Components/InventorySettings/inventorySettings.scss +15 -0
  110. data/webpack/ForemanInventoryUpload/Components/PageHeader/PageHeader.js +40 -0
  111. data/webpack/ForemanInventoryUpload/Components/PageHeader/__tests__/PageHeader.test.js +13 -0
  112. data/webpack/ForemanInventoryUpload/Components/PageHeader/__tests__/__snapshots__/PageHeader.test.js.snap +61 -0
  113. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/DocsButton/DocsButton.js +17 -0
  114. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/DocsButton/__tests__/DocsButton.test.js +13 -0
  115. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/DocsButton/__tests__/__snapshots__/DocsButton.test.js.snap +20 -0
  116. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/DocsButton/index.js +1 -0
  117. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/HistoryButton/HistoryButton.js +18 -0
  118. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/HistoryButton/__tests__/HistoryButton.test.js +14 -0
  119. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/HistoryButton/__tests__/__snapshots__/HistoryButton.test.js.snap +21 -0
  120. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/HistoryButton/index.js +1 -0
  121. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/PageDescription/PageDescription.js +24 -0
  122. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/PageDescription/__tests__/PageDescription.test.js +11 -0
  123. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/PageDescription/__tests__/__snapshots__/PageDescription.test.js.snap +17 -0
  124. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/PageDescription/index.js +1 -0
  125. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/ToolbarButtons/ToolbarButtons.js +13 -0
  126. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/ToolbarButtons/__tests__/ToolbarButtons.test.js +12 -0
  127. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/ToolbarButtons/__tests__/__snapshots__/ToolbarButtons.test.js.snap +10 -0
  128. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/ToolbarButtons/index.js +1 -0
  129. data/webpack/ForemanInventoryUpload/Components/PageHeader/components/ToolbarButtons/toolbarButtons.scss +7 -0
  130. data/webpack/ForemanInventoryUpload/Components/PageHeader/index.js +1 -0
  131. data/webpack/ForemanInventoryUpload/Components/PageHeader/pageHeader.scss +3 -0
  132. data/webpack/ForemanInventoryUpload/Components/ScheduledRun/ScheduledRun.fixtures.js +1 -0
  133. data/webpack/ForemanInventoryUpload/Components/ScheduledRun/ScheduledRun.js +4 -2
  134. data/webpack/ForemanInventoryUpload/Components/ScheduledRun/__tests__/ScheduledRun.test.js +0 -1
  135. data/webpack/ForemanInventoryUpload/Components/ScheduledRun/__tests__/__snapshots__/ScheduledRun.test.js.snap +0 -2
  136. data/webpack/ForemanInventoryUpload/Components/ScheduledRun/index.js +12 -1
  137. data/webpack/ForemanInventoryUpload/Components/TabBody/__tests__/__snapshots__/TabBody.test.js.snap +1 -1
  138. data/webpack/ForemanInventoryUpload/ForemanInventoryConstants.js +7 -0
  139. data/webpack/ForemanInventoryUpload/ForemanInventoryHelpers.js +12 -0
  140. data/webpack/ForemanInventoryUpload/ForemanInventoryUpload.js +18 -9
  141. data/webpack/ForemanInventoryUpload/ForemanInventoryUploadReducers.js +4 -4
  142. data/webpack/ForemanInventoryUpload/SubscriptionsPageExtension/SubscriptionsPageExtensionActions.js +34 -0
  143. data/webpack/ForemanInventoryUpload/__tests__/ForemanInventoryHelpers.test.js +12 -0
  144. data/webpack/ForemanInventoryUpload/{ForemanInventoryUpload.test.js → __tests__/ForemanInventoryUpload.test.js} +1 -1
  145. data/webpack/ForemanInventoryUpload/__tests__/__snapshots__/ForemanInventoryHelpers.test.js.snap +5 -0
  146. data/webpack/ForemanInventoryUpload/{__snapshots__ → __tests__/__snapshots__}/ForemanInventoryUpload.test.js.snap +6 -5
  147. data/webpack/ForemanRhCloudHelpers.js +6 -0
  148. data/webpack/ForemanRhCloudReducers.js +10 -0
  149. data/webpack/ForemanRhCloudSelectors.js +5 -0
  150. data/webpack/ForemanRhCloudTestHelpers.js +6 -0
  151. data/webpack/InsightsCloudSync/InsightsCloudSync.js +55 -0
  152. data/webpack/InsightsCloudSync/InsightsCloudSync.test.js +16 -0
  153. data/webpack/InsightsCloudSync/InsightsCloudSyncActions.js +23 -0
  154. data/webpack/InsightsCloudSync/InsightsCloudSyncConstants.js +6 -0
  155. data/webpack/InsightsCloudSync/InsightsCloudSyncHelpers.js +3 -0
  156. data/webpack/InsightsCloudSync/InsightsCloudSyncReducers.js +3 -0
  157. data/webpack/InsightsCloudSync/__snapshots__/InsightsCloudSync.test.js.snap +62 -0
  158. data/webpack/InsightsCloudSync/__tests__/InsightsCloudSyncHelpers.test.js +11 -0
  159. data/webpack/InsightsCloudSync/__tests__/__snapshots__/InsightsCloudSyncHelpers.test.js.snap +3 -0
  160. data/webpack/InsightsCloudSync/index.js +17 -0
  161. data/webpack/__mocks__/foremanReact/components/Layout/LayoutConstants.js +1 -0
  162. data/webpack/__tests__/ForemanRhCloudHelpers.test.js +11 -0
  163. data/webpack/__tests__/ForemanRhCloudSelectors.test.js +19 -0
  164. data/webpack/__tests__/ForemanRhCloudTestHelpers.test.js +10 -0
  165. data/webpack/__tests__/__snapshots__/ForemanRhCloudHelpers.test.js.snap +3 -0
  166. data/webpack/__tests__/__snapshots__/ForemanRhCloudSelectors.test.js.snap +24 -0
  167. data/webpack/__tests__/__snapshots__/ForemanRhCloudTestHelpers.test.js.snap +14 -0
  168. data/webpack/index.js +8 -1
  169. data/webpack/subscriptions_extension_index.js +8 -0
  170. metadata +124 -7
  171. data/app/controllers/foreman_inventory_upload/react_controller.rb +0 -7
  172. data/webpack/ForemanInventoryUpload/ForemanInventoryUploadSelectors.js +0 -4
@@ -0,0 +1,19 @@
1
+ module ForemanInventoryUpload
2
+ module Notifications
3
+ module ManifestImportSuccessNotificationOverride
4
+ extend ActiveSupport::Concern
5
+
6
+ def actions
7
+ {
8
+ :links => [
9
+ {
10
+ :href => Rails.application.routes.url_helpers.foreman_rh_cloud_inventory_upload_path,
11
+ :title => _('Enable inventory upload'),
12
+ :external => false,
13
+ },
14
+ ],
15
+ }
16
+ end
17
+ end
18
+ end
19
+ 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"]
@@ -26,17 +30,41 @@ module ForemanRhCloud
26
30
 
27
31
  # Add permissions
28
32
  security_block :foreman_rh_cloud do
29
- permission :view_foreman_rh_cloud, :'foreman_rh_cloud/reports' => [:last]
33
+ permission(
34
+ :generate_foreman_rh_cloud,
35
+ 'foreman_inventory_upload/reports': [:generate]
36
+ )
37
+ permission(
38
+ :view_foreman_rh_cloud,
39
+ 'foreman_inventory_upload/accounts': [:index],
40
+ 'foreman_inventory_upload/reports': [:last],
41
+ 'foreman_inventory_upload/uploads': [:auto_upload, :download_file, :last],
42
+ 'foreman_rh_cloud/react': [:inventory_upload]
43
+ )
30
44
  end
31
45
 
32
- # Add a new role called 'Discovery' if it doesn't exist
33
- role 'ForemanRhCloud', [:view_foreman_rh_cloud]
46
+ plugin_permissions = [:view_foreman_rh_cloud, :generate_foreman_rh_cloud]
47
+
48
+ role 'ForemanRhCloud', plugin_permissions, 'Role granting permissions to view the hosts inventory,
49
+ generate a report, upload it to the cloud and download it locally'
50
+
51
+ add_permissions_to_default_roles Role::ORG_ADMIN => plugin_permissions,
52
+ Role::MANAGER => plugin_permissions,
53
+ Role::SYSTEM_ADMIN => plugin_permissions
34
54
 
35
55
  # Adding a sub menu after hosts menu
36
- sub_menu :top_menu, :foreman_rh_cloud, :caption => N_('RH Cloud'), :icon => 'fa fa-cloud-upload' do
37
- menu :top_menu, :level1, :caption => N_('Inventory Upload'), :url_hash => { controller: :'foreman_inventory_upload/react', :action => :index}
56
+ divider :top_menu, caption: N_('RH Cloud'), parent: :configure_menu
57
+ menu :top_menu, :inventory_upload, :caption => N_('Inventory Upload'), :url_hash => { controller: :'foreman_rh_cloud/react', :action => :inventory_upload}, parent: :configure_menu
58
+ menu :top_menu, :insights_hits_import, :caption => N_('Insights'), :url_hash => { controller: :'foreman_rh_cloud/react', :action => :insights_cloud }, parent: :configure_menu
59
+
60
+ register_facet InsightsFacet, :insights do
61
+ configure_host
38
62
  end
63
+
64
+ register_global_js_file 'subscriptions_extension'
39
65
  end
66
+
67
+ ::Katello::UINotifications::Subscriptions::ManifestImportSuccess.include ForemanInventoryUpload::Notifications::ManifestImportSuccessNotificationOverride if defined?(Katello)
40
68
  end
41
69
 
42
70
  initializer "foreman_rh_cloud.set_dynflow.config.on_init", :before => :finisher_hook do |_app|
@@ -1,3 +1,3 @@
1
1
  module ForemanRhCloud
2
- VERSION = '1.0.4.1'.freeze
2
+ VERSION = '1.0.9'.freeze
3
3
  end
@@ -0,0 +1,15 @@
1
+ module InsightsCloud
2
+ def self.base_url
3
+ # for testing set ENV to 'https://ci.cloud.redhat.com'
4
+ @base_url ||= ENV['SATELLITE_INSIGHTS_CLOUD_URL'] || 'https://cloud.redhat.com'
5
+ end
6
+
7
+ def self.authentication_url
8
+ # https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token
9
+ @authentication_url ||= ENV['SATELLITE_INSIGHTS_CLOUD_SSO_URL'] || 'https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token'
10
+ end
11
+
12
+ def self.hits_export_url
13
+ base_url + '/api/insights/v1/export/hits/'
14
+ end
15
+ end
@@ -0,0 +1,96 @@
1
+ require 'rest-client'
2
+
3
+ module InsightsCloud
4
+ module Async
5
+ class InsightsFullSync < ::ApplicationJob
6
+ def perform(organization)
7
+ @organization = organization
8
+
9
+ hits = query_insights_hits
10
+
11
+ host_names = hits.map { |hit| hit['hostname'] }.uniq
12
+ setup_host_names(host_names)
13
+
14
+ replace_hits_data(hits)
15
+ end
16
+
17
+ def logger
18
+ Foreman::Logging.logger('background')
19
+ end
20
+
21
+ def rh_credentials
22
+ @rh_credentials ||= query_refresh_token
23
+ end
24
+
25
+ private
26
+
27
+ def query_insights_hits
28
+ hits_response = RestClient::Request.execute(
29
+ method: :get,
30
+ url: InsightsCloud.hits_export_url,
31
+ verify_ssl: ENV['SATELLITE_INSIGHTS_CLOUD_URL'] ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER,
32
+ headers: {
33
+ Authorization: "Bearer #{rh_credentials}",
34
+ }
35
+ )
36
+
37
+ JSON.parse(hits_response)
38
+ end
39
+
40
+ def query_refresh_token
41
+ token_response = RestClient::Request.execute(
42
+ method: :post,
43
+ url: InsightsCloud.authentication_url,
44
+ verify_ssl: ENV['SATELLITE_INSIGHTS_CLOUD_URL'] ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER,
45
+ payload: {
46
+ grant_type: 'refresh_token',
47
+ client_id: 'rhsm-api',
48
+ refresh_token: Setting[:rh_cloud_token],
49
+ }
50
+ )
51
+
52
+ JSON.parse(token_response)['access_token']
53
+ rescue RestClient::ExceptionWithResponse => e
54
+ Foreman::Logging.exception('Unable to authenticate using rh_cloud_token setting', e)
55
+ raise ::Foreman::WrappedException.new(e, N_('Unable to authenticate using rh_cloud_token setting'))
56
+ end
57
+
58
+ def setup_host_names(host_names)
59
+ @host_ids = Hash[
60
+ Host.where(name: host_names).pluck(:name, :id)
61
+ ]
62
+ end
63
+
64
+ def host_id(host_name)
65
+ @host_ids[host_name]
66
+ end
67
+
68
+ def replace_hits_data(hits)
69
+ InsightsHit.transaction do
70
+ InsightsHit.delete_all
71
+ InsightsHit.create(hits.map { |hits_hash| to_model_hash(hits_hash) }.compact)
72
+ # create new facets for hosts that are missing one
73
+ hosts_with_existing_facets = InsightsFacet.where(host_id: @host_ids.values).pluck(:host_id)
74
+ InsightsFacet.create((@host_ids.values - hosts_with_existing_facets).map { |id| {host_id: id} })
75
+ end
76
+ end
77
+
78
+ def to_model_hash(hit_hash)
79
+ hit_host_id = host_id(hit_hash['hostname'])
80
+
81
+ return unless hit_host_id
82
+
83
+ {
84
+ host_id: hit_host_id,
85
+ last_seen: DateTime.parse(hit_hash['last_seen']),
86
+ publish_date: DateTime.parse(hit_hash['publish_date']),
87
+ title: hit_hash['title'],
88
+ solution_url: hit_hash['solution_url'],
89
+ total_risk: hit_hash['total_risk'].to_i,
90
+ likelihood: hit_hash['likelihood'].to_i,
91
+ results_url: hit_hash['results_url'],
92
+ }
93
+ end
94
+ end
95
+ end
96
+ 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
@@ -0,0 +1,92 @@
1
+ require 'test_helper'
2
+
3
+ class InsightsFullSyncTest < ActiveJob::TestCase
4
+ setup do
5
+ @host1 = FactoryBot.create(:host, :managed, name: 'host1')
6
+ @host2 = FactoryBot.create(:host, :managed, name: 'host2')
7
+
8
+ hits_json = <<-HITS_JSON
9
+ [
10
+ {
11
+ "hostname": "#{@host1.name}",
12
+ "rhel_version": "7.5",
13
+ "uuid": "accdf444-5628-451d-bf3e-cf909ad72756",
14
+ "last_seen": "2019-11-22T08:41:42.447244Z",
15
+ "title": "New Ansible Engine packages are inaccessible when dedicated Ansible repo is not enabled",
16
+ "solution_url": "",
17
+ "total_risk": 2,
18
+ "likelihood": 2,
19
+ "publish_date": "2018-04-16T10:03:16Z",
20
+ "results_url": "https://cloud.redhat.com/insights/overview/stability/ansible_deprecated_repo%7CANSIBLE_DEPRECATED_REPO/accdf444-5628-451d-bf3e-cf909ad72756/"
21
+ },
22
+ {
23
+ "hostname": "#{@host1.name}",
24
+ "rhel_version": "7.5",
25
+ "uuid": "accdf444-5628-451d-bf3e-cf909ad72756",
26
+ "last_seen": "2019-11-22T08:41:42.447244Z",
27
+ "title": "CPU vulnerable to side-channel attacks using Microarchitectural Data Sampling (CVE-2018-12130, CVE-2018-12126, CVE-2018-12127, CVE-2019-11091)",
28
+ "solution_url": "https://access.redhat.com/node/4134081",
29
+ "total_risk": 2,
30
+ "likelihood": 2,
31
+ "publish_date": "2019-05-14T17:00:00Z",
32
+ "results_url": "https://cloud.redhat.com/insights/overview/security/CVE_2018_12130_cpu_kernel%7CCVE_2018_12130_CPU_KERNEL_NEED_UPDATE/accdf444-5628-451d-bf3e-cf909ad72756/"
33
+ },
34
+ {
35
+ "hostname": "#{@host2.name}",
36
+ "rhel_version": "7.5",
37
+ "uuid": "accdf444-5628-451d-bf3e-cf909ad72757",
38
+ "last_seen": "2019-11-22T08:41:42.447244Z",
39
+ "title": "CPU vulnerable to side-channel attacks using L1 Terminal Fault (CVE-2018-3620)",
40
+ "solution_url": "https://access.redhat.com/node/3560291",
41
+ "total_risk": 2,
42
+ "likelihood": 2,
43
+ "publish_date": "2018-08-14T17:00:00Z",
44
+ "results_url": "https://cloud.redhat.com/insights/overview/security/CVE_2018_3620_cpu_kernel%7CCVE_2018_3620_CPU_KERNEL_NEED_UPDATE/accdf444-5628-451d-bf3e-cf909ad72756/"
45
+ }
46
+ ]
47
+ HITS_JSON
48
+ @hits = JSON.parse(hits_json)
49
+ end
50
+
51
+ test 'Hits data is replaced with data from cloud' do
52
+ InsightsCloud::Async::InsightsFullSync.any_instance.expects(:query_insights_hits).returns(@hits)
53
+
54
+ InsightsCloud::Async::InsightsFullSync.perform_now(@host1.organization)
55
+
56
+ @host1.reload
57
+ @host2.reload
58
+
59
+ assert_equal 2, @host1.insights.hits.count
60
+ assert_equal 1, @host2.insights.hits.count
61
+ end
62
+
63
+ test 'Hits ignoring non-existent hosts' do
64
+ hits_json = <<-HITS_JSON
65
+ [
66
+ {
67
+ "hostname": "#{@host1.name}_non_existent",
68
+ "rhel_version": "7.5",
69
+ "uuid": "accdf444-5628-451d-bf3e-cf909ad72756",
70
+ "last_seen": "2019-11-22T08:41:42.447244Z",
71
+ "title": "New Ansible Engine packages are inaccessible when dedicated Ansible repo is not enabled",
72
+ "solution_url": "",
73
+ "total_risk": 2,
74
+ "likelihood": 2,
75
+ "publish_date": "2018-04-16T10:03:16Z",
76
+ "results_url": "https://cloud.redhat.com/insights/overview/stability/ansible_deprecated_repo%7CANSIBLE_DEPRECATED_REPO/accdf444-5628-451d-bf3e-cf909ad72756/"
77
+ }
78
+ ]
79
+ HITS_JSON
80
+ hits = JSON.parse(hits_json)
81
+
82
+ InsightsCloud::Async::InsightsFullSync.any_instance.expects(:query_insights_hits).returns(hits)
83
+
84
+ InsightsCloud::Async::InsightsFullSync.perform_now(@host1.organization)
85
+
86
+ @host1.reload
87
+ @host2.reload
88
+
89
+ assert_nil @host1.insights
90
+ assert_nil @host2.insights
91
+ end
92
+ 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,17 @@ 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',
41
+ 'dmi::system::manufacturer',
42
+ 'dmi::system::product_name',
43
+ 'dmi::chassis::asset_tag',
44
+ 'insights_client::obfuscate_hostname_enabled',
45
+ 'insights_client::hostname',
36
46
  ]
37
47
  end
38
48
 
@@ -46,22 +56,99 @@ class ReportGeneratorTest < ActiveSupport::TestCase
46
56
 
47
57
  test 'generates a report for a single host' do
48
58
  batch = Host.where(id: @host.id).in_batches.first
49
- generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], 'slice_123')
59
+ generator = create_generator(batch)
60
+
61
+ json_str = generator.render
62
+ actual = JSON.parse(json_str.join("\n"))
63
+
64
+ assert_equal 'slice_123', actual['report_slice_id']
65
+ assert_not_nil(actual_host = actual['hosts'].first)
66
+ assert_nil actual_host['ip_addresses']
67
+ assert_nil actual_host['mac_addresses']
68
+ assert_equal @host.fqdn, actual_host['fqdn']
69
+ assert_equal '1234', actual_host['account']
70
+ assert_equal 1, generator.hosts_count
71
+ end
72
+
73
+ test 'generates ip_address and mac_address fields' do
74
+ @host.interfaces << FactoryBot.build(:nic_managed)
75
+ batch = Host.where(id: @host.id).in_batches.first
76
+ generator = create_generator(batch)
50
77
 
51
78
  json_str = generator.render
52
79
  actual = JSON.parse(json_str.join("\n"))
53
80
 
54
81
  assert_equal 'slice_123', actual['report_slice_id']
55
82
  assert_not_nil(actual_host = actual['hosts'].first)
56
- assert_equal @host.name, actual_host['display_name']
83
+ assert_equal @host.interfaces.where.not(ip: nil).first.ip, actual_host['ip_addresses'].first
84
+ assert_equal @host.interfaces.where.not(mac: nil).first.mac, actual_host['mac_addresses'].first
57
85
  assert_equal @host.fqdn, actual_host['fqdn']
58
86
  assert_equal '1234', actual_host['account']
87
+ assert_equal 1, generator.hosts_count
88
+ end
89
+
90
+ test 'obfuscates fqdn when instructed by insights-client' do
91
+ FactoryBot.create(:fact_value, fact_name: fact_names['insights_client::obfuscate_hostname_enabled'], value: 'true', host: @host)
92
+ FactoryBot.create(:fact_value, fact_name: fact_names['insights_client::hostname'], value: 'obfuscated_name', host: @host)
93
+
94
+ batch = Host.where(id: @host.id).in_batches.first
95
+ generator = create_generator(batch)
96
+
97
+ json_str = generator.render
98
+ actual = JSON.parse(json_str.join("\n"))
99
+
100
+ assert_equal 'slice_123', actual['report_slice_id']
101
+ assert_not_nil(actual_host = actual['hosts'].first)
102
+ assert_equal 'obfuscated_name', actual_host['fqdn']
103
+ assert_equal '1234', actual_host['account']
104
+ assert_not_nil(actual_facts = actual_host['facts'].first['facts'])
105
+ assert_equal true, actual_facts['is_hostname_obfuscated']
106
+ assert_equal 1, generator.hosts_count
107
+ end
108
+
109
+ test 'obfuscates fqdn when setting set' do
110
+ FactoryBot.create(:setting, :name => 'obfuscate_inventory_hostnames', :value => true)
111
+
112
+ batch = Host.where(id: @host.id).in_batches.first
113
+ generator = create_generator(batch)
114
+
115
+ json_str = generator.render
116
+ actual = JSON.parse(json_str.join("\n"))
117
+
118
+ obfuscated_fqdn = Digest::SHA1.hexdigest(@host.fqdn)
119
+
120
+ assert_equal 'slice_123', actual['report_slice_id']
121
+ assert_not_nil(actual_host = actual['hosts'].first)
122
+ assert_equal obfuscated_fqdn, actual_host['fqdn']
123
+ assert_equal '1234', actual_host['account']
124
+ assert_not_nil(actual_facts = actual_host['facts'].first['facts'])
125
+ assert_equal true, actual_facts['is_hostname_obfuscated']
126
+ assert_equal 1, generator.hosts_count
127
+ end
128
+
129
+ test 'does not obfuscate fqdn when insights-client sets to false' do
130
+ FactoryBot.create(:fact_value, fact_name: fact_names['insights_client::obfuscate_hostname_enabled'], value: 'false', host: @host)
131
+ FactoryBot.create(:fact_value, fact_name: fact_names['insights_client::hostname'], value: 'obfuscated_name', host: @host)
132
+
133
+ batch = Host.where(id: @host.id).in_batches.first
134
+ generator = create_generator(batch)
135
+
136
+ json_str = generator.render
137
+ actual = JSON.parse(json_str.join("\n"))
138
+
139
+ assert_equal 'slice_123', actual['report_slice_id']
140
+ assert_not_nil(actual_host = actual['hosts'].first)
141
+ assert_equal @host.fqdn, actual_host['fqdn']
142
+ assert_equal '1234', actual_host['account']
143
+ assert_not_nil(actual_facts = actual_host['facts'].first['facts'])
144
+ assert_equal false, actual_facts['is_hostname_obfuscated']
145
+ assert_equal 1, generator.hosts_count
59
146
  end
60
147
 
61
148
  test 'generates a report with satellite facts' do
62
149
  Foreman.expects(:instance_id).twice.returns('satellite-id')
63
150
  batch = Host.where(id: @host.id).in_batches.first
64
- generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], 'slice-123')
151
+ generator = create_generator(batch)
65
152
 
66
153
  json_str = generator.render
67
154
  actual = JSON.parse(json_str.join("\n"))
@@ -79,6 +166,7 @@ class ReportGeneratorTest < ActiveSupport::TestCase
79
166
  org_id_tag = actual['hosts'].first['tags'].find { |tag| tag['namespace'] == 'satellite' && tag['key'] == 'organization_id'}
80
167
  assert_not_nil org_id_tag
81
168
  assert_equal @host.organization_id.to_s, org_id_tag['value']
169
+ assert_equal false, satellite_facts['is_hostname_obfuscated']
82
170
 
83
171
  version = satellite_facts['satellite_version']
84
172
  if defined?(ForemanThemeSatellite)
@@ -102,14 +190,13 @@ class ReportGeneratorTest < ActiveSupport::TestCase
102
190
  @host.save!
103
191
 
104
192
  batch = Host.where(id: @host.id).in_batches.first
105
- generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], 'slice_123')
193
+ generator = create_generator(batch)
106
194
 
107
195
  json_str = generator.render
108
196
  actual = JSON.parse(json_str.join("\n"))
109
197
 
110
198
  assert_equal 'slice_123', actual['report_slice_id']
111
199
  assert_not_nil(actual_host = actual['hosts'].first)
112
- assert_equal @host.name, actual_host['display_name']
113
200
  assert_equal @host.fqdn, actual_host['fqdn']
114
201
  assert_not_nil(host_facts = actual_host['facts']&.first)
115
202
  assert_equal 'satellite', host_facts['namespace']
@@ -124,14 +211,13 @@ class ReportGeneratorTest < ActiveSupport::TestCase
124
211
  @host.subscription_facet.save!
125
212
 
126
213
  batch = Host.where(id: @host.id).in_batches.first
127
- generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], 'slice_123')
214
+ generator = create_generator(batch)
128
215
 
129
216
  json_str = generator.render
130
217
  actual = JSON.parse(json_str.join("\n"))
131
218
 
132
219
  assert_equal 'slice_123', actual['report_slice_id']
133
220
  assert_not_nil(actual_host = actual['hosts'].first)
134
- assert_equal @host.name, actual_host['display_name']
135
221
  assert_equal @host.fqdn, actual_host['fqdn']
136
222
  assert_not_nil(host_facts = actual_host['facts']&.first)
137
223
  assert_equal 'satellite', host_facts['namespace']
@@ -140,6 +226,22 @@ class ReportGeneratorTest < ActiveSupport::TestCase
140
226
  assert_equal 'test_role', fact_values['system_purpose_role']
141
227
  end
142
228
 
229
+ test 'generates a report for a golden ticket' do
230
+ batch = Host.where(id: @host.id).in_batches.first
231
+ generator = create_generator(batch) do |generator|
232
+ generator.stubs(:golden_ticket?).returns(true)
233
+ end
234
+
235
+ json_str = generator.render
236
+ actual = JSON.parse(json_str.join("\n"))
237
+
238
+ assert_equal 'slice_123', actual['report_slice_id']
239
+ assert_not_nil(actual_host = actual['hosts'].first)
240
+ assert_equal @host.fqdn, actual_host['fqdn']
241
+ assert_equal '1234', actual_host['account']
242
+ assert_equal 1, generator.hosts_count
243
+ end
244
+
143
245
  test 'skips hosts without subscription' do
144
246
  a_host = FactoryBot.create(
145
247
  :host,
@@ -148,23 +250,23 @@ class ReportGeneratorTest < ActiveSupport::TestCase
148
250
 
149
251
  # make a_host last
150
252
  batch = Host.where(id: [@host.id, a_host.id]).order(:name).in_batches.first
151
- generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], 'slice_123')
253
+ generator = create_generator(batch)
152
254
 
153
255
  json_str = generator.render
154
256
  actual = JSON.parse(json_str.join("\n"))
155
257
 
156
258
  assert_equal 'slice_123', actual['report_slice_id']
157
259
  assert_not_nil(actual_host = actual['hosts'].first)
158
- assert_equal @host.name, actual_host['display_name']
159
260
  assert_equal @host.fqdn, actual_host['fqdn']
160
261
  assert_equal '1234', actual_host['account']
262
+ assert_equal 1, generator.hosts_count
161
263
  end
162
264
 
163
265
  test 'shows system_memory_bytes in bytes' do
164
266
  FactoryBot.create(:fact_value, fact_name: fact_names['memory::memtotal'], value: '1', host: @host)
165
267
 
166
268
  batch = Host.where(id: @host.id).in_batches.first
167
- generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], 'slice_123')
269
+ generator = create_generator(batch)
168
270
 
169
271
  json_str = generator.render
170
272
  actual = JSON.parse(json_str.join("\n"))
@@ -174,4 +276,164 @@ class ReportGeneratorTest < ActiveSupport::TestCase
174
276
  assert_not_nil(actual_profile = actual_host['system_profile'])
175
277
  assert_equal 1024, actual_profile['system_memory_bytes']
176
278
  end
279
+
280
+ test 'reports an account for hosts with multiple pools' do
281
+ first_pool = @host.organization.pools.first
282
+ second_pool = FactoryBot.create(:katello_pool, account_number: nil, cp_id: 2)
283
+ new_org = FactoryBot.create(:organization, pools: [first_pool, second_pool])
284
+
285
+ another_host = FactoryBot.create(
286
+ :host,
287
+ :with_subscription,
288
+ :with_content,
289
+ content_view: @host.content_view,
290
+ lifecycle_environment: @host.lifecycle_environment,
291
+ organization: new_org
292
+ )
293
+
294
+ batch = Host.where(id: another_host.id).in_batches.first
295
+ generator = create_generator(batch)
296
+
297
+ json_str = generator.render
298
+ actual = JSON.parse(json_str.join("\n"))
299
+
300
+ assert_equal 'slice_123', actual['report_slice_id']
301
+ assert_not_nil(actual_host = actual['hosts'].first)
302
+ assert_not_nil(actual_host['account'])
303
+ assert_not_empty(actual_host['account'])
304
+ end
305
+
306
+ test 'Generates os_release with version and id' do
307
+ FactoryBot.create(:fact_value, fact_name: fact_names['distribution::name'], value: 'Red Hat Test Linux', host: @host)
308
+ FactoryBot.create(:fact_value, fact_name: fact_names['distribution::version'], value: '7.1', host: @host)
309
+ FactoryBot.create(:fact_value, fact_name: fact_names['distribution::id'], value: 'TestId', host: @host)
310
+
311
+ batch = Host.where(id: @host.id).in_batches.first
312
+ generator = create_generator(batch)
313
+
314
+ json_str = generator.render
315
+ actual = JSON.parse(json_str.join("\n"))
316
+
317
+ assert_equal 'slice_123', actual['report_slice_id']
318
+ assert_not_nil(actual_host = actual['hosts'].first)
319
+ assert_not_nil(actual_profile = actual_host['system_profile'])
320
+ assert_equal 'Red Hat Test Linux 7.1 (TestId)', actual_profile['os_release']
321
+ end
322
+
323
+ test 'sets infrastructure_type to "virtual" based on virt.is_guest fact' do
324
+ FactoryBot.create(:fact_value, fact_name: fact_names['virt::is_guest'], value: true, host: @host)
325
+
326
+ batch = Host.where(id: @host.id).in_batches.first
327
+ generator = create_generator(batch)
328
+
329
+ json_str = generator.render
330
+ actual = JSON.parse(json_str.join("\n"))
331
+
332
+ assert_equal 'slice_123', actual['report_slice_id']
333
+ assert_not_nil(actual_host = actual['hosts'].first)
334
+ assert_not_nil(actual_profile = actual_host['system_profile'])
335
+ assert_equal 'virtual', actual_profile['infrastructure_type']
336
+ end
337
+
338
+ test 'sets infrastructure_type to "physical" based on virt.is_guest fact' do
339
+ FactoryBot.create(:fact_value, fact_name: fact_names['virt::is_guest'], value: false, host: @host)
340
+
341
+ batch = Host.where(id: @host.id).in_batches.first
342
+ generator = create_generator(batch)
343
+
344
+ json_str = generator.render
345
+ actual = JSON.parse(json_str.join("\n"))
346
+
347
+ assert_equal 'slice_123', actual['report_slice_id']
348
+ assert_not_nil(actual_host = actual['hosts'].first)
349
+ assert_not_nil(actual_profile = actual_host['system_profile'])
350
+ assert_equal 'physical', actual_profile['infrastructure_type']
351
+ end
352
+
353
+ test 'Identifies Amazon cloud provider' do
354
+ FactoryBot.create(:fact_value, fact_name: fact_names['dmi::bios::version'], value: 'Test Amazon version', host: @host)
355
+
356
+ batch = Host.where(id: @host.id).in_batches.first
357
+ generator = create_generator(batch)
358
+
359
+ json_str = generator.render
360
+ actual = JSON.parse(json_str.join("\n"))
361
+
362
+ assert_equal 'slice_123', actual['report_slice_id']
363
+ assert_not_nil(actual_host = actual['hosts'].first)
364
+ assert_not_nil(actual_profile = actual_host['system_profile'])
365
+ assert_equal 'aws', actual_profile['cloud_provider']
366
+ end
367
+
368
+ test 'Identifies Google cloud provider' do
369
+ FactoryBot.create(:fact_value, fact_name: fact_names['dmi::bios::version'], value: 'Test Google version', host: @host)
370
+
371
+ batch = Host.where(id: @host.id).in_batches.first
372
+ generator = create_generator(batch)
373
+
374
+ json_str = generator.render
375
+ actual = JSON.parse(json_str.join("\n"))
376
+
377
+ assert_equal 'slice_123', actual['report_slice_id']
378
+ assert_not_nil(actual_host = actual['hosts'].first)
379
+ assert_not_nil(actual_profile = actual_host['system_profile'])
380
+ assert_equal 'google', actual_profile['cloud_provider']
381
+ end
382
+
383
+ test 'Identifies Azure cloud provider' do
384
+ FactoryBot.create(:fact_value, fact_name: fact_names['dmi::chassis::asset_tag'], value: '7783-7084-3265-9085-8269-3286-77', host: @host)
385
+
386
+ batch = Host.where(id: @host.id).in_batches.first
387
+ generator = create_generator(batch)
388
+
389
+ json_str = generator.render
390
+ actual = JSON.parse(json_str.join("\n"))
391
+
392
+ assert_equal 'slice_123', actual['report_slice_id']
393
+ assert_not_nil(actual_host = actual['hosts'].first)
394
+ assert_not_nil(actual_profile = actual_host['system_profile'])
395
+ assert_equal 'azure', actual_profile['cloud_provider']
396
+ end
397
+
398
+ test 'Identifies Alibaba cloud provider via manufacturer' do
399
+ FactoryBot.create(:fact_value, fact_name: fact_names['dmi::system::manufacturer'], value: 'Test Alibaba Cloud version', host: @host)
400
+
401
+ batch = Host.where(id: @host.id).in_batches.first
402
+ generator = create_generator(batch)
403
+
404
+ json_str = generator.render
405
+ actual = JSON.parse(json_str.join("\n"))
406
+
407
+ assert_equal 'slice_123', actual['report_slice_id']
408
+ assert_not_nil(actual_host = actual['hosts'].first)
409
+ assert_not_nil(actual_profile = actual_host['system_profile'])
410
+ assert_equal 'alibaba', actual_profile['cloud_provider']
411
+ end
412
+
413
+ test 'Identifies Alibaba cloud provider via product name' do
414
+ FactoryBot.create(:fact_value, fact_name: fact_names['dmi::system::product_name'], value: 'Test Alibaba Cloud ECS product', host: @host)
415
+
416
+ batch = Host.where(id: @host.id).in_batches.first
417
+ generator = create_generator(batch)
418
+
419
+ json_str = generator.render
420
+ actual = JSON.parse(json_str.join("\n"))
421
+
422
+ assert_equal 'slice_123', actual['report_slice_id']
423
+ assert_not_nil(actual_host = actual['hosts'].first)
424
+ assert_not_nil(actual_profile = actual_host['system_profile'])
425
+ assert_equal 'alibaba', actual_profile['cloud_provider']
426
+ end
427
+
428
+ private
429
+
430
+ def create_generator(batch, name = 'slice_123')
431
+ generator = ForemanInventoryUpload::Generators::Slice.new(batch, [], name)
432
+ if block_given?
433
+ yield(generator)
434
+ else
435
+ generator.stubs(:golden_ticket?).returns(false)
436
+ end
437
+ generator
438
+ end
177
439
  end