Skip to content

Commit

Permalink
e2e-test: get containers and red test working
Browse files Browse the repository at this point in the history
  • Loading branch information
djm2k committed Jul 22, 2024
1 parent e53ddee commit eceba35
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 39 deletions.
29 changes: 9 additions & 20 deletions docker/tests/container-util.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
import { writeFile } from 'fs/promises';
import { GenericContainer, Wait } from 'testcontainers';

/**
* Add a log consumer to container which captures and logs events.
* @param {GenericContainer} container
* @param {string} logPrefix
* @returns {GenericContainer}
*/
export function consumeLogsWithPrefix(container, logPrefix) {
return container.withLogConsumer((stream) => {
stream.on('data', (line) => console.log(logPrefix + line));
stream.on('err', (line) => console.error(logPrefix + line));
stream.on('end', () => console.log(logPrefix + 'Stream closed'));
});

Check failure on line 4 in docker/tests/container-util.js

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
export async function buildActionServer(buildContextPath) {
return GenericContainer.fromDockerfile(buildContextPath).build();

Check failure on line 6 in docker/tests/container-util.js

View workflow job for this annotation

GitHub Actions / lint

Delete `·`
}

/**
* Start an `actual-server` instance from the local Dockerfile.
* @param {GenericContainer} newContainer
* @returns {Promise<import('testcontainers').StartedTestContainer>}
*/
export async function startActualContainer() {
let newContainer = new GenericContainer('../../Dockerfile')
.withExposedPorts(5006)
.withWaitStrategy(Wait.forListeningPorts());

newContainer = consumeLogsWithPrefix(newContainer, 'ActualServer: ');
export async function startActualContainer(newContainer) {
newContainer = newContainer.withExposedPorts(5006)

Check failure on line 15 in docker/tests/container-util.js

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎····`
.withWaitStrategy(Wait.forListeningPorts());

Check failure on line 16 in docker/tests/container-util.js

View workflow job for this annotation

GitHub Actions / lint

Delete `·························`

return newContainer.start();
}
Expand All @@ -37,9 +26,9 @@ export async function startActualContainer() {
* @returns {Promise<import('testcontainers').StartedTestContainer>}
*/
export async function startCaddyContainer(actualServerPort) {
let caddyContainer = new GenericContainer('caddyContainer:latest')
let caddyContainer = new GenericContainer('caddy:latest')
.withExposedPorts(80)
.withWaitStrategy(Wait.forListeningPorts());
.withWaitStrategy(Wait.forHttp('/', 80).forStatusCode(200));

await writeFile(
'./Caddyfile',
Expand Down
36 changes: 17 additions & 19 deletions docker/tests/reverse-proxies.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { unlink } from 'fs/promises';
import { request } from 'http';
import { startActualContainer, startCaddyContainer } from './container-util.js';
import request from 'supertest';
import { buildActionServer, startActualContainer, startCaddyContainer } from './container-util.js';

Check failure on line 3 in docker/tests/reverse-proxies.test.js

View workflow job for this annotation

GitHub Actions / lint

Replace `·buildActionServer,·startActualContainer,·startCaddyContainer·` with `⏎··buildActionServer,⏎··startActualContainer,⏎··startCaddyContainer,⏎`

// Requires testcontainers:
// `yarn add -D testcontainers`
Expand All @@ -24,32 +24,30 @@ import { startActualContainer, startCaddyContainer } from './container-util.js';
// DEBUG=testcontainers* DOCKER_HOST=unix:///var/run/docker.sock yarn run e2e-test
// https://node.testcontainers.org/configuration/

let newActualServerBuild = await buildActionServer('./');
describe('Actual Server with Caddy', () => {
let actualServerContainer;
let caddyContainer;

beforeAll(async () => {
actualServerContainer = await startActualContainer();
actualServerContainer = await startActualContainer(newActualServerBuild);
caddyContainer = await startCaddyContainer(
actualServerContainer.getMappedPort(5006),
);
}, 61 * 1000);
}, 66 * 1000);

it('should work with default config', async () => {
const host = caddyContainer.getHost();
console.log('Caddy host: ' + host);
request(
{
hostname: host,
port: caddyContainer.getMappedPort(80),
method: 'get',
path: '/health',
},
(res) => {
expect(res.statusCode).toBe(200);
},
);
}, 30000);
it('should not allow login with no password', async () => {
const hostname = caddyContainer.getHost();
const port = caddyContainer.getMappedPort(80);
const caddyHost = `${hostname}:${port}`;
console.log('Caddy host: ' + caddyHost);

Check failure on line 44 in docker/tests/reverse-proxies.test.js

View workflow job for this annotation

GitHub Actions / lint

Delete `····`
const caddyRequest = request(caddyHost);

Check failure on line 46 in docker/tests/reverse-proxies.test.js

View workflow job for this annotation

GitHub Actions / lint

Delete `····`
caddyRequest.post('/account/login').expect({"status":"error","reason":"invalid-password"}, (err) => {

Check failure on line 47 in docker/tests/reverse-proxies.test.js

View workflow job for this annotation

GitHub Actions / lint

Replace `.post('/account/login').expect({"status":"error","reason":"invalid-password"` with `⏎······.post('/account/login')⏎······.expect({·status:·'error',·reason:·'invalid-password'·`
throw err;

Check failure on line 48 in docker/tests/reverse-proxies.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
});

Check failure on line 49 in docker/tests/reverse-proxies.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
});

afterAll(async () => {
if (caddyContainer) await caddyContainer.stop();
Expand Down

0 comments on commit eceba35

Please sign in to comment.