Skip to content

Commit

Permalink
refactor(podman-extension): uses api env.isLinux (#10403)
Browse files Browse the repository at this point in the history
Signed-off-by: axel7083 <[email protected]>
  • Loading branch information
axel7083 authored Dec 17, 2024
1 parent 8c79939 commit 9bcbf15
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 43 deletions.
39 changes: 19 additions & 20 deletions extensions/podman/packages/extension/src/extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import * as podmanCli from './podman-cli';
import { PodmanConfiguration } from './podman-configuration';
import type { UpdateCheck } from './podman-install';
import { PodmanInstall } from './podman-install';
import { getAssetsFolder, isLinux, isMac, LIBKRUN_LABEL, LoggerDelegator, VMTYPE } from './util';
import { getAssetsFolder, isMac, LIBKRUN_LABEL, LoggerDelegator, VMTYPE } from './util';
import * as util from './util';

const config: Configuration = {
Expand Down Expand Up @@ -232,9 +232,9 @@ vi.mock('@podman-desktop/api', async () => {
},
env: {
createTelemetryLogger: vi.fn(),
isWindows: (): (() => boolean) => vi.fn(),
isMac: (): (() => boolean) => vi.fn(),
isLinux: (): (() => boolean) => vi.fn(),
isWindows: false,
isMac: false,
isLinux: false,
},
containerEngine: {
info: vi.fn(),
Expand Down Expand Up @@ -322,7 +322,6 @@ vi.mock('./util', async () => {
...actual,
isMac: vi.fn(),
isWindows: vi.fn(),
isLinux: vi.fn(),
getAssetsFolder: vi.fn(),
};
});
Expand All @@ -332,7 +331,7 @@ beforeEach(() => {
console.error = consoleErrorMock;

(extensionApi.env.isMac as boolean) = true;
(extensionApi.env.isLinux as boolean) = false;
vi.mocked(extensionApi.env).isLinux = false;
vi.mocked(extensionApi.env).isWindows = false;

const mock = vi.spyOn(compatibilityModeLib, 'getSocketCompatibility');
Expand Down Expand Up @@ -1231,7 +1230,7 @@ test('handlecompatibilitymodesetting: disable compatibility called when configur
});

test('ensure started machine reports default configuration', async () => {
vi.mocked(isLinux).mockReturnValue(true);
vi.mocked(extensionApi.env).isLinux = true;
extension.initExtensionContext({ subscriptions: [] } as unknown as extensionApi.ExtensionContext);
vi.spyOn(extensionApi.process, 'exec').mockImplementation(
(_command, args) =>
Expand Down Expand Up @@ -1261,7 +1260,7 @@ test('ensure started machine reports default configuration', async () => {

test('ensure stopped machine reports stopped provider', async () => {
extension.initExtensionContext({ subscriptions: [] } as unknown as extensionApi.ExtensionContext);
vi.mocked(isLinux).mockReturnValue(false);
vi.mocked(extensionApi.env).isLinux = false;
vi.mocked(isMac).mockReturnValue(true);
vi.spyOn(extensionApi.process, 'exec').mockImplementation(
(_command, args) =>
Expand Down Expand Up @@ -1291,7 +1290,7 @@ test('ensure stopped machine reports stopped provider', async () => {

test('ensure running and starting machine reports starting provider', async () => {
extension.initExtensionContext({ subscriptions: [] } as unknown as extensionApi.ExtensionContext);
vi.mocked(isLinux).mockReturnValue(false);
vi.mocked(extensionApi.env).isLinux = false;
vi.mocked(isMac).mockReturnValue(true);
vi.spyOn(extensionApi.process, 'exec').mockImplementation(
(_command, args) =>
Expand Down Expand Up @@ -1322,7 +1321,7 @@ test('ensure running and starting machine reports starting provider', async () =

test('ensure running and not starting machine reports ready provider', async () => {
extension.initExtensionContext({ subscriptions: [] } as unknown as extensionApi.ExtensionContext);
vi.mocked(isLinux).mockReturnValue(false);
vi.mocked(extensionApi.env).isLinux = false;
vi.mocked(isMac).mockReturnValue(true);
vi.spyOn(extensionApi.process, 'exec').mockImplementation(
(_command, args) =>
Expand Down Expand Up @@ -1390,7 +1389,7 @@ test('ensure started machine reports configuration', async () => {

test('ensure stopped machine reports configuration', async () => {
extension.initExtensionContext({ subscriptions: [] } as unknown as extensionApi.ExtensionContext);
vi.mocked(isLinux).mockReturnValue(true);
vi.mocked(extensionApi.env).isLinux = true;
vi.spyOn(extensionApi.process, 'exec').mockImplementation(
(_command, args) =>
new Promise<extensionApi.RunResult>(resolve => {
Expand Down Expand Up @@ -1540,7 +1539,7 @@ test('provider is registered without edit capabilities on Windows', async () =>
});

test('provider is registered without edit capabilities on Linux', async () => {
vi.mocked(isLinux).mockReturnValue(true);
vi.mocked(extensionApi.env).isLinux = true;
extension.initExtensionContext({ subscriptions: [] } as unknown as extensionApi.ExtensionContext);
const spyExecPromise = vi.spyOn(extensionApi.process, 'exec');
spyExecPromise.mockImplementation(() => {
Expand All @@ -1558,19 +1557,19 @@ test('provider is registered without edit capabilities on Linux', async () => {
});

test('checkDisguisedPodmanSocket: does not run updateWarnings when called with Linux', async () => {
vi.mocked(isLinux).mockReturnValue(true);
vi.mocked(extensionApi.env).isLinux = true;
await checkDisguisedPodmanSocket(provider);
expect(updateWarningsMock).not.toBeCalled();
});

test('checkDisguisedPodmanSocket: runs updateWarnings when called not on Linux', async () => {
vi.mocked(isLinux).mockReturnValue(false);
vi.mocked(extensionApi.env).isLinux = false;
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.mocked(extensionApi.env).isLinux = true;
vi.spyOn(extensionApi.process, 'exec').mockRejectedValue({
name: 'name',
message: 'description',
Expand All @@ -1581,7 +1580,7 @@ test('Even with getJSONMachineList erroring, do not show setup notification on L
});

test('If machine list is empty, do not show setup notification on Linux', async () => {
vi.mocked(isLinux).mockReturnValue(true);
vi.mocked(extensionApi.env).isLinux = true;
const spyExecPromise = vi.spyOn(extensionApi.process, 'exec');
spyExecPromise.mockResolvedValue({ stdout: '[]' } as extensionApi.RunResult);
await extension.updateMachines(provider, podmanConfiguration);
Expand Down Expand Up @@ -2471,7 +2470,7 @@ describe('sendTelemetryRecords', () => {
} as Record<string, unknown>,
false,
);
(extensionApi.env.isLinux as boolean) = true;
vi.mocked(extensionApi.env).isLinux = true;
(extensionApi.env.isMac as boolean) = false;
vi.mocked(extensionApi.env).isWindows = false;

Expand Down Expand Up @@ -2644,7 +2643,7 @@ async function testAudit(path: string, uri: string, condition: typeof expect | t
test('activate on mac register commands for setting compatibility moide ', async () => {
vi.mocked(isMac).mockReturnValue(true);
vi.mocked(extensionApi.env).isWindows = false;
vi.mocked(isLinux).mockReturnValue(false);
vi.mocked(extensionApi.env).isLinux = false;
vi.spyOn(PodmanInstall.prototype, 'checkForUpdate').mockResolvedValue({
hasUpdate: false,
} as unknown as UpdateCheck);
Expand Down Expand Up @@ -2726,7 +2725,7 @@ describe.each(['windows', 'mac', 'linux'])('podman machine properties audit on %
beforeEach(() => {
vi.mocked(extensionApi.env).isWindows = os === 'windows';
vi.mocked(util.isMac).mockReturnValue(os === 'mac');
vi.mocked(util.isLinux).mockReturnValue(os === 'linux');
vi.mocked(extensionApi.env).isLinux = os === 'windows';
});
if (os === 'linux') {
test('is not used', async () => {
Expand Down Expand Up @@ -3112,7 +3111,7 @@ describe('connectionAuditor', () => {
test('activate and autostart should not duplicate machines ', async () => {
vi.mocked(isMac).mockReturnValue(true);
vi.mocked(extensionApi.env).isWindows = false;
vi.mocked(isLinux).mockReturnValue(false);
vi.mocked(extensionApi.env).isLinux = false;
vi.spyOn(PodmanInstall.prototype, 'checkForUpdate').mockResolvedValue({
hasUpdate: false,
} as unknown as UpdateCheck);
Expand Down
31 changes: 16 additions & 15 deletions extensions/podman/packages/extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import {
getAssetsFolder,
getProviderByLabel,
getProviderLabel,
isLinux,
isMac,
LoggerDelegator,
VMTYPE,
Expand All @@ -71,8 +70,8 @@ let autoMachineStarted = false;
let autoMachineName: string | undefined;

// System default notifier
let defaultMachineNotify = !isLinux();
let defaultConnectionNotify = !isLinux();
let defaultMachineNotify = !extensionApi.env.isLinux;
let defaultConnectionNotify = !extensionApi.env.isLinux;
let defaultMachineMonitor = true;

// current status of machines
Expand Down Expand Up @@ -202,7 +201,7 @@ export async function updateMachines(

// Only on macOS and Windows should we show the setup notification
// if for some reason doing getJSONMachineList fails..
if (shouldNotifySetup && !isLinux()) {
if (shouldNotifySetup && !extensionApi.env.isLinux) {
// push setup notification
notifySetupPodman();
shouldNotifySetup = false;
Expand All @@ -224,7 +223,7 @@ export async function updateMachines(
}

// invalid machines is not making the provider working properly so always notify
if (shouldCleanMachine && shouldNotifySetup && !isLinux()) {
if (shouldCleanMachine && shouldNotifySetup && !extensionApi.env.isLinux) {
// push setup notification
notifySetupPodman();
shouldNotifySetup = false;
Expand All @@ -234,7 +233,7 @@ export async function updateMachines(

// 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()) {
if (shouldNotifySetup && machines.length === 0 && !extensionApi.env.isLinux) {
// push setup notification
notifySetupPodman();
shouldNotifySetup = false;
Expand All @@ -243,7 +242,7 @@ export async function updateMachines(
// if there is at least one machine whihc does not need to be cleaned and the OS is not Linux
// podman is correctly setup so if there is an old notification asking the user to take action
// we dispose it as not needed anymore
if (!shouldCleanMachine && machines.length > 0 && !isLinux()) {
if (!shouldCleanMachine && machines.length > 0 && !extensionApi.env.isLinux) {
notificationDisposable?.dispose();
shouldNotifySetup = true;
}
Expand Down Expand Up @@ -350,7 +349,7 @@ export async function updateMachines(
if (!socketPath) {
if (isMac()) {
socketPath = calcMacosSocketPath(machineName);
} else if (isLinux()) {
} else if (extensionApi.env.isLinux) {
socketPath = calcLinuxSocketPath(machineName);
} else if (extensionApi.env.isWindows) {
socketPath = calcWinPipeName(machineName);
Expand All @@ -376,7 +375,7 @@ export async function updateMachines(
// 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()) {
if (!extensionApi.env.isLinux) {
if (machines.length === 0) {
if (provider.status !== 'configuring') {
provider.updateStatus('installed');
Expand Down Expand Up @@ -720,7 +719,7 @@ async function monitorProvider(provider: extensionApi.Provider): Promise<void> {
// if podman is not installed and the OS is linux we show the podman onboarding notification (if it has not been shown earlier)
// this should be limited to Linux as in other OSes the onboarding workflow is enabled based on the podman machine existance
// and the notification is handled by checking the machine
if (isLinux() && shouldNotifySetup) {
if (extensionApi.env.isLinux && shouldNotifySetup) {
// push setup notification
notifySetupPodman();
shouldNotifySetup = false;
Expand All @@ -734,7 +733,7 @@ async function monitorProvider(provider: extensionApi.Provider): Promise<void> {

extensionApi.context.setValue('podmanIsNotInstalled', false, 'onboarding');
// if podman has been installed, we reset the notification flag so if podman is uninstalled in future we can show the notification again
if (isLinux()) {
if (extensionApi.env.isLinux) {
shouldNotifySetup = true;
// notification is no more required
notificationDisposable?.dispose();
Expand Down Expand Up @@ -1579,7 +1578,7 @@ export async function start(
// Below is Linux specific code:
// * Monitors the system service for an unlimited time
// * Uses the native system socket
if (isLinux()) {
if (extensionApi.env.isLinux) {
// on Linux, need to run the system service for unlimited time
let command = 'podman';
let args = ['system', 'service', '--time=0'];
Expand Down Expand Up @@ -1903,7 +1902,9 @@ export function isRootfulMachineInitSupported(podmanVersion: string): boolean {
const PODMAN_MINIMUM_VERSION_FOR_NEW_SOCKET_LOCATION = '4.5.0';

export function isPodmanSocketLocationMoved(podmanVersion: string): boolean {
return isLinux() && compareVersions(podmanVersion, PODMAN_MINIMUM_VERSION_FOR_NEW_SOCKET_LOCATION) >= 0;
return (
extensionApi.env.isLinux && compareVersions(podmanVersion, PODMAN_MINIMUM_VERSION_FOR_NEW_SOCKET_LOCATION) >= 0
);
}

const PODMAN_MINIMUM_VERSION_FOR_USER_MODE_NETWORKING = '4.6.0';
Expand Down Expand Up @@ -2218,7 +2219,7 @@ function setupDisguisedPodmanSocketWatcher(
});

let socketWatcher: extensionApi.FileSystemWatcher | undefined = undefined;
if (isLinux() || isMac()) {
if (extensionApi.env.isLinux || isMac()) {
socketWatcher = extensionApi.fs.createFileSystemWatcher(socketFile);
}

Expand Down Expand Up @@ -2261,7 +2262,7 @@ export async function checkDisguisedPodmanSocket(provider: extensionApi.Provider

// NOTE: LINUX SUPPORT
// Linux does not support compatibility mode button, so do not sending the warning
if (!isLinux()) {
if (!extensionApi.env.isLinux) {
const retrievedWarnings = isDisguisedPodmanSocket ? [] : [getDisguisedPodmanInformation()];
provider.updateWarnings(retrievedWarnings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type { ProxySettings } from '@podman-desktop/api';
import * as extensionApi from '@podman-desktop/api';
import * as toml from 'smol-toml';

import { isLinux, isMac } from './util';
import { isMac } from './util';

const configurationRosetta = 'setting.rosetta';

Expand Down Expand Up @@ -321,7 +321,7 @@ export class PodmanConfiguration {
podmanConfigContainersPath = path.resolve(os.homedir(), '.config', 'containers');
} else if (extensionApi.env.isWindows) {
podmanConfigContainersPath = path.resolve(os.homedir(), 'AppData', 'Roaming', 'containers');
} else if (isLinux()) {
} else if (extensionApi.env.isLinux) {
const xdgRuntimeDirectory = process.env['XDG_RUNTIME_DIR'] ?? '';
podmanConfigContainersPath = path.resolve(xdgRuntimeDirectory, 'containers');
}
Expand Down
4 changes: 2 additions & 2 deletions extensions/podman/packages/extension/src/registry-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import * as path from 'node:path';

import * as extensionApi from '@podman-desktop/api';

import { isLinux, isMac } from './util';
import { isMac } from './util';

export type ContainerAuthConfigEntry = {
[key: string]: {
Expand All @@ -43,7 +43,7 @@ export class RegistrySetup {

if (isMac() || extensionApi.env.isWindows) {
podmanConfigContainersPath = path.resolve(os.homedir(), '.config/containers');
} else if (isLinux()) {
} else if (extensionApi.env.isLinux) {
const xdgRuntimeDirectory = process.env['XDG_RUNTIME_DIR'] ?? '';
podmanConfigContainersPath = path.resolve(xdgRuntimeDirectory, 'containers');
}
Expand Down
4 changes: 0 additions & 4 deletions extensions/podman/packages/extension/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ const mac = os.platform() === 'darwin';
export function isMac(): boolean {
return mac;
}
const linux = os.platform() === 'linux';
export function isLinux(): boolean {
return linux;
}
const xdgDataDirectory = '.local/share/containers';
export function appHomeDir(): string {
return xdgDataDirectory + '/podman';
Expand Down

0 comments on commit 9bcbf15

Please sign in to comment.