monitoring_spec.rb 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. require 'rails_helper'
  2. RSpec.describe 'Monitoring', type: :request do
  3. let!(:admin_user) do
  4. create(:admin_user, groups: Group.all)
  5. end
  6. let!(:agent_user) do
  7. create(:agent_user, groups: Group.all)
  8. end
  9. let!(:customer_user) do
  10. create(:customer_user)
  11. end
  12. let!(:token) do
  13. SecureRandom.urlsafe_base64(64)
  14. end
  15. before do
  16. Setting.set('monitoring_token', token)
  17. # channel cleanup
  18. Channel.where.not(area: 'Email::Notification').destroy_all
  19. Channel.all.each do |channel|
  20. channel.status_in = 'ok'
  21. channel.status_out = 'ok'
  22. channel.last_log_in = nil
  23. channel.last_log_out = nil
  24. channel.save!
  25. end
  26. dir = Rails.root.join('tmp', 'unprocessable_mail')
  27. Dir.glob("#{dir}/*.eml") do |entry|
  28. File.delete(entry)
  29. end
  30. Scheduler.where(active: true).each do |scheduler|
  31. scheduler.last_run = Time.zone.now
  32. scheduler.save!
  33. end
  34. permission = Permission.find_by(name: 'admin.monitoring')
  35. permission.active = true
  36. permission.save!
  37. end
  38. describe 'request handling' do
  39. it 'does monitoring without token' do
  40. # health_check
  41. get '/api/v1/monitoring/health_check', params: {}, as: :json
  42. expect(response).to have_http_status(:unauthorized)
  43. expect(json_response).to be_a_kind_of(Hash)
  44. expect(json_response['healthy']).to be_falsey
  45. expect(json_response['error']).to eq('Not authorized')
  46. # status
  47. get '/api/v1/monitoring/status', params: {}, as: :json
  48. expect(response).to have_http_status(:unauthorized)
  49. expect(json_response).to be_a_kind_of(Hash)
  50. expect(json_response['agents']).to be_falsey
  51. expect(json_response['last_login']).to be_falsey
  52. expect(json_response['counts']).to be_falsey
  53. expect(json_response['last_created_at']).to be_falsey
  54. expect(json_response['error']).to eq('Not authorized')
  55. # token
  56. post '/api/v1/monitoring/token', params: {}, as: :json
  57. expect(response).to have_http_status(:unauthorized)
  58. expect(json_response).to be_a_kind_of(Hash)
  59. expect(json_response['token']).to be_falsey
  60. expect(json_response['error']).to eq('authentication failed')
  61. end
  62. it 'does monitoring with wrong token' do
  63. # health_check
  64. get '/api/v1/monitoring/health_check?token=abc', params: {}, as: :json
  65. expect(response).to have_http_status(:unauthorized)
  66. expect(json_response).to be_a_kind_of(Hash)
  67. expect(json_response['healthy']).to be_falsey
  68. expect(json_response['error']).to eq('Not authorized')
  69. # status
  70. get '/api/v1/monitoring/status?token=abc', params: {}, as: :json
  71. expect(response).to have_http_status(:unauthorized)
  72. expect(json_response).to be_a_kind_of(Hash)
  73. expect(json_response['agents']).to be_falsey
  74. expect(json_response['last_login']).to be_falsey
  75. expect(json_response['counts']).to be_falsey
  76. expect(json_response['last_created_at']).to be_falsey
  77. expect(json_response['error']).to eq('Not authorized')
  78. # token
  79. post '/api/v1/monitoring/token', params: { token: 'abc' }, as: :json
  80. expect(response).to have_http_status(:unauthorized)
  81. expect(json_response).to be_a_kind_of(Hash)
  82. expect(json_response['token']).to be_falsey
  83. expect(json_response['error']).to eq('authentication failed')
  84. end
  85. it 'does monitoring with correct token' do
  86. # test storage usage
  87. string = ''
  88. 1000.times do
  89. string += 'Some Text Some Text Some Text Some Text Some Text Some Text Some Text Some Text'
  90. end
  91. Store.add(
  92. object: 'User',
  93. o_id: 1,
  94. data: string,
  95. filename: 'filename.txt',
  96. created_by_id: 1,
  97. )
  98. # health_check
  99. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  100. expect(response).to have_http_status(:ok)
  101. expect(json_response).to be_a_kind_of(Hash)
  102. expect(json_response['error']).to be_falsey
  103. expect(json_response['healthy']).to eq(true)
  104. expect(json_response['message']).to eq('success')
  105. # status
  106. get "/api/v1/monitoring/status?token=#{token}", params: {}, as: :json
  107. expect(response).to have_http_status(:ok)
  108. expect(json_response).to be_a_kind_of(Hash)
  109. expect(json_response['error']).to be_falsey
  110. expect(json_response).to be_key('agents')
  111. expect(json_response).to be_key('last_login')
  112. expect(json_response).to be_key('counts')
  113. expect(json_response).to be_key('last_created_at')
  114. first_json_response_kb = 0
  115. if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
  116. expect(json_response['storage']).to be_truthy
  117. expect(json_response['storage']).to be_key('kB')
  118. expect(json_response['storage']['kB']).to be > 0
  119. expect(json_response['storage']).to be_key('MB')
  120. expect(json_response['storage']).to be_key('GB')
  121. first_json_response_kb = json_response['storage']['kB']
  122. else
  123. expect(json_response['storage']).to be_falsey
  124. end
  125. # save same file again
  126. Store.add(
  127. object: 'User',
  128. o_id: 1,
  129. data: string,
  130. filename: 'filename.txt',
  131. created_by_id: 1,
  132. )
  133. # status
  134. get "/api/v1/monitoring/status?token=#{token}", params: {}, as: :json
  135. expect(response).to have_http_status(:ok)
  136. expect(json_response).to be_a_kind_of(Hash)
  137. expect(json_response['error']).to be_falsey
  138. expect(json_response).to be_key('agents')
  139. expect(json_response).to be_key('last_login')
  140. expect(json_response).to be_key('counts')
  141. expect(json_response).to be_key('last_created_at')
  142. if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
  143. expect(json_response['storage']).to be_truthy
  144. expect(json_response['storage']).to be_key('kB')
  145. # check if the stores got summarized. value should be the same because the file has the same fingerprint
  146. expect(json_response['storage']['kB']).to eq(first_json_response_kb)
  147. expect(json_response['storage']).to be_key('MB')
  148. expect(json_response['storage']).to be_key('GB')
  149. else
  150. expect(json_response['storage']).to be_falsey
  151. end
  152. Store.add(
  153. object: 'User',
  154. o_id: 1,
  155. data: string + '123',
  156. filename: 'filename2.txt',
  157. created_by_id: 1,
  158. )
  159. # status
  160. get "/api/v1/monitoring/status?token=#{token}", params: {}, as: :json
  161. expect(response).to have_http_status(:ok)
  162. expect(json_response).to be_a_kind_of(Hash)
  163. expect(json_response['error']).to be_falsey
  164. expect(json_response).to be_key('agents')
  165. expect(json_response).to be_key('last_login')
  166. expect(json_response).to be_key('counts')
  167. expect(json_response).to be_key('last_created_at')
  168. if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
  169. expect(json_response['storage']).to be_truthy
  170. expect(json_response['storage']).to be_key('kB')
  171. # check if the stores got summarized. value should be greather than the size of just one file (saved 2 times)
  172. expect(json_response['storage']['kB']).to be > first_json_response_kb
  173. expect(json_response['storage']).to be_key('MB')
  174. expect(json_response['storage']).to be_key('GB')
  175. else
  176. expect(json_response['storage']).to be_falsey
  177. end
  178. # token
  179. post '/api/v1/monitoring/token', params: { token: token }, as: :json
  180. expect(response).to have_http_status(:unauthorized)
  181. expect(json_response).to be_a_kind_of(Hash)
  182. expect(json_response['token']).to be_falsey
  183. expect(json_response['error']).to eq('authentication failed')
  184. end
  185. it 'does monitoring with admin user' do
  186. # health_check
  187. authenticated_as(admin_user)
  188. get '/api/v1/monitoring/health_check', params: {}, as: :json
  189. expect(response).to have_http_status(:ok)
  190. expect(json_response).to be_a_kind_of(Hash)
  191. expect(json_response['error']).to be_falsey
  192. expect(json_response['healthy']).to eq(true)
  193. expect(json_response['message']).to eq('success')
  194. # status
  195. get '/api/v1/monitoring/status', params: {}, as: :json
  196. expect(response).to have_http_status(:ok)
  197. expect(json_response).to be_a_kind_of(Hash)
  198. expect(json_response['error']).to be_falsey
  199. expect(json_response).to be_key('agents')
  200. expect(json_response).to be_key('last_login')
  201. expect(json_response).to be_key('counts')
  202. expect(json_response).to be_key('last_created_at')
  203. # token
  204. post '/api/v1/monitoring/token', params: { token: token }, as: :json
  205. expect(response).to have_http_status(:created)
  206. expect(json_response).to be_a_kind_of(Hash)
  207. expect(json_response['token']).to be_truthy
  208. expect(json_response['error']).to be_falsey
  209. end
  210. it 'does monitoring with agent user' do
  211. # health_check
  212. authenticated_as(agent_user)
  213. get '/api/v1/monitoring/health_check', params: {}, as: :json
  214. expect(response).to have_http_status(:unauthorized)
  215. expect(json_response).to be_a_kind_of(Hash)
  216. expect(json_response['healthy']).to be_falsey
  217. expect(json_response['error']).to eq('Not authorized (user)!')
  218. # status
  219. get '/api/v1/monitoring/status', params: {}, as: :json
  220. expect(response).to have_http_status(:unauthorized)
  221. expect(json_response).to be_a_kind_of(Hash)
  222. expect(json_response['agents']).to be_falsey
  223. expect(json_response['last_login']).to be_falsey
  224. expect(json_response['counts']).to be_falsey
  225. expect(json_response['last_created_at']).to be_falsey
  226. expect(json_response['error']).to eq('Not authorized (user)!')
  227. # token
  228. post '/api/v1/monitoring/token', params: { token: token }, as: :json
  229. expect(response).to have_http_status(:unauthorized)
  230. expect(json_response).to be_a_kind_of(Hash)
  231. expect(json_response['token']).to be_falsey
  232. expect(json_response['error']).to eq('Not authorized (user)!')
  233. end
  234. it 'does monitoring with admin user and invalid permission' do
  235. permission = Permission.find_by(name: 'admin.monitoring')
  236. permission.active = false
  237. permission.save!
  238. # health_check
  239. authenticated_as(admin_user)
  240. get '/api/v1/monitoring/health_check', params: {}, as: :json
  241. expect(response).to have_http_status(:unauthorized)
  242. expect(json_response).to be_a_kind_of(Hash)
  243. expect(json_response['healthy']).to be_falsey
  244. expect(json_response['error']).to eq('Not authorized (user)!')
  245. # status
  246. get '/api/v1/monitoring/status', params: {}, as: :json
  247. expect(response).to have_http_status(:unauthorized)
  248. expect(json_response).to be_a_kind_of(Hash)
  249. expect(json_response['agents']).to be_falsey
  250. expect(json_response['last_login']).to be_falsey
  251. expect(json_response['counts']).to be_falsey
  252. expect(json_response['last_created_at']).to be_falsey
  253. expect(json_response['error']).to eq('Not authorized (user)!')
  254. # token
  255. post '/api/v1/monitoring/token', params: { token: token }, as: :json
  256. expect(response).to have_http_status(:unauthorized)
  257. expect(json_response).to be_a_kind_of(Hash)
  258. expect(json_response['token']).to be_falsey
  259. expect(json_response['error']).to eq('Not authorized (user)!')
  260. permission.active = true
  261. permission.save!
  262. end
  263. it 'does monitoring with correct token and invalid permission' do
  264. permission = Permission.find_by(name: 'admin.monitoring')
  265. permission.active = false
  266. permission.save!
  267. # health_check
  268. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  269. expect(response).to have_http_status(:ok)
  270. expect(json_response).to be_a_kind_of(Hash)
  271. expect(json_response['error']).to be_falsey
  272. expect(json_response['healthy']).to eq(true)
  273. expect(json_response['message']).to eq('success')
  274. # status
  275. get "/api/v1/monitoring/status?token=#{token}", params: {}, as: :json
  276. expect(response).to have_http_status(:ok)
  277. expect(json_response).to be_a_kind_of(Hash)
  278. expect(json_response['error']).to be_falsey
  279. expect(json_response).to be_key('agents')
  280. expect(json_response).to be_key('last_login')
  281. expect(json_response).to be_key('counts')
  282. expect(json_response).to be_key('last_created_at')
  283. # token
  284. post '/api/v1/monitoring/token', params: { token: token }, as: :json
  285. expect(response).to have_http_status(:unauthorized)
  286. expect(json_response).to be_a_kind_of(Hash)
  287. expect(json_response['token']).to be_falsey
  288. expect(json_response['error']).to eq('authentication failed')
  289. permission.active = true
  290. permission.save!
  291. end
  292. it 'does check health false' do
  293. channel = Channel.find_by(active: true)
  294. channel.status_in = 'ok'
  295. channel.status_out = 'error'
  296. channel.last_log_in = nil
  297. channel.last_log_out = nil
  298. channel.save!
  299. # health_check - channel
  300. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  301. expect(response).to have_http_status(:ok)
  302. expect(json_response).to be_a_kind_of(Hash)
  303. expect(json_response['message']).to be_truthy
  304. expect(json_response['issues']).to be_truthy
  305. expect(json_response['healthy']).to eq(false)
  306. expect(json_response['message']).to eq('Channel: Email::Notification out ')
  307. # health_check - scheduler may not run
  308. scheduler = Scheduler.where(active: true).last
  309. scheduler.last_run = Time.zone.now - 20.minutes
  310. scheduler.period = 600
  311. scheduler.save!
  312. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  313. expect(response).to have_http_status(:ok)
  314. expect(json_response).to be_a_kind_of(Hash)
  315. expect(json_response['message']).to be_truthy
  316. expect(json_response['issues']).to be_truthy
  317. expect(json_response['healthy']).to eq(false)
  318. expect(json_response['message']).to eq("Channel: Email::Notification out ;scheduler may not run (last execution of #{scheduler.method} 10 minutes over) - please contact your system administrator")
  319. # health_check - scheduler may not run
  320. scheduler = Scheduler.where(active: true).last
  321. scheduler.last_run = Time.zone.now - 1.day
  322. scheduler.period = 600
  323. scheduler.save!
  324. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  325. expect(response).to have_http_status(:ok)
  326. expect(json_response).to be_a_kind_of(Hash)
  327. expect(json_response['message']).to be_truthy
  328. expect(json_response['issues']).to be_truthy
  329. expect(json_response['healthy']).to eq(false)
  330. expect(json_response['message']).to eq("Channel: Email::Notification out ;scheduler may not run (last execution of #{scheduler.method} about 24 hours over) - please contact your system administrator")
  331. # health_check - scheduler job count
  332. travel 2.seconds
  333. 8001.times do |fake_ticket_id|
  334. SearchIndexJob.perform_later('Ticket', fake_ticket_id)
  335. end
  336. Scheduler.where(active: true).each do |local_scheduler|
  337. local_scheduler.last_run = Time.zone.now
  338. local_scheduler.save!
  339. end
  340. total_jobs = Delayed::Job.count
  341. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  342. expect(response).to have_http_status(:ok)
  343. expect(json_response).to be_a_kind_of(Hash)
  344. expect(json_response['message']).to be_truthy
  345. expect(json_response['issues']).to be_truthy
  346. expect(json_response['healthy']).to eq(false)
  347. expect(json_response['message']).to eq('Channel: Email::Notification out ')
  348. travel 20.minutes
  349. Scheduler.where(active: true).each do |local_scheduler|
  350. local_scheduler.last_run = Time.zone.now
  351. local_scheduler.save!
  352. end
  353. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  354. expect(response).to have_http_status(:ok)
  355. expect(json_response).to be_a_kind_of(Hash)
  356. expect(json_response['message']).to be_truthy
  357. expect(json_response['issues']).to be_truthy
  358. expect(json_response['healthy']).to eq(false)
  359. expect(json_response['message']).to eq("Channel: Email::Notification out ;#{total_jobs} background jobs in queue")
  360. Delayed::Job.delete_all
  361. travel_back
  362. # health_check - unprocessable mail
  363. dir = Rails.root.join('tmp', 'unprocessable_mail')
  364. FileUtils.mkdir_p(dir)
  365. FileUtils.touch("#{dir}/test.eml")
  366. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  367. expect(response).to have_http_status(:ok)
  368. expect(json_response).to be_a_kind_of(Hash)
  369. expect(json_response['message']).to be_truthy
  370. expect(json_response['issues']).to be_truthy
  371. expect(json_response['healthy']).to eq(false)
  372. expect(json_response['message']).to eq('Channel: Email::Notification out ;unprocessable mails: 1')
  373. # health_check - ldap
  374. Setting.set('ldap_integration', true)
  375. ImportJob.create(
  376. name: 'Import::Ldap',
  377. started_at: Time.zone.now,
  378. finished_at: Time.zone.now,
  379. result: {
  380. error: 'Some bad error'
  381. }
  382. )
  383. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  384. expect(response).to have_http_status(:ok)
  385. expect(json_response).to be_a_kind_of(Hash)
  386. expect(json_response['message']).to be_truthy
  387. expect(json_response['issues']).to be_truthy
  388. expect(json_response['healthy']).to eq(false)
  389. expect(json_response['message']).to eq("Channel: Email::Notification out ;unprocessable mails: 1;Failed to run import backend 'Import::Ldap'. Cause: Some bad error")
  390. stuck_updated_at_timestamp = 15.minutes.ago
  391. ImportJob.create(
  392. name: 'Import::Ldap',
  393. started_at: Time.zone.now,
  394. finished_at: nil,
  395. updated_at: stuck_updated_at_timestamp,
  396. )
  397. # health_check
  398. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  399. expect(response).to have_http_status(:ok)
  400. expect(json_response).to be_a_kind_of(Hash)
  401. expect(json_response['message']).to be_truthy
  402. expect(json_response['issues']).to be_truthy
  403. expect(json_response['healthy']).to eq(false)
  404. expect(json_response['message']).to eq("Channel: Email::Notification out ;unprocessable mails: 1;Failed to run import backend 'Import::Ldap'. Cause: Some bad error;Stuck import backend 'Import::Ldap' detected. Last update: #{stuck_updated_at_timestamp}")
  405. Setting.set('ldap_integration', false)
  406. end
  407. it 'does check restart_failed_jobs' do
  408. authenticated_as(admin_user)
  409. post '/api/v1/monitoring/restart_failed_jobs', params: {}, as: :json
  410. expect(response).to have_http_status(:ok)
  411. end
  412. it 'does check failed delayed job', db_strategy: :reset do
  413. # disable elasticsearch
  414. prev_es_config = Setting.get('es_url')
  415. Setting.set('es_url', 'http://127.0.0.1:92001')
  416. # delete all background jobs created while seeding
  417. # to have a clean state for checking for failed ones
  418. Delayed::Job.destroy_all
  419. # add a new object
  420. object = create(:object_manager_attribute_text)
  421. migration = ObjectManager::Attribute.migration_execute
  422. expect(true).to eq(migration)
  423. authenticated_as(admin_user)
  424. post "/api/v1/object_manager_attributes/#{object.id}", params: {}, as: :json
  425. token = @response.headers['CSRF-TOKEN']
  426. # parameters for updating
  427. params = {
  428. 'name': 'test4',
  429. 'object': 'Ticket',
  430. 'display': 'Test 4',
  431. 'active': true,
  432. 'data_type': 'input',
  433. 'data_option': {
  434. 'default': 'test',
  435. 'type': 'text',
  436. 'maxlength': 120
  437. },
  438. 'screens': {
  439. 'create_middle': {
  440. 'ticket.customer': {
  441. 'shown': true,
  442. 'item_class': 'column'
  443. },
  444. 'ticket.agent': {
  445. 'shown': true,
  446. 'item_class': 'column'
  447. }
  448. },
  449. 'edit': {
  450. 'ticket.customer': {
  451. 'shown': true
  452. },
  453. 'ticket.agent': {
  454. 'shown': true
  455. }
  456. }
  457. },
  458. 'id': 'c-196'
  459. }
  460. # update the object
  461. put "/api/v1/object_manager_attributes/#{object.id}", params: params, as: :json
  462. migration = ObjectManager::Attribute.migration_execute
  463. expect(true).to eq(migration)
  464. expect(response).to have_http_status(:ok)
  465. expect(json_response).to be_truthy
  466. expect(json_response['data_option']['null']).to be_truthy
  467. expect('test4').to eq(json_response['name'])
  468. expect('Test 4').to eq(json_response['display'])
  469. 4.times do
  470. Delayed::Worker.new.work_off
  471. end
  472. # health_check
  473. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  474. expect(response).to have_http_status(:ok)
  475. expect(json_response).to be_a_kind_of(Hash)
  476. expect(json_response['message']).to be_truthy
  477. expect(json_response['issues']).to be_truthy
  478. expect(json_response['healthy']).to eq(false)
  479. expect( json_response['message']).to eq("Failed to run background job #1 'SearchIndexJob' 1 time(s) with 1 attempt(s).")
  480. # add another job
  481. manual_added = SearchIndexJob.perform_later('Ticket', 1)
  482. Delayed::Job.find(manual_added.provider_job_id).update!(attempts: 10)
  483. # health_check
  484. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  485. expect(response).to have_http_status(:ok)
  486. expect(json_response).to be_a_kind_of(Hash)
  487. expect(json_response['message']).to be_truthy
  488. expect(json_response['issues']).to be_truthy
  489. expect(json_response['healthy']).to eq(false)
  490. expect( json_response['message']).to eq("Failed to run background job #1 'SearchIndexJob' 2 time(s) with 11 attempt(s).")
  491. # add another job
  492. dummy_class = Class.new(ApplicationJob) do
  493. def perform
  494. puts 'work work'
  495. end
  496. end
  497. manual_added = Delayed::Job.enqueue( dummy_class.new )
  498. manual_added.update!(attempts: 5)
  499. # health_check
  500. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  501. expect(response).to have_http_status(:ok)
  502. expect(json_response).to be_a_kind_of(Hash)
  503. expect(json_response['message']).to be_truthy
  504. expect(json_response['issues']).to be_truthy
  505. expect(json_response['healthy']).to eq(false)
  506. expect( json_response['message']).to eq("Failed to run background job #1 'Object' 1 time(s) with 5 attempt(s).;Failed to run background job #2 'SearchIndexJob' 2 time(s) with 11 attempt(s).")
  507. # reset settings
  508. Setting.set('es_url', prev_es_config)
  509. # add some more failing job
  510. 10.times do
  511. manual_added = Delayed::Job.enqueue( dummy_class.new )
  512. manual_added.update!(attempts: 5)
  513. end
  514. # health_check
  515. get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
  516. expect(response).to have_http_status(:ok)
  517. expect(json_response).to be_a_kind_of(Hash)
  518. expect(json_response['message']).to be_truthy
  519. expect(json_response['issues']).to be_truthy
  520. expect(json_response['healthy']).to eq(false)
  521. expect(json_response['message']).to eq("13 failing background jobs;Failed to run background job #1 'Object' 8 time(s) with 40 attempt(s).;Failed to run background job #2 'SearchIndexJob' 2 time(s) with 11 attempt(s).")
  522. # cleanup
  523. Delayed::Job.delete_all
  524. end
  525. it 'does check amount' do
  526. Ticket.destroy_all
  527. # amount_check - ok
  528. get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h", params: {}, as: :json
  529. expect(response).to have_http_status(:ok)
  530. expect(json_response).to be_a_kind_of(Hash)
  531. expect(json_response.key?('state')).to eq(false)
  532. expect(json_response.key?('message')).to eq(false)
  533. expect(json_response['count']).to eq(0)
  534. Ticket.destroy_all
  535. (1..6).each do |i|
  536. create(:ticket, title: "Ticket-#{i}")
  537. travel 10.seconds
  538. end
  539. get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&min_warning=10&min_critical=8", params: {}, as: :json
  540. expect(response).to have_http_status(:ok)
  541. expect(json_response).to be_a_kind_of(Hash)
  542. expect(json_response['state']).to eq('critical')
  543. expect(json_response['message']).to eq('The minimum of 8 was undercut by 6 in the last 1h')
  544. expect(json_response['count']).to eq(6)
  545. get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&min_warning=7&min_critical=2", params: {}, as: :json
  546. expect(response).to have_http_status(:ok)
  547. expect(json_response).to be_a_kind_of(Hash)
  548. expect(json_response['state']).to eq('warning')
  549. expect(json_response['message']).to eq('The minimum of 7 was undercut by 6 in the last 1h')
  550. expect(json_response['count']).to eq(6)
  551. get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&max_warning=10&max_critical=20", params: {}, as: :json
  552. expect(response).to have_http_status(:ok)
  553. expect(json_response).to be_a_kind_of(Hash)
  554. expect(json_response['state']).to eq('ok')
  555. expect(json_response.key?('message')).to eq(false)
  556. expect(json_response['count']).to eq(6)
  557. (1..6).each do |i|
  558. create(:ticket, title: "Ticket-#{i}")
  559. travel 1.second
  560. end
  561. get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&max_warning=10&max_critical=20", params: {}, as: :json
  562. expect(response).to have_http_status(:ok)
  563. expect(json_response).to be_a_kind_of(Hash)
  564. expect(json_response['state']).to eq('warning')
  565. expect(json_response['message']).to eq('The limit of 10 was exceeded with 12 in the last 1h')
  566. expect(json_response['count']).to eq(12)
  567. (1..10).each do |i|
  568. create(:ticket, title: "Ticket-#{i}")
  569. travel 1.second
  570. end
  571. get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&max_warning=10&max_critical=20", params: {}, as: :json
  572. expect(response).to have_http_status(:ok)
  573. expect(json_response).to be_a_kind_of(Hash)
  574. expect(json_response['state']).to eq('critical')
  575. expect(json_response['message']).to eq('The limit of 20 was exceeded with 22 in the last 1h')
  576. expect(json_response['count']).to eq(22)
  577. get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&max_warning=30", params: {}, as: :json
  578. expect(response).to have_http_status(:ok)
  579. expect(json_response).to be_a_kind_of(Hash)
  580. expect(json_response['state']).to eq('ok')
  581. expect(json_response.key?('message')).to eq(false)
  582. expect(json_response['count']).to eq(22)
  583. get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h", params: {}, as: :json
  584. expect(response).to have_http_status(:ok)
  585. expect(json_response).to be_a_kind_of(Hash)
  586. expect(json_response.key?('state')).to eq(false)
  587. expect(json_response.key?('message')).to eq(false)
  588. expect(json_response['count']).to eq(22)
  589. travel 2.hours
  590. get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&max_warning=30", params: {}, as: :json
  591. expect(response).to have_http_status(:ok)
  592. expect(json_response).to be_a_kind_of(Hash)
  593. expect(json_response['state']).to eq('ok')
  594. expect(json_response.key?('message')).to eq(false)
  595. expect(json_response['count']).to eq(0)
  596. get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h", params: {}, as: :json
  597. expect(response).to have_http_status(:ok)
  598. expect(json_response).to be_a_kind_of(Hash)
  599. expect(json_response.key?('state')).to eq(false)
  600. expect(json_response.key?('message')).to eq(false)
  601. expect(json_response['count']).to eq(0)
  602. end
  603. end
  604. end