2020-10-19 19:17:06 +00:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2020-08-23 01:31:38 +00:00
|
|
|
import * as semver from 'semver';
|
2021-06-22 17:52:21 +00:00
|
|
|
import * as exec from '@actions/exec';
|
2020-11-17 20:38:45 +00:00
|
|
|
|
2020-08-17 20:18:15 +00:00
|
|
|
import * as buildx from '../src/buildx';
|
2020-10-19 19:17:06 +00:00
|
|
|
import * as context from '../src/context';
|
2020-08-17 20:18:15 +00:00
|
|
|
|
2020-10-23 16:21:44 +00:00
|
|
|
const tmpNameSync = path.join('/tmp/.docker-build-push-jest', '.tmpname-jest').split(path.sep).join(path.posix.sep);
|
2020-08-17 20:18:15 +00:00
|
|
|
const digest = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
|
2021-08-16 21:44:13 +00:00
|
|
|
const metadata = `{
|
|
|
|
"containerimage.config.digest": "sha256:059b68a595b22564a1cbc167af369349fdc2ecc1f7bc092c2235cbf601a795fd",
|
|
|
|
"containerimage.digest": "sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c"
|
|
|
|
}`;
|
2020-08-17 20:18:15 +00:00
|
|
|
|
2020-10-19 19:17:06 +00:00
|
|
|
jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
|
|
|
|
const tmpDir = path.join('/tmp/.docker-build-push-jest').split(path.sep).join(path.posix.sep);
|
|
|
|
if (!fs.existsSync(tmpDir)) {
|
|
|
|
fs.mkdirSync(tmpDir, {recursive: true});
|
|
|
|
}
|
|
|
|
return tmpDir;
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => {
|
2020-10-23 16:21:44 +00:00
|
|
|
return tmpNameSync;
|
2020-10-19 19:17:06 +00:00
|
|
|
});
|
|
|
|
|
2020-08-17 20:18:15 +00:00
|
|
|
describe('getImageID', () => {
|
|
|
|
it('matches', async () => {
|
|
|
|
const imageIDFile = await buildx.getImageIDFile();
|
|
|
|
await fs.writeFileSync(imageIDFile, digest);
|
|
|
|
const imageID = await buildx.getImageID();
|
|
|
|
expect(imageID).toEqual(digest);
|
|
|
|
});
|
|
|
|
});
|
2020-08-23 01:31:38 +00:00
|
|
|
|
2021-08-16 21:44:13 +00:00
|
|
|
describe('getMetadata', () => {
|
|
|
|
it('matches', async () => {
|
|
|
|
const metadataFile = await buildx.getMetadataFile();
|
|
|
|
await fs.writeFileSync(metadataFile, metadata);
|
|
|
|
const expected = await buildx.getMetadata();
|
|
|
|
expect(expected).toEqual(metadata);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-10-20 13:18:02 +00:00
|
|
|
describe('isLocalOrTarExporter', () => {
|
|
|
|
// 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'
|
|
|
|
],
|
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'type=docker',
|
|
|
|
'type=tar,dest=/tmp/image.tar'
|
|
|
|
],
|
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'"type=tar","dest=/tmp/image.tar"'
|
|
|
|
],
|
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'" type= local" , dest=./release-out'
|
|
|
|
],
|
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'.'
|
|
|
|
],
|
2020-10-20 15:53:03 +00:00
|
|
|
true
|
2020-10-20 13:18:02 +00:00
|
|
|
],
|
|
|
|
])(
|
|
|
|
'given %p returns %p',
|
|
|
|
async (outputs: Array<string>, expected: boolean) => {
|
|
|
|
expect(buildx.isLocalOrTarExporter(outputs)).toEqual(expected);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-06-22 17:52:21 +00:00
|
|
|
describe('isAvailable', () => {
|
|
|
|
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'getExecOutput');
|
|
|
|
buildx.isAvailable();
|
|
|
|
|
|
|
|
expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], {
|
|
|
|
silent: true,
|
|
|
|
ignoreReturnCode: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-23 01:31:38 +00:00
|
|
|
describe('getVersion', () => {
|
2020-10-23 19:58:59 +00:00
|
|
|
async function isDaemonRunning() {
|
2021-06-22 17:52:21 +00:00
|
|
|
return await exec
|
|
|
|
.getExecOutput(`docker`, ['version', '--format', '{{.Server.Os}}'], {
|
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
return !res.stdout.includes(' ') && res.exitCode == 0;
|
|
|
|
});
|
2020-10-23 19:58:59 +00:00
|
|
|
}
|
|
|
|
(isDaemonRunning() ? it : it.skip)(
|
|
|
|
'valid',
|
|
|
|
async () => {
|
|
|
|
const version = await buildx.getVersion();
|
|
|
|
expect(semver.valid(version)).not.toBeNull();
|
|
|
|
},
|
|
|
|
100000
|
|
|
|
);
|
2020-08-23 01:31:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('parseVersion', () => {
|
|
|
|
test.each([
|
2020-08-23 02:01:20 +00:00
|
|
|
['github.com/docker/buildx 0.4.1+azure bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
|
2020-08-23 01:31:38 +00:00
|
|
|
['github.com/docker/buildx v0.4.1 bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
|
2021-07-01 13:29:36 +00:00
|
|
|
['github.com/docker/buildx v0.4.2 fb7b670b764764dc4716df3eba07ffdae4cc47b2', '0.4.2'],
|
|
|
|
['github.com/docker/buildx f117971 f11797113e5a9b86bd976329c5dbb8a8bfdfadfa', 'f117971']
|
2020-08-23 01:31:38 +00:00
|
|
|
])('given %p', async (stdout, expected) => {
|
2021-07-01 13:29:36 +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-08-23 01:31:38 +00:00
|
|
|
});
|
|
|
|
});
|
2020-10-23 16:21:44 +00:00
|
|
|
|
|
|
|
describe('getSecret', () => {
|
2020-10-23 20:51:59 +00:00
|
|
|
test.each([
|
2021-02-15 09:08:19 +00:00
|
|
|
['A_SECRET=abcdef0123456789', false, 'A_SECRET', 'abcdef0123456789', false],
|
|
|
|
['GIT_AUTH_TOKEN=abcdefghijklmno=0123456789', false, 'GIT_AUTH_TOKEN', 'abcdefghijklmno=0123456789', false],
|
|
|
|
['MY_KEY=c3RyaW5nLXdpdGgtZXF1YWxzCg==', false, 'MY_KEY', 'c3RyaW5nLXdpdGgtZXF1YWxzCg==', false],
|
|
|
|
['aaaaaaaa', false, '', '', true],
|
|
|
|
['aaaaaaaa=', false, '', '', true],
|
|
|
|
['=bbbbbbb', false, '', '', true],
|
2021-07-01 13:29:36 +00:00
|
|
|
[`foo=${path.join(__dirname, 'fixtures', 'secret.txt').split(path.sep).join(path.posix.sep)}`, true, 'foo', 'bar', false],
|
2021-02-15 09:08:19 +00:00
|
|
|
[`notfound=secret`, true, '', '', true]
|
|
|
|
])('given %p key and %p secret', async (kvp, file, exKey, exValue, invalid) => {
|
2020-11-17 20:38:45 +00:00
|
|
|
try {
|
2021-02-15 09:08:19 +00:00
|
|
|
let secret: string;
|
|
|
|
if (file) {
|
|
|
|
secret = await buildx.getSecretFile(kvp);
|
|
|
|
} else {
|
|
|
|
secret = await buildx.getSecretString(kvp);
|
|
|
|
}
|
2020-11-17 20:38:45 +00:00
|
|
|
expect(true).toBe(!invalid);
|
2021-02-15 09:08:19 +00:00
|
|
|
expect(secret).toEqual(`id=${exKey},src=${tmpNameSync}`);
|
|
|
|
const secretValue = await fs.readFileSync(tmpNameSync, 'utf-8');
|
|
|
|
expect(secretValue).toEqual(exValue);
|
2020-11-17 20:38:45 +00:00
|
|
|
} catch (err) {
|
|
|
|
expect(true).toBe(invalid);
|
|
|
|
}
|
2020-10-23 16:21:44 +00:00
|
|
|
});
|
|
|
|
});
|