-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathcli.set-up.test.ts
144 lines (103 loc) · 6.46 KB
/
cli.set-up.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import * as path from 'path';
import { shellExec } from './testUtils';
const pkg = require('../../package.json');
describe('Dev Containers CLI', function () {
this.timeout('120s');
const tmp = path.join(__dirname, 'tmp');
const cli = `npx --prefix ${tmp} devcontainer`;
before('Install', async () => {
await shellExec(`rm -rf ${tmp}/node_modules`);
await shellExec(`mkdir -p ${tmp}`);
await shellExec(`npm --prefix ${tmp} install devcontainers-cli-${pkg.version}.tgz`);
});
describe('Command set-up', () => {
it('should succeed and run postAttachCommand from config', async () => {
const containerId = (await shellExec(`docker run -d -e TEST_CE=TEST_VALUE alpine:3.17 sleep inf`)).stdout.trim();
const res = await shellExec(`${cli} set-up --container-id ${containerId} --config ${__dirname}/configs/set-up-with-config/devcontainer.json --include-configuration --include-merged-configuration`);
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'success');
assert.equal(response.configuration?.remoteEnv?.TEST_RE, 'TEST_VALUE');
assert.equal(response.mergedConfiguration?.remoteEnv?.TEST_RE, 'TEST_VALUE');
await shellExec(`docker exec ${containerId} test -f /postAttachCommand.txt`);
await shellExec(`docker rm -f ${containerId}`);
});
it('should succeed and run postCreateCommand from metadata', async () => {
await shellExec(`docker build -t devcontainer-set-up-test ${__dirname}/configs/set-up-with-metadata`);
const containerId = (await shellExec(`docker run -d -e TEST_CE=TEST_VALUE2 devcontainer-set-up-test sleep inf`)).stdout.trim();
const res = await shellExec(`${cli} set-up --container-id ${containerId} --include-configuration --include-merged-configuration`);
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'success');
assert.equal(Object.keys(response.configuration).length, 0);
assert.equal(response.mergedConfiguration?.remoteEnv?.TEST_RE, 'TEST_VALUE2');
await shellExec(`docker exec ${containerId} test -f /postCreateCommand.txt`);
await shellExec(`docker rm -f ${containerId}`);
});
});
describe('Command run-user-commands', () => {
it('should succeed and run postAttachCommand from config', async () => {
const containerId = (await shellExec(`docker run -d alpine:3.17 sleep inf`)).stdout.trim();
const res = await shellExec(`${cli} run-user-commands --container-id ${containerId} --config ${__dirname}/configs/set-up-with-config/devcontainer.json`);
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'success');
await shellExec(`docker exec ${containerId} test -f /postAttachCommand.txt`);
await shellExec(`docker rm -f ${containerId}`);
});
it('should succeed and run postCreateCommand from metadata', async () => {
await shellExec(`docker build -t devcontainer-set-up-test ${__dirname}/configs/set-up-with-metadata`);
const containerId = (await shellExec(`docker run -d devcontainer-set-up-test sleep inf`)).stdout.trim();
const res = await shellExec(`${cli} run-user-commands --container-id ${containerId}`);
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'success');
await shellExec(`docker exec ${containerId} test -f /postCreateCommand.txt`);
await shellExec(`docker rm -f ${containerId}`);
});
});
describe('Command read-configuration', () => {
it('should succeed and return postAttachCommand from config', async () => {
const containerId = (await shellExec(`docker run -d alpine:3.17 sleep inf`)).stdout.trim();
const res = await shellExec(`${cli} read-configuration --container-id ${containerId} --config ${__dirname}/configs/set-up-with-config/devcontainer.json --include-merged-configuration`);
const response = JSON.parse(res.stdout);
assert.ok(response.configuration.postAttachCommand);
assert.strictEqual(response.mergedConfiguration.postAttachCommands.length, 1);
await shellExec(`docker rm -f ${containerId}`);
});
it('should succeed and return postCreateCommand from metadata', async () => {
await shellExec(`docker build -t devcontainer-set-up-test ${__dirname}/configs/set-up-with-metadata`);
const containerId = (await shellExec(`docker run -d devcontainer-set-up-test sleep inf`)).stdout.trim();
const res = await shellExec(`${cli} read-configuration --container-id ${containerId} --include-merged-configuration`);
const response = JSON.parse(res.stdout);
assert.strictEqual(response.mergedConfiguration.postCreateCommands.length, 1);
await shellExec(`docker rm -f ${containerId}`);
});
it('should succeed without a workspace folder', async () => {
await shellExec(`docker build -t devcontainer-set-up-test ${__dirname}/configs/set-up-with-metadata`);
const containerId = (await shellExec(`docker run -d devcontainer-set-up-test sleep inf`)).stdout.trim();
const res = await shellExec(`${cli} read-configuration --include-merged-configuration`, { cwd: `${__dirname}/configs/set-up-with-metadata` });
const response = JSON.parse(res.stdout);
assert.strictEqual(response.mergedConfiguration.postCreateCommands.length, 1);
await shellExec(`docker rm -f ${containerId}`);
});
});
describe('Command exec', () => {
it('should succeed with config', async () => {
const containerId = (await shellExec(`docker run -d alpine:3.17 sleep inf`)).stdout.trim();
const res = await shellExec(`${cli} exec --container-id ${containerId} --config ${__dirname}/configs/set-up-with-config/devcontainer.json echo test-output`);
assert.strictEqual(res.error, null);
assert.match(res.stdout, /test-output/);
await shellExec(`docker rm -f ${containerId}`);
});
it('should succeed with metadata', async () => {
await shellExec(`docker build -t devcontainer-set-up-test ${__dirname}/configs/set-up-with-metadata`);
const containerId = (await shellExec(`docker run -d devcontainer-set-up-test sleep inf`)).stdout.trim();
const res = await shellExec(`${cli} exec --container-id ${containerId} echo test-output`);
assert.strictEqual(res.error, null);
assert.match(res.stdout, /test-output/);
await shellExec(`docker rm -f ${containerId}`);
});
});
});