[Fleet] Use a docker registry for fleet integration test (#124405)

This commit is contained in:
Nicolas Chaulet 2022-02-03 11:53:12 -05:00 committed by GitHub
parent fd041c5fe3
commit 4f6be55f0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 5 deletions

View file

@ -0,0 +1,69 @@
/*
* 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 { spawn } from 'child_process';
import type { ChildProcess } from 'child_process';
import fetch from 'node-fetch';
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
export function useDockerRegistry() {
const packageRegistryPort = process.env.FLEET_PACKAGE_REGISTRY_PORT || '8081';
if (!packageRegistryPort.match(/^[0-9]{4}/)) {
throw new Error('Invalid FLEET_PACKAGE_REGISTRY_PORT');
}
let dockerProcess: ChildProcess | undefined;
async function startDockerRegistryServer() {
const dockerImage = `docker.elastic.co/package-registry/distribution@sha256:de952debe048d903fc73e8a4472bb48bb95028d440cba852f21b863d47020c61`;
const args = ['run', '--rm', '-p', `${packageRegistryPort}:8080`, dockerImage];
dockerProcess = spawn('docker', args, { stdio: 'inherit' });
let isExited = dockerProcess.exitCode !== null;
dockerProcess.once('exit', () => {
isExited = true;
});
let retries = 0;
while (!isExited && retries++ <= 20) {
try {
const res = await fetch(`http://localhost:${packageRegistryPort}/`);
if (res.status === 200) {
return;
}
} catch (err) {
// swallow errors
}
await delay(3000);
}
dockerProcess.kill();
throw new Error('Unable to setup docker registry');
}
async function cleanupDockerRegistryServer() {
if (dockerProcess && !dockerProcess.killed) {
dockerProcess.kill();
}
}
beforeAll(async () => {
jest.setTimeout(5 * 60 * 1000); // 5 minutes timeout
await startDockerRegistryServer();
});
afterAll(async () => {
await cleanupDockerRegistryServer();
});
return `http://localhost:${packageRegistryPort}`;
}

View file

@ -14,6 +14,8 @@ import type { HttpMethod } from 'src/core/test_helpers/kbn_server';
import type { AgentPolicySOAttributes } from '../types';
import { useDockerRegistry } from './docker_registry_helper';
const logFilePath = Path.join(__dirname, 'logs.log');
type Root = ReturnType<typeof kbnTestServer.createRoot>;
@ -46,6 +48,8 @@ describe('Fleet preconfiguration reset', () => {
let esServer: kbnTestServer.TestElasticsearchUtils;
let kbnServer: kbnTestServer.TestKibanaUtils;
const registryUrl = useDockerRegistry();
const startServers = async () => {
const { startES } = kbnTestServer.createTestServers({
adjustTimeout: (t) => jest.setTimeout(t),
@ -63,6 +67,7 @@ describe('Fleet preconfiguration reset', () => {
{
xpack: {
fleet: {
registryUrl,
packages: [
{
name: 'fleet_server',
@ -195,8 +200,7 @@ describe('Fleet preconfiguration reset', () => {
await stopServers();
});
// FLAKY: https://github.com/elastic/kibana/issues/123103
describe.skip('Reset all policy', () => {
describe('Reset all policy', () => {
it('Works and reset all preconfigured policies', async () => {
const resetAPI = getSupertestWithAdminUser(
kbnServer.root,
@ -225,9 +229,7 @@ describe('Fleet preconfiguration reset', () => {
});
});
// FLAKY: https://github.com/elastic/kibana/issues/123104
// FLAKY: https://github.com/elastic/kibana/issues/123105
describe.skip('Reset one preconfigured policy', () => {
describe('Reset one preconfigured policy', () => {
const POLICY_ID = 'test-12345';
it('Works and reset one preconfigured policies if the policy is already deleted (with a ghost package policy)', async () => {