2023-03-13 07:16:24 +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.
|
|
|
|
*/
|
|
|
|
|
2024-04-24 23:33:01 +08:00
|
|
|
import {afterEach, beforeEach, describe, expect, it, jest, test} from '@jest/globals';
|
2023-03-25 23:03:52 +08:00
|
|
|
import * as fs from 'fs';
|
2023-03-13 07:16:24 +08:00
|
|
|
import * as path from 'path';
|
2024-04-24 23:33:01 +08:00
|
|
|
import * as rimraf from 'rimraf';
|
2023-03-13 07:16:24 +08:00
|
|
|
|
|
|
|
import {Bake} from '../../src/buildx/bake';
|
2024-04-24 23:33:01 +08:00
|
|
|
import {Context} from '../../src/context';
|
2024-03-26 16:46:22 +08:00
|
|
|
|
|
|
|
import {ExecOptions} from '@actions/exec';
|
2024-04-24 23:33:01 +08:00
|
|
|
import {BakeDefinition, BakeMetadata} from '../../src/types/bake';
|
2023-03-13 07:16:24 +08:00
|
|
|
|
|
|
|
const fixturesDir = path.join(__dirname, '..', 'fixtures');
|
2024-04-24 23:33:01 +08:00
|
|
|
// prettier-ignore
|
|
|
|
const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-inputs-jest');
|
|
|
|
const tmpName = path.join(tmpDir, '.tmpname-jest');
|
|
|
|
const metadata: BakeMetadata = {
|
|
|
|
app: {
|
|
|
|
'buildx.build.ref': 'default/default/7frbdw1fmfozgtqavghowsepk'
|
|
|
|
},
|
|
|
|
db: {
|
|
|
|
'buildx.build.ref': 'default/default/onic7g2axylf56rxetob7qruy'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
jest.spyOn(Context, 'tmpDir').mockImplementation((): string => {
|
|
|
|
if (!fs.existsSync(tmpDir)) {
|
|
|
|
fs.mkdirSync(tmpDir, {recursive: true});
|
|
|
|
}
|
|
|
|
return tmpDir;
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.spyOn(Context, 'tmpName').mockImplementation((): string => {
|
|
|
|
return tmpName;
|
|
|
|
});
|
2023-03-13 07:16:24 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2024-04-24 23:33:01 +08:00
|
|
|
afterEach(() => {
|
|
|
|
rimraf.sync(tmpDir);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('resolveMetadata', () => {
|
|
|
|
it('matches', async () => {
|
|
|
|
const metadataFile = Bake.getMetadataFilePath();
|
|
|
|
await fs.writeFileSync(metadataFile, JSON.stringify(metadata));
|
|
|
|
const expected = Bake.resolveMetadata();
|
|
|
|
expect(expected).toEqual(metadata as BakeMetadata);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('resolveRefs', () => {
|
|
|
|
it('matches', async () => {
|
|
|
|
const metadataFile = Bake.getMetadataFilePath();
|
|
|
|
await fs.writeFileSync(metadataFile, JSON.stringify(metadata));
|
|
|
|
const expected = Bake.resolveRefs();
|
|
|
|
expect(expected).toEqual(['default/default/7frbdw1fmfozgtqavghowsepk', 'default/default/onic7g2axylf56rxetob7qruy']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-03-26 16:46:22 +08:00
|
|
|
describe('getDefinition', () => {
|
2023-03-13 07:16:24 +08:00
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[
|
2023-03-27 01:26:59 +08:00
|
|
|
[path.join(fixturesDir, 'bake-01.hcl')],
|
2023-03-13 07:16:24 +08:00
|
|
|
['validate'],
|
2023-04-18 20:16:46 +08:00
|
|
|
[],
|
2024-03-26 16:46:22 +08:00
|
|
|
{silent: true},
|
2023-03-27 01:26:59 +08:00
|
|
|
path.join(fixturesDir, 'bake-01-validate.json')
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[path.join(fixturesDir, 'bake-02.hcl')],
|
|
|
|
['build'],
|
2023-04-18 20:16:46 +08:00
|
|
|
[],
|
2024-03-26 16:46:22 +08:00
|
|
|
undefined,
|
2023-03-27 01:26:59 +08:00
|
|
|
path.join(fixturesDir, 'bake-02-build.json')
|
2023-04-18 20:16:46 +08:00
|
|
|
],
|
|
|
|
[
|
|
|
|
[path.join(fixturesDir, 'bake-01.hcl')],
|
|
|
|
['image'],
|
|
|
|
['*.output=type=docker', '*.platform=linux/amd64'],
|
2024-03-26 16:46:22 +08:00
|
|
|
undefined,
|
2023-04-18 20:16:46 +08:00
|
|
|
path.join(fixturesDir, 'bake-01-overrides.json')
|
2023-03-13 07:16:24 +08:00
|
|
|
]
|
2024-03-26 16:46:22 +08:00
|
|
|
])('given %p', async (files: string[], targets: string[], overrides: string[], execOptions: ExecOptions | undefined, out: string) => {
|
2023-03-13 07:16:24 +08:00
|
|
|
const bake = new Bake();
|
2023-03-25 23:03:52 +08:00
|
|
|
const expectedDef = <BakeDefinition>JSON.parse(fs.readFileSync(out, {encoding: 'utf-8'}).trim())
|
2024-03-26 16:46:22 +08:00
|
|
|
expect(await bake.getDefinition({
|
|
|
|
files: files,
|
|
|
|
targets: targets,
|
|
|
|
overrides: overrides
|
|
|
|
}, execOptions)).toEqual(expectedDef);
|
2023-03-13 07:16:24 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('hasLocalExporter', () => {
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"build": {
|
|
|
|
"output": [
|
|
|
|
"type=docker"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false
|
|
|
|
],
|
2023-03-27 01:26:59 +08:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"build": {
|
|
|
|
"target": "build"
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-27 01:26:59 +08:00
|
|
|
false
|
|
|
|
],
|
2023-03-13 07:16:24 +08:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"local": {
|
|
|
|
"output": [
|
|
|
|
"type=local,dest=./release-out"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"tar": {
|
|
|
|
"output": [
|
|
|
|
"type=tar,dest=/tmp/image.tar"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"tar": {
|
|
|
|
"output": [
|
|
|
|
'"type=tar","dest=/tmp/image.tar"',
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"local": {
|
|
|
|
"output": [
|
|
|
|
'" type= local" , dest=./release-out',
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"local": {
|
|
|
|
"output": [
|
|
|
|
".",
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
true
|
|
|
|
]
|
|
|
|
])('given %o returns %p', async (def: BakeDefinition, expected: boolean) => {
|
|
|
|
expect(Bake.hasLocalExporter(def)).toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('hasTarExporter', () => {
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"reg": {
|
|
|
|
"output": [
|
|
|
|
"type=registry,ref=user/app"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"build": {
|
|
|
|
"output": [
|
|
|
|
"type=docker"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"local": {
|
|
|
|
"output": [
|
|
|
|
"type=local,dest=./release-out"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"tar": {
|
|
|
|
"output": [
|
|
|
|
"type=tar,dest=/tmp/image.tar"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"multi": {
|
|
|
|
"output": [
|
|
|
|
"type=docker",
|
|
|
|
"type=tar,dest=/tmp/image.tar"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"tar": {
|
|
|
|
"output": [
|
|
|
|
'"type=tar","dest=/tmp/image.tar"',
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"local": {
|
|
|
|
"output": [
|
|
|
|
'" type= local" , dest=./release-out',
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"local": {
|
|
|
|
"output": [
|
|
|
|
".",
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false
|
|
|
|
],
|
|
|
|
])('given %o returns %p', async (def: BakeDefinition, expected: boolean) => {
|
|
|
|
expect(Bake.hasTarExporter(def)).toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('hasDockerExporter', () => {
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"reg": {
|
|
|
|
"output": [
|
|
|
|
"type=registry,ref=user/app"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false,
|
|
|
|
undefined
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"build": {
|
|
|
|
"output": [
|
|
|
|
"type=docker"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
true,
|
|
|
|
undefined
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"multi": {
|
|
|
|
"output": [
|
|
|
|
"type=docker",
|
|
|
|
"type=tar,dest=/tmp/image.tar"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
true,
|
|
|
|
undefined
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"local": {
|
|
|
|
"output": [
|
|
|
|
'" type= local" , dest=./release-out'
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false,
|
|
|
|
undefined
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"local": {
|
|
|
|
"output": [
|
|
|
|
"type=local,dest=./release-out"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false,
|
|
|
|
undefined
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"tar": {
|
|
|
|
"output": [
|
|
|
|
"type=tar,dest=/tmp/image.tar"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false,
|
|
|
|
undefined
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"multi": {
|
|
|
|
"output": [
|
|
|
|
"type=docker",
|
|
|
|
"type=tar,dest=/tmp/image.tar"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
true,
|
|
|
|
undefined
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"tar": {
|
|
|
|
"output": [
|
|
|
|
'"type=tar","dest=/tmp/image.tar"'
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false,
|
|
|
|
undefined
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"tar": {
|
|
|
|
"output": [
|
|
|
|
'"type=tar","dest=/tmp/image.tar"'
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false,
|
|
|
|
undefined
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"local": {
|
|
|
|
"output": [
|
|
|
|
'" type= local" , dest=./release-out'
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
false,
|
|
|
|
undefined
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"build": {
|
|
|
|
"output": [
|
|
|
|
"type=docker"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
true,
|
|
|
|
false
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"build": {
|
|
|
|
"output": [
|
|
|
|
"type=docker"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
true,
|
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"target": {
|
|
|
|
"build": {
|
|
|
|
"output": [
|
|
|
|
"."
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
2023-03-27 02:58:13 +08:00
|
|
|
} as unknown as BakeDefinition,
|
2023-03-13 07:16:24 +08:00
|
|
|
true,
|
|
|
|
true
|
|
|
|
],
|
2023-03-27 02:58:13 +08:00
|
|
|
])('given %o and load:%p returns %p', async (def: BakeDefinition, expected: boolean, load: boolean | undefined) => {
|
2023-03-13 07:16:24 +08:00
|
|
|
expect(Bake.hasDockerExporter(def, load)).toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|