|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------------------------------------------*/ |
| 4 | + |
| 5 | +import { assert } from 'chai'; |
| 6 | +import * as fs from 'fs'; |
| 7 | +import * as path from 'path'; |
| 8 | +import { CLIHost } from '../spec-common/cliHost'; |
| 9 | +import { resolveDockerfileIncludesIfNeeded } from '../spec-node/dockerfilePreprocess'; |
| 10 | + |
| 11 | +function createMockCLIHost(files: Record<string, string>, platform: NodeJS.Platform = 'linux'): CLIHost { |
| 12 | + const pathModule = platform === 'win32' ? path.win32 : path.posix; |
| 13 | + return { |
| 14 | + type: 'local', |
| 15 | + platform, |
| 16 | + arch: 'x64', |
| 17 | + path: pathModule, |
| 18 | + cwd: platform === 'win32' ? 'C:\\' : '/', |
| 19 | + env: {}, |
| 20 | + exec: () => { throw new Error('Not implemented'); }, |
| 21 | + ptyExec: () => { throw new Error('Not implemented'); }, |
| 22 | + homedir: async () => platform === 'win32' ? 'C:\\Users\\test' : '/home/test', |
| 23 | + tmpdir: async () => platform === 'win32' ? 'C:\\tmp' : '/tmp', |
| 24 | + isFile: async (filepath: string) => filepath in files, |
| 25 | + isFolder: async () => false, |
| 26 | + readFile: async (filepath: string) => { |
| 27 | + if (!(filepath in files)) { |
| 28 | + throw new Error(`File not found: ${filepath}`); |
| 29 | + } |
| 30 | + return Buffer.from(files[filepath]); |
| 31 | + }, |
| 32 | + writeFile: async (filepath: string, content: Buffer) => { |
| 33 | + files[filepath] = content.toString(); |
| 34 | + }, |
| 35 | + rename: async () => { }, |
| 36 | + mkdirp: async () => { }, |
| 37 | + readDir: async () => [], |
| 38 | + getUsername: async () => 'test', |
| 39 | + toCommonURI: async () => undefined, |
| 40 | + connect: () => { throw new Error('Not implemented'); }, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +describe('resolveDockerfileIncludesIfNeeded', () => { |
| 45 | + it('returns source Dockerfile unchanged when not using .in extension', async () => { |
| 46 | + const files: Record<string, string> = { |
| 47 | + '/workspace/Dockerfile': 'FROM debian:latest\nRUN echo ok', |
| 48 | + }; |
| 49 | + const cliHost = createMockCLIHost(files); |
| 50 | + const result = await resolveDockerfileIncludesIfNeeded(cliHost, '/workspace/Dockerfile'); |
| 51 | + assert.isFalse(result.preprocessed); |
| 52 | + assert.equal(result.effectiveDockerfilePath, '/workspace/Dockerfile'); |
| 53 | + assert.equal(result.effectiveDockerfileContent, files['/workspace/Dockerfile']); |
| 54 | + }); |
| 55 | + |
| 56 | + it('expands #include lines and writes a generated Dockerfile for .in files', async () => { |
| 57 | + const podmanTestConfigPath = path.resolve(__dirname, 'configs', 'podman-test'); |
| 58 | + const sourceDockerfilePath = path.join(podmanTestConfigPath, 'cpp.Dockerfile.in'); |
| 59 | + const includedDockerfilePath = path.join(podmanTestConfigPath, 'tools.Dockerfile'); |
| 60 | + const sourceDockerfileContent = fs.readFileSync(sourceDockerfilePath).toString(); |
| 61 | + const includedDockerfileContent = fs.readFileSync(includedDockerfilePath).toString(); |
| 62 | + const files: Record<string, string> = { |
| 63 | + [sourceDockerfilePath]: sourceDockerfileContent, |
| 64 | + [includedDockerfilePath]: includedDockerfileContent, |
| 65 | + }; |
| 66 | + const cliHost = createMockCLIHost(files); |
| 67 | + const result = await resolveDockerfileIncludesIfNeeded(cliHost, sourceDockerfilePath); |
| 68 | + assert.isTrue(result.preprocessed); |
| 69 | + assert.notEqual(result.effectiveDockerfilePath, sourceDockerfilePath); |
| 70 | + assert.include(result.effectiveDockerfilePath, '/tmp/devcontainercli-test/dockerfile-preprocess/'); |
| 71 | + assert.equal( |
| 72 | + result.effectiveDockerfileContent, |
| 73 | + 'FROM docker.io/debian:latest\nRUN apt-get update && apt-get install -y vim\nRUN apt-get update && apt-get install -y clang' |
| 74 | + ); |
| 75 | + assert.equal(files[result.effectiveDockerfilePath], result.effectiveDockerfileContent); |
| 76 | + }); |
| 77 | + |
| 78 | + it('fails with a clear error when #include has a cycle', async () => { |
| 79 | + const files: Record<string, string> = { |
| 80 | + '/workspace/a.Dockerfile.in': '#include "b.Dockerfile"\nRUN echo a', |
| 81 | + '/workspace/b.Dockerfile': '#include "a.Dockerfile.in"\nRUN echo b', |
| 82 | + }; |
| 83 | + const cliHost = createMockCLIHost(files); |
| 84 | + let err: any; |
| 85 | + try { |
| 86 | + await resolveDockerfileIncludesIfNeeded(cliHost, '/workspace/a.Dockerfile.in'); |
| 87 | + } catch (e) { |
| 88 | + err = e; |
| 89 | + } |
| 90 | + assert.ok(err); |
| 91 | + assert.include(String(err.message || err), 'Cyclic #include detected while preprocessing Dockerfile'); |
| 92 | + }); |
| 93 | +}); |
0 commit comments