docker: contextInspect func

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2024-07-04 10:13:17 +02:00
parent c70efab546
commit 61967435c1
No known key found for this signature in database
GPG Key ID: ADE44D8C9D44FBE4
4 changed files with 72 additions and 2 deletions

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
import {describe, expect, test} from '@jest/globals'; import {describe, expect, it, test} from '@jest/globals';
import {Docker} from '../../src/docker/docker'; import {Docker} from '../../src/docker/docker';
@ -54,3 +54,14 @@ maybe('pull', () => {
} }
}, 600000); }, 600000);
}); });
maybe('contextInspect', () => {
it('inspect default context', async () => {
const contextInfo = await Docker.contextInspect();
expect(contextInfo).toBeDefined();
console.log('contextInfo', contextInfo);
expect(contextInfo?.Name).toBeDefined();
expect(contextInfo?.Endpoints).toBeDefined();
expect(Object.keys(contextInfo?.Endpoints).length).toBeGreaterThan(0);
});
});

View File

@ -118,6 +118,19 @@ describe('context', () => {
}); });
}); });
describe('contextInspect', () => {
it('call docker context inspect', async () => {
const execSpy = jest.spyOn(Exec, 'getExecOutput');
await Docker.contextInspect('foo').catch(() => {
// noop
});
expect(execSpy).toHaveBeenCalledWith(`docker`, ['context', 'inspect', '--format=json', 'foo'], {
ignoreReturnCode: true,
silent: true
});
});
});
describe('printVersion', () => { describe('printVersion', () => {
it('call docker version', async () => { it('call docker version', async () => {
const execSpy = jest.spyOn(Exec, 'exec'); const execSpy = jest.spyOn(Exec, 'exec');

View File

@ -25,7 +25,7 @@ import {Cache} from '../cache';
import {Exec} from '../exec'; import {Exec} from '../exec';
import {Util} from '../util'; import {Util} from '../util';
import {ConfigFile} from '../types/docker/docker'; import {ConfigFile, ContextInfo} from '../types/docker/docker';
export class Docker { export class Docker {
static get configDir(): string { static get configDir(): string {
@ -69,6 +69,22 @@ export class Docker {
}); });
} }
public static async contextInspect(name?: string): Promise<ContextInfo> {
const args = ['context', 'inspect', '--format=json'];
if (name) {
args.push(name);
}
return await Exec.getExecOutput(`docker`, args, {
ignoreReturnCode: true,
silent: true
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
return (<Array<ContextInfo>>JSON.parse(res.stdout.trim()))[0];
});
}
public static async printVersion(): Promise<void> { public static async printVersion(): Promise<void> {
await Exec.exec('docker', ['version']); await Exec.exec('docker', ['version']);
} }

View File

@ -64,3 +64,33 @@ export interface AuthConfig {
identitytoken?: string; identitytoken?: string;
registrytoken?: string; registrytoken?: string;
} }
export interface ContextInfo {
Name: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Metadata: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Endpoints: Record<string, EndpointInfo>;
TLSMaterial: Record<string, Array<string>>;
Storage: StorageInfo;
}
export interface EndpointInfo {
Host?: string;
SkipVerify: boolean;
TLSData?: TLSData;
}
export interface TLSData {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
CA: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Key: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Cert: any;
}
export interface StorageInfo {
MetadataPath: string;
TLSPath: string;
}