Skip to content

Commit

Permalink
EMT-146: add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nnamdifrankie committed Apr 18, 2020
1 parent ad9dad3 commit c653765
Show file tree
Hide file tree
Showing 4 changed files with 3,409 additions and 0 deletions.
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/endpoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default function endpointAPIIntegrationTests({
loadTestFile(require.resolve('./index_pattern'));
loadTestFile(require.resolve('./resolver'));
loadTestFile(require.resolve('./metadata'));
loadTestFile(require.resolve('./metadata_status'));
loadTestFile(require.resolve('./alerts'));
});
}
153 changes: 153 additions & 0 deletions x-pack/test/api_integration/apis/endpoint/metadata_status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import uuid from 'uuid';
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';
import { getSupertestWithoutAuth } from '../fleet/agents/services';

export default function(providerContext: FtrProviderContext) {
const { getService } = providerContext;
const esArchiver = getService('esArchiver');
const supertest = getService('supertest');
const supertestWithoutAuth = getSupertestWithoutAuth(providerContext);
const esClient = getService('es');
// agent that is enrolled and know to fleet
const enrolledAgentId = '3efef370-8164-11ea-bc0d-9196b6de96eb';
// host that is connected to enrolledAgentId
const enrolledHostId = 'fdb346d9-6f56-426a-90a7-a9328d1b0528';
// host that is not connected to an enrolled agent id
const notEnrolledHostId = 'bcf0d070-d9f2-40aa-8620-cbc004747722';

let apiKey: { id: string; api_key: string };

describe('test metadata api status', () => {
describe('/api/endpoint/metadata when index is not empty', () => {
before(async () => {
await esArchiver.load('endpoint/metadata/endpoint_status_feature');
const { body: apiKeyBody } = await esClient.security.createApiKey({
body: {
name: `test access api key: ${uuid.v4()}`,
},
});
apiKey = apiKeyBody;
const {
body: { _source: agentDoc },
} = await esClient.get({
index: '.kibana',
id: `agents:${enrolledAgentId}`,
});

agentDoc.agents.access_api_key_id = apiKey.id;

await esClient.update({
index: '.kibana',
id: `agents:${enrolledAgentId}`,
refresh: 'true',
body: {
doc: agentDoc,
},
});
});

after(async () => await esArchiver.unload('endpoint/metadata/endpoint_status_feature'));
it('should return single metadata with status error when agent status is error', async () => {
const { body: metadataResponse } = await supertest
.get(`/api/endpoint/metadata/${enrolledHostId}`)
.set('kbn-xsrf', 'xxx')

This comment has been minimized.

Copy link
@jfsiii

jfsiii Apr 18, 2020

Contributor

kbn-xsrf isn't required for GETs

.expect(200);
expect(metadataResponse.host_status).to.be('error');
});

it('should return single metadata with status error when agent is not enrolled', async () => {
const { body: metadataResponse } = await supertest
.get(`/api/endpoint/metadata/${notEnrolledHostId}`)
.set('kbn-xsrf', 'xxx')
.expect(200);
expect(metadataResponse.host_status).to.be('error');
});

it('should return metadata list with status error when no agent is not enrolled', async () => {
const { body } = await supertest
.post('/api/endpoint/metadata')
.set('kbn-xsrf', 'xxx')
.expect(200);
expect(body.total).to.eql(2);
expect(body.hosts.length).to.eql(2);
const enrolledHost = body.hosts.filter(
(hostInfo: Record<string, any>) => hostInfo.metadata.host.id === enrolledHostId
);
const notEnrolledHost = body.hosts.filter(
(hostInfo: Record<string, any>) => hostInfo.metadata.host.id === notEnrolledHostId
);
expect(enrolledHost.host_status === 'error');
expect(notEnrolledHost.host_status === 'error');
});

it('should return metadata list with status only when agent is checked in', async () => {
const { body: checkInResponse } = await supertestWithoutAuth
.post(`/api/ingest_manager/fleet/agents/${enrolledAgentId}/checkin`)
.set('kbn-xsrf', 'xx')
.set(
'Authorization',
`ApiKey ${Buffer.from(`${apiKey.id}:${apiKey.api_key}`).toString('base64')}`
)
.send({
events: [],
local_metadata: {},
})
.expect(200);

expect(checkInResponse.action).to.be('checkin');
expect(checkInResponse.success).to.be(true);

const { body } = await supertest
.post('/api/endpoint/metadata')
.set('kbn-xsrf', 'xxx')
.expect(200);
expect(body.total).to.eql(2);
expect(body.hosts.length).to.eql(2);
const enrolledHost = body.hosts.filter(
(hostInfo: Record<string, any>) => hostInfo.metadata.host.id === enrolledHostId
);
const notEnrolledHost = body.hosts.filter(
(hostInfo: Record<string, any>) => hostInfo.metadata.host.id === notEnrolledHostId
);
expect(enrolledHost.host_status === 'online');
expect(notEnrolledHost.host_status === 'error');
});

it('should return single metadata with status online when agent status is online', async () => {
await inactivity();
const { body: checkInResponse } = await supertestWithoutAuth
.post(`/api/ingest_manager/fleet/agents/${enrolledAgentId}/checkin`)
.set('kbn-xsrf', 'xx')
.set(
'Authorization',
`ApiKey ${Buffer.from(`${apiKey.id}:${apiKey.api_key}`).toString('base64')}`
)
.send({
events: [],
local_metadata: {},
})
.expect(200);

expect(checkInResponse.action).to.be('checkin');
expect(checkInResponse.success).to.be(true);

const { body: metadataResponse } = await supertest
.get(`/api/endpoint/metadata/${enrolledHostId}`)
.set('kbn-xsrf', 'xxx')
.expect(200);
expect(metadataResponse.host_status).to.be('online');
});
});
});
}

function inactivity() {
return new Promise(resolve => setTimeout(resolve, 35000));
}
Binary file not shown.
Loading

0 comments on commit c653765

Please sign in to comment.