Skip to content

Commit

Permalink
fix: fix lint, format and add tests
Browse files Browse the repository at this point in the history
Signed-off-by: lstocchi <[email protected]>
  • Loading branch information
lstocchi committed Feb 26, 2024
1 parent de9085b commit a0af678
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 47 deletions.
24 changes: 10 additions & 14 deletions packages/backend/src/managers/playground.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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',
Expand Down
9 changes: 1 addition & 8 deletions packages/backend/src/managers/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
12 changes: 5 additions & 7 deletions packages/backend/src/managers/podmanConnection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
3 changes: 1 addition & 2 deletions packages/backend/src/managers/podmanConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions packages/backend/src/models/WSLUploader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
Expand Down
17 changes: 15 additions & 2 deletions packages/backend/src/models/WSLUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
90 changes: 90 additions & 0 deletions packages/backend/src/utils/podman.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { beforeEach } from 'node:test';
const mocks = vi.hoisted(() => {
return {
getConfigurationMock: vi.fn(),
execMock: vi.fn(),
getContainerConnectionsMock: vi.fn(),
};
});

Expand All @@ -40,6 +42,12 @@ vi.mock('@podman-desktop/api', () => ({
configuration: {
getConfiguration: () => config,
},
process: {
exec: mocks.execMock,
},
provider: {
getContainerConnections: mocks.getContainerConnectionsMock,
},
}));

beforeEach(() => {
Expand All @@ -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');
});
});
23 changes: 13 additions & 10 deletions packages/backend/src/utils/podman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,13 +59,15 @@ export async function getFirstRunningPodmanConnection(): Promise<ProviderContain
const machineListOutput = await getJSONMachineList();
const machines = JSON.parse(machineListOutput) as MachineJSON[];
const machine = machines.find(machine => 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;
}
}

0 comments on commit a0af678

Please sign in to comment.