import {beforeEach, describe, expect, jest, test} from '@jest/globals'; import * as uuid from 'uuid'; import {Toolkit} from '@docker/actions-toolkit/lib/toolkit'; import {Node} from '@docker/actions-toolkit/lib/types/builder'; import * as context from '../src/context'; jest.mock('uuid'); jest.spyOn(uuid, 'v4').mockReturnValue('9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'); 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, new Map([ ['install', 'false'], ['use', 'true'], ]), [ '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, new Map([ ['driver', 'docker'], ['install', 'false'], ['use', 'true'], ]), [ 'create', '--name', 'default', '--driver', 'docker', '--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host', '--use' ] ], [ 2, new Map([ ['install', 'false'], ['use', 'false'], ['driver-opts', 'image=moby/buildkit:master\nnetwork=host'], ]), [ '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, new Map([ ['driver', 'remote'], ['endpoint', 'tls://foo:1234'], ['install', 'false'], ['use', 'true'], ]), [ 'create', '--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d', '--driver', 'remote', '--use', 'tls://foo:1234' ] ], [ 4, new Map([ ['driver', 'remote'], ['platforms', 'linux/arm64,linux/arm/v7'], ['endpoint', 'tls://foo:1234'], ['install', 'false'], ['use', 'true'], ]), [ 'create', '--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d', '--driver', 'remote', '--platform', 'linux/arm64,linux/arm/v7', '--use', 'tls://foo:1234' ] ], [ 5, new Map([ ['install', 'false'], ['use', 'false'], ['driver-opts', `"env.no_proxy=localhost,127.0.0.1,.mydomain"`], ]), [ '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' ] ], [ 6, new Map([ ['install', 'false'], ['use', 'false'], ['platforms', 'linux/amd64\n"linux/arm64,linux/arm/v7"'], ]), [ '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' ] ], ])( '[%d] given %p as inputs, returns %p', async (num: number, inputs: Map, expected: Array) => { inputs.forEach((value: string, name: string) => { setInput(name, value); }); const inp = await context.getInputs(); const res = await context.getCreateArgs(inp, new Toolkit()); expect(res).toEqual(expected); } ); }); 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, new Map([ ['install', 'false'], ['use', 'true'], ]), { "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' ] ] ])( '[%d] given %p as inputs, returns %p', async (num: number, inputs: Map, node: Node, expected: Array) => { inputs.forEach((value: string, name: string) => { setInput(name, value); }); const inp = await context.getInputs(); const res = await context.getAppendArgs(inp, node, new Toolkit()); expect(res).toEqual(expected); } ); }); // 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; }