Skip to content

Commit

Permalink
feat(containers): dockerfile and compose API helpers (#442)
Browse files Browse the repository at this point in the history
* feat(container): dockerfile and docker-compose API helpers

* ci: debug

* ci: debug

* ci: debug

* ci: debug

* ci: disable container tests for Windows

* ci: disable container tests for Windows

* chore: include detach option

* chore: remove old helper

* docs: improve documentation
  • Loading branch information
wellwelwel authored Jun 24, 2024
1 parent 467b462 commit ed3d8c6
Show file tree
Hide file tree
Showing 69 changed files with 1,816 additions and 1,011 deletions.
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
[deno-version-url]: https://github.com/denoland/deno
[typescript-url]: https://github.com/microsoft/TypeScript
[ci-linux-url]: https://github.com/wellwelwel/poku/actions/workflows/ci_coverage-linux.yml?query=branch%3Amain
[ci-linux-image]: https://img.shields.io/github/actions/workflow/status/wellwelwel/poku/ci_coverage-linux.yml?event=push&style=flat-square&label=&branch=main&logo=ubuntu&logoColor=white
[ci-linux-image]: https://img.shields.io/github/actions/workflow/status/wellwelwel/poku/ci_coverage-linux.yml?event=push&label=&branch=main&logo=ubuntu&logoColor=white
[ci-osx-url]: https://github.com/wellwelwel/poku/actions/workflows/ci_coverage-osx.yml?query=branch%3Amain
[ci-osx-image]: https://img.shields.io/github/actions/workflow/status/wellwelwel/poku/ci_coverage-osx.yml?event=push&style=flat-square&label=&branch=main&logo=apple&logoColor=white
[ci-osx-image]: https://img.shields.io/github/actions/workflow/status/wellwelwel/poku/ci_coverage-osx.yml?event=push&label=&branch=main&logo=apple&logoColor=white
[ci-windows-url]: https://github.com/wellwelwel/poku/actions/workflows/ci_coverage-windows.yml?query=branch%3Amain
[ci-windows-image]: https://img.shields.io/github/actions/workflow/status/wellwelwel/poku/ci_coverage-windows.yml?event=push&style=flat-square&label=&branch=main&logo=windows&logoColor=white
[ci-windows-image]: https://img.shields.io/github/actions/workflow/status/wellwelwel/poku/ci_coverage-windows.yml?event=push&label=&branch=main&logo=windows&logoColor=white
[ql-url]: https://github.com/wellwelwel/poku/actions/workflows/ci_codeql.yml?query=branch%3Amain
[ql-image]: https://img.shields.io/github/actions/workflow/status/wellwelwel/poku/ci_codeql.yml?event=push&style=flat-square&label=&branch=main&logo=github&logoColor=white
[coverage-image]: https://img.shields.io/codecov/c/github/wellwelwel/poku?style=flat-square&label=Coverage
[ql-image]: https://img.shields.io/github/actions/workflow/status/wellwelwel/poku/ci_codeql.yml?event=push&label=&branch=main&logo=github&logoColor=white
[coverage-image]: https://img.shields.io/codecov/c/github/wellwelwel/poku
[coverage-url]: https://app.codecov.io/github/wellwelwel/poku
[downloads-image]: https://img.shields.io/npm/dt/poku.svg?style=flat-square&label=Downloads&logo=npm&logoColor=white&color=1e90ff
[downloads-image]: https://img.shields.io/npm/dt/poku.svg?logo=npm&logoColor=white&color=1e90ff
[downloads-url]: https://www.npmjs.com/package/poku
[license-url]: https://github.com/wellwelwel/poku/blob/main/LICENSE
[license-image]: https://img.shields.io/npm/l/poku.svg?maxAge=2592000&color=9c88ff&style=flat-square&label=License
[license-url]: https://licenses.dev/npm/poku
[license-image]: https://licenses.dev/b/npm/poku

<div align="center">
<img width="125" height="125" alt="Logo" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/poku.svg">
Expand All @@ -40,15 +40,14 @@ Enjoying **Poku**? Give him a star to show your support 🌟
**Poku** makes testing easy and takes on the testers' difficulties to _let you focus on your tests_:

<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> No configurations<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Auto detect **ESM** and **CJS**<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Auto detect **Typescript** files<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Auto detect **ESM**, **CJS**, and **Typescript** files<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Run the same test suite for [**Node.js**][node-version-url] _(6+)_, [**Bun**][bun-version-url] and [**Deno**][deno-version-url]<br />

<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Easier and Less Verbose<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> [**Node.js**][node-version-url] familiar **API**<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Instantly re-run related tests in `watch` mode<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Run **CJS** (**CommonJS**) files directly with [**Deno**][deno-version-url]<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Easily handle **servers**, **services**, **processes**, and **ports**<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Easily handle **containers**, **servers**, **services**, **processes**, and **ports**<br />

<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Safety and Reliability<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> High **isolation** level per file<br />
Expand Down Expand Up @@ -168,19 +167,19 @@ deno run npm:poku

### Essentials

- [**poku**](https://poku.io/docs/category/poku) _(test runner)_
- [**poku**](https://poku.io/docs/category/-poku) _(test runner)_
- [**assert**](https://poku.io/docs/documentation/assert) _(test assertion)_

### Helpers

- [**test**](https://poku.io/docs/documentation/helpers/test)
, [**describe**](https://poku.io/docs/documentation/helpers/describe) and [**it**](https://poku.io/docs/documentation/helpers/it) _(organize, group, and isolate tests)_
- [**test**](https://poku.io/docs/documentation/helpers/test), [**describe**](https://poku.io/docs/documentation/helpers/describe) and [**it**](https://poku.io/docs/documentation/helpers/it) _(organize, group, and isolate tests)_
- [**watch**](https://poku.io/docs/documentation/poku/options/watch) _(watch for changes and re-run related test files)_
- [**beforeEach**](https://poku.io/docs/category/beforeeach-and-aftereach) and [**afterEach**](https://poku.io/docs/category/beforeeach-and-aftereach) _(hooks for test setup and teardown)_
- [**startScript**](https://poku.io/docs/documentation/startScript) _(run **package.json** scripts in background)_
- [**startService**](https://poku.io/docs/documentation/startService) _(run files in background)_
- [**kill**](https://poku.io/docs/documentation/processes/kill) _(terminate ports, port ranges, and PIDs)_
- [**getPIDs**](https://poku.io/docs/documentation/processes/get-pids) _(debug processes IDs using ports and port ranges)_
- [**beforeEach**](https://poku.io/docs/category/-before-and-after-each) and [**afterEach**](https://poku.io/docs/category/-before-and-after-each) _(hooks for test setup and teardown)_
- [**docker**](https://poku.io/docs/documentation/helpers/containers) _(build, start, compose, stop, remove, and test containers)_
- [**startScript**](https://poku.io/docs/documentation/helpers/startScript) _(run **package.json** scripts in background)_
- [**startService**](https://poku.io/docs/documentation/helpers/startService) _(run files in background)_
- [**kill**](https://poku.io/docs/documentation/helpers/processes/kill) _(terminate ports, port ranges, and PIDs)_
- [**getPIDs**](https://poku.io/docs/documentation/helpers/processes/get-pids) _(debug processes IDs using ports and port ranges)_
- _and much more_ 👇🏻

---
Expand Down Expand Up @@ -238,7 +237,7 @@ Please check the [**SECURITY.md**](https://github.com/wellwelwel/poku/blob/main/

## Acknowledgements

[![Contributors](https://img.shields.io/github/contributors/wellwelwel/poku?style=flat-square)](https://github.com/wellwelwel/poku/graphs/contributors)
[![Contributors](https://img.shields.io/github/contributors/wellwelwel/poku)](https://github.com/wellwelwel/poku/graphs/contributors)

[![Contributors](https://opencollective.com/poku/contributors.svg?width=890&button=false)](https://github.com/wellwelwel/poku/graphs/contributors)

Expand Down
7 changes: 7 additions & 0 deletions fixtures/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:lts-alpine

WORKDIR /usr/app

COPY src/dockerfile-server.js /usr/app/server.js

CMD ["node", "server.js"]
7 changes: 7 additions & 0 deletions fixtures/docker/compose.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:lts-alpine

WORKDIR /usr/app

COPY src/docker-compose-server.js /usr/app/server.js

CMD ["node", "server.js"]
11 changes: 11 additions & 0 deletions fixtures/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3.9'
services:
server:
container_name: poku-test-docker-compose-server
build:
context: .
dockerfile: compose.Dockerfile
ports:
- '127.0.0.1:6001:6001'
environment:
NODE_ENV: production
1 change: 1 addition & 0 deletions fixtures/docker/src/.some.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

10 changes: 10 additions & 0 deletions fixtures/docker/src/docker-compose-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const http = require('node:http');

const server = http.createServer((_, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});

server.listen(6001);
10 changes: 10 additions & 0 deletions fixtures/docker/src/dockerfile-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const http = require('node:http');

const server = http.createServer((_, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});

server.listen(6000);
138 changes: 138 additions & 0 deletions src/@types/container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
export type DockerfileConfigs = {
/**
* Specifies the imange name
*
* E.g., `"name"`, `"name:tag"`.
*/
tagName: string;

/**
* Specifies the container name.
*/
containerName: string;

/**
* Specifies the **Dockerfile** path
*
* ---
*
* @default "./Dockerfile"
*/
file?: string;

/**
* Specifies the context path of the Dockerfile
*
* - It's different from `cwd`.
*
* ---
*
* @default "."
*/
context?: string;

/**
* Specifies the ports to expose.
*
* E.g., `"6000:6000"`, `"8080:80"`, `"127.0.0.1:3306:3306"`.
*/
ports?: string[];

/**
* Specifies the container environments variables.
*
* E.g, `"VAR1"`, `"VAR1=value1"`
*/
environments?: string[];

/**
* Specifies a `.env` path to **Dockerfile**.
*/
envFile?: string;

/**
* Forces the image build without cache.
*/
cache?: boolean;

/**
* Doesn't run the container in the background and returns the container's process exit result (boolean).
*
* - Set to `false` to test whether a container was executed and finished successfully.
*
* ---
*
* @default true
*/
detach?: boolean;

/**
* Defines the root directory where the process will run.
*
* ---
*
* @default "."
*/
cwd?: string;

/**
* Show logs from **Docker** in real time.
*/
verbose?: boolean;
};

export type DockerComposeConfigs = {
/**
* Specifies the **docker-compose.yml** path
*
* ---
*
* @default "./docker-compose.yml"
*/
file?: string;

/**
* Specifies the project name.
*/
projectName?: string;

/**
* Specifies a `.env` path to **docker-compose.yml**.
*/
envFile?: string;

/**
* Defines the root directory where the process will run.
*
* ---
*
* @default "."
*/
cwd?: string;

/**
* Forces the images (**Dockerfile**) to be rebuilt.
*/
build?: boolean;

/**
* Starts only a specific **docker-compose.yml** service.
*/
serviceName?: string;

/**
* Doesn't run the container in the background and returns the container's process exit result (boolean).
*
* - Set to `false` to test whether a container was executed and finished successfully.
*
* ---
*
* @default true
*/
detach?: boolean;

/**
* Show logs from **Docker** in real time.
*/
verbose?: boolean;
};
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ export { publicListFiles as listFiles } from './modules/list-files-sync.js';
export { startService, startScript } from './modules/create-service.js';
export { getPIDs, kill } from './modules/processes.js';
export { exit } from './modules/exit.js';
export { docker } from './modules/container.js';
export type { Code } from './@types/code.js';
export type { Configs } from './@types/poku.js';
export type { Configs as ListFilesConfigs } from './@types/list-files.js';
export type {
DockerComposeConfigs,
DockerfileConfigs,
} from './@types/container.js';
export type {
StartServiceOptions,
StartScriptOptions,
Expand Down
21 changes: 21 additions & 0 deletions src/modules/container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* c8 ignore next */
import {
DockerComposeConfigs,
DockerfileConfigs,
} from '../@types/container.js';
import { DockerCompose, DockerContainer } from '../services/container.js';

/**
*
* A minimal API to assist tests that require containers or tests that run inside containers using a **Dockerfile**.
*/
const dockerfile = (configs: DockerfileConfigs) => new DockerContainer(configs);

/**
*
* A minimal API to assist tests that require containers or tests that run inside containers using a **docker-compose.yml**.
*/
const compose = (configs: DockerComposeConfigs) => new DockerCompose(configs);

/* c8 ignore next */ // c8 bug
export const docker = { dockerfile, compose };
Loading

0 comments on commit ed3d8c6

Please sign in to comment.