1
0
Files
setup-buildx-action/__tests__/buildx.test.ts
T

134 lines
4.4 KiB
TypeScript
Raw Normal View History

2022-03-21 13:43:41 +01:00
import {describe, expect, it, jest, test} from '@jest/globals';
2021-07-02 07:02:22 +02:00
import * as fs from 'fs';
import * as os from 'os';
2021-07-02 07:02:22 +02:00
import * as path from 'path';
import * as buildx from '../src/buildx';
import * as context from '../src/context';
2020-09-03 21:02:36 +02:00
import * as semver from 'semver';
import * as exec from '@actions/exec';
2021-09-03 22:21:20 +02:00
const tmpNameSync = path.join('/tmp/.docker-setup-buildx-jest', '.tmpname-jest').split(path.sep).join(path.posix.sep);
2021-07-02 07:02:22 +02:00
jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
const tmpDir = path.join('/tmp/.docker-setup-buildx-jest').split(path.sep).join(path.posix.sep);
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, {recursive: true});
}
return tmpDir;
});
2021-09-03 22:21:20 +02:00
jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => {
return tmpNameSync;
});
2021-06-23 16:11:52 +02:00
describe('isAvailable', () => {
2022-03-21 13:43:41 +01:00
const execSpy = jest.spyOn(exec, 'getExecOutput');
2021-06-23 16:11:52 +02:00
buildx.isAvailable();
2022-03-21 13:43:41 +01:00
// eslint-disable-next-line jest/no-standalone-expect
2021-06-23 16:11:52 +02:00
expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], {
silent: true,
ignoreReturnCode: true
});
});
2020-09-03 21:02:36 +02:00
describe('getVersion', () => {
2022-03-21 13:43:41 +01:00
it('valid', async () => {
const version = await buildx.getVersion();
expect(semver.valid(version)).not.toBeNull();
});
2020-09-03 21:02:36 +02:00
});
2020-09-03 21:02:36 +02:00
describe('parseVersion', () => {
test.each([
['github.com/docker/buildx 0.4.1+azure bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
['github.com/docker/buildx v0.4.1 bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
2021-07-02 07:02:22 +02:00
['github.com/docker/buildx v0.4.2 fb7b670b764764dc4716df3eba07ffdae4cc47b2', '0.4.2'],
['github.com/docker/buildx f117971 f11797113e5a9b86bd976329c5dbb8a8bfdfadfa', 'f117971']
2020-09-03 21:02:36 +02:00
])('given %p', async (stdout, expected) => {
2021-07-02 07:02:22 +02: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-09-03 21:02:36 +02:00
});
});
2021-04-23 18:14:38 +02:00
describe('inspect', () => {
2022-03-21 13:43:41 +01:00
it('valid', async () => {
const builder = await buildx.inspect('');
expect(builder).not.toBeUndefined();
expect(builder.name).not.toEqual('');
expect(builder.driver).not.toEqual('');
expect(builder.node_platforms).not.toEqual('');
}, 100000);
2020-09-03 21:02:36 +02:00
});
2021-07-02 07:02:22 +02:00
describe('build', () => {
2021-07-11 22:42:19 +02:00
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));
2022-03-21 13:43:41 +01:00
// eslint-disable-next-line jest/no-disabled-tests
2021-07-11 22:42:19 +02:00
it.skip('builds refs/pull/648/head', async () => {
2021-07-02 07:02:22 +02:00
const buildxBin = await buildx.build('https://github.com/docker/buildx.git#refs/pull/648/head', tmpDir);
expect(fs.existsSync(buildxBin)).toBe(true);
}, 100000);
2022-03-21 13:43:41 +01:00
// eslint-disable-next-line jest/no-disabled-tests
2021-07-11 22:42:19 +02:00
it.skip('builds 67bd6f4dc82a9cd96f34133dab3f6f7af803bb14', async () => {
const buildxBin = await buildx.build('https://github.com/docker/buildx.git#67bd6f4dc82a9cd96f34133dab3f6f7af803bb14', tmpDir);
expect(fs.existsSync(buildxBin)).toBe(true);
}, 100000);
2021-07-02 07:02:22 +02:00
});
2020-09-03 21:02:36 +02:00
describe('install', () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));
2022-03-21 13:43:41 +01:00
2021-01-04 19:03:58 +01:00
it('acquires v0.4.1 version of buildx', async () => {
const buildxBin = await buildx.install('v0.4.1', tmpDir);
expect(fs.existsSync(buildxBin)).toBe(true);
}, 100000);
2022-03-21 13:43:41 +01:00
it('acquires latest version of buildx', async () => {
const buildxBin = await buildx.install('latest', tmpDir);
expect(fs.existsSync(buildxBin)).toBe(true);
}, 100000);
});
2021-09-03 22:21:20 +02:00
describe('getConfig', () => {
test.each([
['debug = true', false, 'debug = true', false],
[`notfound.toml`, true, '', true],
[
`${path.join(__dirname, 'fixtures', 'buildkitd.toml').split(path.sep).join(path.posix.sep)}`,
true,
`debug = true
[registry."docker.io"]
mirrors = ["mirror.gcr.io"]
`,
false
]
])('given %p config', async (val, file, exValue, invalid) => {
try {
let config: string;
if (file) {
config = await buildx.getConfigFile(val);
} else {
config = await buildx.getConfigInline(val);
}
expect(true).toBe(!invalid);
expect(config).toEqual(`${tmpNameSync}`);
2022-03-21 13:43:41 +01:00
const configValue = fs.readFileSync(tmpNameSync, 'utf-8');
2021-09-03 22:21:20 +02:00
expect(configValue).toEqual(exValue);
} catch (err) {
2022-03-21 13:43:41 +01:00
// eslint-disable-next-line jest/no-conditional-expect
2021-09-03 22:21:20 +02:00
expect(true).toBe(invalid);
}
});
});