2018-12-05 10:43:53 +00:00
require 'rails_helper'
RSpec . describe 'Monitoring' , type : :request do
2020-06-19 09:17:18 +00:00
let! ( :admin ) do
create ( :admin , groups : Group . all )
2018-12-05 10:43:53 +00:00
end
2020-06-19 09:17:18 +00:00
let! ( :agent ) do
create ( :agent , groups : Group . all )
2018-12-05 10:43:53 +00:00
end
2020-06-19 09:17:18 +00:00
let! ( :customer ) do
create ( :customer )
2018-12-05 10:43:53 +00:00
end
let! ( :token ) do
SecureRandom . urlsafe_base64 ( 64 )
end
2019-04-15 01:41:17 +00:00
before do
2018-12-05 10:43:53 +00:00
Setting . set ( 'monitoring_token' , token )
# channel cleanup
Channel . where . not ( area : 'Email::Notification' ) . destroy_all
Channel . all . each do | channel |
channel . status_in = 'ok'
channel . status_out = 'ok'
channel . last_log_in = nil
channel . last_log_out = nil
channel . save!
end
2020-02-18 19:51:31 +00:00
dir = Rails . root . join ( 'tmp/unprocessable_mail' )
2018-12-05 10:43:53 +00:00
Dir . glob ( " #{ dir } /*.eml " ) do | entry |
File . delete ( entry )
end
Scheduler . where ( active : true ) . each do | scheduler |
scheduler . last_run = Time . zone . now
scheduler . save!
end
permission = Permission . find_by ( name : 'admin.monitoring' )
permission . active = true
permission . save!
end
describe 'request handling' do
it 'does monitoring without token' do
# health_check
get '/api/v1/monitoring/health_check' , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'healthy' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'Not authorized' )
# status
get '/api/v1/monitoring/status' , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'agents' ] ) . to be_falsey
expect ( json_response [ 'last_login' ] ) . to be_falsey
expect ( json_response [ 'counts' ] ) . to be_falsey
expect ( json_response [ 'last_created_at' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'Not authorized' )
# token
post '/api/v1/monitoring/token' , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'token' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'authentication failed' )
end
it 'does monitoring with wrong token' do
# health_check
get '/api/v1/monitoring/health_check?token=abc' , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'healthy' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'Not authorized' )
# status
get '/api/v1/monitoring/status?token=abc' , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'agents' ] ) . to be_falsey
expect ( json_response [ 'last_login' ] ) . to be_falsey
expect ( json_response [ 'counts' ] ) . to be_falsey
expect ( json_response [ 'last_created_at' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'Not authorized' )
# token
post '/api/v1/monitoring/token' , params : { token : 'abc' } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'token' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'authentication failed' )
end
it 'does monitoring with correct token' do
# test storage usage
string = ''
2018-12-06 14:35:00 +00:00
1000 . times do
2018-12-05 10:43:53 +00:00
string += 'Some Text Some Text Some Text Some Text Some Text Some Text Some Text Some Text'
end
2018-12-06 14:35:00 +00:00
2018-12-05 10:43:53 +00:00
Store . add (
2018-12-19 17:31:51 +00:00
object : 'User' ,
o_id : 1 ,
data : string ,
filename : 'filename.txt' ,
2018-12-05 10:43:53 +00:00
created_by_id : 1 ,
)
# health_check
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'error' ] ) . to be_falsey
expect ( json_response [ 'healthy' ] ) . to eq ( true )
expect ( json_response [ 'message' ] ) . to eq ( 'success' )
# status
get " /api/v1/monitoring/status?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'error' ] ) . to be_falsey
2019-04-15 01:41:17 +00:00
expect ( json_response ) . to be_key ( 'agents' )
expect ( json_response ) . to be_key ( 'last_login' )
expect ( json_response ) . to be_key ( 'counts' )
expect ( json_response ) . to be_key ( 'last_created_at' )
2018-12-05 10:43:53 +00:00
2018-12-06 14:35:00 +00:00
first_json_response_kb = 0
if ActiveRecord :: Base . connection_config [ :adapter ] == 'postgresql'
expect ( json_response [ 'storage' ] ) . to be_truthy
2019-04-15 01:41:17 +00:00
expect ( json_response [ 'storage' ] ) . to be_key ( 'kB' )
2018-12-06 14:35:00 +00:00
expect ( json_response [ 'storage' ] [ 'kB' ] ) . to be > 0
2019-04-15 01:41:17 +00:00
expect ( json_response [ 'storage' ] ) . to be_key ( 'MB' )
expect ( json_response [ 'storage' ] ) . to be_key ( 'GB' )
2018-12-06 14:35:00 +00:00
first_json_response_kb = json_response [ 'storage' ] [ 'kB' ]
else
expect ( json_response [ 'storage' ] ) . to be_falsey
end
# save same file again
Store . add (
2018-12-19 17:31:51 +00:00
object : 'User' ,
o_id : 1 ,
data : string ,
filename : 'filename.txt' ,
2018-12-06 14:35:00 +00:00
created_by_id : 1 ,
)
# status
get " /api/v1/monitoring/status?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-06 14:35:00 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'error' ] ) . to be_falsey
2019-04-15 01:41:17 +00:00
expect ( json_response ) . to be_key ( 'agents' )
expect ( json_response ) . to be_key ( 'last_login' )
expect ( json_response ) . to be_key ( 'counts' )
expect ( json_response ) . to be_key ( 'last_created_at' )
2018-12-06 14:35:00 +00:00
2018-12-05 10:43:53 +00:00
if ActiveRecord :: Base . connection_config [ :adapter ] == 'postgresql'
expect ( json_response [ 'storage' ] ) . to be_truthy
2019-04-15 01:41:17 +00:00
expect ( json_response [ 'storage' ] ) . to be_key ( 'kB' )
2018-12-06 14:35:00 +00:00
# check if the stores got summarized. value should be the same because the file has the same fingerprint
expect ( json_response [ 'storage' ] [ 'kB' ] ) . to eq ( first_json_response_kb )
2019-04-15 01:41:17 +00:00
expect ( json_response [ 'storage' ] ) . to be_key ( 'MB' )
expect ( json_response [ 'storage' ] ) . to be_key ( 'GB' )
2018-12-06 14:35:00 +00:00
else
expect ( json_response [ 'storage' ] ) . to be_falsey
end
Store . add (
2018-12-19 17:31:51 +00:00
object : 'User' ,
o_id : 1 ,
data : string + '123' ,
filename : 'filename2.txt' ,
2018-12-06 14:35:00 +00:00
created_by_id : 1 ,
)
# status
get " /api/v1/monitoring/status?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-06 14:35:00 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'error' ] ) . to be_falsey
2019-04-15 01:41:17 +00:00
expect ( json_response ) . to be_key ( 'agents' )
expect ( json_response ) . to be_key ( 'last_login' )
expect ( json_response ) . to be_key ( 'counts' )
expect ( json_response ) . to be_key ( 'last_created_at' )
2018-12-06 14:35:00 +00:00
if ActiveRecord :: Base . connection_config [ :adapter ] == 'postgresql'
expect ( json_response [ 'storage' ] ) . to be_truthy
2019-04-15 01:41:17 +00:00
expect ( json_response [ 'storage' ] ) . to be_key ( 'kB' )
2018-12-06 14:35:00 +00:00
# check if the stores got summarized. value should be greather than the size of just one file (saved 2 times)
expect ( json_response [ 'storage' ] [ 'kB' ] ) . to be > first_json_response_kb
2019-04-15 01:41:17 +00:00
expect ( json_response [ 'storage' ] ) . to be_key ( 'MB' )
expect ( json_response [ 'storage' ] ) . to be_key ( 'GB' )
2018-12-05 10:43:53 +00:00
else
expect ( json_response [ 'storage' ] ) . to be_falsey
end
# token
post '/api/v1/monitoring/token' , params : { token : token } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'token' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'authentication failed' )
end
it 'does monitoring with admin user' do
# health_check
2020-06-19 09:17:18 +00:00
authenticated_as ( admin )
2018-12-05 10:43:53 +00:00
get '/api/v1/monitoring/health_check' , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'error' ] ) . to be_falsey
expect ( json_response [ 'healthy' ] ) . to eq ( true )
expect ( json_response [ 'message' ] ) . to eq ( 'success' )
# status
get '/api/v1/monitoring/status' , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'error' ] ) . to be_falsey
2019-04-15 01:41:17 +00:00
expect ( json_response ) . to be_key ( 'agents' )
expect ( json_response ) . to be_key ( 'last_login' )
expect ( json_response ) . to be_key ( 'counts' )
expect ( json_response ) . to be_key ( 'last_created_at' )
2018-12-05 10:43:53 +00:00
# token
post '/api/v1/monitoring/token' , params : { token : token } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :created )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'token' ] ) . to be_truthy
expect ( json_response [ 'error' ] ) . to be_falsey
end
it 'does monitoring with agent user' do
# health_check
2020-06-19 09:17:18 +00:00
authenticated_as ( agent )
2018-12-05 10:43:53 +00:00
get '/api/v1/monitoring/health_check' , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'healthy' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'Not authorized (user)!' )
# status
get '/api/v1/monitoring/status' , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'agents' ] ) . to be_falsey
expect ( json_response [ 'last_login' ] ) . to be_falsey
expect ( json_response [ 'counts' ] ) . to be_falsey
expect ( json_response [ 'last_created_at' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'Not authorized (user)!' )
# token
post '/api/v1/monitoring/token' , params : { token : token } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'token' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'Not authorized (user)!' )
end
it 'does monitoring with admin user and invalid permission' do
permission = Permission . find_by ( name : 'admin.monitoring' )
permission . active = false
permission . save!
# health_check
2020-06-19 09:17:18 +00:00
authenticated_as ( admin )
2018-12-05 10:43:53 +00:00
get '/api/v1/monitoring/health_check' , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'healthy' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'Not authorized (user)!' )
# status
get '/api/v1/monitoring/status' , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'agents' ] ) . to be_falsey
expect ( json_response [ 'last_login' ] ) . to be_falsey
expect ( json_response [ 'counts' ] ) . to be_falsey
expect ( json_response [ 'last_created_at' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'Not authorized (user)!' )
# token
post '/api/v1/monitoring/token' , params : { token : token } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'token' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'Not authorized (user)!' )
permission . active = true
permission . save!
end
it 'does monitoring with correct token and invalid permission' do
permission = Permission . find_by ( name : 'admin.monitoring' )
permission . active = false
permission . save!
# health_check
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'error' ] ) . to be_falsey
expect ( json_response [ 'healthy' ] ) . to eq ( true )
expect ( json_response [ 'message' ] ) . to eq ( 'success' )
# status
get " /api/v1/monitoring/status?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'error' ] ) . to be_falsey
2019-04-15 01:41:17 +00:00
expect ( json_response ) . to be_key ( 'agents' )
expect ( json_response ) . to be_key ( 'last_login' )
expect ( json_response ) . to be_key ( 'counts' )
expect ( json_response ) . to be_key ( 'last_created_at' )
2018-12-05 10:43:53 +00:00
# token
post '/api/v1/monitoring/token' , params : { token : token } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unauthorized )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'token' ] ) . to be_falsey
expect ( json_response [ 'error' ] ) . to eq ( 'authentication failed' )
permission . active = true
permission . save!
end
it 'does check health false' do
channel = Channel . find_by ( active : true )
channel . status_in = 'ok'
channel . status_out = 'error'
channel . last_log_in = nil
channel . last_log_out = nil
channel . save!
# health_check - channel
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'message' ] ) . to be_truthy
expect ( json_response [ 'issues' ] ) . to be_truthy
expect ( json_response [ 'healthy' ] ) . to eq ( false )
expect ( json_response [ 'message' ] ) . to eq ( 'Channel: Email::Notification out ' )
# health_check - scheduler may not run
scheduler = Scheduler . where ( active : true ) . last
scheduler . last_run = Time . zone . now - 20 . minutes
scheduler . period = 600
scheduler . save!
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'message' ] ) . to be_truthy
expect ( json_response [ 'issues' ] ) . to be_truthy
expect ( json_response [ 'healthy' ] ) . to eq ( false )
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 " )
# health_check - scheduler may not run
scheduler = Scheduler . where ( active : true ) . last
scheduler . last_run = Time . zone . now - 1 . day
scheduler . period = 600
scheduler . save!
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'message' ] ) . to be_truthy
expect ( json_response [ 'issues' ] ) . to be_truthy
expect ( json_response [ 'healthy' ] ) . to eq ( false )
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 " )
# health_check - scheduler job count
travel 2 . seconds
2019-11-13 07:03:47 +00:00
8001 . times do | fake_ticket_id |
SearchIndexJob . perform_later ( 'Ticket' , fake_ticket_id )
2018-12-05 10:43:53 +00:00
end
Scheduler . where ( active : true ) . each do | local_scheduler |
local_scheduler . last_run = Time . zone . now
local_scheduler . save!
end
total_jobs = Delayed :: Job . count
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'message' ] ) . to be_truthy
expect ( json_response [ 'issues' ] ) . to be_truthy
expect ( json_response [ 'healthy' ] ) . to eq ( false )
expect ( json_response [ 'message' ] ) . to eq ( 'Channel: Email::Notification out ' )
travel 20 . minutes
Scheduler . where ( active : true ) . each do | local_scheduler |
local_scheduler . last_run = Time . zone . now
local_scheduler . save!
end
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'message' ] ) . to be_truthy
expect ( json_response [ 'issues' ] ) . to be_truthy
expect ( json_response [ 'healthy' ] ) . to eq ( false )
expect ( json_response [ 'message' ] ) . to eq ( " Channel: Email::Notification out ; #{ total_jobs } background jobs in queue " )
Delayed :: Job . delete_all
travel_back
# health_check - unprocessable mail
2020-02-18 19:51:31 +00:00
dir = Rails . root . join ( 'tmp/unprocessable_mail' )
2018-12-05 10:43:53 +00:00
FileUtils . mkdir_p ( dir )
FileUtils . touch ( " #{ dir } /test.eml " )
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'message' ] ) . to be_truthy
expect ( json_response [ 'issues' ] ) . to be_truthy
expect ( json_response [ 'healthy' ] ) . to eq ( false )
expect ( json_response [ 'message' ] ) . to eq ( 'Channel: Email::Notification out ;unprocessable mails: 1' )
# health_check - ldap
Setting . set ( 'ldap_integration' , true )
ImportJob . create (
name : 'Import::Ldap' ,
started_at : Time . zone . now ,
finished_at : Time . zone . now ,
result : {
error : 'Some bad error'
}
)
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'message' ] ) . to be_truthy
expect ( json_response [ 'issues' ] ) . to be_truthy
expect ( json_response [ 'healthy' ] ) . to eq ( false )
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_updated_at_timestamp = 15 . minutes . ago
ImportJob . create (
name : 'Import::Ldap' ,
started_at : Time . zone . now ,
finished_at : nil ,
updated_at : stuck_updated_at_timestamp ,
)
# health_check
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'message' ] ) . to be_truthy
expect ( json_response [ 'issues' ] ) . to be_truthy
expect ( json_response [ 'healthy' ] ) . to eq ( false )
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 } " )
Setting . set ( 'ldap_integration' , false )
end
it 'does check restart_failed_jobs' do
2020-06-19 09:17:18 +00:00
authenticated_as ( admin )
2018-12-05 10:43:53 +00:00
post '/api/v1/monitoring/restart_failed_jobs' , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
end
it 'does check failed delayed job' , db_strategy : :reset do
# disable elasticsearch
prev_es_config = Setting . get ( 'es_url' )
Setting . set ( 'es_url' , 'http://127.0.0.1:92001' )
2019-12-20 15:47:00 +00:00
# delete all background jobs created while seeding
# to have a clean state for checking for failed ones
Delayed :: Job . destroy_all
2018-12-05 10:43:53 +00:00
# add a new object
2020-05-08 09:19:54 +00:00
object = create ( :object_manager_attribute_text , name : 'test4' )
2018-12-05 10:43:53 +00:00
migration = ObjectManager :: Attribute . migration_execute
expect ( true ) . to eq ( migration )
2020-06-19 09:17:18 +00:00
authenticated_as ( admin )
2018-12-05 10:43:53 +00:00
post " /api/v1/object_manager_attributes/ #{ object . id } " , params : { } , as : :json
token = @response . headers [ 'CSRF-TOKEN' ]
# parameters for updating
params = {
2018-12-19 17:31:51 +00:00
'name' : 'test4' ,
'object' : 'Ticket' ,
'display' : 'Test 4' ,
'active' : true ,
'data_type' : 'input' ,
2018-12-05 10:43:53 +00:00
'data_option' : {
2018-12-19 17:31:51 +00:00
'default' : 'test' ,
'type' : 'text' ,
2018-12-05 10:43:53 +00:00
'maxlength' : 120
} ,
2018-12-19 17:31:51 +00:00
'screens' : {
2018-12-05 10:43:53 +00:00
'create_middle' : {
'ticket.customer' : {
2018-12-19 17:31:51 +00:00
'shown' : true ,
2018-12-05 10:43:53 +00:00
'item_class' : 'column'
} ,
2018-12-19 17:31:51 +00:00
'ticket.agent' : {
'shown' : true ,
2018-12-05 10:43:53 +00:00
'item_class' : 'column'
}
} ,
2018-12-19 17:31:51 +00:00
'edit' : {
2018-12-05 10:43:53 +00:00
'ticket.customer' : {
'shown' : true
} ,
2018-12-19 17:31:51 +00:00
'ticket.agent' : {
2018-12-05 10:43:53 +00:00
'shown' : true
}
}
} ,
2018-12-19 17:31:51 +00:00
'id' : 'c-196'
2018-12-05 10:43:53 +00:00
}
# update the object
put " /api/v1/object_manager_attributes/ #{ object . id } " , params : params , as : :json
migration = ObjectManager :: Attribute . migration_execute
expect ( true ) . to eq ( migration )
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_truthy
expect ( json_response [ 'data_option' ] [ 'null' ] ) . to be_truthy
expect ( 'test4' ) . to eq ( json_response [ 'name' ] )
expect ( 'Test 4' ) . to eq ( json_response [ 'display' ] )
4 . times do
2019-11-13 07:03:47 +00:00
Delayed :: Worker . new . work_off
2018-12-05 10:43:53 +00:00
end
# health_check
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'message' ] ) . to be_truthy
expect ( json_response [ 'issues' ] ) . to be_truthy
expect ( json_response [ 'healthy' ] ) . to eq ( false )
2020-07-10 13:30:44 +00:00
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). " )
2018-12-05 10:43:53 +00:00
# add another job
2019-01-15 12:32:14 +00:00
manual_added = SearchIndexJob . perform_later ( 'Ticket' , 1 )
Delayed :: Job . find ( manual_added . provider_job_id ) . update! ( attempts : 10 )
2018-12-05 10:43:53 +00:00
# health_check
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'message' ] ) . to be_truthy
expect ( json_response [ 'issues' ] ) . to be_truthy
expect ( json_response [ 'healthy' ] ) . to eq ( false )
2020-07-10 13:30:44 +00:00
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). " )
2018-12-05 10:43:53 +00:00
# add another job
2019-11-13 07:03:47 +00:00
dummy_class = Class . new ( ApplicationJob ) do
2018-12-05 10:43:53 +00:00
def perform
puts 'work work'
end
end
manual_added = Delayed :: Job . enqueue ( dummy_class . new )
manual_added . update! ( attempts : 5 )
# health_check
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'message' ] ) . to be_truthy
expect ( json_response [ 'issues' ] ) . to be_truthy
expect ( json_response [ 'healthy' ] ) . to eq ( false )
2020-07-10 13:30:44 +00:00
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). " )
2018-12-05 10:43:53 +00:00
# reset settings
Setting . set ( 'es_url' , prev_es_config )
# add some more failing job
10 . times do
manual_added = Delayed :: Job . enqueue ( dummy_class . new )
manual_added . update! ( attempts : 5 )
end
# health_check
get " /api/v1/monitoring/health_check?token= #{ token } " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'message' ] ) . to be_truthy
expect ( json_response [ 'issues' ] ) . to be_truthy
expect ( json_response [ 'healthy' ] ) . to eq ( false )
2020-07-10 13:30:44 +00:00
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). " )
2018-12-05 10:43:53 +00:00
# cleanup
Delayed :: Job . delete_all
end
it 'does check amount' do
Ticket . destroy_all
# amount_check - ok
get " /api/v1/monitoring/amount_check?token= #{ token } &periode=1h " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
2019-01-21 07:01:51 +00:00
expect ( json_response . key? ( 'state' ) ) . to eq ( false )
expect ( json_response . key? ( 'message' ) ) . to eq ( false )
2018-12-05 10:43:53 +00:00
expect ( json_response [ 'count' ] ) . to eq ( 0 )
Ticket . destroy_all
( 1 .. 6 ) . each do | i |
create ( :ticket , title : " Ticket- #{ i } " )
travel 10 . seconds
end
get " /api/v1/monitoring/amount_check?token= #{ token } &periode=1h&min_warning=10&min_critical=8 " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'state' ] ) . to eq ( 'critical' )
expect ( json_response [ 'message' ] ) . to eq ( 'The minimum of 8 was undercut by 6 in the last 1h' )
expect ( json_response [ 'count' ] ) . to eq ( 6 )
get " /api/v1/monitoring/amount_check?token= #{ token } &periode=1h&min_warning=7&min_critical=2 " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'state' ] ) . to eq ( 'warning' )
expect ( json_response [ 'message' ] ) . to eq ( 'The minimum of 7 was undercut by 6 in the last 1h' )
expect ( json_response [ 'count' ] ) . to eq ( 6 )
get " /api/v1/monitoring/amount_check?token= #{ token } &periode=1h&max_warning=10&max_critical=20 " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'state' ] ) . to eq ( 'ok' )
2019-01-21 07:01:51 +00:00
expect ( json_response . key? ( 'message' ) ) . to eq ( false )
2018-12-05 10:43:53 +00:00
expect ( json_response [ 'count' ] ) . to eq ( 6 )
( 1 .. 6 ) . each do | i |
create ( :ticket , title : " Ticket- #{ i } " )
travel 1 . second
end
get " /api/v1/monitoring/amount_check?token= #{ token } &periode=1h&max_warning=10&max_critical=20 " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'state' ] ) . to eq ( 'warning' )
expect ( json_response [ 'message' ] ) . to eq ( 'The limit of 10 was exceeded with 12 in the last 1h' )
expect ( json_response [ 'count' ] ) . to eq ( 12 )
( 1 .. 10 ) . each do | i |
create ( :ticket , title : " Ticket- #{ i } " )
travel 1 . second
end
get " /api/v1/monitoring/amount_check?token= #{ token } &periode=1h&max_warning=10&max_critical=20 " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'state' ] ) . to eq ( 'critical' )
expect ( json_response [ 'message' ] ) . to eq ( 'The limit of 20 was exceeded with 22 in the last 1h' )
expect ( json_response [ 'count' ] ) . to eq ( 22 )
2019-01-21 07:01:51 +00:00
get " /api/v1/monitoring/amount_check?token= #{ token } &periode=1h&max_warning=30 " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'state' ] ) . to eq ( 'ok' )
2019-01-21 07:01:51 +00:00
expect ( json_response . key? ( 'message' ) ) . to eq ( false )
expect ( json_response [ 'count' ] ) . to eq ( 22 )
get " /api/v1/monitoring/amount_check?token= #{ token } &periode=1h " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2019-01-21 07:01:51 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response . key? ( 'state' ) ) . to eq ( false )
expect ( json_response . key? ( 'message' ) ) . to eq ( false )
2018-12-05 10:43:53 +00:00
expect ( json_response [ 'count' ] ) . to eq ( 22 )
travel 2 . hours
2019-01-21 07:01:51 +00:00
get " /api/v1/monitoring/amount_check?token= #{ token } &periode=1h&max_warning=30 " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'state' ] ) . to eq ( 'ok' )
2019-01-21 07:01:51 +00:00
expect ( json_response . key? ( 'message' ) ) . to eq ( false )
expect ( json_response [ 'count' ] ) . to eq ( 0 )
get " /api/v1/monitoring/amount_check?token= #{ token } &periode=1h " , params : { } , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2019-01-21 07:01:51 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response . key? ( 'state' ) ) . to eq ( false )
expect ( json_response . key? ( 'message' ) ) . to eq ( false )
2018-12-05 10:43:53 +00:00
expect ( json_response [ 'count' ] ) . to eq ( 0 )
end
end
end