77import uuid from 'uuid' ;
88import expect from '@kbn/expect' ;
99import { FtrProviderContext } from '../../ftr_provider_context' ;
10+ import { getSupertestWithoutAuth } from '../fleet/agents/services' ;
1011
1112export default function ( providerContext : FtrProviderContext ) {
1213 const { getService } = providerContext ;
1314 const esArchiver = getService ( 'esArchiver' ) ;
1415 const supertest = getService ( 'supertest' ) ;
1516 const esClient = getService ( 'es' ) ;
17+ const supertestWithoutAuth = getSupertestWithoutAuth ( providerContext ) ;
1618 // agent that is enrolled and know to fleet
1719 const enrolledAgentId = '94e689c0-81bd-11ea-a4eb-77680821cd3b' ;
1820 // host that is connected to enrolledAgentId
@@ -25,7 +27,7 @@ export default function(providerContext: FtrProviderContext) {
2527 describe ( 'test metadata api status' , ( ) => {
2628 describe ( '/api/endpoint/metadata when index is not empty' , ( ) => {
2729 beforeEach ( async ( ) => {
28- await esArchiver . load ( 'endpoint/metadata/endpoint_status_feature' ) ;
30+ await esArchiver . loadIfNeeded ( 'endpoint/metadata/endpoint_status_feature' ) ;
2931 const { body : apiKeyBody } = await esClient . security . createApiKey ( {
3032 body : {
3133 name : `test access api key: ${ uuid . v4 ( ) } ` ,
@@ -49,6 +51,13 @@ export default function(providerContext: FtrProviderContext) {
4951 doc : agentDoc ,
5052 } ,
5153 } ) ;
54+ await getService ( 'supertest' )
55+ . post ( `/api/ingest_manager/setup` )
56+ . set ( 'kbn-xsrf' , 'xxx' )
57+ . send ( ) ;
58+ await getService ( 'supertest' )
59+ . post ( `/api/ingest_manager/fleet/setup` )
60+ . set ( 'kbn-xsrf' , 'xxx' ) ;
5261 } ) ;
5362
5463 afterEach ( async ( ) => await esArchiver . unload ( 'endpoint/metadata/endpoint_status_feature' ) ) ;
@@ -83,6 +92,63 @@ export default function(providerContext: FtrProviderContext) {
8392 expect ( enrolledHost . host_status === 'error' ) ;
8493 expect ( notEnrolledHost . host_status === 'error' ) ;
8594 } ) ;
95+
96+ it ( 'should return single metadata with status online when agent status is online' , async ( ) => {
97+ const { body : checkInResponse } = await supertestWithoutAuth
98+ . post ( `/api/ingest_manager/fleet/agents/${ enrolledAgentId } /checkin` )
99+ . set ( 'kbn-xsrf' , 'xx' )
100+ . set (
101+ 'Authorization' ,
102+ `ApiKey ${ Buffer . from ( `${ apiKey . id } :${ apiKey . api_key } ` ) . toString ( 'base64' ) } `
103+ )
104+ . send ( {
105+ events : [ ] ,
106+ local_metadata : { } ,
107+ } )
108+ . expect ( 200 ) ;
109+
110+ expect ( checkInResponse . action ) . to . be ( 'checkin' ) ;
111+ expect ( checkInResponse . success ) . to . be ( true ) ;
112+
113+ const { body : metadataResponse } = await supertest
114+ . get ( `/api/endpoint/metadata/${ enrolledHostId } ` )
115+ . set ( 'kbn-xsrf' , 'xxx' )
116+ . expect ( 200 ) ;
117+ expect ( metadataResponse . host_status ) . to . be ( 'online' ) ;
118+ } ) ;
119+
120+ it ( 'should return metadata list with status only when agent is checked in' , async ( ) => {
121+ const { body : checkInResponse } = await supertestWithoutAuth
122+ . post ( `/api/ingest_manager/fleet/agents/${ enrolledAgentId } /checkin` )
123+ . set ( 'kbn-xsrf' , 'xx' )
124+ . set (
125+ 'Authorization' ,
126+ `ApiKey ${ Buffer . from ( `${ apiKey . id } :${ apiKey . api_key } ` ) . toString ( 'base64' ) } `
127+ )
128+ . send ( {
129+ events : [ ] ,
130+ local_metadata : { } ,
131+ } )
132+ . expect ( 200 ) ;
133+
134+ expect ( checkInResponse . action ) . to . be ( 'checkin' ) ;
135+ expect ( checkInResponse . success ) . to . be ( true ) ;
136+
137+ const { body } = await supertest
138+ . post ( '/api/endpoint/metadata' )
139+ . set ( 'kbn-xsrf' , 'xxx' )
140+ . expect ( 200 ) ;
141+ expect ( body . total ) . to . eql ( 2 ) ;
142+ expect ( body . hosts . length ) . to . eql ( 2 ) ;
143+ const enrolledHost = body . hosts . filter (
144+ ( hostInfo : Record < string , any > ) => hostInfo . metadata . host . id === enrolledHostId
145+ ) ;
146+ const notEnrolledHost = body . hosts . filter (
147+ ( hostInfo : Record < string , any > ) => hostInfo . metadata . host . id === notEnrolledHostId
148+ ) ;
149+ expect ( enrolledHost . host_status === 'online' ) ;
150+ expect ( notEnrolledHost . host_status === 'error' ) ;
151+ } ) ;
86152 } ) ;
87153 } ) ;
88154}
0 commit comments