2022-03-21 12:43:41 +00:00
|
|
|
import {describe, expect, it, jest, test} from '@jest/globals';
|
2021-07-02 05:02:22 +00:00
|
|
|
import * as fs from 'fs';
|
2020-08-18 15:40:31 +00:00
|
|
|
import * as os from 'os';
|
2021-07-02 05:02:22 +00:00
|
|
|
import * as path from 'path';
|
|
|
|
import * as buildx from '../src/buildx';
|
|
|
|
import * as context from '../src/context';
|
2020-09-03 19:02:36 +00:00
|
|
|
import * as semver from 'semver';
|
|
|
|
import * as exec from '@actions/exec';
|
2020-08-18 15:40:31 +00:00
|
|
|
|
2022-09-22 00:48:11 +00:00
|
|
|
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-setup-buildx-')).split(path.sep).join(path.posix.sep);
|
2021-07-02 05:02:22 +00:00
|
|
|
jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
|
2022-09-22 00:48:11 +00:00
|
|
|
return tmpdir;
|
2021-07-02 05:02:22 +00:00
|
|
|
});
|
|
|
|
|
2022-09-22 00:48:11 +00:00
|
|
|
const tmpname = path.join(tmpdir, '.tmpname').split(path.sep).join(path.posix.sep);
|
2021-09-03 20:21:20 +00:00
|
|
|
jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => {
|
2022-09-22 00:48:11 +00:00
|
|
|
return tmpname;
|
2021-09-03 20:21:20 +00:00
|
|
|
});
|
|
|
|
|
2021-06-23 14:11:52 +00:00
|
|
|
describe('isAvailable', () => {
|
2022-03-21 12:43:41 +00:00
|
|
|
const execSpy = jest.spyOn(exec, 'getExecOutput');
|
2021-06-23 14:11:52 +00:00
|
|
|
buildx.isAvailable();
|
|
|
|
|
2022-03-21 12:43:41 +00:00
|
|
|
// eslint-disable-next-line jest/no-standalone-expect
|
2021-06-23 14:11:52 +00:00
|
|
|
expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], {
|
|
|
|
silent: true,
|
|
|
|
ignoreReturnCode: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-17 15:22:03 +00:00
|
|
|
describe('isAvailable standalone', () => {
|
|
|
|
const execSpy = jest.spyOn(exec, 'getExecOutput');
|
|
|
|
buildx.isAvailable(true);
|
|
|
|
|
|
|
|
// eslint-disable-next-line jest/no-standalone-expect
|
|
|
|
expect(execSpy).toHaveBeenCalledWith(`buildx`, [], {
|
|
|
|
silent: true,
|
|
|
|
ignoreReturnCode: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-09-03 19:02:36 +00:00
|
|
|
describe('getVersion', () => {
|
2022-03-21 12:43:41 +00:00
|
|
|
it('valid', async () => {
|
|
|
|
const version = await buildx.getVersion();
|
|
|
|
expect(semver.valid(version)).not.toBeNull();
|
|
|
|
});
|
2020-09-03 19:02:36 +00:00
|
|
|
});
|
2020-08-18 15:40:31 +00:00
|
|
|
|
2020-09-03 19:02:36 +00:00
|
|
|
describe('parseVersion', () => {
|
|
|
|
test.each([
|
|
|
|
['github.com/docker/buildx 0.4.1+azure bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
|
|
|
|
['github.com/docker/buildx v0.4.1 bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
|
2021-07-02 05:02:22 +00:00
|
|
|
['github.com/docker/buildx v0.4.2 fb7b670b764764dc4716df3eba07ffdae4cc47b2', '0.4.2'],
|
|
|
|
['github.com/docker/buildx f117971 f11797113e5a9b86bd976329c5dbb8a8bfdfadfa', 'f117971']
|
2020-09-03 19:02:36 +00:00
|
|
|
])('given %p', async (stdout, expected) => {
|
2021-07-02 05:02:22 +00:00
|
|
|
expect(buildx.parseVersion(stdout)).toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('satisfies', () => {
|
|
|
|
test.each([
|
|
|
|
['0.4.1', '>=0.3.2', true],
|
|
|
|
['bda4882a65349ca359216b135896bddc1d92461c', '>0.1.0', false],
|
|
|
|
['f117971', '>0.6.0', true]
|
|
|
|
])('given %p', async (version, range, expected) => {
|
|
|
|
expect(buildx.satisfies(version, range)).toBe(expected);
|
2020-09-03 19:02:36 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-23 16:14:38 +00:00
|
|
|
describe('inspect', () => {
|
2022-03-21 12:43:41 +00:00
|
|
|
it('valid', async () => {
|
|
|
|
const builder = await buildx.inspect('');
|
|
|
|
expect(builder).not.toBeUndefined();
|
|
|
|
expect(builder.name).not.toEqual('');
|
|
|
|
expect(builder.driver).not.toEqual('');
|
2022-09-18 00:24:38 +00:00
|
|
|
expect(builder.nodes).not.toEqual({});
|
2022-03-21 12:43:41 +00:00
|
|
|
}, 100000);
|
2020-09-03 19:02:36 +00:00
|
|
|
});
|
|
|
|
|
2022-09-21 14:14:10 +00:00
|
|
|
describe('parseInspect', () => {
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
'inspect1.txt',
|
|
|
|
{
|
|
|
|
"nodes": [
|
|
|
|
{
|
|
|
|
"name": "builder-5cb467f7-0940-47e1-b94b-d51f54054d620",
|
|
|
|
"endpoint": "unix:///var/run/docker.sock",
|
|
|
|
"status": "running",
|
|
|
|
"buildkitd-flags": "--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host",
|
|
|
|
"buildkit": "v0.10.4",
|
|
|
|
"platforms": "linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/amd64/v4,linux/arm64,linux/riscv64,linux/386,linux/arm/v7,linux/arm/v6"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "builder-5cb467f7-0940-47e1-b94b-d51f54054d62",
|
|
|
|
"driver": "docker-container"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'inspect2.txt',
|
|
|
|
{
|
|
|
|
"nodes": [
|
|
|
|
{
|
|
|
|
"name": "builder-5f449644-ff29-48af-8344-abb0292d06730",
|
|
|
|
"endpoint": "unix:///var/run/docker.sock",
|
|
|
|
"driver-opts": [
|
|
|
|
"image=moby/buildkit:latest"
|
|
|
|
],
|
|
|
|
"status": "running",
|
|
|
|
"buildkitd-flags": "--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host",
|
|
|
|
"buildkit": "v0.10.4",
|
|
|
|
"platforms": "linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/amd64/v4,linux/386"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "builder-5f449644-ff29-48af-8344-abb0292d0673",
|
|
|
|
"driver": "docker-container"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'inspect3.txt',
|
|
|
|
{
|
|
|
|
"nodes": [
|
|
|
|
{
|
|
|
|
"name": "builder-9929e463-7954-4dc3-89cd-514cca29ff800",
|
|
|
|
"endpoint": "unix:///var/run/docker.sock",
|
|
|
|
"driver-opts": [
|
|
|
|
"image=moby/buildkit:master",
|
|
|
|
"network=host"
|
|
|
|
],
|
|
|
|
"status": "running",
|
|
|
|
"buildkitd-flags": "--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host",
|
|
|
|
"buildkit": "3fab389",
|
|
|
|
"platforms": "linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/amd64/v4,linux/386"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "builder-9929e463-7954-4dc3-89cd-514cca29ff80",
|
|
|
|
"driver": "docker-container"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'inspect4.txt',
|
|
|
|
{
|
|
|
|
"nodes": [
|
|
|
|
{
|
|
|
|
"name": "default",
|
|
|
|
"endpoint": "default",
|
|
|
|
"status": "running",
|
|
|
|
"buildkit": "20.10.17",
|
|
|
|
"platforms": "linux/amd64,linux/arm64,linux/riscv64,linux/ppc64le,linux/s390x,linux/386,linux/arm/v7,linux/arm/v6"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "default",
|
|
|
|
"driver": "docker"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'inspect5.txt',
|
|
|
|
{
|
|
|
|
"nodes": [
|
|
|
|
{
|
|
|
|
"name": "aws_graviton2",
|
|
|
|
"endpoint": "tcp://1.23.45.67:1234",
|
|
|
|
"driver-opts": [
|
|
|
|
"cert=/home/user/.certs/aws_graviton2/cert.pem",
|
|
|
|
"key=/home/user/.certs/aws_graviton2/key.pem",
|
|
|
|
"cacert=/home/user/.certs/aws_graviton2/ca.pem"
|
|
|
|
],
|
|
|
|
"status": "running",
|
|
|
|
"platforms": "darwin/arm64,linux/arm64,linux/arm/v5,linux/arm/v6,linux/arm/v7,windows/arm64"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "remote-builder",
|
|
|
|
"driver": "remote"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'inspect6.txt',
|
|
|
|
{
|
|
|
|
"nodes": [
|
|
|
|
{
|
|
|
|
"name": "builder-17cfff01-48d9-4c3d-9332-9992e308a5100",
|
|
|
|
"endpoint": "unix:///var/run/docker.sock",
|
|
|
|
"status": "running",
|
|
|
|
"buildkitd-flags": "--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host",
|
|
|
|
"platforms": "linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/386"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "builder-17cfff01-48d9-4c3d-9332-9992e308a510",
|
|
|
|
"driver": "docker-container"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
])('given %p', async (inspectFile, expected) => {
|
|
|
|
expect(await buildx.parseInspect(fs.readFileSync(path.join(__dirname, 'fixtures', inspectFile)).toString())).toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-02 05:02:22 +00:00
|
|
|
describe('build', () => {
|
2021-07-11 20:42:19 +00:00
|
|
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));
|
2022-03-21 12:43:41 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line jest/no-disabled-tests
|
2021-07-11 20:42:19 +00:00
|
|
|
it.skip('builds refs/pull/648/head', async () => {
|
2022-04-17 15:22:03 +00:00
|
|
|
const buildxBin = await buildx.build('https://github.com/docker/buildx.git#refs/pull/648/head', tmpDir, false);
|
2021-07-02 05:02:22 +00:00
|
|
|
expect(fs.existsSync(buildxBin)).toBe(true);
|
|
|
|
}, 100000);
|
2022-03-21 12:43:41 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line jest/no-disabled-tests
|
2021-07-11 20:42:19 +00:00
|
|
|
it.skip('builds 67bd6f4dc82a9cd96f34133dab3f6f7af803bb14', async () => {
|
2022-04-17 15:22:03 +00:00
|
|
|
const buildxBin = await buildx.build('https://github.com/docker/buildx.git#67bd6f4dc82a9cd96f34133dab3f6f7af803bb14', tmpDir, false);
|
2021-07-11 20:42:19 +00:00
|
|
|
expect(fs.existsSync(buildxBin)).toBe(true);
|
|
|
|
}, 100000);
|
2021-07-02 05:02:22 +00:00
|
|
|
});
|
|
|
|
|
2020-09-03 19:02:36 +00:00
|
|
|
describe('install', () => {
|
|
|
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));
|
2022-04-17 15:22:03 +00:00
|
|
|
test.each([
|
|
|
|
['v0.4.1', false],
|
|
|
|
['latest', false],
|
|
|
|
['v0.4.1', true],
|
|
|
|
['latest', true]
|
|
|
|
])(
|
|
|
|
'acquires %p of buildx (standalone: %p)',
|
|
|
|
async (version, standalone) => {
|
|
|
|
const buildxBin = await buildx.install(version, tmpDir, standalone);
|
|
|
|
expect(fs.existsSync(buildxBin)).toBe(true);
|
|
|
|
},
|
|
|
|
100000
|
|
|
|
);
|
2020-08-18 15:40:31 +00:00
|
|
|
});
|
2021-09-03 20:21:20 +00:00
|
|
|
|
|
|
|
describe('getConfig', () => {
|
|
|
|
test.each([
|
|
|
|
['debug = true', false, 'debug = true', false],
|
|
|
|
[`notfound.toml`, true, '', true],
|
|
|
|
[
|
|
|
|
`${path.join(__dirname, 'fixtures', 'buildkitd.toml').split(path.sep).join(path.posix.sep)}`,
|
|
|
|
true,
|
|
|
|
`debug = true
|
|
|
|
[registry."docker.io"]
|
|
|
|
mirrors = ["mirror.gcr.io"]
|
|
|
|
`,
|
|
|
|
false
|
|
|
|
]
|
|
|
|
])('given %p config', async (val, file, exValue, invalid) => {
|
|
|
|
try {
|
|
|
|
let config: string;
|
|
|
|
if (file) {
|
|
|
|
config = await buildx.getConfigFile(val);
|
|
|
|
} else {
|
|
|
|
config = await buildx.getConfigInline(val);
|
|
|
|
}
|
|
|
|
expect(true).toBe(!invalid);
|
2022-09-22 00:48:11 +00:00
|
|
|
expect(config).toEqual(tmpname);
|
|
|
|
const configValue = fs.readFileSync(tmpname, 'utf-8');
|
2021-09-03 20:21:20 +00:00
|
|
|
expect(configValue).toEqual(exValue);
|
|
|
|
} catch (err) {
|
2022-03-21 12:43:41 +00:00
|
|
|
// eslint-disable-next-line jest/no-conditional-expect
|
2021-09-03 20:21:20 +00:00
|
|
|
expect(true).toBe(invalid);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|