2023-02-19 01:12:29 +00:00
|
|
|
import {beforeEach, describe, expect, jest, test} from '@jest/globals';
|
2022-09-19 09:36:58 +00:00
|
|
|
import * as uuid from 'uuid';
|
2023-03-03 10:56:21 +00:00
|
|
|
import {Buildx} from '@docker/actions-toolkit/lib/buildx/buildx';
|
2023-03-08 18:12:49 +00:00
|
|
|
import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
|
2023-02-19 01:12:29 +00:00
|
|
|
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
|
|
|
|
import {Node} from '@docker/actions-toolkit/lib/types/builder';
|
2021-07-02 05:02:22 +00:00
|
|
|
|
2023-02-19 01:12:29 +00:00
|
|
|
import * as context from '../src/context';
|
2021-09-03 20:21:20 +00:00
|
|
|
|
2022-09-19 09:36:58 +00:00
|
|
|
jest.mock('uuid');
|
|
|
|
jest.spyOn(uuid, 'v4').mockReturnValue('9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d');
|
|
|
|
|
2023-02-25 15:16:08 +00:00
|
|
|
jest.spyOn(Docker, 'context').mockImplementation((): Promise<string> => {
|
|
|
|
return Promise.resolve('default');
|
|
|
|
});
|
|
|
|
|
2022-09-19 09:36:58 +00:00
|
|
|
describe('getCreateArgs', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
process.env = Object.keys(process.env).reduce((object, key) => {
|
|
|
|
if (!key.startsWith('INPUT_')) {
|
|
|
|
object[key] = process.env[key];
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}, {});
|
|
|
|
});
|
|
|
|
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
0,
|
2023-03-03 10:56:21 +00:00
|
|
|
'v0.10.3',
|
2022-09-19 09:36:58 +00:00
|
|
|
new Map<string, string>([
|
|
|
|
['install', 'false'],
|
|
|
|
['use', 'true'],
|
2023-03-03 10:56:21 +00:00
|
|
|
['cleanup', 'true'],
|
2022-09-19 09:36:58 +00:00
|
|
|
]),
|
|
|
|
[
|
|
|
|
'create',
|
|
|
|
'--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
|
|
|
|
'--driver', 'docker-container',
|
|
|
|
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
|
|
|
|
'--use'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
1,
|
2023-03-03 10:56:21 +00:00
|
|
|
'v0.10.3',
|
2022-09-19 09:36:58 +00:00
|
|
|
new Map<string, string>([
|
|
|
|
['driver', 'docker'],
|
|
|
|
['install', 'false'],
|
|
|
|
['use', 'true'],
|
2023-03-03 10:56:21 +00:00
|
|
|
['cleanup', 'true'],
|
2022-09-19 09:36:58 +00:00
|
|
|
]),
|
|
|
|
[
|
|
|
|
'create',
|
|
|
|
'--name', 'default',
|
|
|
|
'--driver', 'docker',
|
|
|
|
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
|
|
|
|
'--use'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
2,
|
2023-03-03 10:56:21 +00:00
|
|
|
'v0.10.3',
|
2022-09-19 09:36:58 +00:00
|
|
|
new Map<string, string>([
|
|
|
|
['install', 'false'],
|
|
|
|
['use', 'false'],
|
|
|
|
['driver-opts', 'image=moby/buildkit:master\nnetwork=host'],
|
2023-03-03 10:56:21 +00:00
|
|
|
['cleanup', 'true'],
|
2022-09-19 09:36:58 +00:00
|
|
|
]),
|
|
|
|
[
|
|
|
|
'create',
|
|
|
|
'--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
|
|
|
|
'--driver', 'docker-container',
|
|
|
|
'--driver-opt', 'image=moby/buildkit:master',
|
|
|
|
'--driver-opt', 'network=host',
|
|
|
|
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
3,
|
2023-03-03 10:56:21 +00:00
|
|
|
'v0.10.3',
|
2022-09-19 09:36:58 +00:00
|
|
|
new Map<string, string>([
|
|
|
|
['driver', 'remote'],
|
|
|
|
['endpoint', 'tls://foo:1234'],
|
|
|
|
['install', 'false'],
|
|
|
|
['use', 'true'],
|
2023-03-03 10:56:21 +00:00
|
|
|
['cleanup', 'true'],
|
2022-09-19 09:36:58 +00:00
|
|
|
]),
|
|
|
|
[
|
|
|
|
'create',
|
|
|
|
'--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
|
|
|
|
'--driver', 'remote',
|
|
|
|
'--use',
|
|
|
|
'tls://foo:1234'
|
|
|
|
]
|
|
|
|
],
|
2022-09-22 09:54:00 +00:00
|
|
|
[
|
|
|
|
4,
|
2023-03-03 10:56:21 +00:00
|
|
|
'v0.10.3',
|
2022-09-22 09:54:00 +00:00
|
|
|
new Map<string, string>([
|
|
|
|
['driver', 'remote'],
|
|
|
|
['platforms', 'linux/arm64,linux/arm/v7'],
|
|
|
|
['endpoint', 'tls://foo:1234'],
|
|
|
|
['install', 'false'],
|
|
|
|
['use', 'true'],
|
2023-03-03 10:56:21 +00:00
|
|
|
['cleanup', 'true'],
|
2022-09-22 09:54:00 +00:00
|
|
|
]),
|
|
|
|
[
|
|
|
|
'create',
|
|
|
|
'--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
|
|
|
|
'--driver', 'remote',
|
|
|
|
'--platform', 'linux/arm64,linux/arm/v7',
|
|
|
|
'--use',
|
|
|
|
'tls://foo:1234'
|
|
|
|
]
|
|
|
|
],
|
2022-10-17 18:44:03 +00:00
|
|
|
[
|
|
|
|
5,
|
2023-03-03 10:56:21 +00:00
|
|
|
'v0.10.3',
|
2022-10-17 18:44:03 +00:00
|
|
|
new Map<string, string>([
|
|
|
|
['install', 'false'],
|
|
|
|
['use', 'false'],
|
|
|
|
['driver-opts', `"env.no_proxy=localhost,127.0.0.1,.mydomain"`],
|
2023-03-03 10:56:21 +00:00
|
|
|
['cleanup', 'true'],
|
2022-10-17 18:44:03 +00:00
|
|
|
]),
|
|
|
|
[
|
|
|
|
'create',
|
|
|
|
'--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
|
|
|
|
'--driver', 'docker-container',
|
|
|
|
'--driver-opt', '"env.no_proxy=localhost,127.0.0.1,.mydomain"',
|
|
|
|
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host'
|
|
|
|
]
|
|
|
|
],
|
2022-10-18 09:06:34 +00:00
|
|
|
[
|
|
|
|
6,
|
2023-03-03 10:56:21 +00:00
|
|
|
'v0.10.3',
|
2022-10-18 09:06:34 +00:00
|
|
|
new Map<string, string>([
|
|
|
|
['install', 'false'],
|
|
|
|
['use', 'false'],
|
|
|
|
['platforms', 'linux/amd64\n"linux/arm64,linux/arm/v7"'],
|
2023-03-03 10:56:21 +00:00
|
|
|
['cleanup', 'true'],
|
2022-10-18 09:06:34 +00:00
|
|
|
]),
|
|
|
|
[
|
|
|
|
'create',
|
|
|
|
'--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
|
|
|
|
'--driver', 'docker-container',
|
|
|
|
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
|
|
|
|
'--platform', 'linux/amd64,linux/arm64,linux/arm/v7'
|
|
|
|
]
|
2023-06-28 13:12:40 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
7,
|
|
|
|
'v0.10.3',
|
|
|
|
new Map<string, string>([
|
|
|
|
['install', 'false'],
|
|
|
|
['use', 'false'],
|
|
|
|
['driver', 'unknown'],
|
|
|
|
['cleanup', 'true'],
|
|
|
|
]),
|
|
|
|
[
|
|
|
|
'create',
|
|
|
|
'--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
|
|
|
|
'--driver', 'unknown',
|
|
|
|
]
|
2023-03-03 10:56:21 +00:00
|
|
|
]
|
2022-09-19 09:36:58 +00:00
|
|
|
])(
|
2023-03-03 10:56:21 +00:00
|
|
|
'[%d] given buildx %s and %p as inputs, returns %p',
|
|
|
|
async (num: number, buildxVersion: string, inputs: Map<string, string>, expected: Array<string>) => {
|
2022-09-19 09:36:58 +00:00
|
|
|
inputs.forEach((value: string, name: string) => {
|
|
|
|
setInput(name, value);
|
|
|
|
});
|
2023-03-03 10:56:21 +00:00
|
|
|
const toolkit = new Toolkit();
|
|
|
|
jest.spyOn(Buildx.prototype, 'version').mockImplementation(async (): Promise<string> => {
|
|
|
|
return buildxVersion;
|
|
|
|
});
|
2022-09-19 09:36:58 +00:00
|
|
|
const inp = await context.getInputs();
|
2023-03-03 10:56:21 +00:00
|
|
|
const res = await context.getCreateArgs(inp, toolkit);
|
2022-09-19 09:36:58 +00:00
|
|
|
expect(res).toEqual(expected);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-09-19 09:34:47 +00:00
|
|
|
describe('getAppendArgs', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
process.env = Object.keys(process.env).reduce((object, key) => {
|
|
|
|
if (!key.startsWith('INPUT_')) {
|
|
|
|
object[key] = process.env[key];
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}, {});
|
|
|
|
});
|
|
|
|
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
0,
|
2023-03-03 10:56:21 +00:00
|
|
|
'v0.10.3',
|
2022-09-19 09:34:47 +00:00
|
|
|
new Map<string, string>([
|
|
|
|
['install', 'false'],
|
|
|
|
['use', 'true'],
|
2023-03-03 10:56:21 +00:00
|
|
|
['cleanup', 'true'],
|
2022-09-19 09:34:47 +00:00
|
|
|
]),
|
|
|
|
{
|
|
|
|
"name": "aws_graviton2",
|
|
|
|
"endpoint": "ssh://me@graviton2",
|
|
|
|
"driver-opts": [
|
|
|
|
"image=moby/buildkit:latest"
|
|
|
|
],
|
|
|
|
"buildkitd-flags": "--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host",
|
|
|
|
"platforms": "linux/arm64"
|
|
|
|
},
|
|
|
|
[
|
|
|
|
'create',
|
|
|
|
'--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
|
|
|
|
'--append',
|
|
|
|
'--node', 'aws_graviton2',
|
|
|
|
'--driver-opt', 'image=moby/buildkit:latest',
|
|
|
|
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
|
|
|
|
'--platform', 'linux/arm64',
|
|
|
|
'ssh://me@graviton2'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
])(
|
2023-03-03 10:56:21 +00:00
|
|
|
'[%d] given buildx %s and %p as inputs, returns %p',
|
|
|
|
async (num: number, buildxVersion: string, inputs: Map<string, string>, node: Node, expected: Array<string>) => {
|
2022-09-19 09:34:47 +00:00
|
|
|
inputs.forEach((value: string, name: string) => {
|
|
|
|
setInput(name, value);
|
|
|
|
});
|
2023-03-03 10:56:21 +00:00
|
|
|
const toolkit = new Toolkit();
|
|
|
|
jest.spyOn(Buildx.prototype, 'version').mockImplementation(async (): Promise<string> => {
|
|
|
|
return buildxVersion;
|
|
|
|
});
|
2022-09-19 09:34:47 +00:00
|
|
|
const inp = await context.getInputs();
|
2023-03-03 10:56:21 +00:00
|
|
|
const res = await context.getAppendArgs(inp, node, toolkit);
|
2022-09-19 09:34:47 +00:00
|
|
|
expect(res).toEqual(expected);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-09-03 14:23:15 +00:00
|
|
|
// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
|
|
|
|
function getInputName(name: string): string {
|
|
|
|
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setInput(name: string, value: string): void {
|
|
|
|
process.env[getInputName(name)] = value;
|
|
|
|
}
|