chore: fix linux setup loop (#6262)

chore: fix linux setup loop

When using podman machine on Linux, it is OPTIONAL as Podman by default
is installed natively.

We were incorrectly showing the notification in an endless loop thinking
that Podman was setup (podman machine was missing).

This PR, in updateMachines function:
* Does not notify if podman machine errors out when retrieving the json
  (rare, but it happens)
* If zero podman machines are found, do not show the update
* Do not update the provider status to "installed" (would override the
  "ready" status before), since Linux supports podman natively.
This commit is contained in:
Charlie Drage 2024-03-06 03:55:22 -05:00 committed by GitHub
parent ec6e135f44
commit 287c15d429
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 54 additions and 17 deletions

View file

@ -1088,3 +1088,29 @@ test('checkDisguisedPodmanSocket: runs updateWarnings when called not on Linux',
await checkDisguisedPodmanSocket(provider);
expect(updateWarningsMock).toBeCalled();
});
test('Even with getJSONMachineList erroring, do not show setup notification on Linux', async () => {
vi.mocked(isLinux).mockReturnValue(true);
vi.spyOn(extensionApi.process, 'exec').mockRejectedValue({
name: 'name',
message: 'description',
stderr: 'error',
});
await expect(extension.updateMachines(provider)).rejects.toThrow('description');
expect(extensionApi.window.showNotification).not.toBeCalled();
});
test('If machine list is empty, do not show setup notification on Linux', async () => {
vi.mocked(isLinux).mockReturnValue(true);
const spyExecPromise = vi.spyOn(extensionApi.process, 'exec');
spyExecPromise.mockResolvedValue({ stdout: '[]' } as extensionApi.RunResult);
await extension.updateMachines(provider);
expect(extensionApi.window.showNotification).not.toBeCalled();
});
test('if there are no machines, make sure checkDefaultMachine is not being ran inside updateMachines', async () => {
const spyCheckDefaultMachine = vi.spyOn(extension, 'checkDefaultMachine');
vi.spyOn(extensionApi.process, 'exec').mockResolvedValue({ stdout: '[]' } as extensionApi.RunResult);
await extension.updateMachines(provider);
expect(spyCheckDefaultMachine).not.toBeCalled();
});

View file

@ -125,7 +125,9 @@ export async function updateMachines(provider: extensionApi.Provider): Promise<v
try {
machineListOutput = await getJSONMachineList();
} catch (error) {
if (shouldNotifySetup) {
// Only on macOS and Windows should we show the setup notification
// if for some reason doing getJSONMachineList fails..
if (shouldNotifySetup && !isLinux()) {
// push setup notification
notificationDisposable = extensionApi.window.showNotification(setupPodmanNotification);
shouldNotifySetup = false;
@ -136,7 +138,10 @@ export async function updateMachines(provider: extensionApi.Provider): Promise<v
// parse output
const machines = JSON.parse(machineListOutput) as MachineJSON[];
extensionApi.context.setValue('podmanMachineExists', machines.length > 0, 'onboarding');
if (shouldNotifySetup && machines.length === 0) {
// Only show the notification on macOS and Windows
// as Podman is already installed on Linux and machine is OPTIONAL.
if (shouldNotifySetup && machines.length === 0 && !isLinux()) {
// push setup notification
notificationDisposable = extensionApi.window.showNotification(setupPodmanNotification);
shouldNotifySetup = false;
@ -260,29 +265,35 @@ export async function updateMachines(provider: extensionApi.Provider): Promise<v
}
});
// no machine, it's installed
if (machines.length === 0) {
// If the machine length is zero and we are on macOS or Windows,
// we will update the provider as being 'installed', or ready / starting / configured if there is a machine
// if we are on Linux, ignore this as podman machine is OPTIONAL and the provider status in Linux is based upon
// the native podman installation / not machine.
if (!isLinux() && machines.length === 0) {
if (provider.status !== 'configuring') {
provider.updateStatus('installed');
}
} else {
const atLeastOneMachineRunning = machines.some(machine => machine.Running);
const atLeastOneMachineStarting = machines.some(machine => machine.Starting);
// if a machine is running it's started else it is ready
if (atLeastOneMachineRunning) {
provider.updateStatus('ready');
} else if (atLeastOneMachineStarting) {
// update to starting
provider.updateStatus('starting');
} else {
// needs to start a machine
provider.updateStatus('configured');
const atLeastOneMachineRunning = machines.some(machine => machine.Running);
const atLeastOneMachineStarting = machines.some(machine => machine.Starting);
// if a machine is running it's started else it is ready
if (atLeastOneMachineRunning) {
provider.updateStatus('ready');
} else if (atLeastOneMachineStarting) {
// update to starting
provider.updateStatus('starting');
} else {
// needs to start a machine
provider.updateStatus('configured');
}
}
}
// Finally, we check to see if the machine that is running is set by default or not on the CLI
// this will create a dialog that will ask the user if they wish to set the running machine as default.
await checkDefaultMachine(machines);
// this should only run if we have multiple machines
if (machines.length > 1) {
await checkDefaultMachine(machines);
}
}
export async function checkDefaultMachine(machines: MachineJSON[]): Promise<void> {