import expect from '@kbn/expect' ; import { Spaces } from '../../scenarios' ; import { getUrlPrefix , getTestAlertData , ObjectRemover , getEventLog } from '../../../common/lib' ; import { FtrProviderContext } from '../../../common/ftr_provider_context' ; import { IValidatedEvent } from '../../../../../plugins/event_log/server' ; export default function eventLogAlertTests ( { getService } ) { const supertest = getService ( 'supertest' ) ; const retry = getService ( 'retry' ) ; describe ( 'eventLog alerts' , ( ) => { const objectRemover = new ObjectRemover ( supertest ) ; after ( ( ) => objectRemover . removeAll ( ) ) ; it ( 'should generate expected alert events for normal operation' , async ( ) => { const pattern = { instance : [ false , true , true , false , false , true , true , true ] , } ; const response = await supertest . post ( `${ getUrlPrefix ( Spaces . space1 . id ) }/api/alerting/rule` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( getTestAlertData ( { rule_type_id : 'test.patternFiring' , schedule : { interval : '1s' } , throttle : null , params : { pattern , } , actions : [ ] , } ) ) ; expect ( response . status ) . to . eql ( 200 ) ; const ruleId = response . body . id ; objectRemover . add ( Spaces . space1 . id , ruleId , 'rule' , 'alerting' ) ; const events = await retry . try ( async ( ) => { return await getEventLog ( { getService , spaceId : Spaces . space1 . id , type : 'alert' , id : ruleId , provider : 'alerting' , actions : new Map ( [ [ 'execute' , { gte : 9 } ] , [ 'new-instance' , { equal : 2 } ] , [ 'active-instance' , { gte : 4 } ] , [ 'recovered-instance' , { equal : 2 } ] , ] ) , } ) ; } ) ; const instanceEvents = events . filter ( ( event ) => event ?. event ?. action !== 'execute' ) ; const currentAlertSpan = { } ; for ( let i = 0 ; i < instanceEvents . length ; ++ i ) { switch ( instanceEvents [ i ] ?. event ?. action ) { case 'new-instance' : expect ( instanceEvents [ i ] ?. kibana ?. alerting ?. instance_id ) . to . equal ( 'instance' ) ; expect ( instanceEvents [ i ] ?. event ?. end ) . to . be ( undefined ) ; currentAlertSpan . alertId = instanceEvents [ i ] ?. kibana ?. alerting ?. instance_id ; currentAlertSpan . start = instanceEvents [ i ] ?. event ?. start ; currentAlertSpan . durationToDate = instanceEvents [ i ] ?. event ?. duration ; break ; case 'active-instance' : expect ( instanceEvents [ i ] ?. kibana ?. alerting ?. instance_id ) . to . equal ( 'instance' ) ; expect ( instanceEvents [ i ] ?. event ?. start ) . to . equal ( currentAlertSpan . start ) ; expect ( instanceEvents [ i ] ?. event ?. end ) . to . be ( undefined ) ; if ( instanceEvents [ i ] ?. event ?. duration ! !== 0 ) { expect ( instanceEvents [ i ] ?. event ?. duration ! > currentAlertSpan . durationToDate ! ) . to . be ( true ) ; } currentAlertSpan . durationToDate = instanceEvents [ i ] ?. event ?. duration ; break ; case 'recovered-instance' : expect ( instanceEvents [ i ] ?. kibana ?. alerting ?. instance_id ) . to . equal ( 'instance' ) ; expect ( instanceEvents [ i ] ?. event ?. start ) . to . equal ( currentAlertSpan . start ) ; expect ( instanceEvents [ i ] ?. event ?. end ) . not . to . be ( undefined ) ; expect ( new Date ( instanceEvents [ i ] ?. event ?. end ! ) . valueOf ( ) - new Date ( instanceEvents [ i ] ?. event ?. start ! ) . valueOf ( ) ) . to . equal ( instanceEvents [ i ] ?. event ?. duration ! / 1000 / 1000 ) ; break ; } } } ) ; } ) ; }