From a864498d16f62dc3760f65471b2a2797cd4d1aaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Sat, 4 Dec 2021 23:17:07 +0100 Subject: [PATCH] Fixing docker-compose up linking to wrong directory when started in container --- CONTRIBUTING.md | 2 +- docker-compose.testcafe.yml | 5 +- docker-compose.yaml | 4 -- tests/tests/utils/containers.ts | 94 +++++++++++---------------------- 4 files changed, 34 insertions(+), 71 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2cf9435e..e5d406cf 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -97,5 +97,5 @@ the local tests). ```console $ LIVE_RELOAD=0 docker-compose up -d # Wait 2-3 minutes for the environment to start, then: -$ docker-compose -f docker-compose.testcafe.yaml up +$ PROJECT_DIR=$(pwd) docker-compose -f docker-compose.testcafe.yml up ``` diff --git a/docker-compose.testcafe.yml b/docker-compose.testcafe.yml index 88a4adeb..9c7d5115 100644 --- a/docker-compose.testcafe.yml +++ b/docker-compose.testcafe.yml @@ -7,13 +7,10 @@ services: user: root environment: BROWSER: "chromium --use-fake-device-for-media-stream" + PROJECT_DIR: ${PROJECT_DIR} volumes: - ./:/project - ./maps:/maps - /var/run/docker.sock:/var/run/docker.sock # security_opt: # - seccomp:unconfined - -networks: - default: - name: workadventure_dev diff --git a/docker-compose.yaml b/docker-compose.yaml index 2eb8aedc..17e04f7c 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -239,7 +239,3 @@ services: # #- --cert=/root/letsencrypt/fullchain.pem # #- --pkey=/root/letsencrypt/privkey.pem # network_mode: host - -networks: - default: - name: workadventure_dev diff --git a/tests/tests/utils/containers.ts b/tests/tests/utils/containers.ts index 454340ff..bed2bd48 100644 --- a/tests/tests/utils/containers.ts +++ b/tests/tests/utils/containers.ts @@ -4,6 +4,30 @@ import Dockerode = require( 'dockerode') const util = require('util'); const exec = util.promisify(require('child_process').exec); const { execSync } = require('child_process'); +const path = require("path"); +const fs = require('fs'); + +/** + * Execute Docker compose, passing the correct host directory (in case this is run from the TestCafe container) + */ +export function dockerCompose(command: string): void { + let param = ''; + const projectDir = process.env.PROJECT_DIR; + + if (!projectDir && fs.existsSync('/project')) { + // We are probably in the docker-image AND we did not pass PROJECT_DIR env variable + throw new Error('Incorrect docker-compose command used to fire testcafe tests. You need to add a PROJECT_DIR environment variable. Please refer to the CONTRIBUTING.md guide'); + } + + if (projectDir) { + const dirName = path.basename(projectDir); + param = '--project-name '+dirName+' --project-directory '+projectDir; + } + + let stdout = execSync('docker-compose '+param+' '+command, { + cwd: __dirname + '/../../../' + }); +} /** * Returns a container ID based on the container name. @@ -35,81 +59,27 @@ export async function startContainer(container: Dockerode.ContainerInfo): Promis } export async function rebootBack(): Promise { - let stdout = execSync('docker-compose up --force-recreate -d back', { - cwd: __dirname + '/../../../' - }); - /*const container = await findContainer('back'); - await stopContainer(container); - await startContainer(container);*/ + dockerCompose('up --force-recreate -d back'); } export function rebootTraefik(): void { - let stdout = execSync('docker-compose up --force-recreate -d reverse-proxy', { - cwd: __dirname + '/../../../' - }); - - //console.log('rebootTraefik', stdout); + dockerCompose('up --force-recreate -d reverse-proxy'); } export async function rebootPusher(): Promise { - let stdout = execSync('docker-compose up --force-recreate -d pusher', { - cwd: __dirname + '/../../../' - }); - /*const container = await findContainer('pusher'); - await stopContainer(container); - await startContainer(container);*/ + dockerCompose('up --force-recreate -d pusher'); } export async function resetRedis(): Promise { - let stdout = execSync('docker-compose stop redis', { - cwd: __dirname + '/../../../' - }); - //console.log('rebootRedis', stdout); - - stdout = execSync('docker-compose rm -f redis', { - cwd: __dirname + '/../../../' - }); - //console.log('rebootRedis', stdout); - - stdout = execSync('docker-compose up --force-recreate -d redis', { - cwd: __dirname + '/../../../' - }); - - //console.log('rebootRedis', stdout); -/* - let stdout = execSync('docker-compose stop redis', { - cwd: __dirname + '/../../../' - }); - console.log('stdout:', stdout); - stdout = execSync('docker-compose rm redis', { - cwd: __dirname + '/../../../' - }); - //const { stdout, stderr } = await exec('docker-compose down redis'); - console.log('stdout:', stdout); - //console.log('stderr:', stderr); - const { stdout2, stderr2 } = await exec('docker-compose up -d redis'); - console.log('stdout:', stdout2); - console.log('stderr:', stderr2); -*/ - /*const container = await findContainer('redis'); - //await stopContainer(container); - //await startContainer(container); - - const docker = new Dockerode(); - await docker.getContainer(container.Id).stop(); - await docker.getContainer(container.Id).remove(); - const newContainer = await docker.createContainer(container); - await newContainer.start();*/ + dockerCompose('stop redis'); + dockerCompose('rm -f redis'); + dockerCompose('up --force-recreate -d redis'); } export function stopRedis(): void { - let stdout = execSync('docker-compose stop redis', { - cwd: __dirname + '/../../../' - }); + dockerCompose('stop redis'); } export function startRedis(): void { - let stdout = execSync('docker-compose start redis', { - cwd: __dirname + '/../../../' - }); + dockerCompose('start redis'); }