Skip to content

Commit

Permalink
fix and refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienMattiussi committed Jul 19, 2023
1 parent 18aada9 commit 0da08fe
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .envfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GREENFRAME_MY_VAR_ONE=envfile_value_one
GREENFRAME_MY_VAR_TWO=envfile_value_two
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@cliqz/adblocker-playwright": "^1.25.0",
"@kubernetes/client-node": "^0.17.0",
"@oclif/core": "^1.2.0",
"@playwright/test": "^1.36.1",
"@playwright/test": "^1.30.0",
"@sentry/node": "^6.13.3",
"axios": "^0.22.0",
"core-js-pure": "^3.24.0",
Expand Down
4 changes: 2 additions & 2 deletions src/commands/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ class AnalyzeCommand extends Command {
timezoneId: Flags.boolean({
description: 'Set greenframe browser timezoneId',
}),
customEnvVars: Flags.string({
envVar: Flags.string({
char: 'e',
description: 'List of environment vars to read in the scenarios',
required: false,
multiple: true,
}),
customEnvVarsFile: Flags.string({
envFile: Flags.string({
char: 'E',
description: 'File of environment vars',
required: false,
Expand Down
2 changes: 2 additions & 0 deletions src/examples/.envfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GREENFRAME_MY_VAR_ONE=envfile_value_one
GREENFRAME_MY_VAR_TWO=envfile_value_two
7 changes: 7 additions & 0 deletions src/examples/commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
GREENFRAME_MY_VAR_ONE=inline_value_one greenframe analyze https://www.google.fr ../../src/examples/envvar.inline.isolated.js -e GREENFRAME_MY_VAR_ONE -e GREENFRAME_MY_VAR_TWO=${GREENFRAME_MY_VAR_TWO}

greenframe analyze https://www.google.fr ../../src/examples/envvar.inline.envfile.js -E ./src/examples/.envfile

GREENFRAME_MY_VAR_ONE=inline_value_one greenframe analyze -C ./src/examples/envvar.config.isolated.yml

greenframe analyze -C ./src/examples/envvar.config.envfile.yml
14 changes: 14 additions & 0 deletions src/examples/envvar.config.envfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { expect } = require('@playwright/test');
const myVar1 = process.env.GREENFRAME_MY_VAR_ONE;
const myVar2 = process.env.GREENFRAME_MY_VAR_TWO;

const visit = async (page) => {
expect(myVar1).toBe('envfile_value_one');
expect(myVar2).toBe('envfile_value_two');
await page.goto('', {
waitUntil: 'networkidle',
});
await page.scrollToEnd();
};

module.exports = visit;
7 changes: 7 additions & 0 deletions src/examples/envvar.config.envfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
scenarios:
- path: '../../src/examples/envvar.config.envfile.js'
name: 'Visit'
threshold: 0.03
baseURL: 'https://www.google.fr'
envFile: './src/examples/.envfile'
projectName: 'test_visit'
14 changes: 14 additions & 0 deletions src/examples/envvar.config.isolated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { expect } = require('@playwright/test');
const myVar1 = process.env.GREENFRAME_MY_VAR_ONE;
const myVar2 = process.env.GREENFRAME_MY_VAR_TWO;

const visit = async (page) => {
expect(myVar1).toBe('inline_value_one');
expect(myVar2).toBe('defined_value_two');
await page.goto('', {
waitUntil: 'networkidle',
});
await page.scrollToEnd();
};

module.exports = visit;
9 changes: 9 additions & 0 deletions src/examples/envvar.config.isolated.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
scenarios:
- path: '../../src/examples/envvar.config.isolated.js'
name: 'Visit'
threshold: 0.03
baseURL: 'https://www.google.fr'
envVar:
- GREENFRAME_MY_VAR_ONE
- GREENFRAME_MY_VAR_TWO=${GREENFRAME_MY_VAR_TWO}
projectName: 'test_visit'
14 changes: 14 additions & 0 deletions src/examples/envvar.inline.envfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { expect } = require('@playwright/test');
const myVar1 = process.env.GREENFRAME_MY_VAR_ONE;
const myVar2 = process.env.GREENFRAME_MY_VAR_TWO;

const visit = async (page) => {
expect(myVar1).toBe('envfile_value_one');
expect(myVar2).toBe('envfile_value_two');
await page.goto('', {
waitUntil: 'networkidle',
});
await page.scrollToEnd();
};

module.exports = visit;
14 changes: 14 additions & 0 deletions src/examples/envvar.inline.isolated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { expect } = require('@playwright/test');
const myVar1 = process.env.GREENFRAME_MY_VAR_ONE;
const myVar2 = process.env.GREENFRAME_MY_VAR_TWO;

const visit = async (page) => {
expect(myVar1).toBe('inline_value_one');
expect(myVar2).toBe('defined_value_two');
await page.goto('', {
waitUntil: 'networkidle',
});
await page.scrollToEnd();
};

module.exports = visit;
40 changes: 17 additions & 23 deletions src/services/container/execScenarioContainer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const fs = require('node:fs');
const util = require('node:util');
const path = require('node:path');
const exec = util.promisify(require('node:child_process').exec);
Expand All @@ -9,11 +8,7 @@ const initDebug = require('debug');
const PROJECT_ROOT = path.resolve(__dirname, '../../../');
const debug = initDebug('greenframe:services:container:execScenarioContainer');

const createContainer = async (
extraHosts = [],
customEnvVars = [],
customEnvVarsFile = ''
) => {
const createContainer = async (extraHosts = [], envVars = [], envFile = '') => {
const { stdout } = await exec(`${PROJECT_ROOT}/dist/bash/getHostIP.sh`);
const HOSTIP = stdout;
const extraHostsFlags = extraHosts
Expand All @@ -23,15 +18,12 @@ const createContainer = async (
const extraHostsEnv =
extraHosts.length > 0 ? ` -e EXTRA_HOSTS=${extraHosts.join(',')}` : '';

const envVars =
customEnvVars.length > 0
? await buildEnvVarList(customEnvVars, customEnvVarsFile)
: '';
const envString = await buildEnvVarList(envVars, envFile);

debug(`Creating container ${CONTAINER_DEVICE_NAME} with extraHosts: ${extraHosts}`);

const dockerCleanPreviousCommand = `docker rm -f ${CONTAINER_DEVICE_NAME}`;
const allEnvVars = ` -e HOSTIP=${HOSTIP}${extraHostsEnv}${envVars}`;
const allEnvVars = ` -e HOSTIP=${HOSTIP}${extraHostsEnv}${envString}`;
const dockerCreateCommand = `docker create --tty --name ${CONTAINER_DEVICE_NAME} --rm ${allEnvVars} --add-host localhost:${HOSTIP} ${extraHostsFlags} mcr.microsoft.com/playwright:v1.30.0-focal`;

const dockerStatCommand = `${dockerCleanPreviousCommand} && ${dockerCreateCommand}`;
Expand Down Expand Up @@ -113,20 +105,22 @@ const stopContainer = async () => {
return 'OK';
};

const buildEnvVarList = async (customEnvVars, customEnvVarsFile) => {
let uniqueEnvVars = [...new Set(customEnvVars)];
const uniqueEnvVarsString = uniqueEnvVars.reduce((list, envVarName) => {
if (envVarName.includes('=')) {
return `${list} -e ${envVarName} `;
}

const envVarValue = process.env[envVarName];
return `${list} -e ${envVarName}=${envVarValue} `;
}, '');
const buildEnvVarList = async (envVars, envFile) => {
const envVarString =
envVars.length > 0
? envVars.reduce((list, envVarName) => {
if (envVarName.includes('=')) {
return `${list} -e ${envVarName} `;
}

const envVarValue = process.env[envVarName];
return `${list} -e ${envVarName}=${envVarValue} `;
}, '')
: '';

const envVarFileString = customEnvVarsFile ? ` --env-file ${customEnvVarsFile}` : '';
const envVarFileString = envFile ? ` --env-file ${envFile}` : '';

return `${uniqueEnvVarsString} ${envVarFileString}`;
return `${envVarString} ${envVarFileString}`;
};

module.exports = {
Expand Down
10 changes: 5 additions & 5 deletions src/services/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export const executeScenarioAndGetContainerStats = async ({
kubeContainers = [],
kubeDatabaseContainers = [],
extraHosts = [],
customEnvVars = [],
customEnvVarsFile = '',
envVars = [],
envFile = '',
dockerdHost,
dockerdPort,
}: {
Expand All @@ -60,15 +60,15 @@ export const executeScenarioAndGetContainerStats = async ({
kubeContainers?: string[];
kubeDatabaseContainers?: string[];
extraHosts?: string[];
customEnvVars?: string[];
customEnvVarsFile?: string;
envVars?: string[];
envFile?: string;
dockerdHost?: string;
dockerdPort?: number;
}) => {
try {
debug('Starting container');
await stopContainer();
await createContainer(extraHosts, customEnvVars, customEnvVarsFile);
await createContainer(extraHosts, envVars, envFile);
await startContainer();
debug('Container started');
let allContainers: {
Expand Down
8 changes: 4 additions & 4 deletions src/services/parseConfigFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const parseConfigFile = async (path) => {
kubeContainers,
kubeDatabaseContainers,
extraHosts,
customEnvVars,
customEnvVarsFile,
envVar,
envFile,
kubeConfig,
dockerdHost,
dockerdPort,
Expand All @@ -49,8 +49,8 @@ const parseConfigFile = async (path) => {
kubeContainers,
kubeDatabaseContainers,
extraHosts,
customEnvVars,
customEnvVarsFile,
envVar,
envFile,
kubeConfig,
dockerdHost,
dockerdPort,
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/runScenariosAndSaveResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export default async (ctx: any) => {
kubeContainers: flags.kubeContainers,
kubeDatabaseContainers: flags.kubeDatabaseContainers,
extraHosts: flags.extraHosts,
customEnvVars: flags.customEnvVars,
customEnvVarsFile: flags.customEnvVarsFile,
envVars: flags.envVar,
envFile: flags.envFile,
dockerdHost: flags.dockerdHost,
dockerdPort: flags.dockerdPort,
ignoreHTTPSErrors: flags.ignoreHTTPSErrors,
Expand Down
10 changes: 0 additions & 10 deletions src/visit.yml

This file was deleted.

4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2249,7 +2249,7 @@ __metadata:
languageName: node
linkType: hard

"@playwright/test@npm:^1.36.1":
"@playwright/test@npm:^1.30.0":
version: 1.36.1
resolution: "@playwright/test@npm:1.36.1"
dependencies:
Expand Down Expand Up @@ -7057,7 +7057,7 @@ __metadata:
"@cliqz/adblocker-playwright": ^1.25.0
"@kubernetes/client-node": ^0.17.0
"@oclif/core": ^1.2.0
"@playwright/test": ^1.36.1
"@playwright/test": ^1.30.0
"@sentry/node": ^6.13.3
"@types/axios": ^0.14.0
"@types/babel__core": ^7.1.19
Expand Down

0 comments on commit 0da08fe

Please sign in to comment.