foreman_wreckingball 3.0.1 → 3.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -1
  3. data/app/assets/javascripts/foreman_wreckingball/modal.js +26 -18
  4. data/app/assets/javascripts/foreman_wreckingball/status_hosts_table.js +81 -0
  5. data/app/assets/javascripts/foreman_wreckingball/status_managed_hosts_dashboard.js +14 -0
  6. data/app/assets/javascripts/foreman_wreckingball/status_row.js +17 -0
  7. data/app/assets/stylesheets/foreman_wreckingball/status_hosts_table.css.scss +13 -0
  8. data/app/assets/stylesheets/foreman_wreckingball/status_managed_hosts_dashboard.css.scss +8 -0
  9. data/app/controllers/foreman_wreckingball/hosts_controller.rb +143 -43
  10. data/app/helpers/concerns/foreman_wreckingball/hosts_helper_extensions.rb +0 -15
  11. data/app/helpers/foreman_wreckingball/statuses_helper.rb +21 -0
  12. data/app/lib/actions/foreman_wreckingball/bulk_remediate.rb +27 -0
  13. data/app/lib/actions/foreman_wreckingball/host/remediate_hardware_version.rb +63 -0
  14. data/app/lib/actions/foreman_wreckingball/host/remediate_spectre_v2.rb +58 -0
  15. data/app/lib/fog_extensions/foreman_wreckingball/vsphere/mock.rb +13 -0
  16. data/app/lib/fog_extensions/foreman_wreckingball/vsphere/real.rb +7 -0
  17. data/app/lib/fog_extensions/foreman_wreckingball/vsphere/server.rb +14 -0
  18. data/app/models/concerns/foreman_wreckingball/host_extensions.rb +10 -20
  19. data/app/models/concerns/foreman_wreckingball/host_status_extensions.rb +13 -0
  20. data/app/models/concerns/foreman_wreckingball/user_extensions.rb +12 -0
  21. data/app/models/concerns/foreman_wreckingball/usergroup_extensions.rb +18 -0
  22. data/app/models/concerns/foreman_wreckingball/vmware_facet_host_extensions.rb +10 -0
  23. data/app/models/foreman_wreckingball/cpu_hot_add_status.rb +8 -0
  24. data/app/models/foreman_wreckingball/hardware_version_status.rb +70 -0
  25. data/app/models/foreman_wreckingball/operatingsystem_status.rb +8 -0
  26. data/app/models/foreman_wreckingball/spectre_v2_status.rb +19 -3
  27. data/app/models/foreman_wreckingball/tools_status.rb +9 -1
  28. data/app/models/foreman_wreckingball/vmware_facet.rb +9 -4
  29. data/app/models/foreman_wreckingball/vmware_hypervisor_facet.rb +4 -7
  30. data/app/models/setting/wreckingball.rb +22 -0
  31. data/app/services/foreman_wreckingball/vmware_cluster_importer.rb +7 -7
  32. data/app/services/foreman_wreckingball/vmware_hypervisor_importer.rb +17 -4
  33. data/app/views/foreman_wreckingball/hosts/_hosts.json.rabl +35 -0
  34. data/app/views/foreman_wreckingball/hosts/_status_dashboard_content.erb +13 -23
  35. data/app/views/foreman_wreckingball/hosts/_status_managed_hosts_dashboard_cards.html.erb +16 -0
  36. data/app/views/foreman_wreckingball/hosts/_status_managed_hosts_dashboard_cards_card.html.erb +11 -0
  37. data/app/views/foreman_wreckingball/hosts/_status_row.html.erb +16 -55
  38. data/app/views/foreman_wreckingball/hosts/_status_row_actions.html.erb +22 -0
  39. data/app/views/foreman_wreckingball/hosts/_status_row_hosts_table.html.erb +24 -0
  40. data/app/views/foreman_wreckingball/hosts/_status_row_hosts_table_actions.html.erb +9 -0
  41. data/app/views/foreman_wreckingball/hosts/schedule_remediate.html.erb +44 -26
  42. data/app/views/foreman_wreckingball/hosts/status_dashboard.html.erb +9 -6
  43. data/app/views/foreman_wreckingball/hosts/status_hosts.json.rabl +13 -0
  44. data/app/views/foreman_wreckingball/hosts/status_managed_hosts_dashboard.html.erb +142 -0
  45. data/config/environments/production.rb +7 -0
  46. data/config/routes.rb +4 -4
  47. data/db/migrate/20181020174609_add_power_state_to_vmware_facets.rb +7 -0
  48. data/db/migrate/20181021111543_add_indexes_to_vmware_hypervisor_facets.rb +8 -0
  49. data/lib/foreman_wreckingball/engine.rb +71 -23
  50. data/lib/foreman_wreckingball/version.rb +1 -1
  51. data/lib/tasks/foreman_vmware_checks_tasks.rake +1 -1
  52. data/test/actions/foreman_wreckingball/bulk_remediate_test.rb +31 -0
  53. data/test/actions/foreman_wreckingball/host/refresh_vmware_facet_test.rb +5 -4
  54. data/test/actions/foreman_wreckingball/host/remediate_hardware_version_test.rb +63 -0
  55. data/test/actions/foreman_wreckingball/host/remediate_spectre_v2_test.rb +62 -0
  56. data/test/actions/foreman_wreckingball/host/remediate_vmware_operatingsystem_test.rb +4 -2
  57. data/test/controllers/foreman_wreckingball/hosts_controller_test.rb +278 -39
  58. data/test/factories/compute_resource.rb +6 -1
  59. data/test/factories/foreman_wreckingball_factories.rb +166 -115
  60. data/test/factories/host.rb +16 -0
  61. data/test/factories/task.rb +17 -0
  62. data/test/helpers/foreman_wreckingball/status_helper.rb +10 -0
  63. data/test/integration/hosts_status_dashboard_test.rb +50 -0
  64. data/test/integration/hosts_status_managed_hosts_test.rb +105 -0
  65. data/test/integration_test_plugin_helper.rb +13 -0
  66. data/test/models/foreman_wreckingball/hardware_version_status_test.rb +88 -0
  67. data/test/models/foreman_wreckingball/tools_status_test.rb +5 -2
  68. data/test/models/foreman_wreckingball/vmware_facet_test.rb +27 -6
  69. data/test/models/host_status_test.rb +22 -0
  70. data/test/models/host_test.rb +64 -4
  71. data/test/models/usergroup_test.rb +25 -0
  72. data/test/test_plugin_helper.rb +19 -0
  73. data/test/unit/foreman_wreckingball/vmware_cluster_importer_test.rb +17 -2
  74. data/test/unit/foreman_wreckingball/vmware_hypervisor_importer_test.rb +11 -0
  75. metadata +55 -8
  76. data/app/services/foreman_wreckingball/debris_collector.rb +0 -249
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ForemanWreckingball
4
- VERSION = '3.0.1'
4
+ VERSION = '3.4.1'
5
5
  end
@@ -6,7 +6,7 @@ require 'rake/testtask'
6
6
  namespace :foreman_wreckingball do
7
7
  namespace :vmware do
8
8
  desc 'Synchonize VMware compute resource data'
9
- task :sync => :environment do
9
+ task :sync => ['environment', 'dynflow:client'] do
10
10
  User.as_anonymous_admin do
11
11
  ::ForemanTasks.sync_task(::Actions::ForemanWreckingball::Vmware::ScheduleVmwareSync)
12
12
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ module Actions
6
+ module ForemanWreckingball
7
+ describe Actions::ForemanWreckingball::BulkRemediate do
8
+ include Dynflow::Testing
9
+
10
+ let(:action_class) { ::Actions::ForemanWreckingball::BulkRemediate }
11
+ let(:bulk_action) { ::Actions::BulkAction }
12
+ let(:action) { create_action(action_class) }
13
+
14
+ setup do
15
+ Setting::Wreckingball.load_defaults
16
+ FactoryBot.create_list(:host, 2, :managed, :with_wreckingball_statuses)
17
+ end
18
+
19
+ it 'plans remediate action' do
20
+ ::ForemanWreckingball::Engine::WRECKINGBALL_STATUSES.map(&:constantize)
21
+ .select(&:supports_remediate?)
22
+ .each do |status|
23
+ statuses = HostStatus::Status.where(type: status.to_s)
24
+ plan_action(action, statuses)
25
+
26
+ assert_action_planed_with(action, bulk_action, status.remediate_action, statuses.map(&:host))
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -13,16 +13,17 @@ module Actions
13
13
 
14
14
  setup do
15
15
  ::Fog.mock!
16
- ::Fog::Compute::Vsphere::Mock.any_instance.stubs(:get_vm_ref).returns(vm)
17
- ::Fog::Compute::Vsphere::Server.any_instance.stubs(:ready?).returns(false)
16
+ ::ForemanWreckingball.fog_vsphere_namespace::Mock.any_instance.stubs(:get_vm_ref).returns(vm)
17
+ ::ForemanWreckingball.fog_vsphere_namespace::Server.any_instance.stubs(:ready?).returns(false)
18
18
  ::ForemanWreckingball::SpectreV2Status.any_instance.stubs(:recent_hw_version?).returns(true)
19
19
  # this is not stubbed correctly in fog-vsphere
20
- Fog::Compute::Vsphere::Server.any_instance.stubs(:cpuHotAddEnabled).returns(false)
20
+ ::ForemanWreckingball.fog_vsphere_namespace::Server.any_instance.stubs(:cpuHotAddEnabled).returns(false)
21
+ Setting::Wreckingball.load_defaults
21
22
  end
22
23
  teardown { ::Fog.unmock! }
23
24
 
24
25
  let(:compute_resource) do
25
- cr = FactoryBot.create(:compute_resource, :vmware, :uuid => 'Solutions')
26
+ cr = FactoryBot.create(:compute_resource, :vmware, :with_taxonomy, :uuid => 'Solutions')
26
27
  ComputeResource.find(cr.id)
27
28
  end
28
29
 
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ module Actions
6
+ module ForemanWreckingball
7
+ module Vmware
8
+ class RemediateHardwareVersionTest < ActiveSupport::TestCase
9
+ include ::Dynflow::Testing
10
+ setup do
11
+ ::Fog.mock!
12
+ # this is not stubbed correctly in fog-vsphere
13
+ ::ForemanWreckingball.fog_vsphere_namespace::Server.any_instance.stubs(:cpuHotAddEnabled).returns(false)
14
+ ::ForemanWreckingball.fog_vsphere_namespace::Server.any_instance.stubs(:hardware_version).returns('vmx-13')
15
+ ::ForemanWreckingball::SpectreV2Status.any_instance.stubs(:recent_hw_version?).returns(true)
16
+ ::PowerManager::Virt.any_instance.stubs(:ready?).returns(true)
17
+ Setting::Wreckingball.load_defaults
18
+ end
19
+ teardown { ::Fog.unmock! }
20
+
21
+ let(:compute_resource) do
22
+ cr = FactoryBot.create(:compute_resource, :vmware, :with_taxonomy, :uuid => 'Solutions')
23
+ ComputeResource.find(cr.id)
24
+ end
25
+ let(:uuid) { '5032c8a5-9c5e-ba7a-3804-832a03e16381' }
26
+ let(:vm) { compute_resource.find_vm_by_uuid(uuid) }
27
+
28
+ let(:host) do
29
+ FactoryBot.create(
30
+ :host,
31
+ :managed,
32
+ :with_vmware_facet,
33
+ compute_resource: compute_resource,
34
+ uuid: uuid
35
+ )
36
+ end
37
+
38
+ let(:action_class) { ::Actions::ForemanWreckingball::Host::RemediateHardwareVersion }
39
+ let(:action) do
40
+ create_action(action_class).tap do |action|
41
+ action.stubs(:action_subject).returns(host)
42
+ action.input.update(
43
+ host: {
44
+ id: host.id
45
+ }
46
+ )
47
+ end
48
+ end
49
+ let(:planned_action) do
50
+ plan_action(action, host)
51
+ end
52
+ let(:runned_action) { run_action(planned_action) }
53
+
54
+ test "it remediates the host's hardware version" do
55
+ assert_equal :success, runned_action.state
56
+ assert_equal true, runned_action.output.fetch('state')
57
+ assert_equal true, runned_action.output.fetch('initially_powered_on')
58
+ assert_equal 'vmx-13', host.reload.vmware_facet.hardware_version
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ module Actions
6
+ module ForemanWreckingball
7
+ module Vmware
8
+ class RemediateSpectreV2Test < ActiveSupport::TestCase
9
+ include ::Dynflow::Testing
10
+ setup do
11
+ ::Fog.mock!
12
+ # this is not stubbed correctly in fog-vsphere
13
+ ::ForemanWreckingball.fog_vsphere_namespace::Server.any_instance.stubs(:cpuHotAddEnabled).returns(false)
14
+ ::ForemanWreckingball.fog_vsphere_namespace::Server.any_instance.stubs(:hardware_version).returns('vmx-13')
15
+ ::ForemanWreckingball::SpectreV2Status.any_instance.stubs(:recent_hw_version?).returns(true)
16
+ ::PowerManager::Virt.any_instance.stubs(:ready?).returns(true)
17
+ Setting::Wreckingball.load_defaults
18
+ end
19
+ teardown { ::Fog.unmock! }
20
+
21
+ let(:compute_resource) do
22
+ cr = FactoryBot.create(:compute_resource, :vmware, :with_taxonomy, :uuid => 'Solutions')
23
+ ComputeResource.find(cr.id)
24
+ end
25
+ let(:uuid) { '5032c8a5-9c5e-ba7a-3804-832a03e16381' }
26
+ let(:vm) { compute_resource.find_vm_by_uuid(uuid) }
27
+
28
+ let(:host) do
29
+ FactoryBot.create(
30
+ :host,
31
+ :managed,
32
+ :with_vmware_facet,
33
+ compute_resource: compute_resource,
34
+ uuid: uuid
35
+ )
36
+ end
37
+
38
+ let(:action_class) { ::Actions::ForemanWreckingball::Host::RemediateSpectreV2 }
39
+ let(:action) do
40
+ create_action(action_class).tap do |action|
41
+ action.stubs(:action_subject).returns(host)
42
+ action.input.update(
43
+ host: {
44
+ id: host.id
45
+ }
46
+ )
47
+ end
48
+ end
49
+ let(:planned_action) do
50
+ plan_action(action, host)
51
+ end
52
+ let(:runned_action) { run_action(planned_action) }
53
+
54
+ test "it remediates the host's spectre v2 status" do
55
+ assert_equal :success, runned_action.state
56
+ assert_equal true, runned_action.output.fetch('state')
57
+ assert_equal true, runned_action.output.fetch('initially_powered_on')
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -10,13 +10,15 @@ module Actions
10
10
  setup do
11
11
  ::Fog.mock!
12
12
  # this is not stubbed correctly in fog-vsphere
13
- Fog::Compute::Vsphere::Server.any_instance.stubs(:cpuHotAddEnabled).returns(false)
13
+ ::ForemanWreckingball.fog_vsphere_namespace::Server.any_instance.stubs(:cpuHotAddEnabled).returns(false)
14
14
  ::ForemanWreckingball::SpectreV2Status.any_instance.stubs(:recent_hw_version?).returns(true)
15
+ ::PowerManager::Virt.any_instance.stubs(:ready?).returns(true)
16
+ Setting::Wreckingball.load_defaults
15
17
  end
16
18
  teardown { ::Fog.unmock! }
17
19
 
18
20
  let(:compute_resource) do
19
- cr = FactoryBot.create(:compute_resource, :vmware, :uuid => 'Solutions')
21
+ cr = FactoryBot.create(:compute_resource, :vmware, :with_taxonomy, :uuid => 'Solutions')
20
22
  ComputeResource.find(cr.id)
21
23
  end
22
24
  let(:uuid) { '5032c8a5-9c5e-ba7a-3804-832a03e16381' }
@@ -4,87 +4,326 @@ require 'test_plugin_helper'
4
4
 
5
5
  module ForemanWreckingball
6
6
  class HostsControllerTest < ActionController::TestCase
7
- let(:task_id) { 123 }
8
- let(:fake_task) do
9
- OpenStruct.new(
10
- id: task_id
11
- )
7
+ let(:fake_task) { OpenStruct.new(id: 123) }
8
+
9
+ setup do
10
+ Setting::Wreckingball.load_defaults
12
11
  end
13
12
 
14
13
  describe '#status_dashboard' do
15
- test 'shows an empty status page' do
16
- get :status_dashboard, session: set_session_user
17
- assert_response :success
14
+ context 'when there are no hosts with wreckingball statuses' do
15
+ test 'shows an empty status page' do
16
+ get :status_dashboard, session: set_session_user
17
+ assert_response :success
18
+ end
19
+ end
20
+
21
+ context 'when there are hosts with wreckingball statuses' do
22
+ context 'for admin user' do
23
+ setup do
24
+ FactoryBot.create(:host, :with_wreckingball_statuses, owner: users(:admin))
25
+ FactoryBot.create(:host, :with_wreckingball_statuses, owner: FactoryBot.create(:usergroup, users: [users(:admin)]))
26
+ FactoryBot.create(:host, :with_wreckingball_statuses, owner: FactoryBot.create(:user))
27
+ FactoryBot.create(:host, :with_wreckingball_statuses, owner: FactoryBot.create(:usergroup))
28
+ end
29
+
30
+ test 'shows a status page' do
31
+ get :status_dashboard, session: set_session_user
32
+ assert_response :success
33
+ end
34
+
35
+ test 'should count all hosts' do
36
+ get :status_dashboard, session: set_session_user
37
+ assert_equal 4, assigns[:data].first[:counter][:ok]
38
+ end
39
+
40
+ test 'should count only owned hosts' do
41
+ get :status_dashboard, params: { owned_only: true }, session: set_session_user
42
+ assert_equal 2, assigns[:data].first[:counter][:ok]
43
+ end
44
+ end
45
+
46
+ context 'for user with custom role' do
47
+ let(:search) { "owner = current_user or (owner_type = Usergroup and owner_id = #{user.usergroups.first.id}) or hostgroup_id = #{hostgroup.id} or name ~ #{host_name_prefix}" }
48
+ let(:filter) do
49
+ FactoryBot.create(:filter,
50
+ search: search,
51
+ permissions: Permission.where(name: 'view_hosts'))
52
+ end
53
+ let(:role) { FactoryBot.create(:role) }
54
+ let(:user) { FactoryBot.create(:user, :with_mail, :with_usergroup, admin: false, roles: [role]) }
55
+ let(:hostgroup) { FactoryBot.create(:hostgroup) }
56
+ let(:host_name_prefix) { 'abc' }
57
+
58
+ setup do
59
+ # hosts that match search query
60
+ FactoryBot.create(:host, :with_wreckingball_statuses, owner: user)
61
+ FactoryBot.create(:host, :with_wreckingball_statuses, owner: user.usergroups.first)
62
+ FactoryBot.create(:host, :with_wreckingball_statuses, hostgroup: hostgroup)
63
+ FactoryBot.create(:host, :with_wreckingball_statuses, name: "#{host_name_prefix}123", hostname: "#{host_name_prefix}123")
64
+ # hosts that do not match search query
65
+ FactoryBot.create(:host, :with_wreckingball_statuses, owner: FactoryBot.create(:user))
66
+ FactoryBot.create(:host, :with_wreckingball_statuses, owner: FactoryBot.create(:usergroup))
67
+ FactoryBot.create(:host, :with_wreckingball_statuses, hostgroup: FactoryBot.create(:hostgroup))
68
+ FactoryBot.create(:host, :with_wreckingball_statuses)
69
+ end
70
+
71
+ setup do
72
+ role.filters << filter
73
+ end
74
+
75
+ test 'should count only hosts that match search query' do
76
+ get :status_dashboard, session: set_session_user(user)
77
+
78
+ expected_4_ok = { ok: 4, warning: 0, critical: 0 }
79
+ expected_4_critical = { ok: 0, warning: 0, critical: 4 }
80
+
81
+ assert_equal expected_4_ok, assigns[:data][0][:counter]
82
+ assert_equal expected_4_ok, assigns[:data][1][:counter]
83
+ assert_equal expected_4_ok, assigns[:data][2][:counter]
84
+ assert_equal expected_4_critical, assigns[:data][3][:counter]
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ describe '#status_managed_hosts_dashboard' do
91
+ let(:admin) { users(:admin) }
92
+ let(:organization) { Organization.find_by(name: 'Organization 1') }
93
+ let(:tax_location) { Location.find_by(name: 'Location 1') }
94
+ let(:cr) do
95
+ FactoryBot.create(
96
+ :vmware_cr,
97
+ uuid: 'test',
98
+ organizations: [organization],
99
+ locations: [tax_location]
100
+ )
101
+ end
102
+ let(:other_cr) do
103
+ FactoryBot.create(
104
+ :vmware_cr,
105
+ uuid: 'bla',
106
+ organizations: [organization],
107
+ locations: [tax_location]
108
+ )
109
+ end
110
+
111
+ setup do
112
+ Fog.mock!
113
+ @managed_host = FactoryBot.create(:host, :managed, :with_vmware_facet, compute_resource: cr, owner: admin, uuid: 1)
114
+ @missing_host = FactoryBot.create(:host, :managed, :with_vmware_facet, compute_resource: cr, owner: admin, uuid: 2)
115
+
116
+ mock_vm = mock('vm')
117
+ mock_vm.stubs(:id).returns(@managed_host.uuid)
118
+ mock_vm.stubs(:name).returns(@managed_host.name)
119
+ Foreman::Model::Vmware.any_instance.stubs(:vms).returns(OpenStruct.new(all: Array(mock_vm)))
18
120
  end
19
121
 
20
122
  test 'shows a status page' do
21
- FactoryBot.create_list(:host, 5, :with_wreckingball_statuses)
22
- get :status_dashboard, session: set_session_user
123
+ get :status_managed_hosts_dashboard, session: set_session_user
23
124
  assert_response :success
24
125
  end
126
+
127
+ test 'should contain host with missing vm' do
128
+ get :status_managed_hosts_dashboard, session: set_session_user
129
+ assert_includes assigns[:missing_hosts], @missing_host
130
+ end
131
+
132
+ test 'should filter host with vm' do
133
+ get :status_managed_hosts_dashboard, session: set_session_user
134
+ refute_includes assigns[:missing_hosts], @managed_host
135
+ end
136
+ end
137
+
138
+ describe '#status_hosts' do
139
+ test 'returns correct counts' do
140
+ FactoryBot.create_list(:vmware_hardware_version_status, 3, :with_ok_status)
141
+ FactoryBot.create_list(:vmware_hardware_version_status, 4, :with_out_of_date_status)
142
+
143
+ get :status_hosts, params: { status: ::ForemanWreckingball::HardwareVersionStatus.host_association },
144
+ session: set_session_user, xhr: true
145
+
146
+ assert_response :ok
147
+ json = JSON.parse(response.body)
148
+ assert_equal 4, json['recordsTotal']
149
+ assert_equal 4, json['recordsFiltered']
150
+ assert_equal 4, json['data'].size
151
+ end
152
+
153
+ test 'returns hosts for status' do
154
+ ok_status = FactoryBot.create(:vmware_hardware_version_status, :with_ok_status)
155
+ out_of_date_status = FactoryBot.create(:vmware_hardware_version_status, :with_out_of_date_status)
156
+
157
+ get :status_hosts, params: { status: ::ForemanWreckingball::HardwareVersionStatus.host_association },
158
+ session: set_session_user, xhr: true
159
+
160
+ assert_response :ok
161
+
162
+ data = JSON.parse(response.body)['data']
163
+
164
+ hosts_names = data.map { |host| host['name'] }
165
+ assert_equal 1, data.size
166
+ assert_includes hosts_names, out_of_date_status.host.name
167
+ refute_includes hosts_names, ok_status.host.name
168
+ end
169
+
170
+ test 'returns hosts for spectre v2 status' do
171
+ FactoryBot.create_list(:vmware_spectre_v2_status, 1, :with_enabled)
172
+ FactoryBot.create_list(:vmware_spectre_v2_status, 2, :with_missing)
173
+
174
+ get :status_hosts, params: { status: ::ForemanWreckingball::SpectreV2Status.host_association },
175
+ session: set_session_user, xhr: true
176
+
177
+ data = JSON.parse(response.body)['data']
178
+ assert_equal 2, data.size
179
+ end
180
+
181
+ describe 'filtering by owner' do
182
+ setup do
183
+ FactoryBot.create(:host, :with_wreckingball_statuses, owner: users(:admin))
184
+ FactoryBot.create(:host, :with_wreckingball_statuses, owner: FactoryBot.create(:usergroup, users: [users(:admin)]))
185
+ FactoryBot.create(:host, :with_wreckingball_statuses, owner: FactoryBot.create(:user))
186
+ FactoryBot.create(:host, :with_wreckingball_statuses, owner: FactoryBot.create(:usergroup))
187
+ end
188
+
189
+ test 'should show all hosts' do
190
+ get :status_hosts, params: { status: ::ForemanWreckingball::SpectreV2Status.host_association },
191
+ session: set_session_user, xhr: true
192
+ assert_equal 4, JSON.parse(response.body)['data'].count
193
+ end
194
+
195
+ test 'should show only owned hosts' do
196
+ get :status_hosts, params: { status: ::ForemanWreckingball::SpectreV2Status.host_association, owned_only: true },
197
+ session: set_session_user, xhr: true
198
+ assert_equal 2, JSON.parse(response.body)['data'].count
199
+ end
200
+ end
25
201
  end
26
202
 
27
203
  describe '#refresh_status_dashboard' do
28
204
  test 'redirects to scheduled task' do
205
+ FactoryBot.create(:some_task, :vmware_sync, :stopped)
206
+
29
207
  ForemanTasks.expects(:async_task).returns(fake_task)
30
208
  put :refresh_status_dashboard, session: set_session_user
31
209
  assert_response :redirect
32
210
  assert_includes flash[:success], 'successfully scheduled'
33
211
  assert_redirected_to foreman_tasks_task_path(123)
34
212
  end
213
+
214
+ test 'show flash warning message when task is already running' do
215
+ FactoryBot.create(:some_task, :vmware_sync, :running)
216
+
217
+ put :refresh_status_dashboard, session: set_session_user
218
+ assert_response :redirect
219
+ assert_includes flash[:warning], 'task is already running'
220
+ assert_redirected_to status_dashboard_hosts_path
221
+ end
35
222
  end
36
223
 
37
224
  describe '#schedule_remediate' do
38
- let(:host) do
39
- FactoryBot.create(:host, :with_wreckingball_statuses)
40
- end
225
+ let(:host) { FactoryBot.create(:host, :with_wreckingball_statuses) }
226
+ let(:status_ids) { [host.vmware_tools_status_object.id] }
41
227
 
42
228
  test 'shows a remediation schedule page' do
43
- get :schedule_remediate, params: { status_id: host.vmware_operatingsystem_status_object.id, id: host.id }, session: set_session_user
229
+ get :schedule_remediate, params: { status_ids: status_ids }, session: set_session_user
44
230
  assert_response :success
45
231
  end
46
232
 
47
- test 'returns not found when host id is invalid' do
48
- get :schedule_remediate, params: { status_id: nil, id: 'invalid' }, session: set_session_user
49
- assert_response :not_found
233
+ context 'with status_id' do
234
+ let(:hosts) { FactoryBot.create_list(:host, 2, :with_wreckingball_statuses) }
235
+ let(:status_ids) { [hosts.first.vmware_operatingsystem_status_object.id] }
236
+
237
+ test 'remediate selected statuses' do
238
+ get :schedule_remediate, params: { status_ids: status_ids }, session: set_session_user
239
+ assert_same_elements HostStatus::Status.find(status_ids), assigns['statuses']
240
+ end
50
241
  end
51
242
 
52
- test 'returns not found when status id is invalid' do
53
- FactoryBot.create(:host, :with_wreckingball_statuses)
54
- get :schedule_remediate, params: { status_id: 'invalid', id: host.id }, session: set_session_user
55
- assert_response :not_found
243
+ context 'with host_association' do
244
+ let(:hosts) { FactoryBot.create_list(:host, 2, :with_wreckingball_statuses, owner: users(:admin)) }
245
+ let(:statuses) { hosts.map { |h| h.send(host_association) } }
246
+ let(:status_class) { ForemanWreckingball::OperatingsystemStatus }
247
+ let(:host_association) { status_class.host_association }
248
+
249
+ setup do
250
+ hosts.each { |h| h.send(host_association).update(status: status_class::MISMATCH) }
251
+ end
252
+
253
+ test 'remediate all statuses' do
254
+ get :schedule_remediate, params: { host_association: host_association }, session: set_session_user
255
+ assert_same_elements statuses, assigns['statuses']
256
+ end
257
+
258
+ context 'with owned_only' do
259
+ setup do
260
+ hosts_list = FactoryBot.create_list(:host, 2, :with_wreckingball_statuses, owner: users(:one))
261
+ hosts_list.each { |h| h.send(host_association).update(status: status_class::MISMATCH) }
262
+ end
263
+
264
+ test 'remediate only those statuses where the user is the owner of the host' do
265
+ get :schedule_remediate, params: { host_association: host_association, owned_only: true }, session: set_session_user
266
+ assert_same_elements statuses, assigns['statuses']
267
+ end
268
+ end
56
269
  end
57
270
  end
58
271
 
59
272
  describe '#submit_remediate' do
60
- let(:host) do
61
- FactoryBot.create(:host, :with_wreckingball_statuses)
273
+ let(:host) { FactoryBot.create(:host, :with_wreckingball_statuses) }
274
+ let(:status_ids) { [host.vmware_tools_status_object.id] }
275
+
276
+ setup do
277
+ ForemanTasks.stubs(:async_task).returns(fake_task)
62
278
  end
63
279
 
64
280
  test 'redirects to scheduled task' do
65
- ForemanTasks.expects(:async_task).returns(fake_task)
66
- post :submit_remediate, params: { status_id: host.vmware_operatingsystem_status_object.id, id: host.id }, session: set_session_user
281
+ post :submit_remediate, params: { status_ids: status_ids }, session: set_session_user
67
282
  assert_response :redirect
68
- assert_includes flash[:success], 'successfully scheduled'
69
- assert_redirected_to foreman_tasks_task_path(123)
283
+ assert_redirected_to foreman_tasks_task_path(fake_task.id)
70
284
  end
71
285
 
72
- test 'raises error when status can not be remediated' do
73
- FactoryBot.create(:host, :with_wreckingball_statuses)
74
- assert_raises Foreman::Exception do
75
- post :submit_remediate, params: { status_id: host.vmware_tools_status_object.id, id: host.id }, session: set_session_user
76
- end
286
+ test 'returns not found when status_ids param is invalid' do
287
+ post :submit_remediate, params: { status_ids: 'invalid' }, session: set_session_user
288
+ assert_response :not_found
77
289
  end
78
290
 
79
- test 'returns not found when host id is invalid' do
80
- post :submit_remediate, params: { status_id: nil, id: 'invalid' }, session: set_session_user
81
- assert_response :not_found
291
+ context 'with status_id' do
292
+ let(:hosts) { FactoryBot.create_list(:host, 2, :with_wreckingball_statuses) }
293
+ let(:status_ids) { [hosts.first.vmware_operatingsystem_status_object.id] }
294
+
295
+ test 'remediate selected statuses' do
296
+ post :submit_remediate, params: { status_ids: status_ids }, session: set_session_user
297
+ assert_same_elements HostStatus::Status.find(status_ids), assigns['statuses']
298
+ end
82
299
  end
83
300
 
84
- test 'returns not found when status id is invalid' do
85
- FactoryBot.create(:host, :with_wreckingball_statuses)
86
- post :submit_remediate, params: { status_id: 'invalid', id: host.id }, session: set_session_user
87
- assert_response :not_found
301
+ context 'with host_association' do
302
+ let(:hosts) { FactoryBot.create_list(:host, 2, :with_wreckingball_statuses, owner: users(:admin)) }
303
+ let(:statuses) { hosts.map { |h| h.send(host_association) } }
304
+ let(:status_class) { ForemanWreckingball::OperatingsystemStatus }
305
+ let(:host_association) { status_class.host_association }
306
+
307
+ setup do
308
+ hosts.each { |h| h.send(host_association).update(status: status_class::MISMATCH) }
309
+ end
310
+
311
+ test 'remediate all statuses' do
312
+ post :submit_remediate, params: { host_association: host_association }, session: set_session_user
313
+ assert_same_elements statuses, assigns['statuses']
314
+ end
315
+
316
+ context 'with owned_only' do
317
+ setup do
318
+ hosts_list = FactoryBot.create_list(:host, 2, :with_wreckingball_statuses, owner: users(:one))
319
+ hosts_list.each { |h| h.send(host_association).update(status: status_class::MISMATCH) }
320
+ end
321
+
322
+ test 'remediate only those statuses where the user is the owner of the host' do
323
+ post :submit_remediate, params: { host_association: host_association, owned_only: true }, session: set_session_user
324
+ assert_same_elements statuses, assigns['statuses']
325
+ end
326
+ end
88
327
  end
89
328
  end
90
329
  end