monitoring_spec.rb 29 KB

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