diff --git a/packages/backend/src/managers/playground.spec.ts b/packages/backend/src/managers/playground.spec.ts index 03afa2c3b..cb865d663 100644 --- a/packages/backend/src/managers/playground.spec.ts +++ b/packages/backend/src/managers/playground.spec.ts @@ -106,14 +106,12 @@ test('startPlayground should fail if no provider', async () => { test('startPlayground should download image if not present then create container', async () => { mocks.postMessage.mockResolvedValue(undefined); - mocks.getFirstRunningPodmanConnectionMock.mockResolvedValue( - { - connection: { - type: 'podman', - status: () => 'started', - }, + mocks.getFirstRunningPodmanConnectionMock.mockResolvedValue({ + connection: { + type: 'podman', + status: () => 'started', }, - ); + }); vi.spyOn(manager, 'selectImage') .mockResolvedValueOnce(undefined) .mockResolvedValueOnce({ @@ -164,14 +162,12 @@ test('stopPlayground should fail if no playground is running', async () => { test('stopPlayground should stop a started playground', async () => { mocks.postMessage.mockResolvedValue(undefined); - mocks.getFirstRunningPodmanConnectionMock.mockResolvedValue( - { - connection: { - type: 'podman', - status: () => 'started', - }, + mocks.getFirstRunningPodmanConnectionMock.mockResolvedValue({ + connection: { + type: 'podman', + status: () => 'started', }, - ); + }); vi.spyOn(manager, 'selectImage').mockResolvedValue({ Id: 'image1', engineId: 'engine1', diff --git a/packages/backend/src/managers/playground.ts b/packages/backend/src/managers/playground.ts index 8e5a22cea..448efd866 100644 --- a/packages/backend/src/managers/playground.ts +++ b/packages/backend/src/managers/playground.ts @@ -16,14 +16,7 @@ * SPDX-License-Identifier: Apache-2.0 ***********************************************************************/ -import { - containerEngine, - type Webview, - type ImageInfo, - type ProviderContainerConnection, - provider, - type TelemetryLogger, -} from '@podman-desktop/api'; +import { containerEngine, type Webview, type ImageInfo, type TelemetryLogger } from '@podman-desktop/api'; import path from 'node:path'; import { getFreePort } from '../utils/ports'; diff --git a/packages/backend/src/managers/podmanConnection.spec.ts b/packages/backend/src/managers/podmanConnection.spec.ts index bd19bd6cc..16d402bd7 100644 --- a/packages/backend/src/managers/podmanConnection.spec.ts +++ b/packages/backend/src/managers/podmanConnection.spec.ts @@ -44,14 +44,12 @@ vi.mock('../utils/podman', () => { test('startupSubscribe should execute immediately if provider already registered', async () => { const manager = new PodmanConnection(); // one provider is already registered - mocks.getFirstRunningPodmanConnectionMock.mockResolvedValue( - { - connection: { - type: 'podman', - status: () => 'started', - }, + mocks.getFirstRunningPodmanConnectionMock.mockResolvedValue({ + connection: { + type: 'podman', + status: () => 'started', }, - ); + }); mocks.onDidRegisterContainerConnection.mockReturnValue({ dispose: vi.fn, }); diff --git a/packages/backend/src/managers/podmanConnection.ts b/packages/backend/src/managers/podmanConnection.ts index 647d241a9..36f6dbefa 100644 --- a/packages/backend/src/managers/podmanConnection.ts +++ b/packages/backend/src/managers/podmanConnection.ts @@ -45,7 +45,7 @@ export class PodmanConnection implements Disposable { #onEventDisposable: Disposable | undefined; init(): void { - this.listenRegistration(); + this.listenRegistration().catch((e: unknown) => console.error(String(e))); this.listenMachine(); this.watchPods(); } @@ -78,7 +78,6 @@ export class PodmanConnection implements Disposable { disposable.dispose(); this.#firstFound = true; } - } // startupSubscribe registers f to be executed when a podman container provider diff --git a/packages/backend/src/models/WSLUploader.spec.ts b/packages/backend/src/models/WSLUploader.spec.ts index 465608b98..4f0d1a74f 100644 --- a/packages/backend/src/models/WSLUploader.spec.ts +++ b/packages/backend/src/models/WSLUploader.spec.ts @@ -65,14 +65,15 @@ describe('upload', () => { endpoint: { socketPath: '/endpoint.sock', }, - type: 'podman' + type: 'podman', }, - providerId: 'podman' + providerId: 'podman', }); test('throw if localpath is not defined', async () => { - await expect(wslUploader.upload('')).rejects.toThrowError('invalid local path'); }); + await expect(wslUploader.upload('')).rejects.toThrowError('invalid local path'); + }); test('copy model if not exists on podman machine', async () => { - mocks.execMock.mockRejectedValueOnce(''); + mocks.execMock.mockRejectedValueOnce(''); await wslUploader.upload('C:\\Users\\podman\\folder\\file'); expect(mocks.execMock).toBeCalledWith('podman', ['machine', 'ssh', 'test', 'stat', '/home/user/file']); }); diff --git a/packages/backend/src/models/WSLUploader.ts b/packages/backend/src/models/WSLUploader.ts index d1e9a41ca..f03845d3f 100644 --- a/packages/backend/src/models/WSLUploader.ts +++ b/packages/backend/src/models/WSLUploader.ts @@ -40,14 +40,27 @@ export class WSLUploader implements UploadWorker { // check if model already loaded on the podman machine let existsRemote = true; try { - await podmanDesktopApi.process.exec(getPodmanCli(), ['machine', 'ssh', connection.connection.name, 'stat', remotePath]); + await podmanDesktopApi.process.exec(getPodmanCli(), [ + 'machine', + 'ssh', + connection.connection.name, + 'stat', + remotePath, + ]); } catch (e) { existsRemote = false; } // if not exists remotely it copies it from the local path if (!existsRemote) { - await podmanDesktopApi.process.exec(getPodmanCli(), ['machine', 'ssh', connection.connection.name, 'cp', convertToMntPath, remotePath]); + await podmanDesktopApi.process.exec(getPodmanCli(), [ + 'machine', + 'ssh', + connection.connection.name, + 'cp', + convertToMntPath, + remotePath, + ]); } return remotePath; diff --git a/packages/backend/src/utils/podman.spec.ts b/packages/backend/src/utils/podman.spec.ts index f636d0540..0b856fa78 100644 --- a/packages/backend/src/utils/podman.spec.ts +++ b/packages/backend/src/utils/podman.spec.ts @@ -24,6 +24,8 @@ import { beforeEach } from 'node:test'; const mocks = vi.hoisted(() => { return { getConfigurationMock: vi.fn(), + execMock: vi.fn(), + getContainerConnectionsMock: vi.fn(), }; }); @@ -40,6 +42,12 @@ vi.mock('@podman-desktop/api', () => ({ configuration: { getConfiguration: () => config, }, + process: { + exec: mocks.execMock, + }, + provider: { + getContainerConnections: mocks.getContainerConnectionsMock, + }, })); beforeEach(() => { @@ -65,3 +73,85 @@ describe('getPodmanCli', () => { expect(result).equals('podman'); }); }); + +describe('getFirstRunningPodmanConnection', () => { + test('should return undefined if failing at retrieving machine list', async () => { + mocks.execMock.mockRejectedValue('error'); + const result = await utils.getFirstRunningPodmanConnection(); + expect(result).toBeUndefined(); + }); + test('should return undefined if default podman machine is not running', async () => { + const machine = { + Name: 'machine', + CPUs: 2, + Memory: 2000, + DiskSize: '100', + Running: false, + Starting: false, + Default: true, + }; + const machine2 = { + Name: 'machine2', + CPUs: 2, + Memory: 2000, + DiskSize: '100', + Running: true, + Starting: false, + Default: false, + }; + mocks.execMock.mockResolvedValue({ + stdout: JSON.stringify([machine2, machine]), + }); + const result = await utils.getFirstRunningPodmanConnection(); + expect(result).toBeUndefined(); + }); + test('should return default running podman connection', async () => { + const machine = { + Name: 'machine', + CPUs: 2, + Memory: 2000, + DiskSize: '100', + Running: false, + Starting: false, + Default: false, + }; + const machine2 = { + Name: 'machine2', + CPUs: 2, + Memory: 2000, + DiskSize: '100', + Running: true, + Starting: false, + Default: true, + }; + mocks.execMock.mockResolvedValue({ + stdout: JSON.stringify([machine, machine2]), + }); + mocks.getContainerConnectionsMock.mockReturnValue([ + { + connection: { + name: 'machine', + status: vi.fn(), + endpoint: { + socketPath: '/endpoint.sock', + }, + type: 'podman', + }, + providerId: 'podman', + }, + { + connection: { + name: 'machine2', + status: vi.fn(), + endpoint: { + socketPath: '/endpoint.sock', + }, + type: 'podman', + }, + providerId: 'podman2', + }, + ]); + const result = await utils.getFirstRunningPodmanConnection(); + expect(result.connection.name).equal('machine2'); + }); +}); diff --git a/packages/backend/src/utils/podman.ts b/packages/backend/src/utils/podman.ts index b0e28ff19..b874a319b 100644 --- a/packages/backend/src/utils/podman.ts +++ b/packages/backend/src/utils/podman.ts @@ -15,7 +15,8 @@ * * SPDX-License-Identifier: Apache-2.0 ***********************************************************************/ -import { ProviderContainerConnection, configuration, env, process, provider } from '@podman-desktop/api'; +import type { ProviderContainerConnection } from '@podman-desktop/api'; +import { configuration, env, process, provider } from '@podman-desktop/api'; export type MachineJSON = { Name: string; @@ -58,13 +59,15 @@ export async function getFirstRunningPodmanConnection(): Promise machine.Default && machine.Running); - engine = provider - .getContainerConnections() - .filter(connection => connection.connection.type === 'podman') - .find(connection => connection.connection.name === machine.Name); - } catch(e) { - console.log(e) - } - + if (machine) { + engine = provider + .getContainerConnections() + .filter(connection => connection.connection.type === 'podman') + .find(connection => connection.connection.name === machine.Name); + } + } catch (e) { + console.log(e); + } + return engine; -} \ No newline at end of file +}