Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(testcontainers): improve probes stability #116

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/chainfile-agent/src/routers/probes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,11 @@ class Probes {
return this.values[value.$value] ?? '';
}
}

// TODO(fuxingloh): Probes (Liveness, Readiness, Startup) currently only allow a single probe per endpoint
// We should allow multiple endpoints per container where some conditions can only be checked by through calling
// multiple endpoints.
// For example, a container with 3 conditions required for it to be liveness:
// Container: /api/condition-1 <-|
// : /api/condition-2 <-| Agent: /probes/liveness
// : /api/condition-3 <-|
130 changes: 79 additions & 51 deletions packages/chainfile-testcontainers-node/src/testcontainers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,77 +66,105 @@ const chainfile: Chainfile = {
},
};

const testcontainers = new ChainfileTestcontainers(chainfile);
describe('testcontainers.start()', () => {
const testcontainers = new ChainfileTestcontainers(chainfile);

beforeAll(async () => {
await testcontainers.start();
});

afterAll(async () => {
await testcontainers.stop();
});
beforeAll(async () => {
await testcontainers.start();
});

describe('container', () => {
it('should get rpc port', async () => {
const port = testcontainers.get('bitcoind').getHostPort('rpc');
expect(port).toStrictEqual(expect.any(Number));
afterAll(async () => {
await testcontainers.stop();
});

it('should rpc(getblockchaininfo)', async () => {
const response = await testcontainers.get('bitcoind').rpc({
method: 'getblockchaininfo',
describe('container', () => {
it('should get rpc port', async () => {
const port = testcontainers.get('bitcoind').getHostPort('rpc');
expect(port).toStrictEqual(expect.any(Number));
});

expect(response.status).toStrictEqual(200);
it('should rpc(getblockchaininfo)', async () => {
const response = await testcontainers.get('bitcoind').rpc({
method: 'getblockchaininfo',
});

expect(await response.json()).toMatchObject({
result: {
bestblockhash: '0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206',
chain: 'regtest',
blocks: 0,
},
expect(response.status).toStrictEqual(200);

expect(await response.json()).toMatchObject({
result: {
bestblockhash: '0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206',
chain: 'regtest',
blocks: 0,
},
});
});
});
});

describe('agent', () => {
let agent: AgentContainer;
describe('agent', () => {
let agent: AgentContainer;

beforeAll(() => {
agent = testcontainers.getAgent();
});
beforeAll(() => {
agent = testcontainers.getAgent();
});

it('should call GET /chainfile', async () => {
const result = await agent.getChainfile();
expect(result).toEqual(chainfile);
});
it('should call GET /chainfile', async () => {
const result = await agent.getChainfile();
expect(result).toEqual(chainfile);
});

it('should call GET /probes/startup', async () => {
const response = await agent.probe('startup');
expect(response.status).toStrictEqual(200);
expect(await response.json()).toMatchObject({
ok: true,
it('should call GET /probes/startup', async () => {
const response = await agent.probe('startup');
expect(response.status).toStrictEqual(200);
expect(await response.json()).toMatchObject({
ok: true,
});
});
});

it('should call GET /probes/liveness', async () => {
const response = await agent.probe('liveness');
expect(response.status).toStrictEqual(200);
expect(await response.json()).toMatchObject({
ok: true,
it('should call GET /probes/liveness', async () => {
const response = await agent.probe('liveness');
expect(response.status).toStrictEqual(200);
expect(await response.json()).toMatchObject({
ok: true,
});
});

it('should call GET /probes/readiness', async () => {
const response = await agent.probe('readiness');
expect(response.status).toStrictEqual(200);
expect(await response.json()).toMatchObject({
containers: {
bitcoind: {
ok: true,
},
},
ok: true,
});
});
});
});

it('should call GET /probes/readiness', async () => {
const response = await agent.probe('readiness');
expect(response.status).toStrictEqual(200);
expect(await response.json()).toMatchObject({
describe('new ChainfileTestcontainers()', () => {
it('should have different suffix', async () => {
const file: Chainfile = {
$schema: 'https://chainfile.org/schema.json',
caip2: 'eip155:1337',
name: 'Ganache',
containers: {
bitcoind: {
ok: true,
ganache: {
image: 'docker.io/trufflesuite/ganache',
tag: 'v7.9.2',
source: 'https://github.com/trufflesuite/ganache',
resources: {
cpu: 0.25,
memory: 256,
},
endpoints: {},
},
},
ok: true,
});
};

const test1 = new ChainfileTestcontainers(file);
const test2 = new ChainfileTestcontainers(file);
expect(test1.suffix).not.toEqual(test2.suffix);
});
});
4 changes: 2 additions & 2 deletions packages/chainfile-testcontainers-node/src/testcontainers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { AgentContainer } from './agent';
import { ChainfileContainer } from './container';

export class ChainfileTestcontainers {
public readonly suffix = randomBytes(4).toString('hex');
protected readonly cwd: string = join(process.cwd(), '.chainfile', 'testcontainers');
protected readonly suffix = randomBytes(4).toString('hex');
protected readonly filename = `compose.${this.suffix}.yml`;
protected readonly chainfile: Chainfile;

Expand All @@ -39,7 +39,7 @@ export class ChainfileTestcontainers {
this.composeInstance = await new DockerComposeEnvironment(this.cwd, this.filename)
.withEnvironment(environment)
// The readiness probe of @chainfile/agent is to determine if the deployment is ready to accept requests.
.withWaitStrategy(`agent-${this.suffix}`, Wait.forHttp('/probes/readiness', 1569))
.withWaitStrategy(`agent-${this.suffix}`, Wait.forHttp('/probes/readiness', 1569).forStatusCode(200))
.up();
}

Expand Down
2 changes: 2 additions & 0 deletions workspace/tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
"declarationMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"inlineSourceMap": true,
"inlineSources": false,
"isolatedModules": true,
"moduleResolution": "node",
"noEmit": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"preserveWatchOutput": true,
Expand Down