Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions src/cli/install-tool/install-tool.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
import { initializeTools, prepareTools } from '../prepare-tool/index.ts';
import {
BuildListService,
CacheService,
EnvService,
LinkToolService,
Expand Down Expand Up @@ -219,6 +220,22 @@ class StuckInstallService extends TestInstallService {
override readonly parent = 'root-only';
}

/** a npm package, which is listed with its type */
@injectable()
@injectFromHierarchy()
class NpmTypedInstallService extends TestInstallService {
override readonly name = 'npm-typed';
override readonly type = 'npm';
}

/** a dotnet tool, which is listed as a plain tool */
@injectable()
@injectFromHierarchy()
class DotnetTypedInstallService extends TestInstallService {
override readonly name = 'dotnet-typed';
override readonly type = 'dotnet';
}

/** The pointer of a command. */
function linkPath(name: string): string {
return rootPath(`opt/containerbase/links/${name}`);
Expand All @@ -242,6 +259,8 @@ describe('cli/install-tool/install-tool.service', () => {
parent.bind(INSTALL_TOOL_TOKEN).to(ChildInstallService);
parent.bind(INSTALL_TOOL_TOKEN).to(OrphanInstallService);
parent.bind(INSTALL_TOOL_TOKEN).to(StuckInstallService);
parent.bind(INSTALL_TOOL_TOKEN).to(NpmTypedInstallService);
parent.bind(INSTALL_TOOL_TOKEN).to(DotnetTypedInstallService);

let child: Container;
let install: InstallToolService;
Expand Down Expand Up @@ -825,6 +844,109 @@ describe('cli/install-tool/install-tool.service', () => {
});
});

describe('build-time list', () => {
/** Makes the install a root install of an image build. */
function buildTime(): void {
vi.mocked(isDockerBuild).mockResolvedValue(true);
vi.spyOn(EnvService.prototype, 'isRoot', 'get').mockReturnValue(true);
}

test('records the installs of an image build', async () => {
buildTime();
const record = vi
.spyOn(BuildListService.prototype, 'record')
.mockResolvedValue();

expect(await install.install('multi', '30.0.0')).toBeUndefined();
expect(await install.install('child', '6.0.0')).toBeUndefined();

expect(record).toHaveBeenNthCalledWith(
1,
{ type: 'tool', tool: 'multi', version: '30.0.0' },
['multi', 'multi30'],
'multi',
);
expect(record).toHaveBeenNthCalledWith(
2,
{
type: 'tool',
tool: 'child',
version: '6.0.0',
parent: { name: 'multi', version: '30.0.0' },
},
['child'],
'child',
);
});

test('records the type of a package', async () => {
buildTime();
const record = vi
.spyOn(BuildListService.prototype, 'record')
.mockResolvedValue();

expect(await install.install('npm-typed', '1.0.0')).toBeUndefined();
expect(await install.install('dotnet-typed', '1.0.0')).toBeUndefined();

expect(record).toHaveBeenNthCalledWith(
1,
{ type: 'npm', tool: 'npm-typed', version: '1.0.0' },
[],
'npm-typed',
);
expect(record).toHaveBeenNthCalledWith(
2,
{ type: 'tool', tool: 'dotnet-typed', version: '1.0.0' },
[],
'dotnet-typed',
);
});

test('does not record the v2 shell tools', async () => {
buildTime();
const record = vi.spyOn(BuildListService.prototype, 'record');

expect(await install.install('shell-tool', '3.0.0')).toBeUndefined();

expect(record).not.toHaveBeenCalled();
});

test('does not record without a root install of an image build', async () => {
const record = vi.spyOn(BuildListService.prototype, 'record');

// not root
vi.mocked(isDockerBuild).mockResolvedValue(true);
expect(await install.install('multi', '31.0.0')).toBeUndefined();
// not an image build
vi.mocked(isDockerBuild).mockResolvedValue(false);
vi.spyOn(EnvService.prototype, 'isRoot', 'get').mockReturnValue(true);
expect(await install.install('multi', '31.1.0')).toBeUndefined();

expect(record).not.toHaveBeenCalled();
});

test('uninstall removes the version from the list', async () => {
expect(await install.install('multi', '32.0.0')).toBeUndefined();
buildTime();
const remove = vi
.spyOn(BuildListService.prototype, 'remove')
.mockResolvedValue();

expect(await install.uninstall('multi', '32.0.0')).toBeUndefined();

expect(remove).toHaveBeenCalledExactlyOnceWith('multi', '32.0.0');
});

test('uninstall keeps the list without a root build', async () => {
expect(await install.install('multi', '33.0.0')).toBeUndefined();
const remove = vi.spyOn(BuildListService.prototype, 'remove');

expect(await install.uninstall('multi', '33.0.0')).toBeUndefined();

expect(remove).not.toHaveBeenCalled();
});
});

describe('links', () => {
test('a command keeps the version which linked it last', async () => {
const pathSvc = await child.getAsync(PathService);
Expand Down
49 changes: 49 additions & 0 deletions src/cli/install-tool/install-tool.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { deleteAsync } from 'del';
import { inject, injectable, multiInject, optional } from 'inversify';
import { initializeTools, prepareTools } from '../prepare-tool/index.ts';
import {
BuildListService,
CacheService,
EnvService,
IpcServer,
Expand Down Expand Up @@ -43,6 +44,8 @@ export class InstallToolService {
@multiInject(INSTALL_TOOL_TOKEN)
@optional()
private readonly toolSvcs: BaseInstallService[] = [];
@inject(BuildListService)
private readonly _list!: BuildListService;
@inject(CacheService)
private readonly cacheSvc!: CacheService;
@inject(EnvService)
Expand Down Expand Up @@ -396,6 +399,10 @@ export class InstallToolService {
}

await this.versionSvc.removeLinks(vTool);

if (await this.isBuildTime()) {
await this._list.remove(tool, version);
}
}
} else {
logger.fatal({ tool, version }, 'legacy tools cannot be uninstalled');
Expand Down Expand Up @@ -602,11 +609,53 @@ export class InstallToolService {
if (!this.envSvc.skipTests) {
await toolSvc.test(version);
}

await this.recordBuildTimeInstall(toolSvc, version, parent);
} finally {
this._link.resetContext();
}
}

/**
* Whether the install runs as root during an image build, so it is part of
* the image, see `BuildListService`.
*/
private async isBuildTime(): Promise<boolean> {
return this.envSvc.isRoot && (await isDockerBuild());
}

/**
* Records an install of an image build in the build-time list, with the
* commands the version linked, as the current version of the tool. The v2
* shell tools can't be installed again by the shims, so they are not listed.
*/
private async recordBuildTimeInstall(
toolSvc: BaseInstallService,
version: string,
parent: ToolState | null,
): Promise<void> {
if (
toolSvc instanceof V2ToolInstallService ||
!(await this.isBuildTime())
) {
return;
}
const links = await this.versionSvc.findLinks({
name: toolSvc.name,
version,
});
await this._list.record(
{
type: toolSvc.type && toolSvc.type !== 'dotnet' ? toolSvc.type : 'tool',
tool: toolSvc.name,
version,
...(parent && { parent: parent.tool }),
},
links.map(({ name }) => name).toSorted(),
toolSvc.alias,
);
}

/**
* Records which shell wrappers the last link step created, so they can be
* removed again when the version is uninstalled, and points the commands at
Expand Down
Loading
Loading