foreman_dlm 0.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +18 -6
  3. data/app/controllers/api/v2/dlmlock_events_controller.rb +42 -0
  4. data/app/controllers/api/v2/dlmlocks_controller.rb +31 -16
  5. data/app/controllers/concerns/foreman_dlm/find_host_by_client_cert.rb +7 -13
  6. data/app/controllers/concerns/foreman_dlm/find_host_by_ip.rb +1 -1
  7. data/app/controllers/concerns/foreman_dlm/update_checkin_time.rb +22 -0
  8. data/app/controllers/foreman_dlm/application_controller.rb +19 -0
  9. data/app/controllers/foreman_dlm/dlmlocks_controller.rb +82 -0
  10. data/app/helpers/foreman_dlm/dlmlock_helper.rb +33 -0
  11. data/app/jobs/foreman_dlm/refresh_dlmlock_status.rb +17 -0
  12. data/app/models/concerns/foreman_dlm/dlm_facet_host_extensions.rb +13 -0
  13. data/app/models/concerns/foreman_dlm/expirable.rb +36 -0
  14. data/app/models/concerns/foreman_dlm/host_extensions.rb +21 -2
  15. data/app/models/concerns/foreman_dlm/host_monitoring_extensions.rb +2 -0
  16. data/app/models/concerns/foreman_dlm/user_extensions.rb +13 -0
  17. data/app/models/foreman_dlm/dlm_facet.rb +7 -0
  18. data/app/models/foreman_dlm/dlmlock/update.rb +9 -0
  19. data/app/models/foreman_dlm/dlmlock.rb +137 -0
  20. data/app/models/foreman_dlm/dlmlock_event.rb +23 -0
  21. data/app/models/host_status/dlmlock_status.rb +44 -0
  22. data/app/views/api/v2/dlmlock_events/index.json.rabl +2 -0
  23. data/app/views/api/v2/dlmlocks/show.json.rabl +1 -1
  24. data/app/views/api/v2/errors/precondition_failed.json.rabl +1 -1
  25. data/app/views/foreman_dlm/api/v2/dlm_facets/base.json.rabl +1 -0
  26. data/app/views/foreman_dlm/api/v2/dlm_facets/base_with_root.json.rabl +3 -0
  27. data/app/views/foreman_dlm/api/v2/dlm_facets/show.json.rabl +3 -0
  28. data/app/views/foreman_dlm/dlmlocks/_details.html.erb +42 -0
  29. data/app/views/{dlmlocks → foreman_dlm/dlmlocks}/_list.html.erb +3 -1
  30. data/app/views/foreman_dlm/dlmlocks/index.html.erb +2 -0
  31. data/app/views/foreman_dlm/dlmlocks/show.html.erb +10 -0
  32. data/app/views/{dlmlocks → foreman_dlm/dlmlocks}/welcome.html.erb +2 -2
  33. data/app/views/hosts/_dlmlocks_tab.html.erb +39 -0
  34. data/config/routes.rb +20 -13
  35. data/contrib/systemd/foreman-dlm-expire-events.service +10 -0
  36. data/contrib/systemd/foreman-dlm-expire-events.timer +8 -0
  37. data/db/migrate/20170824084100_add_dlmlock.foreman_dlm.rb +1 -1
  38. data/db/migrate/20180627150003_rename_dlmlock_sti_models.rb +9 -0
  39. data/db/migrate/20180704162345_add_dlmlock_events.rb +11 -0
  40. data/db/migrate/20180711090022_add_hosts_fk_to_dlmlocks.rb +5 -0
  41. data/db/migrate/20180711111903_create_dlm_facets.foreman_dlm.rb +10 -0
  42. data/db/migrate/20180713113208_update_permissions_for_scoped_models.rb +13 -0
  43. data/lib/foreman_dlm/engine.rb +58 -19
  44. data/lib/foreman_dlm/version.rb +1 -1
  45. data/lib/tasks/dlmlock_events.rake +19 -0
  46. data/lib/tasks/foreman_dlm_tasks.rake +2 -4
  47. data/test/controllers/api/v2/dlmlocks_controller_test.rb +73 -52
  48. data/test/controllers/api/v2/dlmlocks_dlmlock_events_controller_test.rb +81 -0
  49. data/test/controllers/api/v2/hosts_controller_test.rb +28 -0
  50. data/test/controllers/find_host_by_client_cert_test.rb +2 -2
  51. data/test/controllers/foreman_dlm/dlmlocks_test.rb +55 -0
  52. data/test/controllers/hosts_controller_test.rb +12 -0
  53. data/test/factories/dlm_facets.rb +6 -0
  54. data/test/factories/dlmlock.rb +6 -2
  55. data/test/factories/dlmlock_events.rb +13 -0
  56. data/test/factories/host.rb +7 -0
  57. data/test/integration/foreman_dlm/dlmlocks_test.rb +28 -0
  58. data/test/jobs/refresh_dlmlock_status_test.rb +10 -0
  59. data/test/models/foreman_dlm/dlm_facet_test.rb +13 -0
  60. data/test/models/foreman_dlm/dlmlock_event_test.rb +19 -0
  61. data/test/models/foreman_dlm/dlmlock_test.rb +307 -0
  62. data/test/models/host_managed_test.rb +41 -0
  63. data/test/models/host_monitoring_test.rb +1 -1
  64. data/test/models/host_status/dlmlock_status_test.rb +45 -0
  65. data/test/models/user_test.rb +5 -0
  66. data/test/test_plugin_helper.rb +5 -3
  67. metadata +108 -30
  68. data/app/controllers/dlmlocks_controller.rb +0 -13
  69. data/app/models/dlmlock/update.rb +0 -5
  70. data/app/models/dlmlock.rb +0 -79
  71. data/app/views/dlmlocks/_details.html.erb +0 -35
  72. data/app/views/dlmlocks/index.html.erb +0 -2
  73. data/app/views/dlmlocks/show.html.erb +0 -7
  74. data/test/controllers/dlmlocks_test.rb +0 -24
  75. data/test/models/dlmlock_test.rb +0 -201
@@ -21,7 +21,7 @@ namespace :foreman_dlm do
21
21
  "#{ForemanDlm::Engine.root}/lib/**/*.rb",
22
22
  "#{ForemanDlm::Engine.root}/test/**/*.rb"]
23
23
  end
24
- rescue
24
+ rescue StandardError
25
25
  puts 'Rubocop not loaded.'
26
26
  end
27
27
 
@@ -32,6 +32,4 @@ end
32
32
  Rake::Task[:test].enhance ['test:foreman_dlm']
33
33
 
34
34
  load 'tasks/jenkins.rake'
35
- if Rake::Task.task_defined?(:'jenkins:unit')
36
- Rake::Task['jenkins:unit'].enhance ['test:foreman_dlm', 'foreman_dlm:rubocop']
37
- end
35
+ Rake::Task['jenkins:unit'].enhance ['test:foreman_dlm', 'foreman_dlm:rubocop'] if Rake::Task.task_defined?(:'jenkins:unit')
@@ -13,7 +13,7 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
13
13
  body = ActiveSupport::JSON.decode(@response.body)
14
14
  results = body['results']
15
15
  assert results
16
- entry = results.detect { |entry| entry['id'] == dlmlock.id }
16
+ entry = results.detect { |e| e['id'] == dlmlock.id }
17
17
  assert entry
18
18
  assert_equal dlmlock.name, entry['name']
19
19
  assert_equal dlmlock.type, entry['type']
@@ -24,12 +24,12 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
24
24
 
25
25
  context '#create' do
26
26
  test 'should create dlmlock' do
27
- assert_difference('Dlmlock.unscoped.count') do
28
- post :create, valid_attrs_with_root
27
+ assert_difference('ForemanDlm::Dlmlock.unscoped.count') do
28
+ post :create, params: valid_attrs_with_root
29
29
  end
30
30
  assert_response :success
31
31
  body = ActiveSupport::JSON.decode(@response.body)
32
- dlmlock = Dlmlock.find(body['id'])
32
+ dlmlock = ForemanDlm::Dlmlock.find(body['id'])
33
33
  assert dlmlock
34
34
  assert_equal valid_attrs['name'], dlmlock.name
35
35
  assert_equal valid_attrs['type'], dlmlock.type
@@ -41,10 +41,10 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
41
41
  context '#show' do
42
42
  test 'should show individual record with host' do
43
43
  dlmlock = FactoryBot.create(:dlmlock, :host => host1)
44
- get :show, { :id => dlmlock.to_param }
44
+ get :show, params: { :id => dlmlock.to_param }
45
45
  assert_response :success
46
46
  body = ActiveSupport::JSON.decode(@response.body)
47
- refute_empty body
47
+ assert_not_empty body
48
48
  assert_equal dlmlock.name, body['name']
49
49
  assert_equal dlmlock.type, body['type']
50
50
  assert_equal true, body['enabled']
@@ -53,10 +53,10 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
53
53
 
54
54
  test 'should show individual record that is disabled' do
55
55
  dlmlock = FactoryBot.create(:dlmlock, :enabled => false)
56
- get :show, { :id => dlmlock.to_param }
56
+ get :show, params: { :id => dlmlock.to_param }
57
57
  assert_response :success
58
58
  body = ActiveSupport::JSON.decode(@response.body)
59
- refute_empty body
59
+ assert_not_empty body
60
60
  assert_equal dlmlock.name, body['name']
61
61
  assert_equal dlmlock.type, body['type']
62
62
  assert_equal false, body['enabled']
@@ -65,20 +65,20 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
65
65
 
66
66
  test 'should show individual record by name' do
67
67
  dlmlock = FactoryBot.create(:dlmlock, :host => host1)
68
- get :show, { :id => dlmlock.name }
68
+ get :show, params: { :id => dlmlock.name }
69
69
  assert_response :success
70
70
  body = ActiveSupport::JSON.decode(@response.body)
71
- refute_empty body
71
+ assert_not_empty body
72
72
  assert_equal dlmlock.id, body['id']
73
73
  assert_equal dlmlock.name, body['name']
74
74
  host = body['host']
75
75
  assert host
76
76
  assert_equal host1.name, host['name']
77
- refute host.has_key?('self')
77
+ assert_not host.key?('self')
78
78
  end
79
79
 
80
80
  test 'should not find dlmlock with invalid id' do
81
- get :show, { :id => 9999999 }
81
+ get :show, params: { :id => 9_999_999 }
82
82
  assert_response :not_found
83
83
  end
84
84
  end
@@ -86,7 +86,7 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
86
86
  context '#update' do
87
87
  test 'should update dlmlock' do
88
88
  dlmlock = FactoryBot.create(:dlmlock)
89
- put :update, { :id => dlmlock.to_param, :dlmlock => valid_attrs.merge(:host_id => host1.id, :enabled => false) }
89
+ put :update, params: { :id => dlmlock.to_param, :dlmlock => valid_attrs.merge(:host_id => host1.id, :enabled => false) }
90
90
  assert_response :success
91
91
  dlmlock.reload
92
92
  assert_equal valid_attrs['name'], dlmlock.name
@@ -99,18 +99,18 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
99
99
  context '#destroy' do
100
100
  test 'should destroy dlmlock' do
101
101
  dlmlock = FactoryBot.create(:dlmlock)
102
- assert_difference('Dlmlock.unscoped.count', -1) do
103
- delete :destroy, { :id => dlmlock.to_param }
102
+ assert_difference('ForemanDlm::Dlmlock.unscoped.count', -1) do
103
+ delete :destroy, params: { :id => dlmlock.to_param }
104
104
  end
105
105
  assert_response :success
106
- assert_equal 0, Dlmlock.where(:id => dlmlock.id).count
106
+ assert_equal 0, ForemanDlm::Dlmlock.where(:id => dlmlock.id).count
107
107
  end
108
108
  end
109
109
 
110
110
  context '#acquire' do
111
111
  test 'should deny access' do
112
112
  dlmlock = FactoryBot.create(:dlmlock)
113
- put :acquire, { :id => dlmlock.to_param }
113
+ put :acquire, params: { :id => dlmlock.to_param }
114
114
  assert_response :forbidden
115
115
  assert_nil dlmlock.reload.host
116
116
  end
@@ -119,7 +119,7 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
119
119
  context '#release' do
120
120
  test 'should deny access' do
121
121
  dlmlock = FactoryBot.create(:dlmlock, :host => host2)
122
- delete :release, { :id => dlmlock.to_param }
122
+ delete :release, params: { :id => dlmlock.to_param }
123
123
  assert_response :forbidden
124
124
  assert_equal host2, dlmlock.reload.host
125
125
  end
@@ -142,15 +142,15 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
142
142
  context '#index' do
143
143
  test 'should deny access' do
144
144
  dlmlock = as_admin { FactoryBot.create(:dlmlock) }
145
- get :index, { :id => dlmlock.to_param }
145
+ get :index, params: { :id => dlmlock.to_param }
146
146
  assert_response :unauthorized
147
147
  end
148
148
  end
149
149
 
150
150
  context '#create' do
151
151
  test 'should deny access' do
152
- dlmlock = as_admin { FactoryBot.create(:dlmlock) }
153
- post :create, valid_attrs_with_root
152
+ as_admin { FactoryBot.create(:dlmlock) }
153
+ post :create, params: valid_attrs_with_root
154
154
  assert_response :unauthorized
155
155
  end
156
156
  end
@@ -158,7 +158,7 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
158
158
  context '#update' do
159
159
  test 'should deny access' do
160
160
  dlmlock = as_admin { FactoryBot.create(:dlmlock) }
161
- put :update, { :id => dlmlock.to_param, :dlmlock => valid_attrs }
161
+ put :update, params: { :id => dlmlock.to_param, :dlmlock => valid_attrs }
162
162
  assert_response :unauthorized
163
163
  end
164
164
  end
@@ -166,19 +166,19 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
166
166
  context '#destroy' do
167
167
  test 'should deny access' do
168
168
  dlmlock = as_admin { FactoryBot.create(:dlmlock) }
169
- delete :destroy, { :id => dlmlock.to_param }
169
+ delete :destroy, params: { :id => dlmlock.to_param }
170
170
  assert_response :unauthorized
171
- assert_equal 1, as_admin { Dlmlock.where(:id => dlmlock.id).count }
171
+ assert_equal 1, as_admin { ForemanDlm::Dlmlock.where(:id => dlmlock.id).count }
172
172
  end
173
173
  end
174
174
 
175
175
  context '#show' do
176
176
  test 'should show individual free lock' do
177
177
  dlmlock = as_admin { FactoryBot.create(:dlmlock) }
178
- get :show, { :id => dlmlock.to_param }
178
+ get :show, params: { :id => dlmlock.to_param }
179
179
  assert_response :success
180
180
  body = ActiveSupport::JSON.decode(@response.body)
181
- refute_empty body
181
+ assert_not_empty body
182
182
  assert_equal dlmlock.name, body['name']
183
183
  assert_equal dlmlock.type, body['type']
184
184
  assert_equal true, body['enabled']
@@ -188,10 +188,10 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
188
188
 
189
189
  test 'should show individual acquired lock by me' do
190
190
  dlmlock = as_admin { FactoryBot.create(:dlmlock, :host => host1) }
191
- get :show, { :id => dlmlock.to_param }
191
+ get :show, params: { :id => dlmlock.to_param }
192
192
  assert_response :success
193
193
  body = ActiveSupport::JSON.decode(@response.body)
194
- refute_empty body
194
+ assert_not_empty body
195
195
  assert_equal dlmlock.name, body['name']
196
196
  assert_equal dlmlock.type, body['type']
197
197
  assert_equal true, body['enabled']
@@ -204,10 +204,10 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
204
204
 
205
205
  test 'should show individual acquired lock by other' do
206
206
  dlmlock = as_admin { FactoryBot.create(:dlmlock, :host => host2) }
207
- get :show, { :id => dlmlock.to_param }
207
+ get :show, params: { :id => dlmlock.to_param }
208
208
  assert_response :success
209
209
  body = ActiveSupport::JSON.decode(@response.body)
210
- refute_empty body
210
+ assert_not_empty body
211
211
  assert_equal dlmlock.name, body['name']
212
212
  assert_equal dlmlock.type, body['type']
213
213
  assert_equal true, body['enabled']
@@ -217,72 +217,93 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
217
217
  assert_equal host2.name, host['name']
218
218
  assert_equal false, host['self']
219
219
  end
220
+
221
+ test 'should update checkin time' do
222
+ dlmlock = as_admin { FactoryBot.create(:dlmlock) }
223
+ assert_not as_admin { host1.dlm_facet }
224
+ put :show, params: { :id => dlmlock.to_param }
225
+ assert as_admin { host1.reload.dlm_facet.last_checkin_at }
226
+ end
220
227
  end
221
228
 
222
229
  context '#acquire' do
223
230
  test 'should acquire empty dlmlock' do
224
231
  dlmlock = as_admin { FactoryBot.create(:dlmlock) }
225
- put :acquire, { :id => dlmlock.to_param }
232
+ put :acquire, params: { :id => dlmlock.to_param }
226
233
  assert_response :success
227
234
  assert_equal host1, as_admin { dlmlock.reload.host }
228
235
  end
229
236
 
230
237
  test 'should acquire own dlmlock' do
231
238
  dlmlock = as_admin { FactoryBot.create(:dlmlock, :host => host1) }
232
- put :acquire, { :id => dlmlock.to_param }
239
+ put :acquire, params: { :id => dlmlock.to_param }
233
240
  assert_response :success
234
241
  assert_equal host1, as_admin { dlmlock.reload.host }
235
242
  end
236
243
 
237
244
  test 'should not acquire foreign dlmlock' do
238
245
  dlmlock = as_admin { FactoryBot.create(:dlmlock, :host => host2) }
239
- put :acquire, { :id => dlmlock.to_param }
246
+ put :acquire, params: { :id => dlmlock.to_param }
240
247
  assert_response :precondition_failed
241
248
  assert_equal host2, as_admin { dlmlock.reload.host }
242
249
  end
243
250
 
244
251
  test 'should transparently create non-existing dlmlock' do
245
252
  lockname = 'Test Lock'
246
- assert_equal 0, as_admin { Dlmlock.where(:name => lockname).count }
247
- put :acquire, { :id => lockname }
253
+ assert_equal 0, as_admin { ForemanDlm::Dlmlock.where(:name => lockname).count }
254
+ put :acquire, params: { :id => lockname }
248
255
  assert_response :success
249
- dlmlock = as_admin { Dlmlock.find_by(:name => lockname) }
256
+ dlmlock = as_admin { ForemanDlm::Dlmlock.find_by(:name => lockname) }
250
257
  assert_equal lockname, dlmlock.name
251
258
  assert_equal host1, dlmlock.host
252
259
  end
260
+
261
+ test 'should update checkin time' do
262
+ dlmlock = as_admin { FactoryBot.create(:dlmlock) }
263
+ assert_not as_admin { host1.dlm_facet }
264
+ put :acquire, params: { :id => dlmlock.to_param }
265
+ assert as_admin { host1.reload.dlm_facet.last_checkin_at }
266
+ end
253
267
  end
254
268
 
255
269
  context '#release' do
256
270
  test 'should release empty dlmlock' do
257
271
  dlmlock = as_admin { FactoryBot.create(:dlmlock) }
258
- delete :release, { :id => dlmlock.to_param }
272
+ delete :release, params: { :id => dlmlock.to_param }
259
273
  assert_response :success
260
274
  assert_nil as_admin { dlmlock.reload.host }
261
275
  end
262
276
 
263
277
  test 'should release own dlmlock' do
264
278
  dlmlock = as_admin { FactoryBot.create(:dlmlock, :host => host1) }
265
- delete :release, { :id => dlmlock.to_param }
279
+ delete :release, params: { :id => dlmlock.to_param }
266
280
  assert_response :success
267
281
  assert_nil as_admin { dlmlock.reload.host }
268
282
  end
269
283
 
270
284
  test 'should not acquire foreign dlmlock' do
271
285
  dlmlock = as_admin { FactoryBot.create(:dlmlock, :host => host2) }
272
- delete :release, { :id => dlmlock.to_param }
286
+ delete :release, params: { :id => dlmlock.to_param }
273
287
  assert_response :precondition_failed
274
288
  assert_equal host2, as_admin { dlmlock.reload.host }
275
289
  end
276
290
 
277
291
  test 'should transparently create non-existing dlmlock' do
278
292
  lockname = 'Test Lock'
279
- assert_equal 0, as_admin { Dlmlock.where(:name => lockname).count }
280
- delete :release, { :id => lockname }
293
+ assert_equal 0, as_admin { ForemanDlm::Dlmlock.where(:name => lockname).count }
294
+ delete :release, params: { :id => lockname }
281
295
  assert_response :success
282
- dlmlock = as_admin { Dlmlock.find_by(:name => lockname) }
296
+ dlmlock = as_admin { ForemanDlm::Dlmlock.find_by(:name => lockname) }
283
297
  assert_equal lockname, dlmlock.name
284
298
  assert_nil dlmlock.host
285
299
  end
300
+
301
+ test 'should update checkin time' do
302
+ dlmlock = as_admin { FactoryBot.create(:dlmlock) }
303
+ assert_not as_admin { host1.dlm_facet }
304
+ put :release, params: { :id => dlmlock.to_param }
305
+ assert as_admin { host1.reload.dlm_facet.last_checkin_at }
306
+ end
286
307
  end
287
308
  end
288
309
 
@@ -295,7 +316,7 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
295
316
  context '#index' do
296
317
  test 'should deny access' do
297
318
  dlmlock = as_admin { FactoryBot.create(:dlmlock) }
298
- get :index, { :id => dlmlock.to_param }
319
+ get :index, params: { :id => dlmlock.to_param }
299
320
  assert_response :unauthorized
300
321
  end
301
322
  end
@@ -303,15 +324,15 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
303
324
  context '#show' do
304
325
  test 'should deny access' do
305
326
  dlmlock = as_admin { FactoryBot.create(:dlmlock) }
306
- get :show, { :id => dlmlock.to_param }
327
+ get :show, params: { :id => dlmlock.to_param }
307
328
  assert_response :unauthorized
308
329
  end
309
330
  end
310
331
 
311
332
  context '#create' do
312
333
  test 'should deny access' do
313
- dlmlock = as_admin { FactoryBot.create(:dlmlock) }
314
- post :create, valid_attrs_with_root
334
+ as_admin { FactoryBot.create(:dlmlock) }
335
+ post :create, params: valid_attrs_with_root
315
336
  assert_response :unauthorized
316
337
  end
317
338
  end
@@ -319,7 +340,7 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
319
340
  context '#update' do
320
341
  test 'should deny access' do
321
342
  dlmlock = as_admin { FactoryBot.create(:dlmlock) }
322
- put :update, { :id => dlmlock.to_param, :dlmlock => valid_attrs }
343
+ put :update, params: { :id => dlmlock.to_param, :dlmlock => valid_attrs }
323
344
  assert_response :unauthorized
324
345
  end
325
346
  end
@@ -327,16 +348,16 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
327
348
  context '#destroy' do
328
349
  test 'should deny access' do
329
350
  dlmlock = as_admin { FactoryBot.create(:dlmlock) }
330
- delete :destroy, { :id => dlmlock.to_param }
351
+ delete :destroy, params: { :id => dlmlock.to_param }
331
352
  assert_response :unauthorized
332
- assert_equal 1, as_admin { Dlmlock.where(:id => dlmlock.id).count }
353
+ assert_equal 1, as_admin { ForemanDlm::Dlmlock.where(:id => dlmlock.id).count }
333
354
  end
334
355
  end
335
356
 
336
357
  context '#acquire' do
337
358
  test 'should deny access' do
338
359
  dlmlock = as_admin { FactoryBot.create(:dlmlock) }
339
- put :acquire, { :id => dlmlock.to_param }
360
+ put :acquire, params: { :id => dlmlock.to_param }
340
361
  assert_response :unauthorized
341
362
  assert_nil as_admin { dlmlock.reload.host }
342
363
  end
@@ -345,7 +366,7 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
345
366
  context '#release' do
346
367
  test 'should deny access' do
347
368
  dlmlock = as_admin { FactoryBot.create(:dlmlock, :host => host2) }
348
- delete :release, { :id => dlmlock.to_param }
369
+ delete :release, params: { :id => dlmlock.to_param }
349
370
  assert_response :unauthorized
350
371
  assert_equal host2, as_admin { dlmlock.reload.host }
351
372
  end
@@ -357,7 +378,7 @@ class Api::V2::DlmlocksControllerTest < ActionController::TestCase
357
378
  def valid_attrs
358
379
  {
359
380
  'name' => 'testlock',
360
- 'type' => 'Dlmlock::Update'
381
+ 'type' => 'ForemanDlm::Dlmlock::Update'
361
382
  }
362
383
  end
363
384
 
@@ -0,0 +1,81 @@
1
+ require 'test_plugin_helper'
2
+
3
+ class Api::V2::DlmlockEventsControllerTest < ActionController::TestCase
4
+ let(:host) { as_admin { FactoryBot.create(:host, :managed) } }
5
+
6
+ context 'with user authentication' do
7
+ context '#index' do
8
+ test 'should return the dlmlock_events for a given dlm_lock' do
9
+ # A random dlmlock with events, that should not be in the response.
10
+ dlmlock1 = FactoryBot.create(:dlmlock, host: host)
11
+ FactoryBot.create_list(:dlmlock_event, 2, dlmlock: dlmlock1)
12
+
13
+ dlmlock = FactoryBot.create(:dlmlock, host: host)
14
+ FactoryBot.create_list(:dlmlock_event, 3, dlmlock: dlmlock)
15
+
16
+ expected_size = dlmlock.dlmlock_events.count
17
+ expected_ids = dlmlock.dlmlock_events.pluck(:id).sort
18
+ expected_keys = ['id', 'event_type', 'created_at', 'updated_at']
19
+
20
+ get :index, params: { dlmlock_id: dlmlock.id }
21
+ assert_response :success
22
+
23
+ body = ActiveSupport::JSON.decode(@response.body)
24
+ assert_equal expected_size, body['total']
25
+ assert_equal expected_keys, body['results'].first.keys
26
+ assert_equal expected_ids, body['results'].map { |event| event['id'] }.sort
27
+ end
28
+ end
29
+ end
30
+
31
+ context 'as user with required permissions' do
32
+ let(:permissions) { Permission.where(name: ['view_dlmlocks', 'view_dlmlock_events']) }
33
+ let(:role) { FactoryBot.create(:role, permissions: permissions) }
34
+ let(:user) { FactoryBot.create(:user, roles: [role]) }
35
+
36
+ setup do
37
+ User.current = user
38
+ reset_api_credentials
39
+ end
40
+
41
+ context '#index' do
42
+ test 'should allow access' do
43
+ dlmlock = as_admin { FactoryBot.create(:dlmlock) }
44
+ get :index, params: { dlmlock_id: dlmlock.id }
45
+ assert_response :success
46
+ end
47
+ end
48
+ end
49
+
50
+ context 'as user without required permissions' do
51
+ let(:user) { FactoryBot.create(:user, roles: []) }
52
+
53
+ setup do
54
+ User.current = user
55
+ reset_api_credentials
56
+ end
57
+
58
+ context '#index' do
59
+ test 'should deny access' do
60
+ dlmlock = as_admin { FactoryBot.create(:dlmlock) }
61
+ get :index, params: { dlmlock_id: dlmlock.id }
62
+ assert_response :forbidden
63
+ end
64
+ end
65
+ end
66
+
67
+ context 'without any credentials' do
68
+ setup do
69
+ User.current = nil
70
+ reset_api_credentials
71
+ end
72
+
73
+ context '#index' do
74
+ test 'should deny access' do
75
+ dlmlock = as_admin { FactoryBot.create(:dlmlock) }
76
+ get :index, params: { dlmlock_id: dlmlock.id }
77
+ assert_response :unauthorized
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_plugin_helper'
2
+
3
+ class Api::V2::HostsControllerTest < ActionController::TestCase
4
+ let(:host) { FactoryBot.create(:host, :managed, :with_dlm_facet) }
5
+
6
+ test 'should get index with dlm_facet attributes' do
7
+ host
8
+ get :index
9
+ assert_response :success
10
+ results = ActiveSupport::JSON.decode(@response.body)
11
+ hosts = results['results']
12
+ assert_not_empty hosts
13
+ host_with_facet = hosts.detect { |h| h['dlm_facet_attributes'].present? }
14
+ assert_not_nil host_with_facet
15
+ assert_equal host.dlm_facet.id, host_with_facet['dlm_facet_attributes']['id']
16
+ assert_equal host.dlm_facet.last_checkin_at.to_s, host_with_facet['dlm_facet_attributes']['last_checkin_at']
17
+ end
18
+
19
+ test 'should show individual record with dlm_facet attributes' do
20
+ get :show, params: { :id => host.to_param }
21
+ assert_response :success
22
+ show_response = ActiveSupport::JSON.decode(@response.body)
23
+ assert_not_empty show_response
24
+ assert_includes show_response.keys, 'dlm_facet_attributes'
25
+ assert_equal host.dlm_facet.id, show_response['dlm_facet_attributes']['id']
26
+ assert_equal host.dlm_facet.last_checkin_at.to_s, show_response['dlm_facet_attributes']['last_checkin_at']
27
+ end
28
+ end
@@ -1,7 +1,7 @@
1
1
  require 'test_plugin_helper'
2
2
 
3
3
  class FindHostByClientCertTest < ActionController::TestCase
4
- tests "api/v2/dlmlocks"
4
+ tests 'api/v2/dlmlocks'
5
5
 
6
6
  def described_class
7
7
  Api::V2::DlmlocksController
@@ -57,7 +57,7 @@ class FindHostByClientCertTest < ActionController::TestCase
57
57
 
58
58
  test 'certificate with unknown dn denies access' do
59
59
  @request.env['HTTPS'] = 'on'
60
- @request.env['SSL_CLIENT_S_DN'] = "CN=doesnotexist.example.com,DN=example,DN=com"
60
+ @request.env['SSL_CLIENT_S_DN'] = 'CN=doesnotexist.example.com,DN=example,DN=com'
61
61
  @request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
62
62
 
63
63
  get :index
@@ -0,0 +1,55 @@
1
+ require 'test_plugin_helper'
2
+
3
+ module ForemanDlm
4
+ class DlmlocksControllerTest < ActionController::TestCase
5
+ test '#index' do
6
+ FactoryBot.create(:dlmlock)
7
+ get :index, session: set_session_user
8
+ assert_response :success
9
+ assert_not_nil assigns('dlmlocks')
10
+ assert_template 'index'
11
+ end
12
+
13
+ test '#index with no lock shows welcome page' do
14
+ get :index, session: set_session_user
15
+ assert_response :success
16
+ assert_template 'welcome'
17
+ end
18
+
19
+ test '#show' do
20
+ dlmlock = FactoryBot.create(:dlmlock)
21
+ get :show, params: { :id => dlmlock.id }, session: set_session_user
22
+ assert_response :success
23
+ assert_template 'show'
24
+ end
25
+
26
+ test '#destroy' do
27
+ dlmlock = FactoryBot.create(:dlmlock)
28
+ delete :destroy, params: { :id => dlmlock.id }, session: set_session_user
29
+ assert_redirected_to foreman_dlm_dlmlocks_url
30
+ assert_not Dlmlock.exists?(dlmlock.id)
31
+ end
32
+
33
+ test '#enable' do
34
+ dlmlock = FactoryBot.create(:dlmlock, enabled: false)
35
+ put :enable, params: { :id => dlmlock.id }, session: set_session_user
36
+ assert_redirected_to foreman_dlm_dlmlocks_url
37
+ assert dlmlock.reload.enabled?
38
+ end
39
+
40
+ test '#disable' do
41
+ dlmlock = FactoryBot.create(:dlmlock, enabled: true)
42
+ put :disable, params: { :id => dlmlock.id }, session: set_session_user
43
+ assert_redirected_to foreman_dlm_dlmlocks_url
44
+ assert dlmlock.reload.disabled?
45
+ end
46
+
47
+ test '#release' do
48
+ host = FactoryBot.create(:host)
49
+ dlmlock = FactoryBot.create(:dlmlock, host: host)
50
+ put :release, params: { :id => dlmlock.id }, session: set_session_user
51
+ assert_redirected_to foreman_dlm_dlmlocks_url
52
+ assert_not dlmlock.reload.taken?
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,12 @@
1
+ require 'test_plugin_helper'
2
+
3
+ class HostsControllerTest < ActionController::TestCase
4
+ let(:host) { FactoryBot.create(:host, :with_dlm_facet) }
5
+
6
+ test '#show shows dlm locks of that host' do
7
+ FactoryBot.create_list(:dlmlock, 2, host: host)
8
+ get :show, params: { :id => host.to_param }, session: set_session_user
9
+ assert_response :success
10
+ assert @response.body.match(/id='pagelet-id-locks'/)
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ FactoryBot.define do
2
+ factory :dlm_facet, class: 'ForemanDlm::DlmFacet' do
3
+ sequence(:last_checkin_at) { |n| n.minutes.ago }
4
+ host
5
+ end
6
+ end
@@ -1,6 +1,10 @@
1
1
  FactoryBot.define do
2
- factory :dlmlock do
2
+ factory :dlmlock, class: ::ForemanDlm::Dlmlock::Update do
3
3
  sequence(:name) { |n| "Lock #{n}" }
4
- type 'Dlmlock::Update'
4
+ type { 'ForemanDlm::Dlmlock::Update' }
5
+
6
+ trait :locked do
7
+ host
8
+ end
5
9
  end
6
10
  end
@@ -0,0 +1,13 @@
1
+ FactoryBot.define do
2
+ factory :dlmlock_event, class: ::ForemanDlm::DlmlockEvent do
3
+ dlmlock
4
+ event_type { 'release' }
5
+ host
6
+
7
+ trait :old_event do
8
+ after(:build) do |event|
9
+ event.created_at = 2.weeks.ago
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ FactoryBot.modify do
2
+ factory :host do
3
+ trait :with_dlm_facet do
4
+ association :dlm_facet, :factory => :dlm_facet, :strategy => :build
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+ require 'integration_test_helper'
5
+
6
+ class DlmlocksTest < IntegrationTestWithJavascript
7
+ setup do
8
+ User.current = users(:admin)
9
+ end
10
+
11
+ test 'the index page works' do
12
+ FactoryBot.create_list(:dlmlock, 10)
13
+ assert_index_page(foreman_dlm_dlmlocks_path, 'Distributed Locks')
14
+ end
15
+
16
+ test 'the search bar has autocomplete' do
17
+ skip if Gem::Version.new(Foreman::Version.new.notag) < Gem::Version.new('1.20')
18
+ FactoryBot.create_list(:dlmlock, 10)
19
+
20
+ visit foreman_dlm_dlmlocks_path
21
+
22
+ search_bar = page.first('#search-bar')
23
+ search_bar.first('input').set('ho')
24
+
25
+ list = search_bar.find_all('a.dropdown-item').map(&:text)
26
+ assert_includes list, 'host'
27
+ end
28
+ end
@@ -0,0 +1,10 @@
1
+ require 'test_plugin_helper'
2
+
3
+ class RefreshDlmlockStatusTest < ActiveJob::TestCase
4
+ let(:host) { FactoryBot.create(:host, :managed) }
5
+
6
+ test 'should refresh dlmlock status' do
7
+ Host::Managed.any_instance.expects(:refresh_dlmlock_status).once
8
+ perform_enqueued_jobs { ForemanDlm::RefreshDlmlockStatus.perform_later(host.id) }
9
+ end
10
+ end