import { resolve } from 'path' ; import Url from 'url' ; import { withProcRunner } from '@kbn/dev-utils' ; import { FtrProviderContext } from './ftr_provider_context' ; import { AgentManager } from './agent' ; import { FleetManager } from './fleet_server' ; async function withFleetAgent ( { getService } , runner ) { const log = getService ( 'log' ) ; const config = getService ( 'config' ) ; const esHost = Url . format ( config . get ( 'servers.elasticsearch' ) ) ; const esConfig = { user : config . get ( 'servers.elasticsearch.username' ) , password : config . get ( 'servers.elasticsearch.password' ) , esHost , port : config . get ( 'servers.elasticsearch.port' ) , } ; const fleetManager = new FleetManager ( esConfig , log ) ; const agentManager = new AgentManager ( { ... esConfig , kibanaUrl : Url . format ( { protocol : config . get ( 'servers.kibana.protocol' ) , hostname : config . get ( 'servers.kibana.hostname' ) , port : config . get ( 'servers.kibana.port' ) , } ) , } , log ) ; process . on ( 'uncaughtException' , ( err ) => { console . error ( 'Encountered error; exiting after cleanup.' , err ) ; process . exit ( 1 ) ; } ) ; await agentManager . setup ( ) ; await fleetManager . setup ( ) ; try { await runner ( { } ) ; } finally { fleetManager . cleanup ( ) ; agentManager . cleanup ( ) ; } } export async function OsqueryCypressCliTestRunner ( context ) { await startOsqueryCypress ( context , 'run' ) ; } export async function OsqueryCypressVisualTestRunner ( context ) { await startOsqueryCypress ( context , 'open' ) ; } function startOsqueryCypress ( context , cypressCommand ) { const log = context . getService ( 'log' ) ; const config = context . getService ( 'config' ) ; return withFleetAgent ( context , ( runnerEnv ) => withProcRunner ( log , async ( procs ) => { await procs . run ( 'cypress' , { cmd : 'yarn' , args : [ `cypress:${ cypressCommand }` ] , cwd : resolve ( __dirname , '../../plugins/osquery' ) , env : { FORCE_COLOR : '1' , CYPRESS_baseUrl : Url . format ( { protocol : config . get ( 'servers.kibana.protocol' ) , hostname : config . get ( 'servers.kibana.hostname' ) , port : config . get ( 'servers.kibana.port' ) , } ) , CYPRESS_protocol : config . get ( 'servers.kibana.protocol' ) , CYPRESS_hostname : config . get ( 'servers.kibana.hostname' ) , CYPRESS_configport : config . get ( 'servers.kibana.port' ) , CYPRESS_ELASTICSEARCH_URL : Url . format ( config . get ( 'servers.elasticsearch' ) ) , CYPRESS_ELASTICSEARCH_USERNAME : config . get ( 'servers.elasticsearch.username' ) , CYPRESS_ELASTICSEARCH_PASSWORD : config . get ( 'servers.elasticsearch.password' ) , CYPRESS_KIBANA_URL : Url . format ( { protocol : config . get ( 'servers.kibana.protocol' ) , hostname : config . get ( 'servers.kibana.hostname' ) , port : config . get ( 'servers.kibana.port' ) , } ) , ... runnerEnv , ... process . env , } , wait : true , } ) ; } ) ) ; }