2023-01-31 10:34:51 +08:00
|
|
|
/**
|
|
|
|
* Copyright 2023 actions-toolkit authors
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-02-01 04:13:39 +08:00
|
|
|
import {afterEach, beforeEach, describe, expect, it, jest, test} from '@jest/globals';
|
2024-07-31 17:01:57 +08:00
|
|
|
import fs from 'fs';
|
|
|
|
import os from 'os';
|
|
|
|
import path from 'path';
|
2023-02-01 19:46:43 +08:00
|
|
|
import * as rimraf from 'rimraf';
|
2023-01-17 18:53:57 +08:00
|
|
|
|
2023-02-01 04:13:39 +08:00
|
|
|
import {Context} from '../../src/context';
|
2024-04-24 23:08:15 +08:00
|
|
|
import {Build} from '../../src/buildx/build';
|
2023-01-23 17:07:14 +08:00
|
|
|
|
2024-07-31 17:04:17 +08:00
|
|
|
const fixturesDir = path.join(__dirname, '..', '.fixtures');
|
2024-07-31 17:01:57 +08:00
|
|
|
const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'buildx-build-'));
|
2023-02-19 07:59:04 +08:00
|
|
|
const tmpName = path.join(tmpDir, '.tmpname-jest');
|
2024-07-31 00:42:53 +08:00
|
|
|
const metadata = JSON.parse(fs.readFileSync(path.join(fixturesDir, 'metadata-build.json'), 'utf-8'));
|
2023-01-17 18:53:57 +08:00
|
|
|
|
2023-02-20 14:19:57 +08:00
|
|
|
jest.spyOn(Context, 'tmpDir').mockImplementation((): string => {
|
2024-07-31 17:01:57 +08:00
|
|
|
fs.mkdirSync(tmpDir, {recursive: true});
|
2023-01-17 18:53:57 +08:00
|
|
|
return tmpDir;
|
|
|
|
});
|
2023-02-20 14:19:57 +08:00
|
|
|
|
|
|
|
jest.spyOn(Context, 'tmpName').mockImplementation((): string => {
|
2023-01-30 07:08:45 +08:00
|
|
|
return tmpName;
|
|
|
|
});
|
2023-01-17 18:53:57 +08:00
|
|
|
|
2023-01-23 17:07:14 +08:00
|
|
|
afterEach(() => {
|
|
|
|
rimraf.sync(tmpDir);
|
|
|
|
});
|
|
|
|
|
2024-04-24 23:33:01 +08:00
|
|
|
describe('resolveImageID', () => {
|
2023-01-30 07:26:20 +08:00
|
|
|
it('matches', async () => {
|
|
|
|
const imageID = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
|
2024-04-26 19:41:03 +08:00
|
|
|
const build = new Build();
|
2024-05-03 16:20:42 +08:00
|
|
|
fs.writeFileSync(build.getImageIDFilePath(), imageID);
|
2024-04-26 19:41:03 +08:00
|
|
|
expect(build.resolveImageID()).toEqual(imageID);
|
2023-01-30 07:26:20 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-04-24 23:33:01 +08:00
|
|
|
describe('resolveMetadata', () => {
|
2023-01-30 07:26:20 +08:00
|
|
|
it('matches', async () => {
|
2024-04-26 19:41:03 +08:00
|
|
|
const build = new Build();
|
2024-05-03 16:20:42 +08:00
|
|
|
fs.writeFileSync(build.getMetadataFilePath(), JSON.stringify(metadata));
|
2024-04-26 19:41:03 +08:00
|
|
|
expect(build.resolveMetadata()).toEqual(metadata);
|
2023-01-30 07:26:20 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-04-24 23:33:01 +08:00
|
|
|
describe('resolveRef', () => {
|
|
|
|
it('matches', async () => {
|
2024-04-26 19:41:03 +08:00
|
|
|
const build = new Build();
|
2024-05-03 16:20:42 +08:00
|
|
|
fs.writeFileSync(build.getMetadataFilePath(), JSON.stringify(metadata));
|
2024-04-26 19:41:03 +08:00
|
|
|
expect(build.resolveRef()).toEqual('default/default/n6ibcp9b2pw108rrz7ywdznvo');
|
2024-04-24 23:33:01 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-06-14 18:24:38 +08:00
|
|
|
describe('resolveProvenance', () => {
|
|
|
|
it('matches', async () => {
|
|
|
|
const build = new Build();
|
|
|
|
fs.writeFileSync(build.getMetadataFilePath(), JSON.stringify(metadata));
|
|
|
|
const provenance = build.resolveProvenance();
|
|
|
|
expect(provenance).toBeDefined();
|
|
|
|
expect(provenance?.buildType).toEqual('https://mobyproject.org/buildkit@v1');
|
|
|
|
expect(provenance?.materials).toBeDefined();
|
|
|
|
expect(provenance?.materials?.length).toEqual(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-06-26 15:06:34 +08:00
|
|
|
describe('resolveWarnings', () => {
|
|
|
|
it('matches', async () => {
|
|
|
|
const build = new Build();
|
|
|
|
fs.writeFileSync(build.getMetadataFilePath(), JSON.stringify(metadata));
|
|
|
|
const warnings = build.resolveWarnings();
|
|
|
|
expect(warnings).toBeDefined();
|
|
|
|
expect(warnings?.length).toEqual(3);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-03 10:33:09 +08:00
|
|
|
describe('resolveDigest', () => {
|
2023-01-30 07:26:20 +08:00
|
|
|
it('matches', async () => {
|
2024-04-26 19:41:03 +08:00
|
|
|
const build = new Build();
|
2024-05-03 16:20:42 +08:00
|
|
|
fs.writeFileSync(build.getMetadataFilePath(), JSON.stringify(metadata));
|
2024-04-26 19:41:03 +08:00
|
|
|
expect(build.resolveDigest()).toEqual('sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c');
|
2023-01-30 07:26:20 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-30 07:57:38 +08:00
|
|
|
describe('getProvenanceInput', () => {
|
|
|
|
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([
|
|
|
|
[
|
|
|
|
'true',
|
2024-05-28 01:57:40 +08:00
|
|
|
'builder-id=https://github.com/docker/actions-toolkit/actions/runs/2188748038/attempts/2'
|
2023-01-30 07:57:38 +08:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'false',
|
|
|
|
'false'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'mode=min',
|
2024-05-28 01:57:40 +08:00
|
|
|
'mode=min,builder-id=https://github.com/docker/actions-toolkit/actions/runs/2188748038/attempts/2'
|
2023-01-30 07:57:38 +08:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'mode=max',
|
2024-05-28 01:57:40 +08:00
|
|
|
'mode=max,builder-id=https://github.com/docker/actions-toolkit/actions/runs/2188748038/attempts/2'
|
2023-01-30 07:57:38 +08:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'builder-id=foo',
|
|
|
|
'builder-id=foo'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'mode=max,builder-id=foo',
|
|
|
|
'mode=max,builder-id=foo'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'',
|
|
|
|
''
|
|
|
|
],
|
|
|
|
])('given input %p', async (input: string, expected: string) => {
|
2024-05-03 16:20:42 +08:00
|
|
|
setInput('provenance', input);
|
2024-04-24 23:08:15 +08:00
|
|
|
expect(Build.getProvenanceInput('provenance')).toEqual(expected);
|
2023-01-30 07:57:38 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-03 10:33:09 +08:00
|
|
|
describe('resolveProvenanceAttrs', () => {
|
2023-01-30 07:57:38 +08:00
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
'mode=min',
|
2024-05-28 01:57:40 +08:00
|
|
|
'mode=min,builder-id=https://github.com/docker/actions-toolkit/actions/runs/2188748038/attempts/2'
|
2023-01-30 07:57:38 +08:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'mode=max',
|
2024-05-28 01:57:40 +08:00
|
|
|
'mode=max,builder-id=https://github.com/docker/actions-toolkit/actions/runs/2188748038/attempts/2'
|
2023-01-30 07:57:38 +08:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'builder-id=foo',
|
|
|
|
'builder-id=foo'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'mode=max,builder-id=foo',
|
|
|
|
'mode=max,builder-id=foo'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'',
|
2024-05-28 01:57:40 +08:00
|
|
|
'builder-id=https://github.com/docker/actions-toolkit/actions/runs/2188748038/attempts/2'
|
2023-01-30 07:57:38 +08:00
|
|
|
],
|
|
|
|
])('given %p', async (input: string, expected: string) => {
|
2024-04-24 23:08:15 +08:00
|
|
|
expect(Build.resolveProvenanceAttrs(input)).toEqual(expected);
|
2023-01-31 07:11:16 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-04-24 23:33:01 +08:00
|
|
|
describe('resolveSecret', () => {
|
2023-01-17 18:53:57 +08:00
|
|
|
test.each([
|
2023-01-23 17:07:14 +08:00
|
|
|
['A_SECRET=abcdef0123456789', false, 'A_SECRET', 'abcdef0123456789', null],
|
|
|
|
['GIT_AUTH_TOKEN=abcdefghijklmno=0123456789', false, 'GIT_AUTH_TOKEN', 'abcdefghijklmno=0123456789', null],
|
|
|
|
['MY_KEY=c3RyaW5nLXdpdGgtZXF1YWxzCg==', false, 'MY_KEY', 'c3RyaW5nLXdpdGgtZXF1YWxzCg==', null],
|
|
|
|
['aaaaaaaa', false, '', '', new Error('aaaaaaaa is not a valid secret')],
|
|
|
|
['aaaaaaaa=', false, '', '', new Error('aaaaaaaa= is not a valid secret')],
|
|
|
|
['=bbbbbbb', false, '', '', new Error('=bbbbbbb is not a valid secret')],
|
2023-02-19 07:59:04 +08:00
|
|
|
[`foo=${path.join(fixturesDir, 'secret.txt')}`, true, 'foo', 'bar', null],
|
2023-01-23 17:07:14 +08:00
|
|
|
[`notfound=secret`, true, '', '', new Error('secret file secret not found')]
|
2023-03-27 02:58:13 +08:00
|
|
|
])('given %p key and %p secret', async (kvp: string, file: boolean, exKey: string, exValue: string, error: Error | null) => {
|
2023-01-17 18:53:57 +08:00
|
|
|
try {
|
|
|
|
let secret: string;
|
|
|
|
if (file) {
|
2024-04-24 23:33:01 +08:00
|
|
|
secret = Build.resolveSecretFile(kvp);
|
2023-01-17 18:53:57 +08:00
|
|
|
} else {
|
2024-04-24 23:33:01 +08:00
|
|
|
secret = Build.resolveSecretString(kvp);
|
2023-01-17 18:53:57 +08:00
|
|
|
}
|
2023-01-23 17:07:14 +08:00
|
|
|
expect(secret).toEqual(`id=${exKey},src=${tmpName}`);
|
|
|
|
expect(fs.readFileSync(tmpName, 'utf-8')).toEqual(exValue);
|
|
|
|
} catch (e) {
|
2023-01-17 18:53:57 +08:00
|
|
|
// eslint-disable-next-line jest/no-conditional-expect
|
2023-01-23 17:07:14 +08:00
|
|
|
expect(e.message).toEqual(error?.message);
|
2023-01-17 18:53:57 +08:00
|
|
|
}
|
|
|
|
});
|
2023-06-01 21:19:50 +08:00
|
|
|
|
|
|
|
test.each([
|
|
|
|
['FOO=bar', 'FOO', 'bar', null],
|
|
|
|
['FOO=', 'FOO', '', new Error('FOO= is not a valid secret')],
|
|
|
|
['=bar', '', '', new Error('=bar is not a valid secret')],
|
|
|
|
['FOO=bar=baz', 'FOO', 'bar=baz', null]
|
|
|
|
])('given %p key and %p env', async (kvp: string, exKey: string, exValue: string, error: Error | null) => {
|
|
|
|
try {
|
2024-04-24 23:33:01 +08:00
|
|
|
const secret = Build.resolveSecretEnv(kvp);
|
2023-09-26 23:36:45 +08:00
|
|
|
expect(secret).toEqual(`id=${exKey},env=${exValue}`);
|
2023-06-01 21:19:50 +08:00
|
|
|
} catch (e) {
|
|
|
|
// eslint-disable-next-line jest/no-conditional-expect
|
|
|
|
expect(e.message).toEqual(error?.message);
|
|
|
|
}
|
|
|
|
});
|
2023-01-17 18:53:57 +08:00
|
|
|
});
|
2023-01-23 17:07:14 +08:00
|
|
|
|
2024-06-05 19:59:57 +08:00
|
|
|
describe('resolveCacheToAttrs', () => {
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
'',
|
|
|
|
undefined,
|
|
|
|
''
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'user/app:cache',
|
|
|
|
undefined,
|
|
|
|
'user/app:cache'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'type=inline',
|
|
|
|
undefined,
|
|
|
|
'type=inline'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'type=gha',
|
|
|
|
undefined,
|
|
|
|
'type=gha,repository=docker/actions-toolkit',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'type=gha,mode=max',
|
|
|
|
undefined,
|
|
|
|
'type=gha,mode=max,repository=docker/actions-toolkit',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'type=gha,mode=max',
|
|
|
|
'abcd1234',
|
|
|
|
'type=gha,mode=max,repository=docker/actions-toolkit,ghtoken=abcd1234',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'type=gha,repository=foo/bar,mode=max',
|
|
|
|
undefined,
|
|
|
|
'type=gha,repository=foo/bar,mode=max',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'type=gha,repository=foo/bar,mode=max',
|
|
|
|
'abcd1234',
|
|
|
|
'type=gha,repository=foo/bar,mode=max,ghtoken=abcd1234',
|
|
|
|
],
|
|
|
|
])('given %p', async (input: string, githubToken: string | undefined, expected: string) => {
|
|
|
|
expect(Build.resolveCacheToAttrs(input, githubToken)).toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-30 07:26:20 +08:00
|
|
|
describe('hasLocalExporter', () => {
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[['type=registry,ref=user/app'], false],
|
|
|
|
[['type=docker'], false],
|
|
|
|
[['type=local,dest=./release-out'], true],
|
|
|
|
[['type=tar,dest=/tmp/image.tar'], false],
|
|
|
|
[['type=docker', 'type=tar,dest=/tmp/image.tar'], false],
|
|
|
|
[['"type=tar","dest=/tmp/image.tar"'], false],
|
|
|
|
[['" type= local" , dest=./release-out'], true],
|
|
|
|
[['.'], true]
|
|
|
|
])('given %p returns %p', async (exporters: Array<string>, expected: boolean) => {
|
2024-04-24 23:08:15 +08:00
|
|
|
expect(Build.hasLocalExporter(exporters)).toEqual(expected);
|
2023-01-30 07:26:20 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('hasTarExporter', () => {
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[['type=registry,ref=user/app'], false],
|
|
|
|
[['type=docker'], false],
|
|
|
|
[['type=local,dest=./release-out'], false],
|
|
|
|
[['type=tar,dest=/tmp/image.tar'], true],
|
|
|
|
[['type=docker', 'type=tar,dest=/tmp/image.tar'], true],
|
|
|
|
[['"type=tar","dest=/tmp/image.tar"'], true],
|
|
|
|
[['" type= local" , dest=./release-out'], false],
|
|
|
|
[['.'], false]
|
|
|
|
])('given %p returns %p', async (exporters: Array<string>, expected: boolean) => {
|
2024-04-24 23:08:15 +08:00
|
|
|
expect(Build.hasTarExporter(exporters)).toEqual(expected);
|
2023-01-30 07:26:20 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('hasDockerExporter', () => {
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[['type=registry,ref=user/app'], false, undefined],
|
|
|
|
[['type=docker'], true, undefined],
|
|
|
|
[['type=local,dest=./release-out'], false, undefined],
|
|
|
|
[['type=tar,dest=/tmp/image.tar'], false, undefined],
|
|
|
|
[['type=docker', 'type=tar,dest=/tmp/image.tar'], true, undefined],
|
|
|
|
[['"type=tar","dest=/tmp/image.tar"'], false, undefined],
|
|
|
|
[['" type= local" , dest=./release-out'], false, undefined],
|
2023-02-20 17:25:50 +08:00
|
|
|
[['type=docker'], true, false],
|
|
|
|
[['type=docker'], true, true],
|
2023-01-30 07:26:20 +08:00
|
|
|
[['.'], true, true],
|
|
|
|
])('given %p returns %p', async (exporters: Array<string>, expected: boolean, load: boolean | undefined) => {
|
2024-04-24 23:08:15 +08:00
|
|
|
expect(Build.hasDockerExporter(exporters, load)).toEqual(expected);
|
2023-01-30 07:26:20 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-03-26 23:23:01 +08:00
|
|
|
describe('hasAttestationType', () => {
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
['type=provenance,mode=min', 'provenance', true],
|
|
|
|
['type=sbom,true', 'sbom', true],
|
|
|
|
['type=foo,bar', 'provenance', false],
|
|
|
|
])('given %p for %p returns %p', async (attrs: string, name: string, expected: boolean) => {
|
2024-04-24 23:08:15 +08:00
|
|
|
expect(Build.hasAttestationType(name, attrs)).toEqual(expected);
|
2024-03-26 23:23:01 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('resolveAttestationAttrs', () => {
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
'type=provenance,mode=min',
|
|
|
|
'type=provenance,mode=min'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'type=provenance,true',
|
|
|
|
'type=provenance,disabled=false'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'type=provenance,false',
|
|
|
|
'type=provenance,disabled=true'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'',
|
|
|
|
''
|
|
|
|
],
|
|
|
|
])('given %p', async (input: string, expected: string) => {
|
2024-04-24 23:08:15 +08:00
|
|
|
expect(Build.resolveAttestationAttrs(input)).toEqual(expected);
|
2024-03-26 23:23:01 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-23 17:07:14 +08:00
|
|
|
describe('hasGitAuthTokenSecret', () => {
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[['A_SECRET=abcdef0123456789'], false],
|
|
|
|
[['GIT_AUTH_TOKEN=abcdefghijklmno=0123456789'], true],
|
|
|
|
])('given %p secret', async (kvp: Array<string>, expected: boolean) => {
|
2024-04-24 23:08:15 +08:00
|
|
|
expect(Build.hasGitAuthTokenSecret(kvp)).toBe(expected);
|
2023-01-23 17:07:14 +08:00
|
|
|
});
|
|
|
|
});
|
2023-01-30 07:57:38 +08:00
|
|
|
|
|
|
|
// See: https://github.com/actions/toolkit/blob/a1b068ec31a042ff1e10a522d8fdf0b8869d53ca/packages/core/src/core.ts#L89
|
|
|
|
function getInputName(name: string): string {
|
|
|
|
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setInput(name: string, value: string): void {
|
|
|
|
process.env[getInputName(name)] = value;
|
|
|
|
}
|