Skip to content

Commit

Permalink
Add api TESTS
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Jul 19, 2024
1 parent a10e0b5 commit e81d02a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Agent } from 'supertest';
import {
CreateAgentPolicyRequest,
CreateAgentPolicyResponse,
CreatePackagePolicyResponse,
GetAgentPoliciesResponse,
GetAgentsResponse,
GetOneAgentPolicyResponse,
Expand All @@ -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) {}
Expand Down Expand Up @@ -63,6 +65,18 @@ export class SpaceTestApiClient {

return res;
}
async createPackagePolicy(
spaceId?: string,
data: Partial<SimplifiedPackagePolicy & { package: { name: string; version: string } }> = {}
): Promise<CreatePackagePolicyResponse> {
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<CreateAgentPolicyResponse> {
const { body: res } = await this.supertest
.post(`${this.getBaseUrl(spaceId)}/api/fleet/agent_policies`)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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"/);
});
});
});
}

0 comments on commit e81d02a

Please sign in to comment.