monitoring_controller_test.rb 25 KB

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