Skip to content

Commit

Permalink
feat: expose listPods to extensions (podman-desktop#5864)
Browse files Browse the repository at this point in the history
* feat: expose listPods to extensions
Signed-off-by: Philippe Martin <[email protected]>

* test: add unit test
Signed-off-by: Philippe Martin <[email protected]>
  • Loading branch information
feloy authored Feb 6, 2024
1 parent 0134a1d commit 110da9a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/extension-api/src/extension-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,28 @@ declare module '@podman-desktop/api' {
MacAddress: string;
}

interface PodContainerInfo {
Id: string;
Names: string;
Status: string;
}

interface PodInfo {
engineId: string;
engineName: string;
kind: 'kubernetes' | 'podman';
Cgroup: string;
Containers: PodContainerInfo[];
Created: string;
Id: string;
InfraId: string;
Labels: { [key: string]: string };
Name: string;
Namespace: string;
Networks: string[];
Status: string;
}

interface AuthConfig {
username: string;
password: string;
Expand Down Expand Up @@ -2388,6 +2410,7 @@ declare module '@podman-desktop/api' {
overrideParameters: PodmanContainerCreateOptions,
): Promise<{ Id: string; Warnings: string[] }>;
export function startPod(engineId: string, podId: string): Promise<void>;
export function listPods(): Promise<PodInfo[]>;
}

/**
Expand Down
40 changes: 40 additions & 0 deletions packages/main/src/plugin/container-registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ const apiSender: ApiSenderType = {
};

beforeEach(() => {
nock.cleanAll();
vi.mocked(apiSender.receive).mockClear();
vi.mocked(apiSender.send).mockClear();

Expand Down Expand Up @@ -3058,3 +3059,42 @@ test('check that fails if selected provider is not a podman one', async () => {
}),
).rejects.toThrowError('No podman provider with a running engine');
});

test('list pods', async () => {
const podsList = [
{
Labels: {
key1: 'value1',
key2: 'value2',
},
},
];

nock('http://localhost').get('/v4.2.0/libpod/pods/json').reply(200, podsList);

const api = new Dockerode({ protocol: 'http', host: 'localhost' });

// set provider
containerRegistry.addInternalProvider('podman', {
name: 'podman',
id: 'podman1',
api,
libpodApi: api,
connection: {
type: 'podman',
},
} as unknown as InternalContainerProvider);

const pods = await containerRegistry.listPods();
// ensure the field are correct
expect(pods).toBeDefined();
expect(pods).toHaveLength(1);
const pod = pods[0];
expect(pod.engineId).toBe('podman1');
expect(pod.engineName).toBe('podman');
expect(pod.kind).toBe('podman');
expect(pod.Labels).toStrictEqual({
key1: 'value1',
key2: 'value2',
});
});
13 changes: 13 additions & 0 deletions packages/main/src/plugin/extension-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const containerProviderRegistry: ContainerProviderRegistry = {
imageExist: vi.fn(),
volumeExist: vi.fn(),
podExist: vi.fn(),
listPods: vi.fn(),
} as unknown as ContainerProviderRegistry;

const inputQuickPickRegistry: InputQuickPickRegistry = {} as unknown as InputQuickPickRegistry;
Expand Down Expand Up @@ -1332,3 +1333,15 @@ test('check version', async () => {
// check we called method
expect(readPodmanVersion).toBe(fakeVersion);
});

test('listPods', async () => {
const listPodsSpy = vi.spyOn(containerProviderRegistry, 'listPods');
const api = extensionLoader.createApi('path', {
name: 'name',
publisher: 'publisher',
version: '1',
displayName: 'dname',
});
await api.containerEngine.listPods();
expect(listPodsSpy).toHaveBeenCalledOnce();
});
4 changes: 4 additions & 0 deletions packages/main/src/plugin/extension-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import type { ImageCheckerImpl } from './image-checker.js';
import type { NavigationManager } from '/@/plugin/navigation/navigation-manager.js';
import type { WebviewRegistry } from '/@/plugin/webview/webview-registry.js';
import type { ImageInspectInfo } from '/@/plugin/api/image-inspect-info.js';
import type { PodInfo } from './api/pod-info.js';

/**
* Handle the loading of an extension
Expand Down Expand Up @@ -1000,6 +1001,9 @@ export class ExtensionLoader {
createPod(podOptions: containerDesktopAPI.PodCreateOptions): Promise<{ engineId: string; Id: string }> {
return containerProviderRegistry.createPod(podOptions);
},
listPods(): Promise<PodInfo[]> {
return containerProviderRegistry.listPods();
},
replicatePodmanContainer(
source: { engineId: string; id: string },
target: { engineId: string },
Expand Down

0 comments on commit 110da9a

Please sign in to comment.