Skip to content

Commit

Permalink
[ftr] add fleetAndAgents service (#191448)
Browse files Browse the repository at this point in the history
## Summary

This PR refactors the `setupFleetAndAgents` helper function into a
`FleetAndAgents` FTR service, which is now initialized during the config
file loading.

Reason: The previous implementation of `setupFleetAndAgents` wrapped a
Mocha `before` hook, which is considered an anti-pattern according to
the Mocha ESLint rules that the Operations and QA teams have agreed to
enable in the Kibana repo. It's best practice to avoid duplicating
hooks, as this can lead to inconsistent test results and make debugging
difficult. Ensuring a single before and after hook sequence guarantees
that the associated logic runs in a predictable and sequential manner.

This change also unblocks #191267, where we plan to enforce the same
recommended Mocha ESLint rules that were previously enabled for UI tests
in #190690.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
dmlemeshko and kibanamachine committed Aug 27, 2024
1 parent 9f01f73 commit 069c158
Show file tree
Hide file tree
Showing 65 changed files with 264 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
LATEST_VULNERABILITIES_INDEX_DEFAULT_NS,
VULNERABILITIES_INDEX_DEFAULT_NS,
} from '@kbn/cloud-security-posture-plugin/common/constants';
import { setupFleetAndAgents } from '../../../../fleet_api_integration/apis/agents/services';
import { generateAgent } from '../../../../fleet_api_integration/helpers';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { deleteIndex, createPackagePolicy } from '../helper';
Expand All @@ -35,12 +34,15 @@ export default function (providerContext: FtrProviderContext) {
const es = getService('es');
const esArchiver = getService('esArchiver');
const kibanaServer = getService('kibanaServer');
const fleetAndAgents = getService('fleetAndAgents');

describe('GET /internal/cloud_security_posture/status', () => {
let agentPolicyId: string;

describe('STATUS = INDEX_TIMEOUT TEST', () => {
setupFleetAndAgents(providerContext);
before(async () => {
await fleetAndAgents.setup();
});

beforeEach(async () => {
await kibanaServer.savedObjects.cleanStandardList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import expect from '@kbn/expect';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import type { CspSetupStatus } from '@kbn/cloud-security-posture-common';
import { setupFleetAndAgents } from '../../../../fleet_api_integration/apis/agents/services';
import { generateAgent } from '../../../../fleet_api_integration/helpers';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { createPackagePolicy } from '../helper';
Expand All @@ -19,13 +18,15 @@ export default function (providerContext: FtrProviderContext) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');
const kibanaServer = getService('kibanaServer');
const fleetAndAgents = getService('fleetAndAgents');

describe('GET /internal/cloud_security_posture/status', () => {
let agentPolicyId: string;

describe('STATUS = WAITING_FOR_RESULT TEST', () => {
setupFleetAndAgents(providerContext);

before(async () => {
await fleetAndAgents.setup();
});
beforeEach(async () => {
await kibanaServer.savedObjects.cleanStandardList();
await esArchiver.load('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
Expand Down
39 changes: 39 additions & 0 deletions x-pack/test/api_integration/services/fleet_and_agents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import { FtrProviderContext } from '../ftr_provider_context';

export async function FleetAndAgents({ getService }: FtrProviderContext) {
// Use elastic/fleet-server service account to execute setup to verify privilege configuration
const es = getService('es');
const supertestWithoutAuth = getService('supertestWithoutAuth');

return {
async setup() {
const { token } = await es.security.createServiceToken({
namespace: 'elastic',
service: 'fleet-server',
});

await supertestWithoutAuth
.post(`/api/fleet/setup`)
.set(ELASTIC_HTTP_VERSION_HEADER, '2023-10-31')
.set('kbn-xsrf', 'xxx')
.set('Authorization', `Bearer ${token.value}`)
.send()
.expect(200);
await supertestWithoutAuth
.post(`/api/fleet/agents/setup`)
.set(ELASTIC_HTTP_VERSION_HEADER, '2023-10-31')
.set('kbn-xsrf', 'xxx')
.set('Authorization', `Bearer ${token.value}`)
.send({ forceRecreate: true })
.expect(200);
},
};
}
2 changes: 2 additions & 0 deletions x-pack/test/api_integration/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { IndexManagementProvider } from './index_management';
import { DataViewApiProvider } from './data_view_api';
import { SloApiProvider } from './slo';
import { SecuritySolutionApiProvider } from './security_solution_api.gen';
import { FleetAndAgents } from './fleet_and_agents';

export const services = {
...commonServices,
Expand All @@ -42,4 +43,5 @@ export const services = {
indexManagement: IndexManagementProvider,
slo: SloApiProvider,
securitySolutionApi: SecuritySolutionApiProvider,
fleetAndAgents: FleetAndAgents,
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import expect from '@kbn/expect';
import { PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '@kbn/fleet-plugin/common';
import { FLEET_AGENT_POLICIES_SCHEMA_VERSION } from '@kbn/fleet-plugin/server/constants';
import { skipIfNoDockerRegistry, generateAgent } from '../../helpers';
import { setupFleetAndAgents } from '../agents/services';
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';

export default function (providerContext: FtrProviderContext) {
Expand All @@ -18,6 +17,7 @@ export default function (providerContext: FtrProviderContext) {
const esArchiver = getService('esArchiver');
const kibanaServer = getService('kibanaServer');
const es = getService('es');
const fleetAndAgents = getService('fleetAndAgents');

const getPackage = async (pkgName: string) => {
const getPkgRes = await supertest
Expand All @@ -42,9 +42,8 @@ export default function (providerContext: FtrProviderContext) {
before(async () => {
await esArchiver.load('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
await kibanaServer.savedObjects.cleanStandardList();
await fleetAndAgents.setup();
});
setupFleetAndAgents(providerContext);

it('should get list agent policies', async () => {
await supertest.get(`/api/fleet/agent_policies`).expect(200);
});
Expand Down Expand Up @@ -102,8 +101,8 @@ export default function (providerContext: FtrProviderContext) {
before(async () => {
await esArchiver.load('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
await kibanaServer.savedObjects.cleanStandardList();
await fleetAndAgents.setup();
});
setupFleetAndAgents(providerContext);
let packagePoliciesToDeleteIds: string[] = [];
after(async () => {
if (systemPkgVersion) {
Expand Down Expand Up @@ -476,8 +475,8 @@ export default function (providerContext: FtrProviderContext) {
describe('POST /api/fleet/agent_policies/{agentPolicyId}/copy', () => {
before(async () => {
await esArchiver.loadIfNeeded('x-pack/test/functional/es_archives/fleet/agents');
await fleetAndAgents.setup();
});
setupFleetAndAgents(providerContext);
const createdPolicyIds: string[] = [];
after(async () => {
const deletedPromises = createdPolicyIds.map((agentPolicyId) =>
Expand Down Expand Up @@ -1460,8 +1459,8 @@ export default function (providerContext: FtrProviderContext) {
let policyId: string;
before(async () => {
await esArchiver.load('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
await fleetAndAgents.setup();
});
setupFleetAndAgents(providerContext);
before(async () => {
const getPkRes = await getPackage('system');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
import expect from '@kbn/expect';
import { v4 as uuidv4 } from 'uuid';
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { setupFleetAndAgents } from '../agents/services';
import { skipIfNoDockerRegistry } from '../../helpers';

export default function (providerContext: FtrProviderContext) {
const { getService } = providerContext;
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');
const fleetAndAgents = getService('fleetAndAgents');

describe('datastream privileges', () => {
skipIfNoDockerRegistry(providerContext);
before(async () => {
await esArchiver.load('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
await fleetAndAgents.setup();
});
setupFleetAndAgents(providerContext);

after(async () => {
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
import expect from '@kbn/expect';
import { v4 as uuidv4 } from 'uuid';
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { setupFleetAndAgents } from '../agents/services';
import { skipIfNoDockerRegistry } from '../../helpers';

export default function (providerContext: FtrProviderContext) {
const { getService } = providerContext;
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');
const fleetAndAgents = getService('fleetAndAgents');

describe('agent policy with root integrations', () => {
skipIfNoDockerRegistry(providerContext);
before(async () => {
await esArchiver.load('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
await fleetAndAgents.setup();
});
setupFleetAndAgents(providerContext);

after(async () => {
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import {
import { ENROLLMENT_API_KEYS_INDEX } from '@kbn/fleet-plugin/common/constants';
import { skipIfNoDockerRegistry } from '../../helpers';
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { setupFleetAndAgents } from '../agents/services';

export default function (providerContext: FtrProviderContext) {
const { getService } = providerContext;
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');
const esClient = getService('es');
const kibanaServer = getService('kibanaServer');
const fleetAndAgents = getService('fleetAndAgents');

async function getEnrollmentKeyForPolicyId(policyId: string) {
const listRes = await supertest.get(`/api/fleet/enrollment_api_keys`).expect(200);
Expand Down Expand Up @@ -89,6 +89,7 @@ export default function (providerContext: FtrProviderContext) {

before(async () => {
await esArchiver.loadIfNeeded('x-pack/test/functional/es_archives/fleet/agents');
await fleetAndAgents.setup();
});
after(async () => {
// Wait before agent status is updated
Expand All @@ -98,8 +99,6 @@ export default function (providerContext: FtrProviderContext) {
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/agents');
});

setupFleetAndAgents(providerContext);

describe('In default space', () => {
describe('POST /api/fleet/agent_policies', () => {
it('should create an enrollment key for the policy', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
AGENT_POLICY_INDEX,
} from '@kbn/fleet-plugin/common';
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { setupFleetAndAgents } from './services';
import { skipIfNoDockerRegistry } from '../../helpers';

const ES_INDEX_OPTIONS = { headers: { 'X-elastic-product-origin': 'fleet' } };
Expand All @@ -22,13 +21,14 @@ export default function (providerContext: FtrProviderContext) {
const supertest = getService('supertest');
const es = getService('es');
const esArchiver = getService('esArchiver');
const fleetAndAgents = getService('fleetAndAgents');

describe('action_status_api', () => {
skipIfNoDockerRegistry(providerContext);
before(async () => {
await esArchiver.load('x-pack/test/functional/es_archives/fleet/agents');
await fleetAndAgents.setup();
});
setupFleetAndAgents(providerContext);

after(async () => {
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/agents');
Expand Down
4 changes: 2 additions & 2 deletions x-pack/test/fleet_api_integration/apis/agents/reassign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { setupFleetAndAgents } from './services';
import { testUsers } from '../test_users';

export default function (providerContext: FtrProviderContext) {
const { getService } = providerContext;
const esArchiver = getService('esArchiver');
const supertest = getService('supertest');
const supertestWithoutAuth = getService('supertestWithoutAuth');
const fleetAndAgents = getService('fleetAndAgents');

describe('fleet_reassign_agent', () => {
before(async () => {
await esArchiver.load('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
await fleetAndAgents.setup();
});
beforeEach(async () => {
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
await esArchiver.load('x-pack/test/functional/es_archives/fleet/agents');
await getService('supertest').post(`/api/fleet/setup`).set('kbn-xsrf', 'xxx').send();
});
setupFleetAndAgents(providerContext);
afterEach(async () => {
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/agents');
await esArchiver.load('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import expect from '@kbn/expect';
import moment from 'moment';

import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { setupFleetAndAgents } from './services';
import { skipIfNoDockerRegistry } from '../../helpers';
import { testUsers } from '../test_users';

Expand All @@ -19,10 +18,14 @@ export default function (providerContext: FtrProviderContext) {
const supertest = getService('supertest');
const supertestWithoutAuth = getService('supertestWithoutAuth');
const es = getService('es');
const fleetAndAgents = getService('fleetAndAgents');

describe('fleet_request_diagnostics', () => {
skipIfNoDockerRegistry(providerContext);
setupFleetAndAgents(providerContext);

before(async () => {
await fleetAndAgents.setup();
});
beforeEach(async () => {
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
await esArchiver.load('x-pack/test/functional/es_archives/fleet/agents');
Expand Down
39 changes: 0 additions & 39 deletions x-pack/test/fleet_api_integration/apis/agents/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,11 @@
* 2.0.
*/

import supertest from 'supertest';
import { Client, HttpConnection } from '@elastic/elasticsearch';
import { format as formatUrl } from 'url';

import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';

import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';

export function getSupertestWithoutAuth({ getService }: FtrProviderContext) {
const config = getService('config');
const kibanaUrl = config.get('servers.kibana');
kibanaUrl.auth = null;
kibanaUrl.password = null;

return supertest(formatUrl(kibanaUrl));
}

export function getEsClientForAPIKey({ getService }: FtrProviderContext, esApiKey: string) {
const config = getService('config');
const url = formatUrl({ ...config.get('servers.elasticsearch'), auth: false });
Expand All @@ -34,30 +22,3 @@ export function getEsClientForAPIKey({ getService }: FtrProviderContext, esApiKe
Connection: HttpConnection,
});
}

export function setupFleetAndAgents(providerContext: FtrProviderContext) {
before(async () => {
// Use elastic/fleet-server service account to execute setup to verify privilege configuration
const es = providerContext.getService('es');
const { token } = await es.security.createServiceToken({
namespace: 'elastic',
service: 'fleet-server',
});
const supetestWithoutAuth = getSupertestWithoutAuth(providerContext);

await supetestWithoutAuth
.post(`/api/fleet/setup`)
.set(ELASTIC_HTTP_VERSION_HEADER, '2023-10-31')
.set('kbn-xsrf', 'xxx')
.set('Authorization', `Bearer ${token.value}`)
.send()
.expect(200);
await supetestWithoutAuth
.post(`/api/fleet/agents/setup`)
.set(ELASTIC_HTTP_VERSION_HEADER, '2023-10-31')
.set('kbn-xsrf', 'xxx')
.set('Authorization', `Bearer ${token.value}`)
.send({ forceRecreate: true })
.expect(200);
});
}
4 changes: 2 additions & 2 deletions x-pack/test/fleet_api_integration/apis/agents/unenroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import { v4 as uuidv4 } from 'uuid';

import { AGENTS_INDEX } from '@kbn/fleet-plugin/common';
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { setupFleetAndAgents } from './services';
import { skipIfNoDockerRegistry } from '../../helpers';

export default function (providerContext: FtrProviderContext) {
const { getService } = providerContext;
const esArchiver = getService('esArchiver');
const supertest = getService('supertest');
const esClient = getService('es');
const fleetAndAgents = getService('fleetAndAgents');

describe('fleet_unenroll_agent', () => {
skipIfNoDockerRegistry(providerContext);
let accessAPIKeyId: string;
let outputAPIKeyId: string;
before(async () => {
await esArchiver.load('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
await fleetAndAgents.setup();
});
setupFleetAndAgents(providerContext);
beforeEach(async () => {
await esArchiver.unload('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
await esArchiver.load('x-pack/test/functional/es_archives/fleet/agents');
Expand Down
Loading

0 comments on commit 069c158

Please sign in to comment.