Skip to content

Commit

Permalink
[features test cmd] Add --remote-env flag
Browse files Browse the repository at this point in the history
ref: #448
  • Loading branch information
joshspicer authored Mar 15, 2023
1 parent e3d12b3 commit b55b27a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/spec-node/featuresCLI/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function featuresTestOptions(y: Argv) {
'remote-user': { type: 'string', alias: 'u', describe: 'Remote user. Not used for scenarios.', }, // TODO: Optionally replace 'scenario' configs with this value?
'log-level': { choices: ['info' as 'info', 'debug' as 'debug', 'trace' as 'trace'], default: 'info' as 'info', description: 'Log level.' },
'skip-image-metadata': { type: 'boolean', default: false, description: 'Skip applying image metadata to the resultant test image. Image metadata is potentially appended to base images built with the dev container CLI' },
'remote-env': { type: 'string', description: 'Remote environment variables of the format name=value. These will be added when executing the user commands.' },
'quiet': { type: 'boolean', alias: 'q', default: false, description: 'Quiets output' },
})
// DEPRECATED: Positional arguments don't play nice with the variadic/array --features option.
Expand All @@ -39,6 +40,10 @@ export function featuresTestOptions(y: Argv) {
if (argv['filter-scenarios'] && argv['skip-scenarios']) {
throw new Error('Cannot combine --filter-scenarios and --skip-scenarios');
}
const remoteEnvs = (argv['remote-env'] && (Array.isArray(argv['remote-env']) ? argv['remote-env'] : [argv['remote-env']])) as string[] | undefined;
if (remoteEnvs?.some(remoteEnv => !/.+=.+/.test(remoteEnv))) {
throw new Error('Unmatched argument format: remote-env must match <name>=<value>');
}
return true;

});
Expand All @@ -56,6 +61,7 @@ export interface FeaturesTestCommandInput {
skipScenarios: boolean;
skipAutogenerated: boolean;
remoteUser: string | undefined;
remoteEnv: string[] | undefined;
quiet: boolean;
skipImageMetadata: boolean;
logLevel: LogLevel;
Expand All @@ -76,6 +82,7 @@ async function featuresTest({
'skip-scenarios': skipScenarios,
'skip-autogenerated': skipAutogenerated,
'remote-user': remoteUser,
'remote-env': remoteEnvString,
quiet,
'skip-image-metadata': skipImageMetadata,
'log-level': inputLogLevel,
Expand All @@ -94,6 +101,8 @@ async function featuresTest({
// Prefer the new --project-folder option over the deprecated positional argument.
const targetProject = collectionFolder !== '.' ? collectionFolder : collectionFolder_deprecated;

const remoteEnv = (remoteEnvString && (Array.isArray(remoteEnvString) ? remoteEnvString : [remoteEnvString])) as string[] | undefined;

const args: FeaturesTestCommandInput = {
baseImage,
cliHost,
Expand All @@ -108,9 +117,11 @@ async function featuresTest({
skipAutogenerated,
remoteUser,
skipImageMetadata,
remoteEnv,
disposables
};


const exitCode = await doFeaturesTestCommand(args);

await dispose();
Expand Down
4 changes: 2 additions & 2 deletions src/spec-node/featuresCLI/testCommandImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CLIHost } from '../../spec-common/cliHost';
import { launch, ProvisionOptions, createDockerParams } from '../devContainers';
import { doExec } from '../devContainersSpecCLI';
import { LaunchResult, staticExecParams, staticProvisionParams, testLibraryScript } from './utils';
import { DockerResolverParameters } from '../utils';
import { DockerResolverParameters, envListToObj } from '../utils';
import { DevContainerConfig } from '../../spec-configuration/configuration';
import { FeaturesTestCommandInput } from './test';
import { cpDirectoryLocal, rmLocal } from '../../spec-utils/pfs';
Expand Down Expand Up @@ -485,7 +485,7 @@ async function generateDockerParams(workspaceFolder: string, args: FeaturesTestC
persistedFolder: undefined,
additionalMounts: [],
updateRemoteUserUIDDefault: 'never',
remoteEnv: {},
remoteEnv: envListToObj(args.remoteEnv),
additionalCacheFroms: [],
omitLoggerHeader: true,
useBuildKit: 'auto',
Expand Down
21 changes: 21 additions & 0 deletions src/test/container-features/featuresCLICommands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,27 @@ describe('CLI features subcommands', async function () {
const hasExpectedTestReport = result.stdout.includes(expectedTestReport);
assert.isTrue(hasExpectedTestReport);
});

it('lifecycle-hooks-remote-env', async function () {
const collectionFolder = `${__dirname}/example-v2-features-sets/lifecycle-hooks-remote-env`;
let success = false;
let result: ExecResult | undefined = undefined;
try {
result = await shellExec(`${cli} features test --project-folder ${collectionFolder} --remote-env MY_SECRET=you-found-my-secret-string --log-level trace `);
success = true;

} catch (error) {
assert.fail('features test sub-command should not throw');
}

assert.isTrue(success);
assert.isDefined(result);

const expectedTestReport = ` ================== TEST REPORT ==================
✅ Passed: 'puppy'`;
const hasExpectedTestReport = result.stdout.includes(expectedTestReport);
assert.isTrue(hasExpectedTestReport);
});
});

describe('features package', function () {
Expand Down

0 comments on commit b55b27a

Please sign in to comment.