2021-07-02 13:02:22 +08:00
|
|
|
import * as fs from 'fs';
|
2020-08-18 23:40:31 +08:00
|
|
|
import * as os from 'os';
|
2021-07-02 13:02:22 +08:00
|
|
|
import * as path from 'path';
|
|
|
|
import * as buildx from '../src/buildx';
|
|
|
|
import * as context from '../src/context';
|
2020-09-04 03:02:36 +08:00
|
|
|
import * as semver from 'semver';
|
|
|
|
import * as exec from '@actions/exec';
|
2020-08-18 23:40:31 +08:00
|
|
|
|
2021-07-02 13:02:22 +08: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-06-23 22:11:52 +08:00
|
|
|
describe('isAvailable', () => {
|
|
|
|
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'getExecOutput');
|
|
|
|
buildx.isAvailable();
|
|
|
|
|
|
|
|
expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], {
|
|
|
|
silent: true,
|
|
|
|
ignoreReturnCode: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-09-04 03:02:36 +08:00
|
|
|
describe('getVersion', () => {
|
2021-06-23 22:11:52 +08:00
|
|
|
async function isDaemonRunning() {
|
|
|
|
return await exec
|
|
|
|
.getExecOutput(`docker`, ['version', '--format', '{{.Server.Os}}'], {
|
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
2021-06-29 20:02:42 +08:00
|
|
|
return !res.stdout.trim().includes(' ') && res.exitCode == 0;
|
2021-06-23 22:11:52 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
(isDaemonRunning() ? it : it.skip)(
|
|
|
|
'valid',
|
|
|
|
async () => {
|
|
|
|
const version = await buildx.getVersion();
|
|
|
|
console.log(`version: ${version}`);
|
|
|
|
expect(semver.valid(version)).not.toBeNull();
|
|
|
|
},
|
|
|
|
100000
|
|
|
|
);
|
2020-09-04 03:02:36 +08:00
|
|
|
});
|
2020-08-18 23:40:31 +08:00
|
|
|
|
2020-09-04 03:02:36 +08: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 13:02:22 +08:00
|
|
|
['github.com/docker/buildx v0.4.2 fb7b670b764764dc4716df3eba07ffdae4cc47b2', '0.4.2'],
|
|
|
|
['github.com/docker/buildx f117971 f11797113e5a9b86bd976329c5dbb8a8bfdfadfa', 'f117971']
|
2020-09-04 03:02:36 +08:00
|
|
|
])('given %p', async (stdout, expected) => {
|
2021-07-02 13:02:22 +08: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-04 03:02:36 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-24 00:14:38 +08:00
|
|
|
describe('inspect', () => {
|
2020-08-27 07:48:38 +08:00
|
|
|
async function isDaemonRunning() {
|
2021-06-23 22:11:52 +08:00
|
|
|
return await exec
|
|
|
|
.getExecOutput(`docker`, ['version', '--format', '{{.Server.Os}}'], {
|
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
2021-06-29 20:02:42 +08:00
|
|
|
return !res.stdout.trim().includes(' ') && res.exitCode == 0;
|
2021-06-23 22:11:52 +08:00
|
|
|
});
|
2020-08-27 07:48:38 +08:00
|
|
|
}
|
|
|
|
(isDaemonRunning() ? it : it.skip)(
|
2020-09-04 03:02:36 +08:00
|
|
|
'valid',
|
2020-08-27 07:48:38 +08:00
|
|
|
async () => {
|
2021-04-24 00:14:38 +08:00
|
|
|
const builder = await buildx.inspect('');
|
|
|
|
console.log('builder', builder);
|
|
|
|
expect(builder).not.toBeUndefined();
|
|
|
|
expect(builder.name).not.toEqual('');
|
|
|
|
expect(builder.driver).not.toEqual('');
|
|
|
|
expect(builder.node_platforms).not.toEqual('');
|
2020-08-27 07:48:38 +08:00
|
|
|
},
|
|
|
|
100000
|
|
|
|
);
|
2020-09-04 03:02:36 +08:00
|
|
|
});
|
|
|
|
|
2021-07-02 13:02:22 +08:00
|
|
|
describe('build', () => {
|
|
|
|
it.skip('valid', async () => {
|
|
|
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));
|
|
|
|
const buildxBin = await buildx.build('https://github.com/docker/buildx.git#refs/pull/648/head', tmpDir);
|
|
|
|
console.log(buildxBin);
|
|
|
|
expect(fs.existsSync(buildxBin)).toBe(true);
|
|
|
|
}, 100000);
|
|
|
|
});
|
|
|
|
|
2020-09-04 03:02:36 +08:00
|
|
|
describe('install', () => {
|
|
|
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));
|
2021-01-05 02:03:58 +08:00
|
|
|
it('acquires v0.4.1 version of buildx', async () => {
|
|
|
|
const buildxBin = await buildx.install('v0.4.1', tmpDir);
|
2020-08-18 23:40:31 +08:00
|
|
|
console.log(buildxBin);
|
|
|
|
expect(fs.existsSync(buildxBin)).toBe(true);
|
|
|
|
}, 100000);
|
|
|
|
it('acquires latest version of buildx', async () => {
|
|
|
|
const buildxBin = await buildx.install('latest', tmpDir);
|
|
|
|
console.log(buildxBin);
|
|
|
|
expect(fs.existsSync(buildxBin)).toBe(true);
|
|
|
|
}, 100000);
|
|
|
|
});
|