Skip to content

Commit e186e22

Browse files
committed
undock: check for availability
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 5abb5fc commit e186e22

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

__tests__/undock/undock.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2024 actions-toolkit authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {describe, expect, it, jest, test} from '@jest/globals';
18+
import * as semver from 'semver';
19+
20+
import {Exec} from '../../src/exec';
21+
import {Undock} from '../../src/undock/undock';
22+
23+
describe('isAvailable', () => {
24+
it('checks undock is available', async () => {
25+
const execSpy = jest.spyOn(Exec, 'getExecOutput');
26+
const undock = new Undock();
27+
await undock.isAvailable();
28+
// eslint-disable-next-line jest/no-standalone-expect
29+
expect(execSpy).toHaveBeenCalledWith(`undock`, [], {
30+
silent: true,
31+
ignoreReturnCode: true
32+
});
33+
});
34+
});
35+
36+
describe('printVersion', () => {
37+
it('prints undock version', async () => {
38+
const execSpy = jest.spyOn(Exec, 'exec');
39+
const undock = new Undock();
40+
await undock.printVersion();
41+
expect(execSpy).toHaveBeenCalledWith(`undock`, ['--version'], {
42+
failOnStdErr: false
43+
});
44+
});
45+
});
46+
47+
describe('version', () => {
48+
it('valid', async () => {
49+
const undock = new Undock();
50+
expect(semver.valid(await undock.version())).not.toBeUndefined();
51+
});
52+
});
53+
54+
describe('versionSatisfies', () => {
55+
test.each([
56+
['v0.4.1', '>=0.3.2', true],
57+
['v0.8.0', '>0.6.0', true],
58+
['v0.8.0', '<0.3.0', false]
59+
])('given %p', async (version, range, expected) => {
60+
const undock = new Undock();
61+
expect(await undock.versionSatisfies(range, version)).toBe(expected);
62+
});
63+
});

dev.Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ARG NODE_VERSION=20
1818
ARG DOCKER_VERSION=27.2.1
1919
ARG BUILDX_VERSION=0.17.1
20+
ARG UNDOCK_VERSION=0.8.0
2021

2122
FROM node:${NODE_VERSION}-alpine AS base
2223
RUN apk add --no-cache cpio findutils git
@@ -75,6 +76,7 @@ RUN --mount=type=bind,target=.,rw \
7576

7677
FROM docker:${DOCKER_VERSION} as docker
7778
FROM docker/buildx-bin:${BUILDX_VERSION} as buildx
79+
FROM crazy-max/undock:${UNDOCK_VERSION} as undock
7880

7981
FROM deps AS test
8082
RUN --mount=type=bind,target=.,rw \
@@ -83,6 +85,7 @@ RUN --mount=type=bind,target=.,rw \
8385
--mount=type=bind,from=docker,source=/usr/local/bin/docker,target=/usr/bin/docker \
8486
--mount=type=bind,from=buildx,source=/buildx,target=/usr/libexec/docker/cli-plugins/docker-buildx \
8587
--mount=type=bind,from=buildx,source=/buildx,target=/usr/bin/buildx \
88+
--mount=type=bind,from=undock,source=/usr/local/bin/undock,target=/usr/bin/undock \
8689
--mount=type=secret,id=GITHUB_TOKEN \
8790
GITHUB_TOKEN=$(cat /run/secrets/GITHUB_TOKEN) yarn run test:coverage --coverageDirectory=/tmp/coverage
8891

src/toolkit.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {Bake as BuildxBake} from './buildx/bake';
2020
import {Install as BuildxInstall} from './buildx/install';
2121
import {Builder} from './buildx/builder';
2222
import {BuildKit} from './buildkit/buildkit';
23+
import {Undock} from './undock/undock';
2324
import {GitHub} from './github';
2425

2526
export interface ToolkitOpts {
@@ -38,6 +39,7 @@ export class Toolkit {
3839
public buildxInstall: BuildxInstall;
3940
public builder: Builder;
4041
public buildkit: BuildKit;
42+
public undock: Undock;
4143

4244
constructor(opts: ToolkitOpts = {}) {
4345
this.github = new GitHub({token: opts.githubToken});
@@ -47,5 +49,6 @@ export class Toolkit {
4749
this.buildxInstall = new BuildxInstall();
4850
this.builder = new Builder({buildx: this.buildx});
4951
this.buildkit = new BuildKit({buildx: this.buildx});
52+
this.undock = new Undock();
5053
}
5154
}

src/undock/undock.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Copyright 2024 actions-toolkit authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import os from 'os';
18+
import path from 'path';
19+
import * as core from '@actions/core';
20+
import * as semver from 'semver';
21+
22+
import {Exec} from '../exec';
23+
24+
export interface UndockOpts {
25+
binPath?: string;
26+
}
27+
28+
export class Undock {
29+
private readonly binPath: string;
30+
private _version: string;
31+
private _versionOnce: boolean;
32+
33+
constructor(opts?: UndockOpts) {
34+
this.binPath = opts?.binPath || 'undock';
35+
this._version = '';
36+
this._versionOnce = false;
37+
}
38+
39+
static get cacheDir(): string {
40+
return process.env.UNDOCK_CACHE_DIR || path.join(os.homedir(), '.local', 'share', 'undock', 'cache');
41+
}
42+
43+
public async isAvailable(): Promise<boolean> {
44+
const ok: boolean = await Exec.getExecOutput(this.binPath, [], {
45+
ignoreReturnCode: true,
46+
silent: true
47+
})
48+
.then(res => {
49+
if (res.stderr.length > 0 && res.exitCode != 0) {
50+
core.debug(`Undock.isAvailable cmd err: ${res.stderr.trim()}`);
51+
return false;
52+
}
53+
return res.exitCode == 0;
54+
})
55+
.catch(error => {
56+
core.debug(`Undock.isAvailable error: ${error}`);
57+
return false;
58+
});
59+
60+
core.debug(`Undock.isAvailable: ${ok}`);
61+
return ok;
62+
}
63+
64+
public async version(): Promise<string> {
65+
if (this._versionOnce) {
66+
return this._version;
67+
}
68+
this._versionOnce = true;
69+
this._version = await Exec.getExecOutput(this.binPath, ['--version'], {
70+
ignoreReturnCode: true,
71+
silent: true
72+
}).then(res => {
73+
if (res.stderr.length > 0 && res.exitCode != 0) {
74+
throw new Error(res.stderr.trim());
75+
}
76+
return res.stdout.trim();
77+
});
78+
return this._version;
79+
}
80+
81+
public async printVersion() {
82+
await Exec.exec(this.binPath, ['--version'], {
83+
failOnStdErr: false
84+
});
85+
}
86+
87+
public async versionSatisfies(range: string, version?: string): Promise<boolean> {
88+
const ver = version ?? (await this.version());
89+
if (!ver) {
90+
core.debug(`Undock.versionSatisfies false: undefined version`);
91+
return false;
92+
}
93+
const res = semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
94+
core.debug(`Undock.versionSatisfies ${ver} statisfies ${range}: ${res}`);
95+
return res;
96+
}
97+
}

0 commit comments

Comments
 (0)