diff --git a/x-pack/test/fleet_api_integration/apis/space_awareness/api_helper.ts b/x-pack/test/fleet_api_integration/apis/space_awareness/api_helper.ts index 633d34cfa2d15..b9e01ba70b11e 100644 --- a/x-pack/test/fleet_api_integration/apis/space_awareness/api_helper.ts +++ b/x-pack/test/fleet_api_integration/apis/space_awareness/api_helper.ts @@ -10,6 +10,7 @@ import type { Agent } from 'supertest'; import { CreateAgentPolicyRequest, CreateAgentPolicyResponse, + CreatePackagePolicyResponse, GetAgentPoliciesResponse, GetAgentsResponse, GetOneAgentPolicyResponse, @@ -29,6 +30,7 @@ import { GetUninstallTokenResponse, GetUninstallTokensMetadataResponse, } from '@kbn/fleet-plugin/common/types/rest_spec/uninstall_token'; +import { SimplifiedPackagePolicy } from '@kbn/fleet-plugin/common/services/simplified_package_policy_helper'; export class SpaceTestApiClient { constructor(private readonly supertest: Agent) {} @@ -63,6 +65,18 @@ export class SpaceTestApiClient { return res; } + async createPackagePolicy( + spaceId?: string, + data: Partial = {} + ): Promise { + const { body: res } = await this.supertest + .post(`${this.getBaseUrl(spaceId)}/api/fleet/package_policies`) + .set('kbn-xsrf', 'xxxx') + .send(data) + .expect(200); + + return res; + } async createFleetServerPolicy(spaceId?: string): Promise { const { body: res } = await this.supertest .post(`${this.getBaseUrl(spaceId)}/api/fleet/agent_policies`) @@ -224,6 +238,18 @@ export class SpaceTestApiClient { return res; } + async uninstallPackage( + { pkgName, pkgVersion, force }: { pkgName: string; pkgVersion: string; force?: boolean }, + spaceId?: string + ) { + const { body: res } = await this.supertest + .delete(`${this.getBaseUrl(spaceId)}/api/fleet/epm/packages/${pkgName}/${pkgVersion}`) + .set('kbn-xsrf', 'xxxx') + .send({ force }) + .expect(200); + + return res; + } async deletePackageKibanaAssets( { pkgName, pkgVersion }: { pkgName: string; pkgVersion: string }, spaceId?: string diff --git a/x-pack/test/fleet_api_integration/apis/space_awareness/package_install.ts b/x-pack/test/fleet_api_integration/apis/space_awareness/package_install.ts index ed464bd8d9f31..4052e9a8de488 100644 --- a/x-pack/test/fleet_api_integration/apis/space_awareness/package_install.ts +++ b/x-pack/test/fleet_api_integration/apis/space_awareness/package_install.ts @@ -21,6 +21,31 @@ export default function (providerContext: FtrProviderContext) { describe('package install', async function () { skipIfNoDockerRegistry(providerContext); const apiClient = new SpaceTestApiClient(supertest); + const createFleetAgent = async (agentPolicyId: string, spaceId?: string) => { + const agentResponse = await esClient.index({ + index: '.fleet-agents', + refresh: true, + body: { + access_api_key_id: 'api-key-3', + active: true, + policy_id: agentPolicyId, + policy_revision_idx: 1, + last_checkin_status: 'online', + type: 'PERMANENT', + local_metadata: { + host: { hostname: 'host123' }, + elastic: { agent: { version: '8.15.0' } }, + }, + user_provided_metadata: {}, + enrolled_at: new Date().toISOString(), + last_checkin: new Date().toISOString(), + tags: ['tag1'], + namespaces: spaceId ? [spaceId] : undefined, + }, + }); + + return agentResponse._id; + }; before(async () => { await kibanaServer.savedObjects.cleanStandardList(); @@ -229,5 +254,61 @@ export default function (providerContext: FtrProviderContext) { }); }); }); + + describe('uninstall', () => { + beforeEach(async () => { + await apiClient.installPackage({ + pkgName: 'nginx', + pkgVersion: '1.20.0', + force: true, // To avoid package verification + }); + const agentPolicyRes = await apiClient.createAgentPolicy(); + + await apiClient.createPackagePolicy(undefined, { + policy_ids: [agentPolicyRes.item.id], + name: `test-nginx-${Date.now()}`, + description: 'test', + package: { + name: 'nginx', + version: '1.20.0', + }, + inputs: {}, + }); + + await createFleetAgent(agentPolicyRes.item.id); + }); + + it('should not allow to delete a package with active agents in the same space', async () => { + let err: Error | undefined; + try { + await apiClient.uninstallPackage({ + pkgName: 'nginx', + pkgVersion: '1.20.0', + force: true, // To avoid package verification + }); + } catch (_err) { + err = _err; + } + expect(err).to.be.an(Error); + expect(err?.message).to.match(/400 "Bad Request"/); + }); + it('should not allow to delete a package with active agents in a different space', async () => { + let err: Error | undefined; + try { + await apiClient.uninstallPackage( + { + pkgName: 'nginx', + pkgVersion: '1.20.0', + force: true, // To avoid package verification + }, + TEST_SPACE_1 + ); + } catch (_err) { + err = _err; + } + expect(err).to.be.an(Error); + expect(err?.message).to.match(/400 "Bad Request"/); + }); + }); }); }