Skip to content

Commit

Permalink
feat: check user for tester container and env file (#57)
Browse files Browse the repository at this point in the history
* feat: check user for tester container and env file
* fix: missing space
* fix: lint
  • Loading branch information
pajgo authored Jul 20, 2022
1 parent 4d9f9bf commit a3f7de4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bin/cmds/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ exports.builder = (yargs) => (
type: 'boolean',
default: false,
})
.option('env-file', {
alias: 'env_file',
describe: '.env file for docker-compose',
type: 'string',
})
.help()
);
exports.handler = () => {};
38 changes: 38 additions & 0 deletions bin/cmds/test_cmds/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ exports.handler = async (argv) => {
}
}

if (argv.envFile) {
composeArgs.unshift('--env-file', argv.envFile);
}

// start containers
if ((await echoAndExec(compose, [...composeArgs, 'up', '-d'])).exitCode !== 0) {
echo('failed to start docker containers. Exit 128');
Expand Down Expand Up @@ -199,6 +203,40 @@ exports.handler = async (argv) => {
};
}

async function checkUser(name) {
echo(`checking container user ${name}`);

try {
await dockerExec('getent', ['passwd', name], { user: 'root' });
return true;
} catch (e) {
return false;
}
}

async function createUser(name, uid) {
echo(`create user "${name}"`);

const userExtraArgs = uid ? ['-u', uid, '-g', uid] : [];
await dockerExec('adduser', ['-D', ...userExtraArgs, name], { user: 'root' });
}

if (!argv.isRootless) {
const uid = process.getuid();

if (!(await checkUser(uid))) {
createUser('tester', uid);
}

if (argv.ruser && !(await checkUser(argv.ruser))) {
createUser(argv.ruser);
}

if (argv.tuser && !(await checkUser(argv.tuser))) {
createUser(argv.tuser);
}
}

// easy way to wait for containers, can do improved detection, but it's not generic
if (argv.rebuild.length > 0) {
echo('rebuilding modules');
Expand Down

0 comments on commit a3f7de4

Please sign in to comment.