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
14 changes: 14 additions & 0 deletions src/cli/command/init-tool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { logger } from '../utils/index.ts';
import { registerCommands } from './index.ts';

const mocks = vi.hoisted(() => ({
setupVolume: vi.fn(),
installTool: vi.fn(),
prepareTools: vi.fn(),
initializeTools: vi.fn(),
Expand All @@ -23,6 +24,19 @@ describe('cli/command/init-tool', () => {
false,
);

// a single tool and a dry run leave the volume alone
expect(mocks.setupVolume).not.toHaveBeenCalled();
expect(await cli.run(['init', 'tool', 'all', '-d'])).toBe(0);
expect(mocks.setupVolume).not.toHaveBeenCalled();

// `all` sets up the volume first
mocks.initializeTools.mockClear();
expect(await cli.run(['init', 'tool', 'all'])).toBe(0);
expect(mocks.setupVolume).toHaveBeenCalledOnce();
expect(mocks.setupVolume.mock.invocationCallOrder[0]).toBeLessThan(
mocks.initializeTools.mock.invocationCallOrder[0]!,
);

mocks.initializeTools.mockRejectedValueOnce(new Error('test'));
expect(await cli.run(['init', 'tool', 'node'])).toBe(1);

Expand Down
10 changes: 9 additions & 1 deletion src/cli/command/init-tool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command, Option } from 'clipanion';
import prettyMilliseconds from 'pretty-ms';
import { setupVolume } from '../install-tool/index.ts';
import { initializeTools } from '../prepare-tool/index.ts';
import { logger } from '../utils/index.ts';
import { command } from './utils.ts';
Expand All @@ -21,12 +22,19 @@ export class InitToolCommand extends Command {

dryRun = Option.Boolean('-d,--dry-run', false);

/** Initializes the passed tools, or all prepared tools for `all`. */
/**
* Initializes the passed tools, or all prepared tools for `all`. For `all`
* it first sets up an empty or older `/opt/containerbase` from the
* build-time list of the image.
*/
async execute(): Promise<number | void> {
const start = Date.now();
let error = false;
logger.info(`Initializing tools ${this.tools.join(', ')}...`);
try {
if (!this.dryRun && this.tools.length === 1 && this.tools[0] === 'all') {
await setupVolume();
}
return await initializeTools(this.tools, this.dryRun);
} catch (err) {
error = true;
Expand Down
62 changes: 62 additions & 0 deletions src/cli/install-tool/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
vi,
} from 'vitest';
import {
BuildListService,
DataService,
IpcServer,
PathService,
ToolVersionsService,
Expand Down Expand Up @@ -38,9 +40,11 @@ import {
linkTool,
listSupportedTools,
resolveVersion,
setupVolume,
uninstallTool,
} from './index.ts';
import { V2ToolInstallService } from './install-legacy-tool.service.ts';
import { InstallToolService } from './install-tool.service.ts';
import { scope } from '~test/http-mock.ts';
import { ensurePaths, rootPath } from '~test/path.ts';

Expand Down Expand Up @@ -68,6 +72,11 @@ describe('cli/install-tool/index', () => {
'opt/containerbase/bin',
'opt/containerbase/data',
'opt/containerbase/links',
// the files of the recorded parent versions, so they aren't installed again
'opt/containerbase/tools/node/1.0.0',
'opt/containerbase/tools/node/1.0.1',
'opt/containerbase/tools/python/1.0.0',
'opt/containerbase/tools/ruby/1.0.0',
'tmp/containerbase/tool.init.d',
'usr/local/containerbase/tools/v2',
'var/lib/containerbase/tool.prep.d',
Expand Down Expand Up @@ -732,6 +741,59 @@ describe('cli/install-tool/index', () => {
});
});

describe('setupVolume', () => {
test('does nothing when the volume has the list of the image', async () => {
vi.spyOn(BuildListService.prototype, 'changed').mockResolvedValue(false);
const spy = vi.spyOn(InstallToolService.prototype, 'setupVolume');

await expect(setupVolume()).resolves.toBeUndefined();

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

test('sets up the volume from a different list', async () => {
vi.spyOn(BuildListService.prototype, 'changed').mockResolvedValue(true);
const spy = vi
.spyOn(InstallToolService.prototype, 'setupVolume')
.mockResolvedValue();

await expect(setupVolume()).resolves.toBeUndefined();

expect(spy).toHaveBeenCalledOnce();
});

test('creates the base folders before the databases load', async () => {
vi.spyOn(BuildListService.prototype, 'changed').mockResolvedValue(true);
const ensure = vi.spyOn(PathService.prototype, 'ensureBasePaths');
const setup = vi
.spyOn(InstallToolService.prototype, 'setupVolume')
.mockResolvedValue();
const load = vi.spyOn(DataService.prototype, 'load');

await expect(setupVolume()).resolves.toBeUndefined();

expect(ensure.mock.invocationCallOrder[0]).toBeLessThan(
load.mock.invocationCallOrder[0]!,
);
expect(setup).toHaveBeenCalledOnce();
});

test('only warns when the setup fails', async () => {
const err = new Error('failed');
vi.spyOn(BuildListService.prototype, 'changed').mockResolvedValue(true);
vi.spyOn(InstallToolService.prototype, 'setupVolume').mockRejectedValue(
err,
);

await expect(setupVolume()).resolves.toBeUndefined();

expect(logger.warn).toHaveBeenCalledWith(
{ err },
'cannot set up the volume',
);
});
});

describe('ensureTool', () => {
test('fails first when the folders are not writable', async () => {
const pathSvc = await createContainer().getAsync(PathService);
Expand Down
25 changes: 25 additions & 0 deletions src/cli/install-tool/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Container, injectFromHierarchy, injectable } from 'inversify';
import type { ShellWrapperConfig } from '../services/index.ts';
import {
BuildListService,
DataService,
IpcClient,
LinkToolService,
Expand Down Expand Up @@ -500,6 +501,30 @@ export async function uninstallTool({
return svc.uninstall(tool, version, dryRun, recursive);
}

/**
* Sets up `/opt/containerbase` from the build-time list of the image, when it
* differs from the copy on the volume, see `InstallToolService.setupVolume`.
* Without a difference, eg. in an image without a volume, it only reads two
* small files. A failure is only logged, as the shims install what is missing.
*/
export async function setupVolume(): Promise<void> {
try {
const base = createContainer();
const list = await base.getAsync(BuildListService);
if (!(await list.changed())) {
return;
}
logger.debug('setting up the volume from the build-time list');
// before the databases load, which would create `data` with the owner and
// mode of the running process
await (await base.getAsync(PathService)).ensureBasePaths();
const container = await prepareInstallContainer();
await (await container.getAsync(InstallToolService)).setupVolume();
} catch (err) {
logger.warn({ err }, 'cannot set up the volume');
}
}

/**
* Makes a command of an installed tool run again, see
* `InstallToolService.ensure`. Generic install services are registered for
Expand Down
Loading
Loading