From 0d5235abb40c78ddc86c8867cd255f30200ff1de Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Wed, 18 Sep 2024 18:32:10 +0200 Subject: [PATCH] feat(telemetry): adding qemu version (#8949) Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com> --- .../packages/extension/src/extension.spec.ts | 165 +++++++++++++----- .../packages/extension/src/extension.ts | 9 + 2 files changed, 126 insertions(+), 48 deletions(-) diff --git a/extensions/podman/packages/extension/src/extension.spec.ts b/extensions/podman/packages/extension/src/extension.spec.ts index fb47daf355389..318efa052968e 100644 --- a/extensions/podman/packages/extension/src/extension.spec.ts +++ b/extensions/podman/packages/extension/src/extension.spec.ts @@ -107,6 +107,7 @@ const telemetryLogger: extensionApi.TelemetryLogger = { const mocks = vi.hoisted(() => ({ getPodmanLocationMacMock: vi.fn(), getKrunkitVersionMock: vi.fn(), + getQemuVersionMock: vi.fn(), })); // mock ps-list @@ -223,6 +224,12 @@ vi.mock('node:os', async () => { }; }); +vi.mock('./qemu-helper', () => ({ + QemuHelper: vi.fn().mockReturnValue({ + getQemuVersion: mocks.getQemuVersionMock, + }), +})); + vi.mock('./krunkit-helper', async () => { return { KrunkitHelper: vi.fn().mockImplementation(() => { @@ -283,6 +290,10 @@ vi.mock('./util', async () => { beforeEach(() => { vi.resetAllMocks(); console.error = consoleErrorMock; + + (extensionApi.env.isMac as boolean) = true; + (extensionApi.env.isLinux as boolean) = false; + (extensionApi.env.isWindows as boolean) = false; }); afterEach(() => { @@ -2098,59 +2109,117 @@ test('isLibkrunSupported should return false with previous 5.1.2 version', async expect(enabled).toBeFalsy(); }); -test('sendTelemetryRecords with krunkit found', async () => { - vi.spyOn(podmanCli, 'getPodmanInstallation').mockResolvedValue({ - version: '5.1.2', +describe('sendTelemetryRecords', () => { + test('krunkit found', async () => { + vi.spyOn(podmanCli, 'getPodmanInstallation').mockResolvedValue({ + version: '5.1.2', + }); + mocks.getPodmanLocationMacMock.mockResolvedValue({ foundPath: '/opt/podman/bin/podman', source: 'installer' }); + mocks.getKrunkitVersionMock.mockResolvedValue('1.2.3'); + + extension.sendTelemetryRecords( + 'evt', + { + provider: 'libkrun', + } as Record, + false, + ); + await new Promise(resolve => setTimeout(resolve, 100)); + expect(telemetryLogger.logUsage).toHaveBeenCalledWith( + 'evt', + expect.objectContaining({ + krunkitPath: '/opt/podman/bin', + krunkitVersion: '1.2.3', + podmanCliFoundPath: '/opt/podman/bin/podman', + podmanCliSource: 'installer', + podmanCliVersion: '5.1.2', + provider: 'libkrun', + }), + ); }); - mocks.getPodmanLocationMacMock.mockResolvedValue({ foundPath: '/opt/podman/bin/podman', source: 'installer' }); - mocks.getKrunkitVersionMock.mockResolvedValue('1.2.3'); - extension.sendTelemetryRecords( - 'evt', - { - provider: 'libkrun', - } as Record, - false, - ); - await new Promise(resolve => setTimeout(resolve, 100)); - expect(telemetryLogger.logUsage).toHaveBeenCalledWith( - 'evt', - expect.objectContaining({ - krunkitPath: '/opt/podman/bin', - krunkitVersion: '1.2.3', - podmanCliFoundPath: '/opt/podman/bin/podman', - podmanCliSource: 'installer', - podmanCliVersion: '5.1.2', - provider: 'libkrun', - }), - ); -}); + test('krunkit not found', async () => { + vi.spyOn(podmanCli, 'getPodmanInstallation').mockResolvedValue({ + version: '5.1.2', + }); + mocks.getPodmanLocationMacMock.mockResolvedValue({ foundPath: '/opt/podman/bin/podman', source: 'installer' }); + mocks.getKrunkitVersionMock.mockRejectedValue('command not found'); + + extension.sendTelemetryRecords( + 'evt', + { + provider: 'libkrun', + } as Record, + false, + ); + await new Promise(resolve => setTimeout(resolve, 100)); + expect(telemetryLogger.logUsage).toHaveBeenCalledWith( + 'evt', + expect.objectContaining({ + errorKrunkitVersion: 'command not found', + podmanCliFoundPath: '/opt/podman/bin/podman', + podmanCliSource: 'installer', + podmanCliVersion: '5.1.2', + provider: 'libkrun', + }), + ); + }); -test('sendTelemetryRecords with krunkit not found', async () => { - vi.spyOn(podmanCli, 'getPodmanInstallation').mockResolvedValue({ - version: '5.1.2', + test('qemu found', async () => { + vi.spyOn(podmanCli, 'getPodmanInstallation').mockResolvedValue({ + version: '5.1.2', + }); + extension.sendTelemetryRecords( + 'evt', + { + provider: 'qemu', + } as Record, + false, + ); + (extensionApi.env.isLinux as boolean) = true; + (extensionApi.env.isMac as boolean) = false; + (extensionApi.env.isWindows as boolean) = false; + + mocks.getQemuVersionMock.mockResolvedValue('5.5.5'); + + await vi.waitFor(() => { + expect(telemetryLogger.logUsage).toHaveBeenCalledWith( + 'evt', + expect.objectContaining({ + provider: 'qemu', + qemuVersion: '5.5.5', + }), + ); + }); }); - mocks.getPodmanLocationMacMock.mockResolvedValue({ foundPath: '/opt/podman/bin/podman', source: 'installer' }); - mocks.getKrunkitVersionMock.mockRejectedValue('command not found'); - extension.sendTelemetryRecords( - 'evt', - { - provider: 'libkrun', - } as Record, - false, - ); - await new Promise(resolve => setTimeout(resolve, 100)); - expect(telemetryLogger.logUsage).toHaveBeenCalledWith( - 'evt', - expect.objectContaining({ - errorKrunkitVersion: 'command not found', - podmanCliFoundPath: '/opt/podman/bin/podman', - podmanCliSource: 'installer', - podmanCliVersion: '5.1.2', - provider: 'libkrun', - }), - ); + test('qemu not found', async () => { + vi.spyOn(podmanCli, 'getPodmanInstallation').mockResolvedValue({ + version: '5.1.2', + }); + extension.sendTelemetryRecords( + 'evt', + { + provider: 'qemu', + } as Record, + false, + ); + (extensionApi.env.isLinux as boolean) = true; + (extensionApi.env.isMac as boolean) = false; + (extensionApi.env.isWindows as boolean) = false; + + mocks.getQemuVersionMock.mockRejectedValue('command not found'); + + await vi.waitFor(() => { + expect(telemetryLogger.logUsage).toHaveBeenCalledWith( + 'evt', + expect.objectContaining({ + provider: 'qemu', + errorQemuVersion: 'command not found', + }), + ); + }); + }); }); test('if a machine stopped is successfully reporting telemetry', async () => { diff --git a/extensions/podman/packages/extension/src/extension.ts b/extensions/podman/packages/extension/src/extension.ts index f821f7d9170e9..4e0f16b1b5711 100644 --- a/extensions/podman/packages/extension/src/extension.ts +++ b/extensions/podman/packages/extension/src/extension.ts @@ -38,6 +38,7 @@ import { PodmanConfiguration } from './podman-configuration'; import { PodmanInfoHelper } from './podman-info-helper'; import { PodmanInstall } from './podman-install'; import { PodmanRemoteConnections } from './podman-remote-connections'; +import { QemuHelper } from './qemu-helper'; import { RegistrySetup } from './registry-setup'; import { appConfigDir, @@ -89,6 +90,7 @@ const configurationCompatibilityMode = 'setting.dockerCompatibility'; let telemetryLogger: extensionApi.TelemetryLogger | undefined; const wslHelper = new WslHelper(); +const qemuHelper = new QemuHelper(); const krunkitHelper = new KrunkitHelper(); const podmanBinaryHelper = new PodmanBinaryLocationHelper(); const podmanInfoHelper = new PodmanInfoHelper(); @@ -1851,6 +1853,13 @@ export function sendTelemetryRecords( console.trace('unable to check wsl version', error); telemetryRecords.errorWslVersion = error; } + } else if (extensionApi.env.isLinux && telemetryRecords.provider === 'qemu') { + try { + telemetryRecords.qemuVersion = await qemuHelper.getQemuVersion(); + } catch (err: unknown) { + console.trace('unable to check qemu version', err); + telemetryRecords.errorQemuVersion = err; + } } // add server side information about the machine