diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7816cf547e..4b725f4ea8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -341,9 +341,9 @@ integration:docker: nix-shell --run $' image="$(docker load --input ./builds/*docker* | cut -d\' \' -f3)" PK_TEST_DOCKER_IMAGE=$image \ - PK_TEST_COMMAND=scripts/docker-run.sh \ + PK_TEST_COMMAND=docker \ PK_TEST_PLATFORM=docker \ - PK_TEST_TMP_DIR=/builds/$CI_PROJECT_PATH/tmp \ + PK_TEST_DATA_DIR=/builds/$CI_PROJECT_PATH/tmp \ exec npm run test -- tests/bin ' rules: diff --git a/jest.config.js b/jest.config.js index 4d36eb3013..7265b4e154 100644 --- a/jest.config.js +++ b/jest.config.js @@ -32,7 +32,7 @@ const globals = { maxTimeout: Math.pow(2, 31) - 1, testCmd: process.env.PK_TEST_COMMAND, testPlatform: process.env.PK_TEST_PLATFORM ?? process.platform, - tmpDir: process.env.PK_TEST_TMP_DIR ?? os.tmpdir(), + tmpDir: process.env.PK_TEST_DATA_DIR ?? os.tmpdir(), }; // The `globalSetup` and `globalTeardown` cannot access the `globals` diff --git a/scripts/docker-run.sh b/scripts/docker-run.sh deleted file mode 100755 index 81142c1906..0000000000 --- a/scripts/docker-run.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env bash - -exec docker run \ - --interactive \ - --rm \ - --network host \ - --pid host \ - --userns host \ - --user "$(id -u)" \ - --mount type=bind,src="$PK_TEST_DATA_PATH",dst="$PK_TEST_DATA_PATH" \ - --env PK_PASSWORD \ - --env PK_NODE_PATH \ - --env PK_RECOVERY_CODE \ - --env PK_TOKEN \ - --env PK_ROOT_KEY \ - --env PK_NODE_ID \ - --env PK_CLIENT_HOST \ - --env PK_CLIENT_PORT \ - "$PK_TEST_DOCKER_IMAGE" \ - polykey "$@" diff --git a/tests/bin/utils.ts b/tests/bin/utils.ts index ba1694e04b..c3c70bb9cf 100644 --- a/tests/bin/utils.ts +++ b/tests/bin/utils.ts @@ -15,6 +15,40 @@ import main from '@/bin/polykey'; import { promise } from '@/utils'; import * as validationUtils from '@/validation/utils'; +const generateDockerArgs = (mountPath: string) => [ + 'run', + '--interactive', + '--rm', + '--network', + 'host', + '--pid', + 'host', + '--userns', + 'host', + `--user`, + `${process.getuid()}`, + '--mount', + `type=bind,src=${mountPath},dst=${mountPath}`, + '--env', + 'PK_PASSWORD', + '--env', + 'PK_NODE_PATH', + '--env', + 'PK_RECOVERY_CODE', + '--env', + 'PK_TOKEN', + '--env', + 'PK_ROOT_KEY', + '--env', + 'PK_NODE_ID', + '--env', + 'PK_CLIENT_HOST', + '--env', + 'PK_CLIENT_PORT', + `${process.env.PK_TEST_DOCKER_IMAGE}`, + 'polykey', +]; + /** * Wrapper for execFile to make it asynchronous and non-blocking */ @@ -262,7 +296,6 @@ async function pkSpawn( /** * Mimics the behaviour of `pkStdio` while running the command as a separate process. * Note that this is incompatible with jest mocking. - * @param cmd - path to the target command relative to the project directory. * @param args - args to be passed to the command. * @param env - environment variables to be passed to the command. * @param cwd - the working directory the command will be executed in. @@ -288,12 +321,15 @@ async function pkStdioTarget( // If using the command override we need to spawn a process env = { - PK_TEST_DATA_PATH: cwd, ...process.env, ...env, }; - const command = path.resolve(path.join(global.projectDir, global.testCmd!)); - const subprocess = child_process.spawn(command, [...args], { + const command = + global.testCmd === 'docker' + ? 'docker' + : path.resolve(path.join(global.projectDir, global.testCmd!)); + const dockerArgs = global.testCmd === 'docker' ? generateDockerArgs(cwd) : []; + const subprocess = child_process.spawn(command, [...dockerArgs, ...args], { env, cwd, stdio: ['pipe', 'pipe', 'pipe'], @@ -319,7 +355,6 @@ async function pkStdioTarget( /** * Execs the target command spawning it as a seperate process - * @param cmd - path to the target command relative to the project directory. * @param args - args to be passed to the command. * @param env Augments env for command execution * @param cwd Defaults to temporary directory @@ -338,7 +373,6 @@ async function pkExecTarget( (await fs.promises.mkdtemp(path.join(global.tmpDir, 'polykey-test-'))), ); env = { - PK_TEST_DATA_PATH: cwd, ...process.env, ...env, }; @@ -347,11 +381,15 @@ async function pkExecTarget( // (if not defined in the env) to ensure no attempted connections. A regular // PolykeyAgent is expected to initially connect to the mainnet seed nodes env['PK_SEED_NODES'] = env['PK_SEED_NODES'] ?? ''; - const command = path.resolve(path.join(global.projectDir, global.testCmd!)); + const command = + global.testCmd === 'docker' + ? 'docker' + : path.resolve(path.join(global.projectDir, global.testCmd!)); + const dockerArgs = global.testCmd === 'docker' ? generateDockerArgs(cwd) : []; return new Promise((resolve, reject) => { child_process.execFile( command, - [...args], + [...dockerArgs, ...args], { env, cwd, @@ -392,7 +430,6 @@ async function pkSpawnTarget( (await fs.promises.mkdtemp(path.join(global.tmpDir, 'polykey-test-'))), ); env = { - PK_TEST_DATA_PATH: cwd, ...process.env, ...env, }; @@ -401,8 +438,12 @@ async function pkSpawnTarget( // (if not defined in the env) to ensure no attempted connections. A regular // PolykeyAgent is expected to initially connect to the mainnet seed nodes env['PK_SEED_NODES'] = env['PK_SEED_NODES'] ?? ''; - const command = path.resolve(path.join(global.projectDir, global.testCmd!)); - const subprocess = child_process.spawn(command, args, { + const command = + global.testCmd === 'docker' + ? 'docker' + : path.resolve(path.join(global.projectDir, global.testCmd!)); + const dockerArgs = global.testCmd === 'docker' ? generateDockerArgs(cwd) : []; + const subprocess = child_process.spawn(command, [...dockerArgs, ...args], { env, cwd, stdio: ['pipe', 'pipe', 'pipe'],