diff --git a/x-pack/platform/test/fleet_api_integration/apis/agent_policy/create_standalone_api_key.ts b/x-pack/platform/test/fleet_api_integration/apis/agent_policy/create_standalone_api_key.ts index 3fa40824e8923..8c088d48f8424 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/agent_policy/create_standalone_api_key.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/agent_policy/create_standalone_api_key.ts @@ -39,7 +39,7 @@ export default function (providerContext: FtrProviderContext) { }); await expectToRejectWithError( () => apiClient.postStandaloneApiKey('tata'), - /403 Forbidden Missing permissions to create standalone API key/ + /403 "Forbidden" Missing permissions to create standalone API key/ ); }); }); diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/actions.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/actions.ts index 162153ce48455..ee5703aec73ef 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/actions.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/actions.ts @@ -16,6 +16,7 @@ import { cleanFleetAgentPolicies, cleanFleetIndices, createFleetAgent, + createTestSpace, } from './helpers'; export default function (providerContext: FtrProviderContext) { @@ -62,7 +63,7 @@ export default function (providerContext: FtrProviderContext) { testSpaceAgent1 = _testSpaceAgent1; testSpaceAgent2 = _testSpaceAgent2; - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); }); beforeEach(async () => { diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/agent_policies.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/agent_policies.ts index 5ba337a57456a..366d7567800b9 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/agent_policies.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/agent_policies.ts @@ -10,7 +10,12 @@ import type { CreateAgentPolicyResponse } from '@kbn/fleet-plugin/common'; import type { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; import { skipIfNoDockerRegistry } from '../../helpers'; import { SpaceTestApiClient } from './api_helper'; -import { cleanFleetIndices, expectToRejectWithError, expectToRejectWithNotFound } from './helpers'; +import { + cleanFleetIndices, + createTestSpace, + expectToRejectWithError, + expectToRejectWithNotFound, +} from './helpers'; import { setupTestUsers, testUsers } from '../test_users'; export default function (providerContext: FtrProviderContext) { @@ -52,7 +57,7 @@ export default function (providerContext: FtrProviderContext) { await apiClient.postEnableSpaceAwareness(); - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); const [ _defaultSpacePolicy1, _spaceTest1Policy1, diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/agent_policies_side_effects.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/agent_policies_side_effects.ts index c48d3afc2da9e..cfbee692b15a6 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/agent_policies_side_effects.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/agent_policies_side_effects.ts @@ -10,7 +10,7 @@ import type { CreateAgentPolicyResponse } from '@kbn/fleet-plugin/common'; import type { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; import { skipIfNoDockerRegistry } from '../../helpers'; import { SpaceTestApiClient } from './api_helper'; -import { cleanFleetIndices } from './helpers'; +import { cleanFleetIndices, createTestSpace } from './helpers'; export default function (providerContext: FtrProviderContext) { const { getService } = providerContext; @@ -36,7 +36,7 @@ export default function (providerContext: FtrProviderContext) { await cleanFleetIndices(esClient); await apiClient.postEnableSpaceAwareness(); - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); const [_defaultSpacePolicy1, _spaceTest1Policy1] = await Promise.all([ apiClient.createAgentPolicy(), diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/agents.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/agents.ts index 580743fc1dc6f..57ebb445694fa 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/agents.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/agents.ts @@ -16,6 +16,7 @@ import { cleanFleetAgents, cleanFleetIndices, createFleetAgent, + createTestSpace, makeAgentsUpgradeable, } from './helpers'; import { pollResult } from '../agents/update_agent_tags'; @@ -50,7 +51,7 @@ export default function (providerContext: FtrProviderContext) { space: TEST_SPACE_1, }); await cleanFleetIndices(esClient); - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); await apiClient.postEnableSpaceAwareness(); diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/api_helper.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/api_helper.ts index c98a231dc08b0..5e699b65fdccc 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/api_helper.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/api_helper.ts @@ -6,7 +6,7 @@ */ import { v4 as uuidV4 } from 'uuid'; -import type { Agent } from 'supertest'; +import type { Agent, Response } from 'supertest'; import type { CreateAgentPolicyRequest, CreateAgentPolicyResponse, @@ -49,6 +49,22 @@ import type { SimplifiedPackagePolicy } from '@kbn/fleet-plugin/common/services/ import { type FleetUsage } from '@kbn/fleet-plugin/server/collectors/register'; import { testUsers } from '../test_users'; +function expectStatusCode200(res: Response) { + if (res.statusCode === 200) { + return; + } + + if (res.statusCode === 404) { + throw new Error('404 "Not Found"'); + } else { + throw new Error( + `${res.statusCode}${res.body?.error ? ` "${res.body?.error}"` : ''}${ + res.body?.message ? ` ${res.body?.message}` : '' + }` + ); + } +} + export class SpaceTestApiClient { constructor( private readonly supertest: Agent, @@ -72,7 +88,7 @@ export class SpaceTestApiClient { spaceId?: string, data: Partial = {} ): Promise { - const { body: res, statusCode } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agent_policies`) .auth(this.auth.username, this.auth.password) .set('kbn-xsrf', 'xxxx') @@ -84,35 +100,23 @@ export class SpaceTestApiClient { ...data, }); - if (statusCode === 200) { - return res; - } + expectStatusCode200(res); - if (statusCode === 404) { - throw new Error('404 "Not Found"'); - } else { - throw new Error(`${statusCode} ${res?.error} ${res.message}`); - } + return res.body; } async createPackagePolicy( spaceId?: string, data: Partial = {} ): Promise { - const { body: res, statusCode } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/package_policies`) .auth(this.auth.username, this.auth.password) .set('kbn-xsrf', 'xxxx') .send(data); - if (statusCode === 200) { - return res; - } + expectStatusCode200(res); - if (statusCode === 404) { - throw new Error('404 "Not Found"'); - } else { - throw new Error(`${statusCode} "${res?.error}" ${res.message}`); - } + return res.body; } async updatePackagePolicy( @@ -120,60 +124,49 @@ export class SpaceTestApiClient { data: Partial = {}, spaceId?: string ): Promise { - const { body: res, statusCode } = await this.supertest + const res = await this.supertest .put(`${this.getBaseUrl(spaceId)}/api/fleet/package_policies/${packagePolicyId}`) .auth(this.auth.username, this.auth.password) .set('kbn-xsrf', 'xxxx') .send(data); + expectStatusCode200(res); - if (statusCode === 200) { - return res; - } - - if (statusCode === 404) { - throw new Error('404 "Not Found"'); - } else { - throw new Error(`${statusCode} "${res?.error}" ${res.message}`); - } + return res.body; } async deletePackagePolicy(packagePolicyId: string, spaceId?: string) { - const { body: res, statusCode } = await this.supertest + const res = await this.supertest .delete(`${this.getBaseUrl(spaceId)}/api/fleet/package_policies/${packagePolicyId}`) .auth(this.auth.username, this.auth.password) .set('kbn-xsrf', 'xxxx') .send(); - if (statusCode === 200) { - return res; - } - - if (statusCode === 404) { - throw new Error('404 "Not Found"'); - } else { - throw new Error(`${statusCode} "${res?.error}" ${res.message}`); - } + expectStatusCode200(res); } async getPackagePolicy( packagePolicyId: string, spaceId?: string ): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .get(`${this.getBaseUrl(spaceId)}/api/fleet/package_policies/${packagePolicyId}`) - .expect(200); + .send(); - return res; + expectStatusCode200(res); + + return res.body; } async getPackagePolicies(spaceId?: string): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .get(`${this.getBaseUrl(spaceId)}/api/fleet/package_policies`) - .expect(200); + .send(); - return res; + expectStatusCode200(res); + + return res.body; } async createFleetServerPolicy(spaceId?: string): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agent_policies`) .set('kbn-xsrf', 'xxxx') .send({ @@ -183,35 +176,38 @@ export class SpaceTestApiClient { inactivity_timeout: 24 * 1000, has_fleet_server: true, force: true, - }) - .expect(200); + }); - return res; + expectStatusCode200(res); + + return res.body; } async deleteAgentPolicy(agentPolicyId: string, spaceId?: string) { - await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agent_policies/delete`) .auth(this.auth.username, this.auth.password) .send({ agentPolicyId, }) - .set('kbn-xsrf', 'xxxx') - .expect(200); + .set('kbn-xsrf', 'xxxx'); + + expectStatusCode200(res); } async getAgentPolicy(policyId: string, spaceId?: string): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .get(`${this.getBaseUrl(spaceId)}/api/fleet/agent_policies/${policyId}`) - .auth(this.auth.username, this.auth.password) - .expect(200); + .auth(this.auth.username, this.auth.password); - return res; + expectStatusCode200(res); + + return res.body; } async putAgentPolicy( policyId: string, data: Partial, spaceId?: string ): Promise { - const { body: res, statusCode } = await this.supertest + const res = await this.supertest .put(`${this.getBaseUrl(spaceId)}/api/fleet/agent_policies/${policyId}`) .auth(this.auth.username, this.auth.password) .send({ @@ -219,34 +215,30 @@ export class SpaceTestApiClient { }) .set('kbn-xsrf', 'xxxx'); - if (statusCode === 200) { - return res; - } + expectStatusCode200(res); - if (statusCode === 404) { - throw new Error('404 "Not Found"'); - } else { - throw new Error(`${statusCode} ${res?.error} ${res.message}`); - } + return res.body; } async getAgentPolicies(spaceId?: string): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .get(`${this.getBaseUrl(spaceId)}/api/fleet/agent_policies`) - .auth(this.auth.username, this.auth.password) - .expect(200); + .auth(this.auth.username, this.auth.password); - return res; + expectStatusCode200(res); + + return res.body; } async getAgentPoliciesSpaces(spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .get(`${this.getBaseUrl(spaceId)}/internal/fleet/agent_policies_spaces`) .auth(this.auth.username, this.auth.password) .set('kbn-xsrf', 'xxxx') - .set('elastic-api-version', '1') - .expect(200); + .set('elastic-api-version', '1'); - return res; + expectStatusCode200(res); + + return res.body; } // Enrollment API Keys @@ -254,222 +246,248 @@ export class SpaceTestApiClient { keyId: string, spaceId?: string ): Promise { - const { body: res } = await this.supertest - .get(`${this.getBaseUrl(spaceId)}/api/fleet/enrollment_api_keys/${keyId}`) - .expect(200); + const res = await this.supertest.get( + `${this.getBaseUrl(spaceId)}/api/fleet/enrollment_api_keys/${keyId}` + ); - return res; + expectStatusCode200(res); + + return res.body; } async getEnrollmentApiKeys(spaceId?: string): Promise { - const { body: res } = await this.supertest - .get(`${this.getBaseUrl(spaceId)}/api/fleet/enrollment_api_keys`) - .expect(200); + const res = await this.supertest.get( + `${this.getBaseUrl(spaceId)}/api/fleet/enrollment_api_keys` + ); - return res; + expectStatusCode200(res); + + return res.body; } + async deleteEnrollmentApiKey( keyId: string, spaceId?: string ): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .delete(`${this.getBaseUrl(spaceId)}/api/fleet/enrollment_api_keys/${keyId}`) - .set('kbn-xsrf', 'xxxx') - .expect(200); + .set('kbn-xsrf', 'xxxx'); - return res; + expectStatusCode200(res); + + return res.body; } async postEnrollmentApiKeys( body: PostEnrollmentAPIKeyRequest['body'], spaceId?: string ): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/enrollment_api_keys`) .set('kbn-xsrf', 'xxxx') - .send(body) - .expect(200); + .send(body); - return res; + expectStatusCode200(res); + + return res.body; } + // Uninstall tokens async getUninstallTokens(spaceId?: string): Promise { - const { body: res } = await this.supertest - .get(`${this.getBaseUrl(spaceId)}/api/fleet/uninstall_tokens`) - .expect(200); + const res = await this.supertest.get(`${this.getBaseUrl(spaceId)}/api/fleet/uninstall_tokens`); - return res; + expectStatusCode200(res); + + return res.body; } async getUninstallToken(tokenId: string, spaceId?: string): Promise { - const { body: res } = await this.supertest - .get(`${this.getBaseUrl(spaceId)}/api/fleet/uninstall_tokens/${tokenId}`) - .expect(200); + const res = await this.supertest.get( + `${this.getBaseUrl(spaceId)}/api/fleet/uninstall_tokens/${tokenId}` + ); - return res; + expectStatusCode200(res); + + return res.body; } // Agents async getAgent(agentId: string, spaceId?: string): Promise { - const { body: res } = await this.supertest - .get(`${this.getBaseUrl(spaceId)}/api/fleet/agents/${agentId}`) - .expect(200); + const res = await this.supertest.get(`${this.getBaseUrl(spaceId)}/api/fleet/agents/${agentId}`); - return res; + expectStatusCode200(res); + + return res.body; } async getAgents(spaceId?: string): Promise { - const { body: res } = await this.supertest - .get(`${this.getBaseUrl(spaceId)}/api/fleet/agents`) - .expect(200); + const res = await this.supertest.get(`${this.getBaseUrl(spaceId)}/api/fleet/agents`); - return res; + expectStatusCode200(res); + + return res.body; } async updateAgent(agentId: string, data: any, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .put(`${this.getBaseUrl(spaceId)}/api/fleet/agents/${agentId}`) .set('kbn-xsrf', 'xxxx') - .send(data) - .expect(200); + .send(data); - return res; + expectStatusCode200(res); + + return res.body; } async deleteAgent(agentId: string, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .delete(`${this.getBaseUrl(spaceId)}/api/fleet/agents/${agentId}`) - .set('kbn-xsrf', 'xxxx') - .expect(200); + .set('kbn-xsrf', 'xxxx'); - return res; + expectStatusCode200(res); + + return res.body; } async reassignAgent(agentId: string, policyId: string, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agents/${agentId}/reassign`) .set('kbn-xsrf', 'xxx') .send({ policy_id: policyId, - }) - .expect(200); + }); - return res; + expectStatusCode200(res); + + return res.body; } async bulkReassignAgents(data: any, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agents/bulk_reassign`) .set('kbn-xsrf', 'xxxx') - .send(data) - .expect(200); + .send(data); - return res; + expectStatusCode200(res); + + return res.body; } async upgradeAgent(agentId: string, data: any, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agents/${agentId}/upgrade`) .set('kbn-xsrf', 'xxxx') - .send(data) - .expect(200); + .send(data); - return res; + expectStatusCode200(res); + + return res.body; } async bulkUpgradeAgents(data: any, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agents/bulk_upgrade`) .set('kbn-xsrf', 'xxxx') - .send(data) - .expect(200); + .send(data); - return res; + expectStatusCode200(res); + + return res.body; } async requestAgentDiagnostics(agentId: string, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agents/${agentId}/request_diagnostics`) - .set('kbn-xsrf', 'xxxx') - .expect(200); + .set('kbn-xsrf', 'xxxx'); - return res; + expectStatusCode200(res); + + return res.body; } async bulkRequestDiagnostics(data: any, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agents/bulk_request_diagnostics`) .set('kbn-xsrf', 'xxxx') - .send(data) - .expect(200); + .send(data); - return res; + expectStatusCode200(res); + + return res.body; } + async unenrollAgent(agentId: string, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agents/${agentId}/unenroll`) - .set('kbn-xsrf', 'xxxx') - .expect(200); + .set('kbn-xsrf', 'xxxx'); - return res; + expectStatusCode200(res); + + return res.body; } async bulkUnenrollAgents(data: any, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agents/bulk_unenroll`) .set('kbn-xsrf', 'xxxx') - .send(data) - .expect(200); + .send(data); - return res; + expectStatusCode200(res); + + return res.body; } async bulkUpdateAgentTags(data: any, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agents/bulk_update_agent_tags`) .set('kbn-xsrf', 'xxxx') - .send(data) - .expect(200); + .send(data); - return res; + expectStatusCode200(res); + + return res.body; } // Enrollment Settings async getEnrollmentSettings(spaceId?: string): Promise { - const { body: res } = await this.supertest - .get(`${this.getBaseUrl(spaceId)}/internal/fleet/settings/enrollment`) - .expect(200); + const res = await this.supertest.get( + `${this.getBaseUrl(spaceId)}/internal/fleet/settings/enrollment` + ); + expectStatusCode200(res); - return res; + return res.body; } // Fleet Usage async getFleetUsage(spaceId?: string): Promise<{ usage: FleetUsage }> { - const { body: res } = await this.supertest + const res = await this.supertest .get(`${this.getBaseUrl(spaceId)}/internal/fleet/telemetry/usage`) .set('kbn-xsrf', 'xxxx') - .set('elastic-api-version', '1') - .expect(200); + .set('elastic-api-version', '1'); - return res; + expectStatusCode200(res); + + return res.body; } // Space Settings async getSpaceSettings(spaceId?: string): Promise { - const { body: res } = await this.supertest - .get(`${this.getBaseUrl(spaceId)}/api/fleet/space_settings`) - .expect(200); + const res = await this.supertest.get(`${this.getBaseUrl(spaceId)}/api/fleet/space_settings`); - return res; + expectStatusCode200(res); + + return res.body; } async putSpaceSettings( data: PutSpaceSettingsRequest['body'], spaceId?: string ): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .put(`${this.getBaseUrl(spaceId)}/api/fleet/space_settings`) .set('kbn-xsrf', 'xxxx') - .send(data) - .expect(200); + .send(data); - return res; + expectStatusCode200(res); + + return res.body; } + // Package install async getPackage( { pkgName, pkgVersion }: { pkgName: string; pkgVersion?: string }, spaceId?: string ): Promise { - const { body: res } = await this.supertest - .get( - pkgVersion - ? `${this.getBaseUrl(spaceId)}/api/fleet/epm/packages/${pkgName}/${pkgVersion}` - : `${this.getBaseUrl(spaceId)}/api/fleet/epm/packages/${pkgName}` - ) - .expect(200); + const res = await this.supertest.get( + pkgVersion + ? `${this.getBaseUrl(spaceId)}/api/fleet/epm/packages/${pkgName}/${pkgVersion}` + : `${this.getBaseUrl(spaceId)}/api/fleet/epm/packages/${pkgName}` + ); - return res; + expectStatusCode200(res); + + return res.body; } async updatePackage( { @@ -479,177 +497,192 @@ export class SpaceTestApiClient { }: { pkgName: string; pkgVersion: string; data: UpdatePackageRequest['body'] }, spaceId?: string ): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .put(`${this.getBaseUrl(spaceId)}/api/fleet/epm/packages/${pkgName}/${pkgVersion}`) .set('kbn-xsrf', 'xxxx') - .send({ ...data }) - .expect(200); + .send({ ...data }); - return res; + expectStatusCode200(res); + + return res.body; } async installPackage( { pkgName, pkgVersion, force }: { pkgName: string; pkgVersion: string; force?: boolean }, spaceId?: string ) { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/epm/packages/${pkgName}/${pkgVersion}`) .auth(this.auth.username, this.auth.password) .set('kbn-xsrf', 'xxxx') - .send({ force }) - .expect(200); + .send({ force }); - return res; + expectStatusCode200(res); + + return res.body; } + async uninstallPackage( { pkgName, pkgVersion, force }: { pkgName: string; pkgVersion: string; force?: boolean }, spaceId?: string ) { - const { body: res } = await this.supertest + const res = await this.supertest .delete(`${this.getBaseUrl(spaceId)}/api/fleet/epm/packages/${pkgName}/${pkgVersion}`) .set('kbn-xsrf', 'xxxx') - .send({ force }) - .expect(200); + .send({ force }); - return res; + expectStatusCode200(res); + + return res.body; } async deletePackageKibanaAssets( { pkgName, pkgVersion }: { pkgName: string; pkgVersion: string }, spaceId?: string ) { - const { body: res } = await this.supertest + const res = await this.supertest .delete( `${this.getBaseUrl(spaceId)}/api/fleet/epm/packages/${pkgName}/${pkgVersion}/kibana_assets` ) - .set('kbn-xsrf', 'xxxx') - .expect(200); + .set('kbn-xsrf', 'xxxx'); - return res; + expectStatusCode200(res); + + return res.body; } async installPackageKibanaAssets( { pkgName, pkgVersion, spaceIds }: { pkgName: string; pkgVersion: string; spaceIds?: string[] }, spaceId?: string ) { - const { body: res } = await this.supertest + const res = await this.supertest .post( `${this.getBaseUrl(spaceId)}/api/fleet/epm/packages/${pkgName}/${pkgVersion}/kibana_assets` ) .set('kbn-xsrf', 'xxxx') - .send(spaceIds ? { space_ids: spaceIds } : {}) - .expect(200); + .send(spaceIds ? { space_ids: spaceIds } : {}); - return res; + expectStatusCode200(res); + + return res.body; } // Actions async getActionStatus(spaceId?: string): Promise { - const { body: res } = await this.supertest - .get(`${this.getBaseUrl(spaceId)}/api/fleet/agents/action_status`) - .expect(200); + const res = await this.supertest.get( + `${this.getBaseUrl(spaceId)}/api/fleet/agents/action_status` + ); - return res; + expectStatusCode200(res); + + return res.body; } async postNewAgentAction(agentId: string, spaceId?: string): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agents/${agentId}/actions`) .set('kbn-xsrf', 'xxxx') - .send({ action: { type: 'UNENROLL' } }) - .expect(200); + .send({ action: { type: 'UNENROLL' } }); - return res; + expectStatusCode200(res); + + return res.body; } async cancelAction(actionId: string, spaceId?: string): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agents/actions/${actionId}/cancel`) - .set('kbn-xsrf', 'xxxx') - .expect(200); - return res; + .set('kbn-xsrf', 'xxxx'); + + expectStatusCode200(res); + + return res.body; } // Enable space awareness async postEnableSpaceAwareness(spaceId?: string): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/internal/fleet/enable_space_awareness`) .auth(this.auth.username, this.auth.password) .set('kbn-xsrf', 'xxxx') - .set('elastic-api-version', '1') - .expect(200); + .set('elastic-api-version', '1'); - return res; + expectStatusCode200(res); + + return res.body; } // Download source async deleteDownloadSource(id: string, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .delete(`${this.getBaseUrl(spaceId)}/api/fleet/agent_download_sources/${id}`) - .set('kbn-xsrf', 'xxxx') - .expect(200); + .set('kbn-xsrf', 'xxxx'); - return res; + expectStatusCode200(res); + + return res.body; } async postDownloadSource( data: PostDownloadSourceRequest['body'], spaceId?: string ): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agent_download_sources`) .set('kbn-xsrf', 'xxxx') - .send(data) - .expect(200); + .send(data); + expectStatusCode200(res); - return res; + return res.body; } // Fleet server hosts async deleteFleetServerHosts(id: string, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .delete(`${this.getBaseUrl(spaceId)}/api/fleet/fleet_server_hosts/${id}`) - .set('kbn-xsrf', 'xxxx') - .expect(200); + .set('kbn-xsrf', 'xxxx'); - return res; + expectStatusCode200(res); + + return res.body; } async postFleetServerHosts( data: PostFleetServerHostsRequest['body'], spaceId?: string ): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/fleet_server_hosts`) .set('kbn-xsrf', 'xxxx') - .send(data) - .expect(200); + .send(data); - return res; + expectStatusCode200(res); + + return res.body; } // Output async deleteOutput(id: string, spaceId?: string) { - const { body: res } = await this.supertest + const res = await this.supertest .delete(`${this.getBaseUrl(spaceId)}/api/fleet/outputs/${id}`) - .set('kbn-xsrf', 'xxxx') - .expect(200); + .set('kbn-xsrf', 'xxxx'); - return res; + expectStatusCode200(res); + + return res.body; } async postOutput( data: PostOutputRequest['body'], spaceId?: string ): Promise { - const { body: res } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/outputs`) .set('kbn-xsrf', 'xxxx') - .send(data) - .expect(200); + .send(data); - return res; + expectStatusCode200(res); + + return res.body; } async postStandaloneApiKey(name: string, spaceId?: string) { - const { body: res, statusCode } = await this.supertest + const res = await this.supertest .post(`${this.getBaseUrl(spaceId)}/internal/fleet/create_standalone_agent_api_key`) .auth(this.auth.username, this.auth.password) .set('kbn-xsrf', 'xxxx') .set('elastic-api-version', '1') .send({ name }); - if (statusCode !== 200) { - throw new Error(`${statusCode} ${res?.error} ${res.message}`); - } + expectStatusCode200(res); - return res; + return res.body; } } diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/change_space_agent_policies.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/change_space_agent_policies.ts index 429c289c71933..5b7c09a14445d 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/change_space_agent_policies.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/change_space_agent_policies.ts @@ -19,6 +19,7 @@ import { SpaceTestApiClient } from './api_helper'; import { cleanFleetIndices, createFleetAgent, + createTestSpace, expectToRejectWithError, expectToRejectWithNotFound, getFleetAgentDoc, @@ -113,7 +114,7 @@ export default function (providerContext: FtrProviderContext) { inputs: {}, }); defaultPackagePolicy1 = packagePolicyRes; - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); }); after(async () => { @@ -289,7 +290,7 @@ export default function (providerContext: FtrProviderContext) { description: 'tata', space_ids: ['default', TEST_SPACE_1], }), - /400 Bad Request Not enough permissions to create policies in space test1/ + /400 "Bad Request" Not enough permissions to create policies in space test1/ ); }); @@ -314,7 +315,7 @@ export default function (providerContext: FtrProviderContext) { description: 'tata', space_ids: ['default'], }), - /400 Bad Request Not enough permissions to remove policies from space test1/ + /400 "Bad Request" Not enough permissions to remove policies from space test1/ ); }); }); diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/enrollment_api_keys.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/enrollment_api_keys.ts index be42c2c926474..7d8465acbbd2f 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/enrollment_api_keys.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/enrollment_api_keys.ts @@ -11,7 +11,7 @@ import { type EnrollmentAPIKey } from '@kbn/fleet-plugin/common/types'; import type { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; import { skipIfNoDockerRegistry } from '../../helpers'; import { SpaceTestApiClient } from './api_helper'; -import { cleanFleetIndices } from './helpers'; +import { cleanFleetIndices, createTestSpace } from './helpers'; export default function (providerContext: FtrProviderContext) { const { getService } = providerContext; @@ -55,7 +55,7 @@ export default function (providerContext: FtrProviderContext) { const defaultSpaceApiKeys = await apiClient.getEnrollmentApiKeys(); defaultSpaceEnrollmentKey1 = defaultSpaceApiKeys.items[0]; spaceTest1EnrollmentKey1 = space1ApiKeys.items[0]; - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); }); after(async () => { diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/enrollment_settings.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/enrollment_settings.ts index b4a8702587e7a..0698858e1ccb8 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/enrollment_settings.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/enrollment_settings.ts @@ -9,7 +9,7 @@ import expect from '@kbn/expect'; import type { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; import { skipIfNoDockerRegistry } from '../../helpers'; import { SpaceTestApiClient } from './api_helper'; -import { cleanFleetIndices, createFleetAgent } from './helpers'; +import { cleanFleetIndices, createFleetAgent, createTestSpace } from './helpers'; export default function (providerContext: FtrProviderContext) { const { getService } = providerContext; @@ -34,7 +34,7 @@ export default function (providerContext: FtrProviderContext) { await cleanFleetIndices(esClient); await apiClient.postEnableSpaceAwareness(); await apiClient.setup(); - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); }); after(async () => { @@ -69,7 +69,7 @@ export default function (providerContext: FtrProviderContext) { await apiClient.setup(); const testSpaceFleetServerPolicy = await apiClient.createFleetServerPolicy(TEST_SPACE_1); await createFleetAgent(esClient, testSpaceFleetServerPolicy.item.id, TEST_SPACE_1); - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); }); after(async () => { @@ -104,7 +104,7 @@ export default function (providerContext: FtrProviderContext) { await apiClient.setup(); const defaultFleetServerPolicy = await apiClient.createFleetServerPolicy(); await createFleetAgent(esClient, defaultFleetServerPolicy.item.id); - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); }); after(async () => { diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/helpers.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/helpers.ts index eaa580efa8874..4ca79fadcde77 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/helpers.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/helpers.ts @@ -7,6 +7,7 @@ import type { Client } from '@elastic/elasticsearch'; import expect from '@kbn/expect'; +import { asyncForEach } from '@kbn/std'; import { AGENT_ACTIONS_INDEX, @@ -16,7 +17,7 @@ import { type FleetServerAgent, } from '@kbn/fleet-plugin/common'; import { ENROLLMENT_API_KEYS_INDEX } from '@kbn/fleet-plugin/common/constants'; -import { asyncForEach } from '@kbn/std'; +import type { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; const ES_INDEX_OPTIONS = { headers: { 'X-elastic-product-origin': 'fleet' } }; @@ -153,3 +154,16 @@ export async function makeAgentsUpgradeable(esClient: Client, agentIds: string[] }); }); } + +export async function createTestSpace(ftrProvider: FtrProviderContext, spaceId: string) { + const spaces = ftrProvider.getService('spaces'); + const space = await spaces.get(spaceId).catch((err) => { + if (err.message.includes('404 Not Found')) { + return undefined; + } + throw err; + }); + if (!space) { + await spaces.createTestSpace(spaceId); + } +} diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/outputs.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/outputs.ts index 9cd5f331e9e8e..701db7e222dca 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/outputs.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/outputs.ts @@ -8,14 +8,13 @@ import type { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; import { skipIfNoDockerRegistry } from '../../helpers'; import { SpaceTestApiClient } from './api_helper'; -import { cleanFleetIndices, createFleetAgent } from './helpers'; +import { cleanFleetIndices, createFleetAgent, createTestSpace } from './helpers'; export default function (providerContext: FtrProviderContext) { const { getService } = providerContext; const supertest = getService('supertest'); const esClient = getService('es'); const kibanaServer = getService('kibanaServer'); - const spaces = getService('spaces'); const TEST_SPACE_1 = 'test1'; describe('outputs', function () { @@ -31,7 +30,7 @@ export default function (providerContext: FtrProviderContext) { await cleanFleetIndices(esClient); await apiClient.postEnableSpaceAwareness(); await apiClient.setup(); - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); const testSpaceFleetServerPolicy = await apiClient.createFleetServerPolicy(TEST_SPACE_1); await createFleetAgent(esClient, testSpaceFleetServerPolicy.item.id, TEST_SPACE_1); }); diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/package_install.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/package_install.ts index b05169dfd6612..b3a81aec3538a 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/package_install.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/package_install.ts @@ -9,7 +9,7 @@ import expect from '@kbn/expect'; import type { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; import { skipIfNoDockerRegistry } from '../../helpers'; import { SpaceTestApiClient } from './api_helper'; -import { cleanFleetIndices, createFleetAgent } from './helpers'; +import { cleanFleetIndices, createFleetAgent, createTestSpace } from './helpers'; export default function (providerContext: FtrProviderContext) { const { getService } = providerContext; @@ -30,7 +30,7 @@ export default function (providerContext: FtrProviderContext) { space: TEST_SPACE_1, }); await cleanFleetIndices(esClient); - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); }); after(async () => { diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/package_policies.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/package_policies.ts index e4496eab35b97..1f58a7b7d7cc5 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/package_policies.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/package_policies.ts @@ -10,7 +10,7 @@ import type { CreateAgentPolicyResponse } from '@kbn/fleet-plugin/common'; import type { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; import { skipIfNoDockerRegistry } from '../../helpers'; import { SpaceTestApiClient } from './api_helper'; -import { cleanFleetIndices, expectToRejectWithError } from './helpers'; +import { cleanFleetIndices, createTestSpace, expectToRejectWithError } from './helpers'; import { setupTestUsers, testUsers } from '../test_users'; export default function (providerContext: FtrProviderContext) { @@ -42,7 +42,7 @@ export default function (providerContext: FtrProviderContext) { await apiClient.postEnableSpaceAwareness(); - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); multiSpacePolicy = await apiClient.createAgentPolicy(undefined, { space_ids: ['default', TEST_SPACE_1], }); diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/space_awareness_migration.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/space_awareness_migration.ts index 0df65faf75125..8dbb50a92c00b 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/space_awareness_migration.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/space_awareness_migration.ts @@ -11,7 +11,7 @@ import { GLOBAL_SETTINGS_SAVED_OBJECT_TYPE } from '@kbn/fleet-plugin/server/cons import type { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; import { skipIfNoDockerRegistry } from '../../helpers'; import { SpaceTestApiClient } from './api_helper'; -import { cleanFleetIndices } from './helpers'; +import { cleanFleetIndices, createTestSpace } from './helpers'; export default function (providerContext: FtrProviderContext) { const { getService } = providerContext; @@ -75,7 +75,7 @@ export default function (providerContext: FtrProviderContext) { inputs: {}, }); - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); }); after(async () => { diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/space_settings.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/space_settings.ts index bffc62488572b..23ea67d07f1b9 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/space_settings.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/space_settings.ts @@ -8,7 +8,7 @@ import expect from '@kbn/expect'; import type { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; import { SpaceTestApiClient } from './api_helper'; -import { expectToRejectWithError } from './helpers'; +import { createTestSpace, expectToRejectWithError } from './helpers'; export default function (providerContext: FtrProviderContext) { const { getService } = providerContext; @@ -24,7 +24,7 @@ export default function (providerContext: FtrProviderContext) { await kibanaServer.savedObjects.cleanStandardList({ space: TEST_SPACE_1, }); - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); }); after(async () => { @@ -102,7 +102,7 @@ export default function (providerContext: FtrProviderContext) { apiClient.createAgentPolicy(TEST_SPACE_1, { namespace: 'default', }), - /400 Bad Request Invalid namespace, supported namespace prefixes: test/ + /400 "Bad Request" Invalid namespace, supported namespace prefixes: test/ ); }); it('should allow authorized agent policy namespace', async () => { diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/telemetry.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/telemetry.ts index dc33d930d23c6..e44a01b6e496f 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/telemetry.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/telemetry.ts @@ -8,6 +8,7 @@ import expect from '@kbn/expect'; import type { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; import { SpaceTestApiClient } from './api_helper'; +import { createTestSpace } from './helpers'; export default function (providerContext: FtrProviderContext) { const { getService } = providerContext; @@ -25,7 +26,7 @@ export default function (providerContext: FtrProviderContext) { await kibanaServer.savedObjects.cleanStandardList({ space: TEST_SPACE_1, }); - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); await apiClient.postEnableSpaceAwareness(); await Promise.all([ apiClient.createAgentPolicy(), diff --git a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/uninstall_tokens.ts b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/uninstall_tokens.ts index e79ee811e1232..8f51dc91221bf 100644 --- a/x-pack/platform/test/fleet_api_integration/apis/space_awareness/uninstall_tokens.ts +++ b/x-pack/platform/test/fleet_api_integration/apis/space_awareness/uninstall_tokens.ts @@ -11,7 +11,7 @@ import type { UninstallTokenMetadata } from '@kbn/fleet-plugin/common/types/mode import type { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; import { skipIfNoDockerRegistry } from '../../helpers'; import { SpaceTestApiClient } from './api_helper'; -import { cleanFleetIndices, expectToRejectWithNotFound } from './helpers'; +import { cleanFleetIndices, createTestSpace, expectToRejectWithNotFound } from './helpers'; export default function (providerContext: FtrProviderContext) { const { getService } = providerContext; @@ -38,7 +38,7 @@ export default function (providerContext: FtrProviderContext) { space: TEST_SPACE_1, }); await cleanFleetIndices(esClient); - await spaces.createTestSpace(TEST_SPACE_1); + await createTestSpace(providerContext, TEST_SPACE_1); // Create agent policies it should create am uninstall token for every keys await apiClient.postEnableSpaceAwareness(); const [_defaultSpacePolicy1, _spaceTest1Policy1, _spaceTest1Policy2] = await Promise.all([