|
1 | 1 | import * as _ from 'lodash';
|
2 | 2 | import * as path from 'path';
|
| 3 | +import * as childProc from 'child_process'; |
3 | 4 | import * as Docker from 'dockerode';
|
4 | 5 |
|
5 | 6 | import { expect } from 'chai';
|
@@ -31,7 +32,59 @@ describe('Docker CLI interception', function () {
|
31 | 32 | await server.stop();
|
32 | 33 | });
|
33 | 34 |
|
34 |
| - it("should intercept 'docker run'", async () => { |
| 35 | + [ |
| 36 | + 'JS', |
| 37 | + 'Java', |
| 38 | + 'Python', |
| 39 | + 'Ruby', |
| 40 | + 'Go' |
| 41 | + ].forEach((target) => { |
| 42 | + it(`should intercept 'docker run' of local ${target} containers`, async function () { |
| 43 | + this.timeout(60000); |
| 44 | + |
| 45 | + // Build the target image (may already have been built by the attachment tests) |
| 46 | + const { exitCode: buildExitCode, stdout: buildStdout } = await spawnToResult( |
| 47 | + 'docker', ['build', '.', '-q'], |
| 48 | + { cwd: path.join(__dirname, '..', 'fixtures', 'docker', target.toLowerCase()) }, |
| 49 | + true |
| 50 | + ); |
| 51 | + |
| 52 | + expect(buildExitCode).to.equal(0); |
| 53 | + const imageId = buildStdout.trim(); |
| 54 | + |
| 55 | + // Run the resulting container via the intercepting Docker proxy: |
| 56 | + const { server, httpsConfig} = await testSetup; |
| 57 | + const mainRule = await server.get('https://example.test').thenReply(200); |
| 58 | + |
| 59 | + const terminalEnvOverrides = getTerminalEnvVars(server.port, httpsConfig, process.env, {}, { |
| 60 | + dockerEnabled: true |
| 61 | + }); |
| 62 | + |
| 63 | + childProc.spawn( |
| 64 | + 'docker', ['run', '--rm', imageId, 'https://example.test'], |
| 65 | + { |
| 66 | + env: { ...process.env, ...terminalEnvOverrides }, |
| 67 | + stdio: 'inherit' |
| 68 | + } |
| 69 | + ); |
| 70 | + |
| 71 | + await new Promise((resolve) => server.on('response', resolve)); |
| 72 | + |
| 73 | + const seenRequests = await mainRule.getSeenRequests(); |
| 74 | + expect(seenRequests.map(r => r.url)).to.include('https://example.test/'); |
| 75 | + |
| 76 | + // Clean up the container (which will otherwise run forever): |
| 77 | + const docker = new Docker(); |
| 78 | + const containers = await docker.listContainers({ |
| 79 | + filters: JSON.stringify({ ancestor: [imageId] }) |
| 80 | + }); |
| 81 | + await Promise.all( |
| 82 | + containers.map((container) => docker.getContainer(container.Id).kill()) |
| 83 | + ); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should intercept 'docker run' launching a remote image", async () => { |
35 | 88 | const { server, httpsConfig } = await testSetup;
|
36 | 89 |
|
37 | 90 | const mainRule = await server.get("https://example.test").thenReply(200);
|
|
0 commit comments