foreman_monitoring 0.1.2 → 2.1.0

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 (44) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -7
  3. data/Rakefile +0 -0
  4. data/app/controllers/api/v2/downtime_controller.rb +63 -0
  5. data/app/controllers/api/v2/monitoring_results_controller.rb +5 -4
  6. data/app/controllers/concerns/foreman_monitoring/find_host_by_client_cert.rb +60 -0
  7. data/app/controllers/concerns/foreman_monitoring/hosts_controller_extensions.rb +21 -20
  8. data/app/helpers/concerns/foreman_monitoring/hosts_helper_ext.rb +16 -19
  9. data/app/lib/proxy_api/monitoring.rb +9 -6
  10. data/app/models/concerns/foreman_monitoring/host_extensions.rb +15 -14
  11. data/app/models/concerns/foreman_monitoring/hostgroup_extensions.rb +7 -3
  12. data/app/models/concerns/orchestration/monitoring.rb +22 -15
  13. data/app/models/host_status/monitoring_status.rb +7 -4
  14. data/app/models/monitoring_result.rb +13 -6
  15. data/app/models/setting/monitoring.rb +4 -2
  16. data/app/overrides/add_host_monitoring_result_tab.rb +8 -6
  17. data/app/overrides/add_host_multiple_power_set_downtime_checkbox.rb +5 -3
  18. data/app/overrides/add_host_set_downtime_modal.rb +4 -2
  19. data/app/services/monitoring.rb +3 -0
  20. data/app/views/hosts/_downtime_fields.html.erb +2 -2
  21. data/app/views/monitoring_results/_host_tab_pane.html.erb +2 -2
  22. data/config/routes.rb +3 -0
  23. data/db/migrate/20160817135723_create_monitoring_results.rb +5 -1
  24. data/db/migrate/20161220201510_add_monitoring_proxy_id_to_host_and_hostgroup.rb +3 -1
  25. data/db/migrate/201910180900_rename_downtime_host_permission.rb +15 -0
  26. data/db/seeds.d/60-monitoring_proxy_feature.rb +2 -0
  27. data/lib/foreman_monitoring.rb +2 -0
  28. data/lib/foreman_monitoring/engine.rb +32 -28
  29. data/lib/foreman_monitoring/version.rb +3 -1
  30. data/lib/tasks/foreman_monitoring_tasks.rake +5 -5
  31. data/locale/gemspec.rb +2 -0
  32. data/test/controllers/api/v2/downtime_controller_test.rb +73 -0
  33. data/test/factories/feature.rb +4 -2
  34. data/test/factories/host.rb +6 -4
  35. data/test/factories/monitoring_results.rb +9 -7
  36. data/test/factories/smart_proxy.rb +3 -1
  37. data/test/functional/hosts_controller_test.rb +65 -52
  38. data/test/lib/proxy_api/monitoring_test.rb +16 -14
  39. data/test/test_plugin_helper.rb +5 -3
  40. data/test/unit/host_status/monitoring_status_test.rb +18 -16
  41. data/test/unit/host_test.rb +6 -4
  42. data/test/unit/monitoring_result_test.rb +75 -0
  43. data/test/unit/monitoring_test.rb +5 -3
  44. metadata +62 -14
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanMonitoring
2
- VERSION = '0.1.2'.freeze
4
+ VERSION = '2.1.0'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Tests
2
4
  namespace :test do
3
5
  desc 'Test ForemanMonitoring'
@@ -11,7 +13,7 @@ namespace :test do
11
13
  end
12
14
 
13
15
  namespace :foreman_monitoring do
14
- task :rubocop do
16
+ task rubocop: :environment do
15
17
  begin
16
18
  require 'rubocop/rake_task'
17
19
  RuboCop::RakeTask.new(:rubocop_foreman_monitoring) do |task|
@@ -19,7 +21,7 @@ namespace :foreman_monitoring do
19
21
  "#{ForemanMonitoring::Engine.root}/lib/**/*.rb",
20
22
  "#{ForemanMonitoring::Engine.root}/test/**/*.rb"]
21
23
  end
22
- rescue
24
+ rescue StandardError
23
25
  puts 'Rubocop not loaded.'
24
26
  end
25
27
 
@@ -30,6 +32,4 @@ end
30
32
  Rake::Task[:test].enhance ['test:foreman_monitoring']
31
33
 
32
34
  load 'tasks/jenkins.rake'
33
- if Rake::Task.task_defined?(:'jenkins:unit')
34
- Rake::Task['jenkins:unit'].enhance ['test:foreman_monitoring', 'foreman_monitoring:rubocop']
35
- end
35
+ Rake::Task['jenkins:unit'].enhance ['test:foreman_monitoring', 'foreman_monitoring:rubocop'] if Rake::Task.task_defined?(:'jenkins:unit')
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Matches foreman_monitoring.gemspec
2
4
  _('Set a downtime for hosts after they are deleted in Foreman.')
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class Api::V2::DowntimeControllerTest < ActionController::TestCase
6
+ let(:host1) { as_admin { FactoryBot.create(:host, :managed) } }
7
+
8
+ context 'with user authentication' do
9
+ context '#create' do
10
+ test 'should deny access' do
11
+ post :create
12
+ assert_response :forbidden
13
+ end
14
+ end
15
+ end
16
+
17
+ context 'with client cert' do
18
+ setup do
19
+ User.current = nil
20
+ reset_api_credentials
21
+
22
+ Setting[:ssl_client_dn_env] = 'SSL_CLIENT_S_DN'
23
+ Setting[:ssl_client_verify_env] = 'SSL_CLIENT_VERIFY'
24
+
25
+ @request.env['HTTPS'] = 'on'
26
+ @request.env['SSL_CLIENT_S_DN'] = "CN=#{host1.name},DN=example,DN=com"
27
+ @request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
28
+ end
29
+
30
+ context '#create' do
31
+ test 'should create downtime' do
32
+ post :create
33
+ assert_response :success
34
+ end
35
+ end
36
+
37
+ context '#create with duration' do
38
+ test 'should create downtime with given duration' do
39
+ Host::Managed.any_instance.expects(:downtime_host).with { |params| params[:end_time] - params[:start_time] == 3600 }
40
+ post :create, params: { duration: 3600 }
41
+ assert_response :success
42
+ end
43
+
44
+ test 'should create downtime with given duration as string' do
45
+ Host::Managed.any_instance.expects(:downtime_host).with { |params| params[:end_time] - params[:start_time] == 3600 }
46
+ post :create, params: { duration: '3600' }
47
+ assert_response :success
48
+ end
49
+ end
50
+
51
+ context '#create with reason' do
52
+ test 'should create downtime with given reason' do
53
+ Host::Managed.any_instance.expects(:downtime_host).with { |params| params[:comment] == 'In testing' }
54
+ post :create, params: { reason: 'In testing' }
55
+ assert_response :success
56
+ end
57
+ end
58
+ end
59
+
60
+ context 'without any credentials' do
61
+ setup do
62
+ User.current = nil
63
+ reset_api_credentials
64
+ end
65
+
66
+ context '#create' do
67
+ test 'should deny access' do
68
+ post :create
69
+ assert_response :unauthorized
70
+ end
71
+ end
72
+ end
73
+ end
@@ -1,7 +1,9 @@
1
- FactoryGirl.modify do
1
+ # frozen_string_literal: true
2
+
3
+ FactoryBot.modify do
2
4
  factory :feature do
3
5
  trait :monitoring do
4
- name 'Monitoring'
6
+ name { 'Monitoring' }
5
7
  end
6
8
  end
7
9
  end
@@ -1,18 +1,20 @@
1
- FactoryGirl.modify do
1
+ # frozen_string_literal: true
2
+
3
+ FactoryBot.modify do
2
4
  factory :host do
3
5
  trait :with_monitoring do
4
6
  monitoring_proxy do
5
- FactoryGirl.create(:smart_proxy, :monitoring)
7
+ FactoryBot.create(:smart_proxy, :monitoring)
6
8
  end
7
9
  end
8
10
 
9
11
  trait :with_monitoring_results do
10
12
  transient do
11
- monitoring_result_count 20
13
+ monitoring_result_count { 20 }
12
14
  end
13
15
  after(:create) do |host, evaluator|
14
16
  evaluator.monitoring_result_count.times do
15
- FactoryGirl.create(:monitoring_result, :host => host)
17
+ FactoryBot.create(:monitoring_result, :host => host)
16
18
  end
17
19
  end
18
20
  end
@@ -1,30 +1,32 @@
1
- FactoryGirl.define do
1
+ # frozen_string_literal: true
2
+
3
+ FactoryBot.define do
2
4
  factory :monitoring_result do
3
5
  sequence(:service) { |n| "Service #{n}" }
4
6
  result { rand(0..3) }
5
7
 
6
8
  trait :ok do
7
- result 0
9
+ result { 0 }
8
10
  end
9
11
 
10
12
  trait :warning do
11
- result 1
13
+ result { 1 }
12
14
  end
13
15
 
14
16
  trait :critical do
15
- result 2
17
+ result { 2 }
16
18
  end
17
19
 
18
20
  trait :unknown do
19
- result 3
21
+ result { 3 }
20
22
  end
21
23
 
22
24
  trait :downtime do
23
- downtime true
25
+ downtime { true }
24
26
  end
25
27
 
26
28
  trait :acknowledged do
27
- acknowledged true
29
+ acknowledged { true }
28
30
  end
29
31
  end
30
32
  end
@@ -1,4 +1,6 @@
1
- FactoryGirl.modify do
1
+ # frozen_string_literal: true
2
+
3
+ FactoryBot.modify do
2
4
  factory :smart_proxy do
3
5
  trait :monitoring do
4
6
  features { |sp| [sp.association(:feature, :monitoring)] }
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_plugin_helper'
2
4
 
3
5
  class HostsControllerExtensionsTest < ActionController::TestCase
@@ -8,7 +10,7 @@ class HostsControllerExtensionsTest < ActionController::TestCase
8
10
  ProxyAPI::Monitoring.stubs(:create_host).returns(true)
9
11
  ProxyAPI::Monitoring.stubs(:update_host).returns(true)
10
12
  ProxyAPI::Monitoring.stubs(:delete_host).returns(true)
11
- @host = FactoryGirl.create(:host, :managed)
13
+ @host = FactoryBot.create(:host, :managed)
12
14
  end
13
15
 
14
16
  context 'when setting a host downtime' do
@@ -18,23 +20,25 @@ class HostsControllerExtensionsTest < ActionController::TestCase
18
20
 
19
21
  test 'the flash should inform it' do
20
22
  Host::Managed.any_instance.stubs(:downtime_host).returns(true)
21
- put :downtime, {
22
- :id => @host.name,
23
- :downtime => {
24
- :comment => 'Maintenance work.',
25
- :starttime => Time.current,
26
- :endtime => Time.current.advance(:hours => 2)
27
- }
28
- }, set_session_user
23
+ put :downtime,
24
+ params: {
25
+ :id => @host.name,
26
+ :downtime => {
27
+ :comment => 'Maintenance work.',
28
+ :starttime => Time.current,
29
+ :endtime => Time.current.advance(:hours => 2)
30
+ }
31
+ },
32
+ session: set_session_user
29
33
  assert_response :found
30
34
  assert_redirected_to host_path(:id => @host)
31
35
  assert_nil flash[:error]
32
- assert_not_nil flash[:notice]
33
- assert_equal "Created downtime for #{@host}", flash[:notice]
36
+ assert_not_nil flash[:success]
37
+ assert_equal "Created downtime for #{@host}", flash[:success]
34
38
  end
35
39
 
36
40
  test 'with missing comment param the flash should inform it' do
37
- put :downtime, { :id => @host.name }, set_session_user
41
+ put :downtime, params: { :id => @host.name }, session: set_session_user
38
42
  assert_response :found
39
43
  assert_redirected_to host_path(:id => @host)
40
44
  assert_not_nil flash[:error]
@@ -42,7 +46,9 @@ class HostsControllerExtensionsTest < ActionController::TestCase
42
46
  end
43
47
 
44
48
  test 'with missing date params the flash should inform it' do
45
- put :downtime, { :id => @host.name, :downtime => { :comment => 'Maintenance work.' } }, set_session_user
49
+ put :downtime,
50
+ params: { :id => @host.name, :downtime => { :comment => 'Maintenance work.' } },
51
+ session: set_session_user
46
52
  assert_response :found
47
53
  assert_redirected_to host_path(:id => @host)
48
54
  assert_not_nil flash[:error]
@@ -50,14 +56,16 @@ class HostsControllerExtensionsTest < ActionController::TestCase
50
56
  end
51
57
 
52
58
  test 'with invalid starttime the flash should inform it' do
53
- put :downtime, {
54
- :id => @host.name,
55
- :downtime => {
56
- :comment => 'Maintenance work.',
57
- :starttime => 'invalid',
58
- :endtime => 'invalid'
59
- }
60
- }, set_session_user
59
+ put :downtime,
60
+ params: {
61
+ :id => @host.name,
62
+ :downtime => {
63
+ :comment => 'Maintenance work.',
64
+ :starttime => 'invalid',
65
+ :endtime => 'invalid'
66
+ }
67
+ },
68
+ session: set_session_user
61
69
  assert_response :found
62
70
  assert_redirected_to host_path(:id => @host)
63
71
  assert_not_nil flash[:error]
@@ -65,28 +73,34 @@ class HostsControllerExtensionsTest < ActionController::TestCase
65
73
  end
66
74
 
67
75
  test 'should parse the times in the correct time zone' do
68
- User.current.update_attribute(:timezone, 'Berlin')
76
+ User.current.timezone = 'Berlin'
77
+ User.current.save
69
78
  Host::Managed.any_instance.expects(:downtime_host).with(has_entries(:start_time => 1_492_676_100, :end_time => 1_492_683_300))
70
- put :downtime, {
71
- :id => @host.name,
72
- :downtime => {
73
- :comment => 'Maintenance work.',
74
- :starttime => '2017-04-20T10:15',
75
- :endtime => '2017-04-20T12:15'
76
- }
77
- }, set_session_user
79
+ put :downtime,
80
+ params: {
81
+ :id => @host.name,
82
+ :downtime => {
83
+ :comment => 'Maintenance work.',
84
+ :starttime => '2017-04-20T10:15',
85
+ :endtime => '2017-04-20T12:15'
86
+ }
87
+ },
88
+ session: set_session_user
78
89
  end
79
90
  end
80
91
 
81
92
  describe 'setting a downtime on multiple hosts' do
82
93
  before do
83
- @hosts = FactoryGirl.create_list(:host, 2, :with_monitoring)
94
+ @hosts = FactoryBot.create_list(:host, 2, :with_monitoring)
84
95
  @request.env['HTTP_REFERER'] = hosts_path
85
96
  end
86
97
 
87
98
  test 'show a host selection' do
88
99
  host_ids = @hosts.map(&:id)
89
- xhr :post, :select_multiple_downtime, { :host_ids => host_ids }, set_session_user
100
+ post :select_multiple_downtime,
101
+ params: { :host_ids => host_ids },
102
+ session: set_session_user,
103
+ xhr: true
90
104
  assert_response :success
91
105
  assert_includes response.body, @hosts.first.name
92
106
  assert_includes response.body, @hosts.last.name
@@ -103,20 +117,19 @@ class HostsControllerExtensionsTest < ActionController::TestCase
103
117
  }
104
118
  }
105
119
 
106
- post :update_multiple_downtime, params,
107
- set_session_user
120
+ post :update_multiple_downtime, params: params, session: set_session_user
108
121
 
109
122
  assert_response :found
110
123
  assert_redirected_to hosts_path
111
124
  assert_nil flash[:error]
112
- assert_not_nil flash[:notice]
113
- assert_equal 'A downtime was set for the selected hosts.', flash[:notice]
125
+ assert_not_nil flash[:success]
126
+ assert_equal 'A downtime was set for the selected hosts.', flash[:success]
114
127
  end
115
128
  end
116
129
 
117
130
  describe 'changing the power state on multiple hosts' do
118
131
  before do
119
- @hosts = FactoryGirl.create_list(:host, 2, :with_monitoring)
132
+ @hosts = FactoryBot.create_list(:host, 2, :with_monitoring)
120
133
  @request.env['HTTP_REFERER'] = hosts_path
121
134
 
122
135
  power_mock = mock('power')
@@ -135,14 +148,13 @@ class HostsControllerExtensionsTest < ActionController::TestCase
135
148
  }
136
149
  }
137
150
 
138
- post :update_multiple_power_state, params,
139
- set_session_user
151
+ post :update_multiple_power_state, params: params, session: set_session_user
140
152
 
141
153
  assert_response :found
142
154
  assert_redirected_to hosts_path
143
155
  assert_nil flash[:error]
144
- assert_not_nil flash[:notice]
145
- assert_equal 'The power state of the selected hosts will be set to poweroff', flash[:notice]
156
+ assert_not_nil flash[:success]
157
+ assert_equal 'The power state of the selected hosts will be set to poweroff', flash[:success]
146
158
  end
147
159
 
148
160
  test 'should not set a downtime if not selected' do
@@ -155,27 +167,29 @@ class HostsControllerExtensionsTest < ActionController::TestCase
155
167
  }
156
168
  }
157
169
 
158
- post :update_multiple_power_state, params,
159
- set_session_user
170
+ post :update_multiple_power_state, params: params, session: set_session_user
160
171
 
161
172
  assert_response :found
162
173
  assert_redirected_to hosts_path
163
174
  assert_nil flash[:error]
164
- assert_not_nil flash[:notice]
165
- assert_equal 'The power state of the selected hosts will be set to poweroff', flash[:notice]
175
+ assert_not_nil flash[:success]
176
+ assert_equal 'The power state of the selected hosts will be set to poweroff', flash[:success]
166
177
  end
167
178
  end
168
179
 
169
180
  describe 'changing the monitoring proxy of multiple hosts' do
170
- let(:hosts) { FactoryGirl.create_list(:host, 2, :with_monitoring) }
171
- let(:monitoring_proxy) { FactoryGirl.create(:smart_proxy, :monitoring, :organizations => [hosts.first.organization], :locations => [hosts.first.location]) }
181
+ let(:hosts) { FactoryBot.create_list(:host, 2, :with_monitoring) }
182
+ let(:monitoring_proxy) { FactoryBot.create(:smart_proxy, :monitoring, :organizations => [hosts.first.organization], :locations => [hosts.first.location]) }
172
183
  before do
173
184
  @request.env['HTTP_REFERER'] = hosts_path
174
185
  end
175
186
 
176
187
  test 'show a host selection' do
177
188
  host_ids = hosts.map(&:id)
178
- xhr :post, :select_multiple_monitoring_proxy, { :host_ids => host_ids }, set_session_user
189
+ post :select_multiple_monitoring_proxy,
190
+ params: { :host_ids => host_ids },
191
+ session: set_session_user,
192
+ xhr: true
179
193
  assert_response :success
180
194
  hosts.each do |host|
181
195
  assert response.body =~ /#{host.name}/m
@@ -184,7 +198,7 @@ class HostsControllerExtensionsTest < ActionController::TestCase
184
198
 
185
199
  test 'should change the proxy' do
186
200
  hosts.each do |host|
187
- refute_equal monitoring_proxy, host.monitoring_proxy
201
+ assert_not_equal monitoring_proxy, host.monitoring_proxy
188
202
  end
189
203
 
190
204
  params = {
@@ -192,13 +206,12 @@ class HostsControllerExtensionsTest < ActionController::TestCase
192
206
  :proxy => { :proxy_id => monitoring_proxy.id }
193
207
  }
194
208
 
195
- post :update_multiple_monitoring_proxy, params,
196
- set_session_user
209
+ post :update_multiple_monitoring_proxy, params: params, session: set_session_user
197
210
 
198
211
  assert_response :found
199
212
  assert_redirected_to hosts_path
200
213
  assert_nil flash[:error]
201
- assert_equal "The Monitoring proxy of the selected hosts was set to #{monitoring_proxy.name}", flash[:notice]
214
+ assert_equal "The Monitoring proxy of the selected hosts was set to #{monitoring_proxy.name}", flash[:success]
202
215
 
203
216
  hosts.each do |host|
204
217
  as_admin do
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
- class ProxyApiDhcpTest < ActiveSupport::TestCase
5
+ class ProxyApiMonitoringTest < ActiveSupport::TestCase
4
6
  def setup
5
7
  @url = 'http://localhost:8443'
6
- @monitoring = ProxyAPI::Monitoring.new({ :url => @url })
8
+ @monitoring = ProxyAPI::Monitoring.new(:url => @url)
7
9
  end
8
10
 
9
11
  test 'constructor should complete' do
@@ -15,38 +17,38 @@ class ProxyApiDhcpTest < ActiveSupport::TestCase
15
17
  end
16
18
 
17
19
  test 'create_host_downtime should do post' do
18
- @monitoring.expects(:post).with({}, 'downtime/host/example.com').
19
- returns(fake_rest_client_response({ 'result' => {} }))
20
+ @monitoring.expects(:post).with({}, 'downtime/host/example.com')
21
+ .returns(fake_rest_client_response('result' => {}))
20
22
  assert_equal({ 'result' => {} }, @monitoring.create_host_downtime('example.com'))
21
23
  end
22
24
 
23
25
  test 'remove_host_downtime should do delete' do
24
- @monitoring.expects(:delete).with('downtime/host/example.com?comment=bla').
25
- returns(fake_rest_client_response({ 'result' => {} }))
26
+ @monitoring.expects(:delete).with('downtime/host/example.com?comment=bla')
27
+ .returns(fake_rest_client_response('result' => {}))
26
28
  assert_equal({ 'result' => {} }, @monitoring.remove_host_downtime('example.com', :comment => 'bla'))
27
29
  end
28
30
 
29
31
  test 'create_host should do put' do
30
- @monitoring.expects(:put).with({ :attributes => { :ip => '1.1.1.1' } }, 'host/example.com').
31
- returns(fake_rest_client_response({ 'result' => {} }))
32
+ @monitoring.expects(:put).with({ :attributes => { :ip => '1.1.1.1' } }, 'host/example.com')
33
+ .returns(fake_rest_client_response('result' => {}))
32
34
  assert_equal({ 'result' => {} }, @monitoring.create_host('example.com', :ip => '1.1.1.1'))
33
35
  end
34
36
 
35
37
  test 'update_host should do post' do
36
- @monitoring.expects(:post).with({ :attributes => { :ip => '1.1.1.1' } }, 'host/example.com').
37
- returns(fake_rest_client_response({ 'result' => {} }))
38
+ @monitoring.expects(:post).with({ :attributes => { :ip => '1.1.1.1' } }, 'host/example.com')
39
+ .returns(fake_rest_client_response('result' => {}))
38
40
  assert_equal({ 'result' => {} }, @monitoring.update_host('example.com', :ip => '1.1.1.1'))
39
41
  end
40
42
 
41
43
  test 'delete_host should do delete' do
42
- @monitoring.expects(:delete).with('host/example.com').
43
- returns(fake_rest_client_response({ 'result' => {} }))
44
+ @monitoring.expects(:delete).with('host/example.com')
45
+ .returns(fake_rest_client_response('result' => {}))
44
46
  assert_equal({ 'result' => {} }, @monitoring.delete_host('example.com'))
45
47
  end
46
48
 
47
49
  test 'query_host should do get' do
48
- @monitoring.expects(:get).with('host/example.com').
49
- returns(fake_rest_client_response({ 'result' => {} }))
50
+ @monitoring.expects(:get).with('host/example.com')
51
+ .returns(fake_rest_client_response('result' => {}))
50
52
  assert_equal({ 'result' => {} }, @monitoring.query_host('example.com'))
51
53
  end
52
54
  end