mirror of
https://github.com/tale/headplane.git
synced 2026-08-13 07:06:51 +00:00
test: add docker and proc e2e testing
This commit is contained in:
@@ -1,51 +1,54 @@
|
||||
import { join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import tc from 'testcontainers';
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import tc from "testcontainers";
|
||||
|
||||
export interface HeadscaleEnv {
|
||||
container: tc.StartedTestContainer;
|
||||
apiUrl: string;
|
||||
apiKey: string;
|
||||
container: tc.StartedTestContainer;
|
||||
apiUrl: string;
|
||||
apiKey: string;
|
||||
}
|
||||
|
||||
export interface HeadscaleOptions {
|
||||
labels?: Record<string, string>;
|
||||
}
|
||||
|
||||
const cwd = fileURLToPath(import.meta.url);
|
||||
const config = join(cwd, '..', 'config.yaml');
|
||||
const config = join(cwd, "..", "config.yaml");
|
||||
|
||||
export async function startHeadscale(version: string): Promise<HeadscaleEnv> {
|
||||
const container = await new tc.GenericContainer(
|
||||
`headscale/headscale:${version}`,
|
||||
)
|
||||
.withExposedPorts(8080)
|
||||
.withWaitStrategy(
|
||||
tc.Wait.forHttp('/health', 8080)
|
||||
.withStartupTimeout(30_000)
|
||||
.forStatusCode(200),
|
||||
)
|
||||
.withCopyFilesToContainer([
|
||||
{
|
||||
source: config,
|
||||
target: '/etc/headscale/config.yaml',
|
||||
},
|
||||
])
|
||||
.withCommand(['serve'])
|
||||
.start();
|
||||
export async function startHeadscale(
|
||||
version: string,
|
||||
options?: HeadscaleOptions,
|
||||
): Promise<HeadscaleEnv> {
|
||||
let builder = new tc.GenericContainer(`headscale/headscale:${version}`)
|
||||
.withExposedPorts(8080)
|
||||
.withWaitStrategy(
|
||||
tc.Wait.forHttp("/health", 8080).withStartupTimeout(30_000).forStatusCode(200),
|
||||
)
|
||||
.withCopyFilesToContainer([
|
||||
{
|
||||
source: config,
|
||||
target: "/etc/headscale/config.yaml",
|
||||
},
|
||||
])
|
||||
.withCommand(["serve"]);
|
||||
|
||||
const host = container.getHost();
|
||||
const port = container.getMappedPort(8080);
|
||||
const apiUrl = `http://${host}:${port}`;
|
||||
if (options?.labels) {
|
||||
builder = builder.withLabels(options.labels);
|
||||
}
|
||||
|
||||
const exec = await container.exec([
|
||||
'headscale',
|
||||
'apikeys',
|
||||
'create',
|
||||
'-o',
|
||||
'json',
|
||||
]);
|
||||
const container = await builder.start();
|
||||
|
||||
if (exec.exitCode !== 0) {
|
||||
throw new Error(`headscale apikeys create failed:\n${exec.stderr}`);
|
||||
}
|
||||
const host = container.getHost();
|
||||
const port = container.getMappedPort(8080);
|
||||
const apiUrl = `http://${host}:${port}`;
|
||||
|
||||
const apiKey = JSON.parse(exec.stdout.toString());
|
||||
return { container, apiUrl, apiKey };
|
||||
const exec = await container.exec(["headscale", "apikeys", "create", "-o", "json"]);
|
||||
|
||||
if (exec.exitCode !== 0) {
|
||||
throw new Error(`headscale apikeys create failed:\n${exec.stderr}`);
|
||||
}
|
||||
|
||||
const apiKey = JSON.parse(exec.stdout.toString());
|
||||
return { container, apiUrl, apiKey };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user