Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use minikube cli to start/stop cluster #50

Merged
merged 2 commits into from
Jul 2, 2024
Merged
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
58 changes: 58 additions & 0 deletions src/extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ vi.mock('@podman-desktop/api', async () => {
containerEngine: {
listContainers: vi.fn(),
},

process: {
exec: vi.fn(),
env: {},
},

env: {
isMac: false,
},
};
});

Expand All @@ -57,3 +66,52 @@ test('check we received notifications ', async () => {
expect(callbackCalled).toBeTruthy();
expect(listContainersMock).toBeCalledTimes(1);
});

test('verify that the minikube cli is used to start/stop the minikube container', async () => {
const onDidUpdateContainerConnectionMock = vi.fn();
(podmanDesktopApi.provider as any).onDidUpdateContainerConnection = onDidUpdateContainerConnectionMock;

const listContainersMock = vi.fn();
(podmanDesktopApi.containerEngine as any).listContainers = listContainersMock;
listContainersMock.mockResolvedValue([
{
Labels: {
'name.minikube.sigs.k8s.io': 'minikube',
},
State: 'stopped',
Ports: [],
engineType: 'podman',
engineId: 'engine',
Id: '1',
} as unknown as podmanDesktopApi.ContainerInfo,
]);

onDidUpdateContainerConnectionMock.mockImplementation((callback: any) => {
callback();
});

const connections: podmanDesktopApi.KubernetesProviderConnection[] = [];
const fakeProvider = {
registerKubernetesProviderConnection: vi
.fn()
.mockImplementation((connection: podmanDesktopApi.KubernetesProviderConnection) => {
connections.push(connection);
}),
} as unknown as podmanDesktopApi.Provider;
const mockExec = vi.spyOn(podmanDesktopApi.process, 'exec');
refreshMinikubeClustersOnProviderConnectionUpdate(fakeProvider);

await vi.waitUntil(() => connections.length > 0, { timeout: 5000 });

await connections[0].lifecycle.start?.({} as unknown as podmanDesktopApi.LifecycleContext);

expect(mockExec).toBeCalledWith(undefined, ['start', '--profile', 'minikube'], expect.any(Object));

await connections[0].lifecycle.stop?.({} as unknown as podmanDesktopApi.LifecycleContext);

expect(mockExec).toBeCalledWith(
undefined,
['stop', '--profile', 'minikube', '--keep-context-active'],
expect.any(Object),
);
});
11 changes: 8 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,21 @@ async function updateClusters(provider: extensionApi.Provider, containers: exten
const lifecycle: extensionApi.ProviderConnectionLifecycle = {
start: async (): Promise<void> => {
try {
// start the container
await extensionApi.containerEngine.startContainer(cluster.engineId, cluster.id);
const env = Object.assign({}, process.env);
env.PATH = getMinikubePath();
await extensionApi.process.exec(minikubeCli, ['start', '--profile', cluster.name], { env });
} catch (err) {
console.error(err);
// propagate the error
throw err;
}
},
stop: async (): Promise<void> => {
await extensionApi.containerEngine.stopContainer(cluster.engineId, cluster.id);
const env = Object.assign({}, process.env);
env.PATH = getMinikubePath();
await extensionApi.process.exec(minikubeCli, ['stop', '--profile', cluster.name, '--keep-context-active'], {
env,
});
},
delete: async (logger): Promise<void> => {
const env = Object.assign({}, process.env);
Expand Down
Loading