monitoring_controller_test.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. require 'test_helper'
  2. class MonitoringControllerTest < ActionDispatch::IntegrationTest
  3. self.use_transactional_tests = false
  4. setup do
  5. # set accept header
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. # set token
  8. @token = SecureRandom.urlsafe_base64(64)
  9. Setting.set('monitoring_token', @token)
  10. # create agent
  11. roles = Role.where(name: %w[Admin Agent])
  12. groups = Group.all
  13. # channel cleanup
  14. Channel.where.not(area: 'Email::Notification').destroy_all
  15. Channel.all.each do |channel|
  16. channel.status_in = 'ok'
  17. channel.status_out = 'ok'
  18. channel.last_log_in = nil
  19. channel.last_log_out = nil
  20. channel.save!
  21. end
  22. dir = Rails.root.join('tmp', 'unprocessable_mail')
  23. Dir.glob("#{dir}/*.eml") do |entry|
  24. File.delete(entry)
  25. end
  26. Scheduler.where(active: true).each do |scheduler|
  27. scheduler.last_run = Time.zone.now
  28. scheduler.save!
  29. end
  30. permission = Permission.find_by(name: 'admin.monitoring')
  31. permission.active = true
  32. permission.save!
  33. UserInfo.current_user_id = 1
  34. @admin = User.create_or_update(
  35. login: 'monitoring-admin',
  36. firstname: 'Monitoring',
  37. lastname: 'Admin',
  38. email: 'monitoring-admin@example.com',
  39. password: 'adminpw',
  40. active: true,
  41. roles: roles,
  42. groups: groups,
  43. )
  44. # create agent
  45. roles = Role.where(name: 'Agent')
  46. @agent = User.create_or_update(
  47. login: 'monitoring-agent@example.com',
  48. firstname: 'Monitoring',
  49. lastname: 'Agent',
  50. email: 'monitoring-agent@example.com',
  51. password: 'agentpw',
  52. active: true,
  53. roles: roles,
  54. groups: groups,
  55. )
  56. # create customer without org
  57. roles = Role.where(name: 'Customer')
  58. @customer_without_org = User.create_or_update(
  59. login: 'monitoring-customer1@example.com',
  60. firstname: 'Monitoring',
  61. lastname: 'Customer1',
  62. email: 'monitoring-customer1@example.com',
  63. password: 'customer1pw',
  64. active: true,
  65. roles: roles,
  66. )
  67. end
  68. test '01 monitoring without token' do
  69. # health_check
  70. get '/api/v1/monitoring/health_check', params: {}, headers: @headers
  71. assert_response(401)
  72. result = JSON.parse(@response.body)
  73. assert_equal(Hash, result.class)
  74. assert_not(result['healthy'])
  75. assert_equal('Not authorized', result['error'])
  76. # status
  77. get '/api/v1/monitoring/status', params: {}, headers: @headers
  78. assert_response(401)
  79. result = JSON.parse(@response.body)
  80. assert_equal(Hash, result.class)
  81. assert_not(result['agents'])
  82. assert_not(result['last_login'])
  83. assert_not(result['counts'])
  84. assert_not(result['last_created_at'])
  85. assert_equal('Not authorized', result['error'])
  86. # token
  87. post '/api/v1/monitoring/token', params: {}, headers: @headers
  88. assert_response(401)
  89. result = JSON.parse(@response.body)
  90. assert_equal(Hash, result.class)
  91. assert_not(result['token'])
  92. assert_equal('authentication failed', result['error'])
  93. end
  94. test '02 monitoring with wrong token' do
  95. # health_check
  96. get '/api/v1/monitoring/health_check?token=abc', params: {}, headers: @headers
  97. assert_response(401)
  98. result = JSON.parse(@response.body)
  99. assert_equal(Hash, result.class)
  100. assert_not(result['healthy'])
  101. assert_equal('Not authorized', result['error'])
  102. # status
  103. get '/api/v1/monitoring/status?token=abc', params: {}, headers: @headers
  104. assert_response(401)
  105. result = JSON.parse(@response.body)
  106. assert_equal(Hash, result.class)
  107. assert_not(result['agents'])
  108. assert_not(result['last_login'])
  109. assert_not(result['counts'])
  110. assert_not(result['last_created_at'])
  111. assert_equal('Not authorized', result['error'])
  112. # token
  113. post '/api/v1/monitoring/token', params: { token: 'abc' }.to_json, headers: @headers
  114. assert_response(401)
  115. result = JSON.parse(@response.body)
  116. assert_equal(Hash, result.class)
  117. assert_not(result['token'])
  118. assert_equal('authentication failed', result['error'])
  119. end
  120. test '03 monitoring with correct token' do
  121. # health_check
  122. get "/api/v1/monitoring/health_check?token=#{@token}", params: {}, headers: @headers
  123. assert_response(200)
  124. result = JSON.parse(@response.body)
  125. assert_equal(Hash, result.class)
  126. assert_not(result['error'])
  127. assert_equal(true, result['healthy'])
  128. assert_equal('success', result['message'])
  129. # status
  130. get "/api/v1/monitoring/status?token=#{@token}", params: {}, headers: @headers
  131. assert_response(200)
  132. result = JSON.parse(@response.body)
  133. assert_equal(Hash, result.class)
  134. assert_not(result['error'])
  135. assert(result.key?('agents'))
  136. assert(result.key?('last_login'))
  137. assert(result.key?('counts'))
  138. assert(result.key?('last_created_at'))
  139. # token
  140. post '/api/v1/monitoring/token', params: { token: @token }.to_json, headers: @headers
  141. assert_response(401)
  142. result = JSON.parse(@response.body)
  143. assert_equal(Hash, result.class)
  144. assert_not(result['token'])
  145. assert_equal('authentication failed', result['error'])
  146. end
  147. test '04 monitoring with admin user' do
  148. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('monitoring-admin@example.com', 'adminpw')
  149. # health_check
  150. get '/api/v1/monitoring/health_check', params: {}, headers: @headers.merge('Authorization' => credentials)
  151. assert_response(200)
  152. result = JSON.parse(@response.body)
  153. assert_equal(Hash, result.class)
  154. assert_not(result['error'])
  155. assert_equal(true, result['healthy'])
  156. assert_equal('success', result['message'])
  157. # status
  158. get '/api/v1/monitoring/status', params: {}, headers: @headers.merge('Authorization' => credentials)
  159. assert_response(200)
  160. result = JSON.parse(@response.body)
  161. assert_equal(Hash, result.class)
  162. assert_not(result['error'])
  163. assert(result.key?('agents'))
  164. assert(result.key?('last_login'))
  165. assert(result.key?('counts'))
  166. assert(result.key?('last_created_at'))
  167. # token
  168. post '/api/v1/monitoring/token', params: { token: @token }.to_json, headers: @headers.merge('Authorization' => credentials)
  169. assert_response(201)
  170. result = JSON.parse(@response.body)
  171. assert_equal(Hash, result.class)
  172. assert(result['token'])
  173. @token = result['token']
  174. assert_not(result['error'])
  175. end
  176. test '05 monitoring with agent user' do
  177. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('monitoring-agent@example.com', 'agentpw')
  178. # health_check
  179. get '/api/v1/monitoring/health_check', params: {}, headers: @headers.merge('Authorization' => credentials)
  180. assert_response(401)
  181. result = JSON.parse(@response.body)
  182. assert_equal(Hash, result.class)
  183. assert_not(result['healthy'])
  184. assert_equal('Not authorized (user)!', result['error'])
  185. # status
  186. get '/api/v1/monitoring/status', params: {}, headers: @headers.merge('Authorization' => credentials)
  187. assert_response(401)
  188. result = JSON.parse(@response.body)
  189. assert_equal(Hash, result.class)
  190. assert_not(result['agents'])
  191. assert_not(result['last_login'])
  192. assert_not(result['counts'])
  193. assert_not(result['last_created_at'])
  194. assert_equal('Not authorized (user)!', result['error'])
  195. # token
  196. post '/api/v1/monitoring/token', params: { token: @token }.to_json, headers: @headers.merge('Authorization' => credentials)
  197. assert_response(401)
  198. result = JSON.parse(@response.body)
  199. assert_equal(Hash, result.class)
  200. assert_not(result['token'])
  201. assert_equal('Not authorized (user)!', result['error'])
  202. end
  203. test '06 monitoring with admin user and invalid permission' do
  204. permission = Permission.find_by(name: 'admin.monitoring')
  205. permission.active = false
  206. permission.save!
  207. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('monitoring-admin@example.com', 'adminpw')
  208. # health_check
  209. get '/api/v1/monitoring/health_check', params: {}, headers: @headers.merge('Authorization' => credentials)
  210. assert_response(401)
  211. result = JSON.parse(@response.body)
  212. assert_equal(Hash, result.class)
  213. assert_not(result['healthy'])
  214. assert_equal('Not authorized (user)!', result['error'])
  215. # status
  216. get '/api/v1/monitoring/status', params: {}, headers: @headers.merge('Authorization' => credentials)
  217. assert_response(401)
  218. result = JSON.parse(@response.body)
  219. assert_equal(Hash, result.class)
  220. assert_not(result['agents'])
  221. assert_not(result['last_login'])
  222. assert_not(result['counts'])
  223. assert_not(result['last_created_at'])
  224. assert_equal('Not authorized (user)!', result['error'])
  225. # token
  226. post '/api/v1/monitoring/token', params: { token: @token }.to_json, headers: @headers.merge('Authorization' => credentials)
  227. assert_response(401)
  228. result = JSON.parse(@response.body)
  229. assert_equal(Hash, result.class)
  230. assert_not(result['token'])
  231. assert_equal('Not authorized (user)!', result['error'])
  232. permission.active = true
  233. permission.save!
  234. end
  235. test '07 monitoring with correct token and invalid permission' do
  236. permission = Permission.find_by(name: 'admin.monitoring')
  237. permission.active = false
  238. permission.save!
  239. # health_check
  240. get "/api/v1/monitoring/health_check?token=#{@token}", params: {}, headers: @headers
  241. assert_response(200)
  242. result = JSON.parse(@response.body)
  243. assert_equal(Hash, result.class)
  244. assert_not(result['error'])
  245. assert_equal(true, result['healthy'])
  246. assert_equal('success', result['message'])
  247. # status
  248. get "/api/v1/monitoring/status?token=#{@token}", params: {}, headers: @headers
  249. assert_response(200)
  250. result = JSON.parse(@response.body)
  251. assert_equal(Hash, result.class)
  252. assert_not(result['error'])
  253. assert(result.key?('agents'))
  254. assert(result.key?('last_login'))
  255. assert(result.key?('counts'))
  256. assert(result.key?('last_created_at'))
  257. # token
  258. post '/api/v1/monitoring/token', params: { token: @token }.to_json, headers: @headers
  259. assert_response(401)
  260. result = JSON.parse(@response.body)
  261. assert_equal(Hash, result.class)
  262. assert_not(result['token'])
  263. assert_equal('authentication failed', result['error'])
  264. permission.active = true
  265. permission.save!
  266. end
  267. test '08 check health false' do
  268. channel = Channel.find_by(active: true)
  269. channel.status_in = 'ok'
  270. channel.status_out = 'error'
  271. channel.last_log_in = nil
  272. channel.last_log_out = nil
  273. channel.save!
  274. # health_check
  275. get "/api/v1/monitoring/health_check?token=#{@token}", params: {}, headers: @headers
  276. assert_response(200)
  277. result = JSON.parse(@response.body)
  278. assert_equal(Hash, result.class)
  279. assert(result['message'])
  280. assert(result['issues'])
  281. assert_equal(false, result['healthy'])
  282. assert_equal('Channel: Email::Notification out ', result['message'])
  283. scheduler = Scheduler.where(active: true).last
  284. scheduler.last_run = Time.zone.now - 1.day
  285. scheduler.period = 600
  286. scheduler.save!
  287. # health_check
  288. get "/api/v1/monitoring/health_check?token=#{@token}", params: {}, headers: @headers
  289. assert_response(200)
  290. result = JSON.parse(@response.body)
  291. assert_equal(Hash, result.class)
  292. assert(result['message'])
  293. assert(result['issues'])
  294. assert_equal(false, result['healthy'])
  295. assert_equal('Channel: Email::Notification out ;scheduler not running', result['message'])
  296. dir = Rails.root.join('tmp', 'unprocessable_mail')
  297. FileUtils.mkdir_p(dir)
  298. FileUtils.touch("#{dir}/test.eml")
  299. # health_check
  300. get "/api/v1/monitoring/health_check?token=#{@token}", params: {}, headers: @headers
  301. assert_response(200)
  302. result = JSON.parse(@response.body)
  303. assert_equal(Hash, result.class)
  304. assert(result['message'])
  305. assert(result['issues'])
  306. assert_equal(false, result['healthy'])
  307. assert_equal('Channel: Email::Notification out ;unprocessable mails: 1;scheduler not running', result['message'])
  308. Setting.set('ldap_integration', true)
  309. ImportJob.create(
  310. name: 'Import::Ldap',
  311. started_at: Time.zone.now,
  312. finished_at: Time.zone.now,
  313. result: {
  314. error: 'Some bad error'
  315. }
  316. )
  317. # health_check
  318. get "/api/v1/monitoring/health_check?token=#{@token}", params: {}, headers: @headers
  319. assert_response(200)
  320. result = JSON.parse(@response.body)
  321. assert_equal(Hash, result.class)
  322. assert(result['message'])
  323. assert(result['issues'])
  324. assert_equal(false, result['healthy'])
  325. assert_equal("Channel: Email::Notification out ;unprocessable mails: 1;scheduler not running;Failed to run import backend 'Import::Ldap'. Cause: Some bad error", result['message'])
  326. stuck_updated_at_timestamp = 15.minutes.ago
  327. ImportJob.create(
  328. name: 'Import::Ldap',
  329. started_at: Time.zone.now,
  330. finished_at: nil,
  331. updated_at: stuck_updated_at_timestamp,
  332. )
  333. # health_check
  334. get "/api/v1/monitoring/health_check?token=#{@token}", params: {}, headers: @headers
  335. assert_response(200)
  336. result = JSON.parse(@response.body)
  337. assert_equal(Hash, result.class)
  338. assert(result['message'])
  339. assert(result['issues'])
  340. assert_equal(false, result['healthy'])
  341. assert_equal("Channel: Email::Notification out ;unprocessable mails: 1;scheduler not running;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'])
  342. Setting.set('ldap_integration', false)
  343. end
  344. test '09 check restart_failed_jobs' do
  345. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('monitoring-admin@example.com', 'adminpw')
  346. post '/api/v1/monitoring/restart_failed_jobs', params: {}, headers: @headers.merge('Authorization' => credentials)
  347. assert_response(200)
  348. end
  349. test '10 check failed delayed job' do
  350. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('monitoring-admin@example.com', 'adminpw')
  351. # disable elasticsearch
  352. prev_es_config = Setting.get('es_url')
  353. Setting.set('es_url', 'http://127.0.0.1:92001')
  354. # add a new object
  355. object = ObjectManager::Attribute.add(
  356. name: 'test3',
  357. object: 'Ticket',
  358. display: 'Test 3',
  359. active: true,
  360. data_type: 'input',
  361. data_option: {
  362. default: 'test',
  363. type: 'text',
  364. maxlength: 120,
  365. null: true
  366. },
  367. screens: {
  368. create_middle: {
  369. 'ticket.customer' => {
  370. shown: true,
  371. item_class: 'column'
  372. },
  373. 'ticket.agent' => {
  374. shown: true,
  375. item_class: 'column'
  376. }
  377. },
  378. edit: {
  379. 'ticket.customer' => {
  380. shown: true
  381. },
  382. 'ticket.agent' => {
  383. shown: true
  384. }
  385. }
  386. },
  387. position: 1550,
  388. editable: true
  389. )
  390. migration = ObjectManager::Attribute.migration_execute
  391. assert_equal(migration, true)
  392. post "/api/v1/object_manager_attributes/#{object.id}", params: {}, headers: @headers
  393. token = @response.headers['CSRF-TOKEN']
  394. # parameters for updating
  395. params = {
  396. 'name': 'test4',
  397. 'object': 'Ticket',
  398. 'display': 'Test 4',
  399. 'active': true,
  400. 'data_type': 'input',
  401. 'data_option': {
  402. 'default': 'test',
  403. 'type': 'text',
  404. 'maxlength': 120
  405. },
  406. 'screens': {
  407. 'create_middle': {
  408. 'ticket.customer': {
  409. 'shown': true,
  410. 'item_class': 'column'
  411. },
  412. 'ticket.agent': {
  413. 'shown': true,
  414. 'item_class': 'column'
  415. }
  416. },
  417. 'edit': {
  418. 'ticket.customer': {
  419. 'shown': true
  420. },
  421. 'ticket.agent': {
  422. 'shown': true
  423. }
  424. }
  425. },
  426. 'id': 'c-196'
  427. }
  428. # update the object
  429. put "/api/v1/object_manager_attributes/#{object.id}", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  430. migration = ObjectManager::Attribute.migration_execute
  431. assert_equal(migration, true)
  432. assert_response(200)
  433. result = JSON.parse(@response.body)
  434. assert(result)
  435. assert(result['data_option']['null'])
  436. assert_equal(result['name'], 'test4')
  437. assert_equal(result['display'], 'Test 4')
  438. jobs = Delayed::Job.all
  439. 4.times do
  440. jobs.each do |job|
  441. Delayed::Worker.new.run(job)
  442. end
  443. end
  444. # health_check
  445. get "/api/v1/monitoring/health_check?token=#{@token}", params: {}, headers: @headers
  446. assert_response(200)
  447. result = JSON.parse(@response.body)
  448. assert_equal(Hash, result.class)
  449. assert(result['message'])
  450. assert(result['issues'])
  451. assert_equal(false, result['healthy'])
  452. assert_equal("Failed to run background job #1 'BackgroundJobSearchIndex' 1 time(s) with 4 attempt(s).", result['message'])
  453. # add another job
  454. manual_added = Delayed::Job.enqueue( BackgroundJobSearchIndex.new('Ticket', 1))
  455. manual_added.update!(attempts: 10)
  456. # health_check
  457. get "/api/v1/monitoring/health_check?token=#{@token}", params: {}, headers: @headers
  458. assert_response(200)
  459. result = JSON.parse(@response.body)
  460. assert_equal(Hash, result.class)
  461. assert(result['message'])
  462. assert(result['issues'])
  463. assert_equal(false, result['healthy'])
  464. assert_equal("Failed to run background job #1 'BackgroundJobSearchIndex' 2 time(s) with 14 attempt(s).", result['message'])
  465. # add another job
  466. dummy_class = Class.new do
  467. def perform
  468. puts 'work work'
  469. end
  470. end
  471. manual_added = Delayed::Job.enqueue( dummy_class.new )
  472. manual_added.update!(attempts: 5)
  473. # health_check
  474. get "/api/v1/monitoring/health_check?token=#{@token}", params: {}, headers: @headers
  475. assert_response(200)
  476. result = JSON.parse(@response.body)
  477. assert_equal(Hash, result.class)
  478. assert(result['message'])
  479. assert(result['issues'])
  480. assert_equal(false, result['healthy'])
  481. 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'])
  482. # reset settings
  483. Setting.set('es_url', prev_es_config)
  484. # add some more failing job
  485. 10.times do
  486. manual_added = Delayed::Job.enqueue( dummy_class.new )
  487. manual_added.update!(attempts: 5)
  488. end
  489. # health_check
  490. get "/api/v1/monitoring/health_check?token=#{@token}", params: {}, headers: @headers
  491. assert_response(200)
  492. result = JSON.parse(@response.body)
  493. assert_equal(Hash, result.class)
  494. assert(result['message'])
  495. assert(result['issues'])
  496. assert_equal(false, result['healthy'])
  497. assert_equal("13 failing background jobs.;Failed to run background job #1 'BackgroundJobSearchIndex' 2 time(s) with 14 attempt(s).;Failed to run background job #2 'Object' 8 time(s) with 40 attempt(s).", result['message'])
  498. # cleanup
  499. Delayed::Job.delete_all
  500. end
  501. end