rocketjob_mission_control 3.0.0.rc1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +10 -6
  3. data/app/assets/javascripts/rocket_job_mission_control/application.js +4 -3
  4. data/app/assets/javascripts/rocket_job_mission_control/base.js +12 -0
  5. data/app/assets/javascripts/rocket_job_mission_control/datatable.js +40 -0
  6. data/app/assets/javascripts/rocket_job_mission_control/dirmon_entries.js +16 -0
  7. data/app/assets/javascripts/rocket_job_mission_control/selectize.js +6 -0
  8. data/app/controllers/rocket_job_mission_control/active_workers_controller.rb +16 -5
  9. data/app/controllers/rocket_job_mission_control/application_controller.rb +2 -0
  10. data/app/controllers/rocket_job_mission_control/dirmon_entries_controller.rb +47 -67
  11. data/app/controllers/rocket_job_mission_control/jobs_controller.rb +113 -19
  12. data/app/controllers/rocket_job_mission_control/servers_controller.rb +71 -28
  13. data/app/datatables/rocket_job_mission_control/abstract_datatable.rb +3 -3
  14. data/app/datatables/rocket_job_mission_control/active_workers_datatable.rb +8 -14
  15. data/app/datatables/rocket_job_mission_control/dirmon_entries_datatable.rb +7 -15
  16. data/app/datatables/rocket_job_mission_control/jobs_datatable.rb +128 -13
  17. data/app/datatables/rocket_job_mission_control/servers_datatable.rb +13 -15
  18. data/app/helpers/rocket_job_mission_control/application_helper.rb +58 -0
  19. data/app/helpers/rocket_job_mission_control/jobs_helper.rb +15 -7
  20. data/app/helpers/rocket_job_mission_control/servers_helper.rb +1 -1
  21. data/app/models/rocket_job_mission_control/job_sanitizer.rb +47 -0
  22. data/app/models/rocket_job_mission_control/query.rb +24 -18
  23. data/app/views/layouts/rocket_job_mission_control/application.html.erb +43 -0
  24. data/app/views/layouts/rocket_job_mission_control/partials/_flash.html.erb +20 -0
  25. data/app/views/layouts/rocket_job_mission_control/partials/_header.html.erb +45 -0
  26. data/app/views/layouts/rocket_job_mission_control/partials/_sidebar.html.erb +12 -0
  27. data/app/views/rocket_job_mission_control/active_workers/index.html.erb +40 -0
  28. data/app/views/rocket_job_mission_control/dirmon_entries/_form.html.erb +60 -0
  29. data/app/views/rocket_job_mission_control/dirmon_entries/_sidebar.html.erb +38 -0
  30. data/app/views/rocket_job_mission_control/dirmon_entries/_status.html.erb +17 -0
  31. data/app/views/rocket_job_mission_control/dirmon_entries/edit.html.erb +4 -0
  32. data/app/views/rocket_job_mission_control/dirmon_entries/index.html.erb +33 -0
  33. data/app/views/rocket_job_mission_control/dirmon_entries/new.html.erb +8 -0
  34. data/app/views/rocket_job_mission_control/dirmon_entries/show.html.erb +29 -0
  35. data/app/views/rocket_job_mission_control/jobs/_pagination.html.erb +26 -0
  36. data/app/views/rocket_job_mission_control/jobs/_sidebar.html.erb +32 -0
  37. data/app/views/rocket_job_mission_control/jobs/_status.html.erb +42 -0
  38. data/app/views/rocket_job_mission_control/jobs/edit.html.erb +32 -0
  39. data/app/views/rocket_job_mission_control/jobs/exception.html.erb +19 -0
  40. data/app/views/rocket_job_mission_control/jobs/exceptions.html.erb +40 -0
  41. data/app/views/rocket_job_mission_control/jobs/index.html.erb +33 -0
  42. data/app/views/rocket_job_mission_control/jobs/show.html.erb +68 -0
  43. data/app/views/rocket_job_mission_control/servers/_sidebar.html.erb +31 -0
  44. data/app/views/rocket_job_mission_control/servers/index.html.erb +56 -0
  45. data/config/locales/en.yml +3 -10
  46. data/config/routes.rb +20 -17
  47. data/lib/rocket_job_mission_control/engine.rb +10 -0
  48. data/lib/rocket_job_mission_control/version.rb +1 -1
  49. data/test/compare_hashes.rb +17 -0
  50. data/test/controllers/rocket_job_mission_control/application_controller_test.rb +49 -0
  51. data/test/controllers/rocket_job_mission_control/dirmon_entries_controller_test.rb +417 -0
  52. data/test/controllers/rocket_job_mission_control/jobs_controller_test.rb +363 -0
  53. data/test/controllers/rocket_job_mission_control/servers_controller_test.rb +251 -0
  54. data/test/helpers/rocket_job_mission_control/application_helper_test.rb +54 -0
  55. data/test/helpers/rocket_job_mission_control/jobs_helper_test.rb +85 -0
  56. data/test/helpers/rocket_job_mission_control/pagination_helper_test.rb +23 -0
  57. data/test/helpers/rocket_job_mission_control/servers_helper_test.rb +64 -0
  58. data/test/helpers/rocket_job_mission_control/slices_helper_test.rb +30 -0
  59. data/test/models/rocket_job_mission_control/job_sanitizer_test.rb +92 -0
  60. data/test/models/rocket_job_mission_control/query_test.rb +136 -0
  61. data/test/test_helper.rb +23 -0
  62. metadata +75 -175
  63. data/app/assets/javascripts/rocket_job_mission_control/base.js.coffee +0 -11
  64. data/app/assets/javascripts/rocket_job_mission_control/datatable.js.coffee +0 -33
  65. data/app/assets/javascripts/rocket_job_mission_control/dirmon_entries.js.coffee +0 -13
  66. data/app/assets/javascripts/rocket_job_mission_control/selectize.js.coffee +0 -5
  67. data/app/assets/stylesheets/rocket_job_mission_control/jquery.bootstrap-touchspin.scss +0 -45
  68. data/app/controllers/rocket_job_mission_control/dirmon_entries/index_filters_controller.rb +0 -45
  69. data/app/controllers/rocket_job_mission_control/jobs/failures_controller.rb +0 -30
  70. data/app/controllers/rocket_job_mission_control/jobs/index_filters_controller.rb +0 -90
  71. data/app/controllers/rocket_job_mission_control/servers/index_filters_controller.rb +0 -49
  72. data/app/datatables/rocket_job_mission_control/completed_jobs_datatable.rb +0 -27
  73. data/app/datatables/rocket_job_mission_control/interrupted_jobs_datatable.rb +0 -26
  74. data/app/datatables/rocket_job_mission_control/queued_jobs_datatable.rb +0 -23
  75. data/app/datatables/rocket_job_mission_control/running_jobs_datatable.rb +0 -47
  76. data/app/datatables/rocket_job_mission_control/scheduled_jobs_datatable.rb +0 -31
  77. data/app/models/job_failures.rb +0 -20
  78. data/app/models/job_sanitizer.rb +0 -17
  79. data/app/views/layouts/rocket_job_mission_control/application.html.haml +0 -45
  80. data/app/views/layouts/rocket_job_mission_control/partials/_header.html.haml +0 -32
  81. data/app/views/layouts/rocket_job_mission_control/partials/_sidebar.html.haml +0 -11
  82. data/app/views/rocket_job_mission_control/active_workers/index.html.haml +0 -24
  83. data/app/views/rocket_job_mission_control/dirmon_entries/_form.html.haml +0 -37
  84. data/app/views/rocket_job_mission_control/dirmon_entries/_properties.html.haml +0 -30
  85. data/app/views/rocket_job_mission_control/dirmon_entries/_sidebar.html.haml +0 -27
  86. data/app/views/rocket_job_mission_control/dirmon_entries/_status.html.haml +0 -23
  87. data/app/views/rocket_job_mission_control/dirmon_entries/edit.html.haml +0 -4
  88. data/app/views/rocket_job_mission_control/dirmon_entries/index.html.haml +0 -18
  89. data/app/views/rocket_job_mission_control/dirmon_entries/index_filters/disabled.html.haml +0 -17
  90. data/app/views/rocket_job_mission_control/dirmon_entries/index_filters/enabled.html.haml +0 -17
  91. data/app/views/rocket_job_mission_control/dirmon_entries/index_filters/failed.html.haml +0 -17
  92. data/app/views/rocket_job_mission_control/dirmon_entries/index_filters/pending.html.haml +0 -17
  93. data/app/views/rocket_job_mission_control/dirmon_entries/new.html.haml +0 -6
  94. data/app/views/rocket_job_mission_control/dirmon_entries/show.html.haml +0 -23
  95. data/app/views/rocket_job_mission_control/jobs/_sidebar.html.haml +0 -28
  96. data/app/views/rocket_job_mission_control/jobs/_status.html.haml +0 -45
  97. data/app/views/rocket_job_mission_control/jobs/edit.html.haml +0 -34
  98. data/app/views/rocket_job_mission_control/jobs/exceptions.html.haml +0 -29
  99. data/app/views/rocket_job_mission_control/jobs/failures/_pagination.html.haml +0 -16
  100. data/app/views/rocket_job_mission_control/jobs/failures/index.html.haml +0 -32
  101. data/app/views/rocket_job_mission_control/jobs/index.html.haml +0 -25
  102. data/app/views/rocket_job_mission_control/jobs/index_filters/aborted.html.haml +0 -23
  103. data/app/views/rocket_job_mission_control/jobs/index_filters/completed.html.haml +0 -24
  104. data/app/views/rocket_job_mission_control/jobs/index_filters/failed.html.haml +0 -23
  105. data/app/views/rocket_job_mission_control/jobs/index_filters/paused.html.haml +0 -23
  106. data/app/views/rocket_job_mission_control/jobs/index_filters/queued.html.haml +0 -24
  107. data/app/views/rocket_job_mission_control/jobs/index_filters/running.html.haml +0 -25
  108. data/app/views/rocket_job_mission_control/jobs/index_filters/scheduled.html.haml +0 -24
  109. data/app/views/rocket_job_mission_control/jobs/show.html.haml +0 -51
  110. data/app/views/rocket_job_mission_control/servers/_actions.html.haml +0 -9
  111. data/app/views/rocket_job_mission_control/servers/_sidebar.html.haml +0 -20
  112. data/app/views/rocket_job_mission_control/servers/index.html.haml +0 -37
  113. data/app/views/rocket_job_mission_control/servers/index_filters/paused.html.haml +0 -37
  114. data/app/views/rocket_job_mission_control/servers/index_filters/running.html.haml +0 -37
  115. data/app/views/rocket_job_mission_control/servers/index_filters/starting.html.haml +0 -37
  116. data/app/views/rocket_job_mission_control/servers/index_filters/stopping.html.haml +0 -37
  117. data/spec/controllers/application_controller_spec.rb +0 -47
  118. data/spec/controllers/dirmon_entries/index_filters_controller_spec.rb +0 -77
  119. data/spec/controllers/dirmon_entries_controller_spec.rb +0 -383
  120. data/spec/controllers/jobs/failures_controller_spec.rb +0 -82
  121. data/spec/controllers/jobs/index_filters_controller_spec.rb +0 -66
  122. data/spec/controllers/jobs_controller_spec.rb +0 -172
  123. data/spec/controllers/workers/index_filters_controller_spec.rb +0 -59
  124. data/spec/controllers/workers_controller_spec.rb +0 -125
  125. data/spec/dummy/Rakefile +0 -7
  126. data/spec/dummy/config.ru +0 -4
  127. data/spec/dummy/config/application.rb +0 -26
  128. data/spec/dummy/config/boot.rb +0 -5
  129. data/spec/dummy/config/database.yml +0 -25
  130. data/spec/dummy/config/environment.rb +0 -5
  131. data/spec/dummy/config/environments/test.rb +0 -37
  132. data/spec/dummy/config/initializers/secret_token.rb +0 -7
  133. data/spec/dummy/config/initializers/session_store.rb +0 -3
  134. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  135. data/spec/dummy/config/mongoid.yml +0 -88
  136. data/spec/dummy/config/routes.rb +0 -3
  137. data/spec/dummy/config/secrets.yml +0 -22
  138. data/spec/dummy/config/symmetric-encryption.yml +0 -8
  139. data/spec/dummy/db/test.sqlite3 +0 -0
  140. data/spec/dummy/log/development.log +0 -0
  141. data/spec/dummy/log/test.log +0 -118933
  142. data/spec/helpers/application_helper_spec.rb +0 -58
  143. data/spec/helpers/jobs_helper_spec.rb +0 -35
  144. data/spec/helpers/pagination_helper_spec.rb +0 -21
  145. data/spec/helpers/servers_helper_spec.rb +0 -16
  146. data/spec/helpers/slices_helper_spec.rb +0 -33
  147. data/spec/models/job_failures_spec.rb +0 -14
  148. data/spec/models/job_sanitizer_spec.rb +0 -16
  149. data/spec/rails_helper.rb +0 -47
  150. data/spec/spec_helper.rb +0 -84
  151. data/vendor/assets/javascripts/jquery.bootstrap-touchspin.js +0 -686
  152. data/vendor/assets/javascripts/prism.js +0 -6
  153. data/vendor/assets/stylesheets/jquery.bootstrap-touchspin.css +0 -45
  154. data/vendor/assets/stylesheets/prism.scss +0 -160
@@ -0,0 +1,363 @@
1
+ require_relative '../../test_helper'
2
+ require_relative '../../compare_hashes'
3
+
4
+ module RocketJobMissionControl
5
+ class JobsControllerTest < ActionController::TestCase
6
+ describe JobsController do
7
+ before do
8
+ RocketJob::Job.delete_all
9
+ end
10
+
11
+ let :job do
12
+ RocketJob::Jobs::SimpleJob.create!
13
+ end
14
+
15
+ let :failed_job do
16
+ job = KaboomBatchJob.new(slice_size: 1)
17
+ job.upload do |stream|
18
+ stream << 'first record'
19
+ stream << 'second record'
20
+ stream << 'third record'
21
+ end
22
+ job.save!
23
+
24
+ # Run all 3 slices now to get exceptions for each.
25
+ 3.times do
26
+ begin
27
+ job.perform_now
28
+ rescue ArgumentError, RuntimeError
29
+ end
30
+ end
31
+
32
+ job
33
+ end
34
+
35
+ job_states = RocketJob::Job.aasm.states.collect(&:name)
36
+
37
+ let :one_job_for_every_state do
38
+ job_states.collect do |state|
39
+ RocketJob::Jobs::SimpleJob.create!(state: state)
40
+ end
41
+ end
42
+
43
+ [:pause, :resume, :abort, :retry, :fail].each do |state|
44
+ describe "PATCH ##{state}" do
45
+ describe 'with an invalid job id' do
46
+ before do
47
+ patch state, id: 42, job: {id: 42, priority: 12}
48
+ end
49
+
50
+ it 'redirects' do
51
+ assert_redirected_to jobs_path
52
+ end
53
+
54
+ it 'adds a flash alert message' do
55
+ assert_equal I18n.t(:failure, scope: [:job, :find], id: 42), flash[:alert]
56
+ end
57
+ end
58
+
59
+ describe 'with a valid job id' do
60
+ before do
61
+ case state
62
+ when :pause, :fail, :abort
63
+ job.start!
64
+ when :resume
65
+ job.pause!
66
+ when :retry
67
+ job.fail!
68
+ end
69
+ patch state, id: job.id, job: {id: job.id, priority: job.priority}
70
+ end
71
+
72
+ it 'redirects to the job' do
73
+ assert_redirected_to job_path(job.id)
74
+ end
75
+
76
+ it 'transitions the job' do
77
+ refute_equal state, job.state
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ describe 'PATCH #run_now' do
84
+ let(:scheduled_job) { RocketJob::Jobs::SimpleJob.create!(run_at: 2.days.from_now) }
85
+
86
+ before do
87
+ patch :run_now, id: scheduled_job.id
88
+ end
89
+
90
+ it 'redirects to the job path' do
91
+ assert_redirected_to job_path(scheduled_job.id)
92
+ end
93
+
94
+ it 'updates run_at' do
95
+ assert_nil scheduled_job.reload.run_at
96
+ end
97
+ end
98
+
99
+
100
+ describe "PATCH #update" do
101
+ describe 'with an invalid job id' do
102
+ before do
103
+ patch :update, id: 42, job: {id: 42, priority: 12}
104
+ end
105
+
106
+ it 'redirects' do
107
+ assert_redirected_to jobs_path
108
+ end
109
+
110
+ it 'adds a flash alert message' do
111
+ assert_equal I18n.t(:failure, scope: [:job, :find], id: 42), flash[:alert]
112
+ end
113
+ end
114
+
115
+ describe "with a valid job id" do
116
+ before do
117
+ patch :update, id: job.id, job: {id: job.id, priority: 12, blah: 23, description: '', log_level: '', state: 'failed'}
118
+ end
119
+
120
+ it 'redirects to the job' do
121
+ assert_redirected_to job_path(job.id)
122
+ end
123
+
124
+ it 'updates the job correctly' do
125
+ assert_equal 12, job.reload.priority
126
+ end
127
+
128
+ it "calls sanitize" do
129
+ job.reload
130
+ assert_nil job.description
131
+ assert_nil job.log_level
132
+ assert_equal :queued, job.state
133
+ end
134
+ end
135
+ end
136
+
137
+ describe 'GET #show' do
138
+ describe 'with an invalid job id' do
139
+ before do
140
+ get :show, id: 42
141
+ end
142
+
143
+ it 'redirects' do
144
+ assert_redirected_to jobs_path
145
+ end
146
+
147
+ it 'adds a flash alert message' do
148
+ assert_equal I18n.t(:failure, scope: [:job, :find], id: 42), flash[:alert]
149
+ end
150
+ end
151
+
152
+ describe "with a valid job id" do
153
+ before do
154
+ get :show, id: job.id
155
+ end
156
+
157
+ it 'succeeds' do
158
+ assert_response :success
159
+ end
160
+
161
+ it 'assigns the job' do
162
+ assert_equal job.id, assigns(:job).id
163
+ end
164
+ end
165
+ end
166
+
167
+ describe 'GET #exception' do
168
+ describe 'with an invalid job id' do
169
+ before do
170
+ get :exception, id: 42
171
+ end
172
+
173
+ it 'redirects' do
174
+ assert_redirected_to jobs_path
175
+ end
176
+
177
+ it 'adds a flash alert message' do
178
+ assert_equal I18n.t(:failure, scope: [:job, :find], id: 42), flash[:alert]
179
+ end
180
+ end
181
+
182
+ describe 'with a valid job id' do
183
+ before do
184
+ skip 'Only tested with Rocket Job Pro' unless defined?(RocketJob::Plugins::Batch)
185
+ get :exception, id: failed_job.id, error_type: 'Blah'
186
+ end
187
+
188
+ describe 'without an exception' do
189
+ before do
190
+ get :exception, id: failed_job.id, error_type: 'Blah'
191
+ end
192
+
193
+ it 'redirects to job path' do
194
+ assert_redirected_to job_path(failed_job.id)
195
+ end
196
+
197
+ it 'notifies the user' do
198
+ assert_equal I18n.t(:no_errors, scope: [:job, :failures]), flash[:notice]
199
+ end
200
+ end
201
+
202
+ describe 'with exception' do
203
+ before do
204
+ get :exception, id: failed_job.id, error_type: 'ArgumentError'
205
+ end
206
+
207
+ it 'succeeds' do
208
+ assert_response :success
209
+ end
210
+
211
+ it 'assigns the job' do
212
+ assert_equal failed_job.id, assigns(:job).id
213
+ end
214
+
215
+ it 'paginates' do
216
+ assert_equal 0, assigns(:pagination)[:offset], assigns(:pagination)
217
+ assert_equal 1, assigns(:pagination)[:total], assigns(:pagination)
218
+ end
219
+
220
+ it 'returns the first exception' do
221
+ assert_equal 'ArgumentError', assigns(:failure_exception).class_name
222
+ assert_equal 'Blowing up on record: 1', assigns(:failure_exception).message
223
+ assert assigns(:failure_exception).backtrace.present?
224
+ end
225
+ end
226
+ end
227
+ end
228
+
229
+ ([:index] + job_states).each do |state|
230
+ describe "GET ##{state}" do
231
+ describe 'html' do
232
+ describe "with no #{state} servers" do
233
+ before do
234
+ get state
235
+ end
236
+
237
+ it 'succeeds' do
238
+ assert_response :success
239
+ end
240
+
241
+ it 'renders template' do
242
+ assert_template :index
243
+ end
244
+ end
245
+
246
+ describe "with #{state} servers" do
247
+ before do
248
+ one_job_for_every_state
249
+ get state
250
+ end
251
+
252
+ it 'succeeds' do
253
+ assert_response :success
254
+ end
255
+
256
+ it 'renders template' do
257
+ assert_template :index
258
+ end
259
+ end
260
+ end
261
+
262
+ describe 'json' do
263
+ describe "with no #{state} server" do
264
+ before do
265
+ get state, format: :json
266
+ end
267
+
268
+ it 'succeeds' do
269
+ assert_response :success
270
+ json = JSON.parse(response.body)
271
+ expected = {
272
+ "data" => [],
273
+ "draw" => 0,
274
+ "recordsFiltered" => 0,
275
+ "recordsTotal" => 0
276
+ }
277
+ assert_equal expected, json
278
+ end
279
+ end
280
+
281
+ describe "with #{state} server" do
282
+ before do
283
+ one_job_for_every_state
284
+ get state, format: :json
285
+ end
286
+
287
+ it 'succeeds' do
288
+ assert_response :success
289
+ json = JSON.parse(response.body)
290
+ expected_data = {
291
+ aborted: {
292
+ "0" => /#{RocketJob::Jobs::SimpleJob.name}/,
293
+ "1" => '',
294
+ "2" => /UTC/,
295
+ "3" => /ms/,
296
+ "4" => /\/jobs\/#{RocketJob::Jobs::SimpleJob.aborted.first.id}/,
297
+ "DT_RowClass" => "card callout callout-aborted"
298
+ },
299
+ failed: {
300
+ "0" => /#{RocketJob::Jobs::SimpleJob.name}/,
301
+ "1" => '',
302
+ "2" => /UTC/,
303
+ "3" => /ms/,
304
+ "4" => /\/jobs\/#{RocketJob::Jobs::SimpleJob.failed.first.id}/,
305
+ "DT_RowClass" => "card callout callout-failed"
306
+ },
307
+ paused: {
308
+ "0" => /#{RocketJob::Jobs::SimpleJob.name}/,
309
+ "1" => '',
310
+ "2" => /UTC/,
311
+ "3" => /ms/,
312
+ "4" => /\/jobs\/#{RocketJob::Jobs::SimpleJob.paused.first.id}/,
313
+ "DT_RowClass" => "card callout callout-paused"
314
+ },
315
+ completed: {
316
+ "0" => /#{RocketJob::Jobs::SimpleJob.name}/,
317
+ "1" => '',
318
+ "2" => /UTC/,
319
+ "3" => /ms/,
320
+ "4" => /\/jobs\/#{RocketJob::Jobs::SimpleJob.completed.first.id}/,
321
+ "DT_RowClass" => "card callout callout-completed"
322
+ },
323
+ running: {
324
+ "0" => /#{RocketJob::Jobs::SimpleJob.name}/,
325
+ "1" => '',
326
+ "2" => /UTC/,
327
+ "3" => /ms/,
328
+ "4" => /\/jobs\/#{RocketJob::Jobs::SimpleJob.running.first.id}/,
329
+ "DT_RowClass" => "card callout callout-running"
330
+ },
331
+ queued: {
332
+ "0" => /#{RocketJob::Jobs::SimpleJob.name}/,
333
+ "1" => '',
334
+ "2" => /UTC/,
335
+ "3" => /ms/,
336
+ "4" => /\/jobs\/#{RocketJob::Jobs::SimpleJob.queued.first.id}/,
337
+ "DT_RowClass" => "card callout callout-queued"
338
+ },
339
+ }
340
+
341
+ if state == :index
342
+ assert_equal 0, json['draw']
343
+ assert_equal 6, json['recordsTotal']
344
+ assert_equal 6, json['recordsFiltered']
345
+ compare_array_of_hashes expected_data.values, json['data']
346
+ else
347
+ assert_equal 0, json['draw']
348
+ assert_equal 1, json['recordsTotal']
349
+ assert_equal 1, json['recordsFiltered']
350
+ # Columns change by state
351
+ #compare_hash expected_data[state], json['data'].first
352
+ end
353
+ end
354
+ end
355
+
356
+ end
357
+
358
+ end
359
+ end
360
+
361
+ end
362
+ end
363
+ end
@@ -0,0 +1,251 @@
1
+ require_relative '../../test_helper'
2
+ require_relative '../../compare_hashes'
3
+
4
+ module RocketJobMissionControl
5
+ class ServersControllerTest < ActionController::TestCase
6
+ describe ServersController do
7
+
8
+ before do
9
+ RocketJob::Job.delete_all
10
+ RocketJob::Server.delete_all
11
+ end
12
+
13
+ let :server do
14
+ s = RocketJob::Server.new
15
+ s.started!
16
+ s
17
+ end
18
+
19
+ server_states = RocketJob::Server.aasm.states.collect(&:name)
20
+
21
+ let :one_server_for_every_state do
22
+ server_states.collect do |state|
23
+ server = RocketJob::Server.new(state: state)
24
+ server.build_heartbeat(updated_at: Time.now, workers: 0)
25
+ server.save!
26
+ end
27
+ end
28
+
29
+ [:stop, :pause, :resume].each do |server_action|
30
+ describe "PATCH ##{server_action}" do
31
+ describe 'with a valid server id' do
32
+ before do
33
+ server.pause! if server_action == :resume
34
+ patch server_action, id: server.id
35
+ end
36
+
37
+ it 'redirects to servers' do
38
+ assert_redirected_to servers_path
39
+ end
40
+
41
+ it "#{server_action} the server" do
42
+ results = {stop: :stopping?, pause: :paused?, resume: :running?}
43
+ assert server.reload.send(results[server_action])
44
+ end
45
+ end
46
+
47
+ describe "when the server fails to #{server_action}" do
48
+ before do
49
+ server.pause! if server_action == :pause
50
+ server.stop! if server_action == :stop
51
+ patch server_action, id: server.id
52
+ end
53
+
54
+ it 'redirects to servers' do
55
+ assert_redirected_to servers_path
56
+ end
57
+
58
+ it 'displays a flash message' do
59
+ assert_equal I18n.t(:failure, scope: [:server, server_action]), flash[:alert]
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ describe 'PATCH #update_all' do
66
+ RocketJobMissionControl::ServersController::VALID_ACTIONS.each do |server_action, action_message|
67
+ describe "with '#{server_action}' as the server_action param" do
68
+ before do
69
+ patch :update_all, server_action: server_action
70
+ end
71
+
72
+ it 'redirects to servers' do
73
+ assert_redirected_to servers_path
74
+ end
75
+
76
+ it 'does not display an error message' do
77
+ assert_nil flash[:alert]
78
+ end
79
+ end
80
+ end
81
+
82
+ describe 'with an invalid server_action param' do
83
+ before do
84
+ patch :update_all, server_action: :bad_server_action
85
+ end
86
+
87
+ it 'redirects to servers' do
88
+ assert_redirected_to servers_path
89
+ end
90
+
91
+ it "does not display a success message" do
92
+ assert_nil flash[:notice]
93
+ end
94
+
95
+ it 'displays an error message' do
96
+ assert_equal I18n.t(:invalid, scope: [:server, :update_all]), flash[:alert]
97
+ end
98
+ end
99
+ end
100
+
101
+ describe 'DELETE #destroy' do
102
+ describe 'with a valid server id' do
103
+ before do
104
+ delete :destroy, id: server.id
105
+ end
106
+
107
+ it 'redirects to servers' do
108
+ assert_redirected_to servers_path
109
+ end
110
+
111
+ it 'displays a flash message' do
112
+ assert_equal I18n.t(:success, scope: [:server, :destroy]), flash[:notice]
113
+ end
114
+
115
+ it 'destroys the server' do
116
+ refute RocketJob::Server.where(id: server.id).exists?
117
+ end
118
+ end
119
+
120
+ describe 'when the server is not found' do
121
+ before do
122
+ delete :destroy, id: 999999
123
+ end
124
+
125
+ it 'redirects to servers' do
126
+ assert_redirected_to servers_path
127
+ end
128
+
129
+ it 'displays a flash message' do
130
+ assert_equal I18n.t(:failure, scope: [:server, :find], id: 999999), flash[:alert]
131
+ end
132
+ end
133
+ end
134
+
135
+ ([:index] + server_states).each do |state|
136
+ describe "GET ##{state}" do
137
+ describe 'html' do
138
+ describe "with no #{state} servers" do
139
+ before do
140
+ get state
141
+ end
142
+
143
+ it 'succeeds' do
144
+ assert_response :success
145
+ end
146
+
147
+ it 'renders template' do
148
+ assert_template :index
149
+ end
150
+ end
151
+
152
+ describe "with #{state} servers" do
153
+ before do
154
+ one_server_for_every_state
155
+ get state
156
+ end
157
+
158
+ it 'succeeds' do
159
+ assert_response :success
160
+ end
161
+
162
+ it 'renders template' do
163
+ assert_template :index
164
+ end
165
+ end
166
+ end
167
+
168
+ describe 'json' do
169
+ describe "with no #{state} server" do
170
+ before do
171
+ get state, format: :json
172
+ end
173
+
174
+ it 'succeeds' do
175
+ assert_response :success
176
+ json = JSON.parse(response.body)
177
+ expected = {
178
+ "data" => [],
179
+ "draw" => 0,
180
+ "recordsFiltered" => 0,
181
+ "recordsTotal" => 0
182
+ }
183
+ assert_equal expected, json
184
+ end
185
+ end
186
+
187
+ describe "with #{state} server" do
188
+ before do
189
+ one_server_for_every_state
190
+ get state, format: :json
191
+ end
192
+
193
+ it 'succeeds' do
194
+ assert_response :success
195
+ json = JSON.parse(response.body)
196
+ expected_data = {
197
+ starting: {
198
+ "0" => /#{RocketJob::Server.starting.first.name}/,
199
+ "1" => "0/10",
200
+ "2" => /s ago/,
201
+ "3" => /s ago/,
202
+ "4" => /\/servers\/#{RocketJob::Server.starting.first.id}\/stop/,
203
+ "DT_RowClass" => "card callout callout-info"
204
+ },
205
+ running: {
206
+ "0" => /#{RocketJob::Server.running.first.name}/,
207
+ "1" => "0/10",
208
+ "2" => /s ago/,
209
+ "3" => /s ago/,
210
+ "4" => /\/servers\/#{RocketJob::Server.running.first.id}\/stop/,
211
+ "DT_RowClass" => "card callout callout-success"
212
+ },
213
+ paused: {
214
+ "0" => /#{RocketJob::Server.paused.first.name}/,
215
+ "1" => "0/10",
216
+ "2" => /s ago/,
217
+ "3" => /s ago/,
218
+ "4" => /\/servers\/#{RocketJob::Server.paused.first.id}\/stop/,
219
+ "DT_RowClass" => "card callout callout-warning"
220
+ },
221
+ stopping: {
222
+ "0" => /#{RocketJob::Server.stopping.first.name}/,
223
+ "1" => "0/10",
224
+ "2" => /s ago/,
225
+ "3" => /s ago/,
226
+ "4" => /\/servers\/#{RocketJob::Server.stopping.first.id}/,
227
+ "DT_RowClass" => "card callout callout-alert"
228
+ }
229
+ }
230
+
231
+ if state == :index
232
+ assert_equal 0, json['draw']
233
+ assert_equal 4, json['recordsTotal']
234
+ assert_equal 4, json['recordsFiltered']
235
+ compare_array_of_hashes [expected_data[:starting], expected_data[:running], expected_data[:paused], expected_data[:stopping]], json['data']
236
+ else
237
+ assert_equal 0, json['draw']
238
+ assert_equal 1, json['recordsTotal']
239
+ assert_equal 1, json['recordsFiltered']
240
+ compare_hash expected_data[state], json['data'].first
241
+ end
242
+ end
243
+ end
244
+ end
245
+
246
+ end
247
+ end
248
+
249
+ end
250
+ end
251
+ end