2020-01-15 19:41:47 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2020 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-01-24 01:21:51 +00:00
|
|
|
// tslint:disable-next-line:no-var-requires
|
|
|
|
require('../config-tests'); // required for side effects
|
2020-01-15 19:41:47 +00:00
|
|
|
|
2020-02-15 20:07:09 +00:00
|
|
|
import { expect } from 'chai';
|
2020-03-06 21:19:24 +00:00
|
|
|
import { stripIndent } from 'common-tags';
|
|
|
|
import mock = require('mock-require');
|
2020-01-24 01:21:51 +00:00
|
|
|
import { fs } from 'mz';
|
2020-01-15 19:41:47 +00:00
|
|
|
import * as path from 'path';
|
|
|
|
|
2020-06-02 15:42:51 +00:00
|
|
|
import { isV12 } from '../../build/utils/version';
|
2020-01-15 19:41:47 +00:00
|
|
|
import { BalenaAPIMock } from '../balena-api-mock';
|
2020-05-01 11:18:11 +00:00
|
|
|
import { expectStreamNoCRLF, testDockerBuildStream } from '../docker-build';
|
|
|
|
import { DockerMock, dockerResponsePath } from '../docker-mock';
|
|
|
|
import { cleanOutput, runCommand } from '../helpers';
|
2020-01-15 19:41:47 +00:00
|
|
|
import {
|
2020-02-15 20:18:00 +00:00
|
|
|
ExpectedTarStreamFiles,
|
|
|
|
ExpectedTarStreamFilesByService,
|
2020-05-01 11:18:11 +00:00
|
|
|
} from '../projects';
|
2020-01-15 19:41:47 +00:00
|
|
|
|
|
|
|
const repoPath = path.normalize(path.join(__dirname, '..', '..'));
|
|
|
|
const projectsPath = path.join(repoPath, 'tests', 'test-data', 'projects');
|
|
|
|
|
2020-02-15 20:18:00 +00:00
|
|
|
const commonResponseLines: { [key: string]: string[] } = {
|
2020-01-24 01:21:51 +00:00
|
|
|
'build-POST.json': [
|
|
|
|
'[Info] Building for amd64/nuc',
|
|
|
|
'[Info] Docker Desktop detected (daemon architecture: "x86_64")',
|
|
|
|
'[Info] Docker itself will determine and enable architecture emulation if required,',
|
|
|
|
'[Info] without balena-cli intervention and regardless of the --emulated option.',
|
|
|
|
'[Success] Build succeeded!',
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2020-02-15 20:18:00 +00:00
|
|
|
const commonQueryParams = [
|
|
|
|
['t', '${tag}'],
|
|
|
|
['buildargs', '{}'],
|
|
|
|
['labels', ''],
|
|
|
|
];
|
|
|
|
|
2020-05-01 11:18:11 +00:00
|
|
|
const commonComposeQueryParams = [
|
|
|
|
['t', '${tag}'],
|
|
|
|
[
|
|
|
|
'buildargs',
|
|
|
|
'{"MY_VAR_1":"This is a variable","MY_VAR_2":"Also a variable"}',
|
|
|
|
],
|
|
|
|
['labels', ''],
|
|
|
|
];
|
|
|
|
|
2020-06-15 22:53:04 +00:00
|
|
|
// "itSS" means "it() Skip Standalone"
|
|
|
|
const itSS = process.env.BALENA_CLI_TEST_TYPE === 'standalone' ? it.skip : it;
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
describe('balena build', function () {
|
2020-01-15 19:41:47 +00:00
|
|
|
let api: BalenaAPIMock;
|
|
|
|
let docker: DockerMock;
|
2020-02-15 20:18:00 +00:00
|
|
|
const isWindows = process.platform === 'win32';
|
2020-01-24 01:21:51 +00:00
|
|
|
|
2020-01-15 19:41:47 +00:00
|
|
|
this.beforeEach(() => {
|
|
|
|
api = new BalenaAPIMock();
|
|
|
|
docker = new DockerMock();
|
|
|
|
api.expectGetWhoAmI({ optional: true, persist: true });
|
|
|
|
api.expectGetMixpanel({ optional: true });
|
|
|
|
docker.expectGetPing();
|
|
|
|
docker.expectGetVersion();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.afterEach(() => {
|
|
|
|
// Check all expected api calls have been made and clean up.
|
|
|
|
api.done();
|
|
|
|
docker.done();
|
|
|
|
});
|
|
|
|
|
2020-02-01 00:29:51 +00:00
|
|
|
it('should create the expected tar stream (single container)', async () => {
|
2020-01-15 19:41:47 +00:00
|
|
|
const projectPath = path.join(projectsPath, 'no-docker-compose', 'basic');
|
2020-06-03 13:41:12 +00:00
|
|
|
const isV12W = isWindows && isV12();
|
2020-02-15 20:18:00 +00:00
|
|
|
const expectedFiles: ExpectedTarStreamFiles = {
|
2020-01-15 19:41:47 +00:00
|
|
|
'src/start.sh': { fileSize: 89, type: 'file' },
|
2020-06-03 13:41:12 +00:00
|
|
|
'src/windows-crlf.sh': {
|
|
|
|
fileSize: isV12W ? 68 : 70,
|
|
|
|
testStream: isV12W ? expectStreamNoCRLF : undefined,
|
|
|
|
type: 'file',
|
|
|
|
},
|
2020-02-01 00:29:51 +00:00
|
|
|
Dockerfile: { fileSize: 88, type: 'file' },
|
2020-01-24 01:21:51 +00:00
|
|
|
'Dockerfile-alt': { fileSize: 30, type: 'file' },
|
2020-01-15 19:41:47 +00:00
|
|
|
};
|
2020-01-24 01:21:51 +00:00
|
|
|
const responseFilename = 'build-POST.json';
|
|
|
|
const responseBody = await fs.readFile(
|
|
|
|
path.join(dockerResponsePath, responseFilename),
|
|
|
|
'utf8',
|
|
|
|
);
|
2020-02-15 20:18:00 +00:00
|
|
|
const expectedResponseLines = [
|
|
|
|
...commonResponseLines[responseFilename],
|
2020-02-15 20:20:56 +00:00
|
|
|
`[Info] No "docker-compose.yml" file found at "${projectPath}"`,
|
|
|
|
`[Info] Creating default composition with source: "${projectPath}"`,
|
2020-06-02 15:42:51 +00:00
|
|
|
isV12()
|
|
|
|
? '[Build] main Step 1/4 : FROM busybox'
|
|
|
|
: '[Build] main Image size: 1.14 MB',
|
2020-02-05 22:26:55 +00:00
|
|
|
];
|
2020-02-15 20:18:00 +00:00
|
|
|
if (isWindows) {
|
2020-06-03 13:41:12 +00:00
|
|
|
const fname = path.join(projectPath, 'src', 'windows-crlf.sh');
|
|
|
|
if (isV12()) {
|
|
|
|
expectedResponseLines.push(
|
|
|
|
`[Info] Converting line endings CRLF -> LF for file: ${fname}`,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
expectedResponseLines.push(
|
|
|
|
`[Warn] CRLF (Windows) line endings detected in file: ${fname}`,
|
|
|
|
'[Warn] Windows-format line endings were detected in some files. Consider using the `--convert-eol` option.',
|
|
|
|
);
|
|
|
|
}
|
2020-02-05 22:26:55 +00:00
|
|
|
}
|
2020-03-06 21:19:24 +00:00
|
|
|
docker.expectGetInfo({});
|
2020-02-15 20:18:00 +00:00
|
|
|
await testDockerBuildStream({
|
2020-05-01 11:18:11 +00:00
|
|
|
commandLine: `build ${projectPath} --deviceType nuc --arch amd64 -G`,
|
2020-02-15 20:18:00 +00:00
|
|
|
dockerMock: docker,
|
|
|
|
expectedFilesByService: { main: expectedFiles },
|
|
|
|
expectedQueryParamsByService: { main: commonQueryParams },
|
|
|
|
expectedResponseLines,
|
|
|
|
projectPath,
|
|
|
|
responseBody,
|
|
|
|
responseCode: 200,
|
|
|
|
services: ['main'],
|
|
|
|
});
|
2020-02-01 00:29:51 +00:00
|
|
|
});
|
|
|
|
|
2020-06-15 22:53:04 +00:00
|
|
|
itSS('should create the expected tar stream (--emulated)', async () => {
|
2020-03-06 21:19:24 +00:00
|
|
|
const projectPath = path.join(projectsPath, 'no-docker-compose', 'basic');
|
2020-06-03 13:41:12 +00:00
|
|
|
const isV12W = isWindows && isV12();
|
2020-03-06 21:19:24 +00:00
|
|
|
const transposedDockerfile =
|
|
|
|
stripIndent`
|
|
|
|
FROM busybox
|
|
|
|
COPY [".balena/qemu-execve","/tmp/qemu-execve"]
|
|
|
|
COPY ["./src","/usr/src/"]
|
|
|
|
RUN ["/tmp/qemu-execve","-execve","/bin/sh","-c","chmod a+x /usr/src/*.sh"]
|
|
|
|
CMD ["/usr/src/start.sh"]` + '\n';
|
|
|
|
const expectedFiles: ExpectedTarStreamFiles = {
|
|
|
|
'src/start.sh': { fileSize: 89, type: 'file' },
|
2020-06-03 13:41:12 +00:00
|
|
|
'src/windows-crlf.sh': {
|
|
|
|
fileSize: isV12W ? 68 : 70,
|
|
|
|
testStream: isV12W ? expectStreamNoCRLF : undefined,
|
|
|
|
type: 'file',
|
|
|
|
},
|
2020-03-06 21:19:24 +00:00
|
|
|
Dockerfile: {
|
|
|
|
fileSize: transposedDockerfile.length,
|
|
|
|
type: 'file',
|
|
|
|
contents: transposedDockerfile,
|
|
|
|
},
|
|
|
|
'Dockerfile-alt': { fileSize: 30, type: 'file' },
|
|
|
|
};
|
|
|
|
const responseFilename = 'build-POST.json';
|
|
|
|
const responseBody = await fs.readFile(
|
|
|
|
path.join(dockerResponsePath, responseFilename),
|
|
|
|
'utf8',
|
|
|
|
);
|
|
|
|
const expectedResponseLines = [
|
|
|
|
`[Info] No "docker-compose.yml" file found at "${projectPath}"`,
|
|
|
|
`[Info] Creating default composition with source: "${projectPath}"`,
|
|
|
|
'[Info] Building for rpi/raspberry-pi',
|
|
|
|
'[Info] Emulation is enabled',
|
2020-06-02 15:42:51 +00:00
|
|
|
isV12()
|
|
|
|
? '[Build] main Step 1/4 : FROM busybox'
|
|
|
|
: '[Build] main Image size: 1.14 MB',
|
2020-03-06 21:19:24 +00:00
|
|
|
'[Success] Build succeeded!',
|
|
|
|
];
|
|
|
|
if (isWindows) {
|
2020-06-03 13:41:12 +00:00
|
|
|
const fname = path.join(projectPath, 'src', 'windows-crlf.sh');
|
|
|
|
if (isV12()) {
|
|
|
|
expectedResponseLines.push(
|
|
|
|
`[Info] Converting line endings CRLF -> LF for file: ${fname}`,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
expectedResponseLines.push(
|
|
|
|
`[Warn] CRLF (Windows) line endings detected in file: ${fname}`,
|
|
|
|
'[Warn] Windows-format line endings were detected in some files. Consider using the `--convert-eol` option.',
|
|
|
|
);
|
|
|
|
}
|
2020-03-06 21:19:24 +00:00
|
|
|
}
|
|
|
|
const arch = 'rpi';
|
|
|
|
const deviceType = 'raspberry-pi';
|
|
|
|
const fsModPath = 'mz/fs';
|
|
|
|
const fsMod = await import(fsModPath);
|
|
|
|
const qemuModPath = '../../build/utils/qemu';
|
|
|
|
const qemuMod = require(qemuModPath);
|
|
|
|
const qemuBinPath = await qemuMod.getQemuPath(arch);
|
|
|
|
try {
|
|
|
|
mock(fsModPath, {
|
|
|
|
...fsMod,
|
|
|
|
exists: async (p: string) =>
|
|
|
|
p === qemuBinPath ? true : fsMod.exists(p),
|
|
|
|
});
|
|
|
|
mock(qemuModPath, {
|
|
|
|
...qemuMod,
|
|
|
|
copyQemu: async () => '',
|
|
|
|
});
|
|
|
|
mock.reRequire('../../build/utils/qemu');
|
|
|
|
docker.expectGetInfo({ OperatingSystem: 'balenaOS 2.44.0+rev1' });
|
|
|
|
await testDockerBuildStream({
|
2020-05-01 11:18:11 +00:00
|
|
|
commandLine: `build ${projectPath} --emulated --deviceType ${deviceType} --arch ${arch} --nogitignore`,
|
2020-03-06 21:19:24 +00:00
|
|
|
dockerMock: docker,
|
|
|
|
expectedFilesByService: { main: expectedFiles },
|
|
|
|
expectedQueryParamsByService: { main: commonQueryParams },
|
|
|
|
expectedResponseLines,
|
|
|
|
projectPath,
|
|
|
|
responseBody,
|
|
|
|
responseCode: 200,
|
|
|
|
services: ['main'],
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
mock.stop(fsModPath);
|
|
|
|
mock.stop(qemuModPath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-03 13:41:12 +00:00
|
|
|
it('should create the expected tar stream (single container, --[no]convert-eol)', async () => {
|
2020-02-01 00:29:51 +00:00
|
|
|
const projectPath = path.join(projectsPath, 'no-docker-compose', 'basic');
|
2020-06-03 13:41:12 +00:00
|
|
|
const eol = isWindows && !isV12();
|
2020-02-15 20:18:00 +00:00
|
|
|
const expectedFiles: ExpectedTarStreamFiles = {
|
2020-02-01 00:29:51 +00:00
|
|
|
'src/start.sh': { fileSize: 89, type: 'file' },
|
|
|
|
'src/windows-crlf.sh': {
|
2020-06-03 13:41:12 +00:00
|
|
|
fileSize: eol ? 68 : 70,
|
|
|
|
testStream: eol ? expectStreamNoCRLF : undefined,
|
2020-02-01 00:29:51 +00:00
|
|
|
type: 'file',
|
|
|
|
},
|
|
|
|
Dockerfile: { fileSize: 88, type: 'file' },
|
|
|
|
'Dockerfile-alt': { fileSize: 30, type: 'file' },
|
|
|
|
};
|
|
|
|
const responseFilename = 'build-POST.json';
|
|
|
|
const responseBody = await fs.readFile(
|
|
|
|
path.join(dockerResponsePath, responseFilename),
|
|
|
|
'utf8',
|
|
|
|
);
|
2020-02-15 20:18:00 +00:00
|
|
|
const expectedResponseLines = [
|
|
|
|
...commonResponseLines[responseFilename],
|
2020-02-15 20:20:56 +00:00
|
|
|
`[Info] No "docker-compose.yml" file found at "${projectPath}"`,
|
|
|
|
`[Info] Creating default composition with source: "${projectPath}"`,
|
2020-06-02 15:42:51 +00:00
|
|
|
isV12()
|
|
|
|
? '[Build] main Step 1/4 : FROM busybox'
|
|
|
|
: '[Build] main Image size: 1.14 MB',
|
2020-02-15 20:18:00 +00:00
|
|
|
];
|
|
|
|
if (isWindows) {
|
2020-06-03 13:41:12 +00:00
|
|
|
const fname = path.join(projectPath, 'src', 'windows-crlf.sh');
|
|
|
|
if (isV12()) {
|
|
|
|
expectedResponseLines.push(
|
|
|
|
`[Warn] CRLF (Windows) line endings detected in file: ${fname}`,
|
|
|
|
'[Warn] Windows-format line endings were detected in some files, but were not converted due to `--noconvert-eol` option.',
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
expectedResponseLines.push(
|
|
|
|
`[Info] Converting line endings CRLF -> LF for file: ${fname}`,
|
|
|
|
);
|
|
|
|
}
|
2020-02-15 20:18:00 +00:00
|
|
|
}
|
2020-03-06 21:19:24 +00:00
|
|
|
docker.expectGetInfo({});
|
2020-02-15 20:18:00 +00:00
|
|
|
await testDockerBuildStream({
|
2020-06-03 13:41:12 +00:00
|
|
|
commandLine: isV12()
|
|
|
|
? `build ${projectPath} --deviceType nuc --arch amd64 --noconvert-eol`
|
|
|
|
: `build ${projectPath} --deviceType nuc --arch amd64 --convert-eol`,
|
2020-02-15 20:18:00 +00:00
|
|
|
dockerMock: docker,
|
|
|
|
expectedFilesByService: { main: expectedFiles },
|
|
|
|
expectedQueryParamsByService: { main: commonQueryParams },
|
|
|
|
expectedResponseLines,
|
|
|
|
projectPath,
|
2020-02-01 00:29:51 +00:00
|
|
|
responseBody,
|
2020-02-15 20:18:00 +00:00
|
|
|
responseCode: 200,
|
|
|
|
services: ['main'],
|
2020-02-01 00:29:51 +00:00
|
|
|
});
|
2020-02-15 20:18:00 +00:00
|
|
|
});
|
2020-02-01 00:29:51 +00:00
|
|
|
|
2020-02-15 20:18:00 +00:00
|
|
|
it('should create the expected tar stream (docker-compose)', async () => {
|
|
|
|
const projectPath = path.join(projectsPath, 'docker-compose', 'basic');
|
|
|
|
const service1Dockerfile = (
|
|
|
|
await fs.readFile(
|
|
|
|
path.join(projectPath, 'service1', 'Dockerfile.template'),
|
|
|
|
'utf8',
|
|
|
|
)
|
|
|
|
).replace('%%BALENA_MACHINE_NAME%%', 'nuc');
|
|
|
|
const expectedFilesByService: ExpectedTarStreamFilesByService = {
|
|
|
|
service1: {
|
|
|
|
Dockerfile: {
|
|
|
|
contents: service1Dockerfile,
|
|
|
|
fileSize: service1Dockerfile.length,
|
|
|
|
type: 'file',
|
|
|
|
},
|
|
|
|
'Dockerfile.template': { fileSize: 144, type: 'file' },
|
|
|
|
'file1.sh': { fileSize: 12, type: 'file' },
|
|
|
|
},
|
|
|
|
service2: {
|
|
|
|
'Dockerfile-alt': { fileSize: 40, type: 'file' },
|
|
|
|
'file2-crlf.sh': {
|
|
|
|
fileSize: isWindows ? 12 : 14,
|
|
|
|
testStream: isWindows ? expectStreamNoCRLF : undefined,
|
|
|
|
type: 'file',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const responseFilename = 'build-POST.json';
|
|
|
|
const responseBody = await fs.readFile(
|
|
|
|
path.join(dockerResponsePath, responseFilename),
|
|
|
|
'utf8',
|
2020-02-01 00:29:51 +00:00
|
|
|
);
|
2020-02-15 20:18:00 +00:00
|
|
|
const expectedQueryParamsByService = {
|
2020-05-01 11:18:11 +00:00
|
|
|
service1: [
|
|
|
|
['t', '${tag}'],
|
|
|
|
[
|
|
|
|
'buildargs',
|
|
|
|
'{"MY_VAR_1":"This is a variable","MY_VAR_2":"Also a variable","SERVICE1_VAR":"This is a service specific variable"}',
|
|
|
|
],
|
|
|
|
['labels', ''],
|
|
|
|
],
|
|
|
|
service2: [...commonComposeQueryParams, ['dockerfile', 'Dockerfile-alt']],
|
2020-02-15 20:18:00 +00:00
|
|
|
};
|
|
|
|
const expectedResponseLines: string[] = [
|
|
|
|
...commonResponseLines[responseFilename],
|
2020-06-02 15:42:51 +00:00
|
|
|
...(isV12()
|
|
|
|
? [
|
|
|
|
'[Build] service1 Step 1/4 : FROM busybox',
|
|
|
|
'[Build] service2 Step 1/4 : FROM busybox',
|
|
|
|
]
|
|
|
|
: [
|
|
|
|
`[Build] service1 Image size: 1.14 MB`,
|
|
|
|
`[Build] service2 Image size: 1.14 MB`,
|
|
|
|
]),
|
2020-02-05 22:26:55 +00:00
|
|
|
];
|
2020-02-15 20:18:00 +00:00
|
|
|
if (isWindows) {
|
|
|
|
expectedResponseLines.push(
|
2020-02-05 22:26:55 +00:00
|
|
|
`[Info] Converting line endings CRLF -> LF for file: ${path.join(
|
|
|
|
projectPath,
|
2020-02-15 20:18:00 +00:00
|
|
|
'service2',
|
|
|
|
'file2-crlf.sh',
|
2020-02-05 22:26:55 +00:00
|
|
|
)}`,
|
|
|
|
);
|
|
|
|
}
|
2020-03-06 21:19:24 +00:00
|
|
|
docker.expectGetInfo({});
|
2020-02-15 20:18:00 +00:00
|
|
|
await testDockerBuildStream({
|
2020-05-01 11:18:11 +00:00
|
|
|
commandLine: `build ${projectPath} --deviceType nuc --arch amd64 --convert-eol -G`,
|
2020-02-15 20:18:00 +00:00
|
|
|
dockerMock: docker,
|
|
|
|
expectedFilesByService,
|
|
|
|
expectedQueryParamsByService,
|
|
|
|
expectedResponseLines,
|
|
|
|
projectPath,
|
|
|
|
responseBody,
|
|
|
|
responseCode: 200,
|
|
|
|
services: ['service1', 'service2'],
|
|
|
|
});
|
2020-01-15 19:41:47 +00:00
|
|
|
});
|
|
|
|
});
|
2020-02-15 20:07:09 +00:00
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
describe('balena build: project validation', function () {
|
2020-02-15 20:07:09 +00:00
|
|
|
it('should raise ExpectedError if a Dockerfile cannot be found', async () => {
|
|
|
|
const projectPath = path.join(
|
|
|
|
projectsPath,
|
|
|
|
'docker-compose',
|
|
|
|
'basic',
|
|
|
|
'service2',
|
|
|
|
);
|
|
|
|
const expectedErrorLines = [
|
|
|
|
'Error: no "Dockerfile[.*]", "docker-compose.yml" or "package.json" file',
|
|
|
|
`found in source folder "${projectPath}"`,
|
|
|
|
];
|
|
|
|
|
2020-06-15 22:53:01 +00:00
|
|
|
const { out, err } = await runCommand(
|
|
|
|
`build ${projectPath} -A amd64 -d nuc`,
|
|
|
|
);
|
2020-06-15 22:53:04 +00:00
|
|
|
expect(cleanOutput(err, true)).to.include.members(expectedErrorLines);
|
2020-02-15 20:07:09 +00:00
|
|
|
expect(out).to.be.empty;
|
|
|
|
});
|
|
|
|
});
|