From 57928860b99b4cba67fd2118c270ad283bfb3ed9 Mon Sep 17 00:00:00 2001 From: Tatjana Dehler Date: Fri, 20 Oct 2023 18:05:47 +0200 Subject: [PATCH] Mock KubeClient listServices for screenshots Mock the `listServices` function of `KubeClient` to show some items on the list as part of the port forwarding page screenshot. Signed-off-by: Tatjana Dehler --- pkg/rancher-desktop/backend/factory.ts | 13 +++++- pkg/rancher-desktop/backend/kube/lima.ts | 4 +- pkg/rancher-desktop/backend/kube/wsl.ts | 4 +- pkg/rancher-desktop/backend/mock.ts | 14 +++++-- .../backend/mock_screenshots.ts | 40 +++++++++++++++++++ screenshots/screenshots.e2e.spec.ts | 3 ++ 6 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 pkg/rancher-desktop/backend/mock_screenshots.ts diff --git a/pkg/rancher-desktop/backend/factory.ts b/pkg/rancher-desktop/backend/factory.ts index 393d3aa47ef..e02051d7913 100644 --- a/pkg/rancher-desktop/backend/factory.ts +++ b/pkg/rancher-desktop/backend/factory.ts @@ -7,6 +7,7 @@ import LimaBackend from './lima'; import MockBackend from './mock'; import WSLBackend from './wsl'; +import { LimaKubernetesBackendMock, WSLKubernetesBackendMock } from '@pkg/backend/mock_screenshots'; import DockerDirManager from '@pkg/utils/dockerDirManager'; export default function factory(arch: Architecture, dockerDirManager: DockerDirManager): VMBackend { @@ -20,11 +21,19 @@ export default function factory(arch: Architecture, dockerDirManager: DockerDirM case 'linux': case 'darwin': return new LimaBackend(arch, dockerDirManager, (backend: LimaBackend) => { - return new LimaKubernetesBackend(arch, backend); + if (process.env.RD_MOCK_FOR_SCREENSHOTS) { + return new LimaKubernetesBackendMock(arch, backend); + } else { + return new LimaKubernetesBackend(arch, backend); + } }); case 'win32': return new WSLBackend((backend: WSLBackend) => { - return new WSLKubernetesBackend(backend); + if (process.env.RD_MOCK_FOR_SCREENSHOTS) { + return new WSLKubernetesBackendMock(backend); + } else { + return new WSLKubernetesBackend(backend); + } }); default: throw new Error(`OS "${ platform }" is not supported.`); diff --git a/pkg/rancher-desktop/backend/kube/lima.ts b/pkg/rancher-desktop/backend/kube/lima.ts index b38fd80b0f5..e89afe1dec0 100644 --- a/pkg/rancher-desktop/backend/kube/lima.ts +++ b/pkg/rancher-desktop/backend/kube/lima.ts @@ -137,7 +137,7 @@ export default class LimaKubernetesBackend extends events.EventEmitter implement * Start Kubernetes. * @returns The Kubernetes endpoint */ - async start(config_: BackendSettings, kubernetesVersion: semver.SemVer): Promise { + async start(config_: BackendSettings, kubernetesVersion: semver.SemVer, kubeClient?: KubeClient): Promise { const config = this.cfg = clone(config_); let k3sEndpoint = ''; @@ -186,7 +186,7 @@ export default class LimaKubernetesBackend extends events.EventEmitter implement return k3sConfigString; })); - this.client = new KubeClient(); + this.client = kubeClient || new KubeClient(); await this.progressTracker.action( 'Waiting for services', diff --git a/pkg/rancher-desktop/backend/kube/wsl.ts b/pkg/rancher-desktop/backend/kube/wsl.ts index deddb4ab6a4..a1fe897f3ef 100644 --- a/pkg/rancher-desktop/backend/kube/wsl.ts +++ b/pkg/rancher-desktop/backend/kube/wsl.ts @@ -172,7 +172,7 @@ export default class WSLKubernetesBackend extends events.EventEmitter implements 'install-k3s', version.raw, await this.vm.wslify(path.join(paths.cache, 'k3s'))); } - async start(config: BackendSettings, activeVersion: semver.SemVer): Promise { + async start(config: BackendSettings, activeVersion: semver.SemVer, kubeClient?: KubeClient): Promise { if (!config) { throw new Error('no config!'); } @@ -218,7 +218,7 @@ export default class WSLKubernetesBackend extends events.EventEmitter implements async() => await this.vm.execCommand({ capture: true }, await this.vm.getWSLHelperPath(), 'k3s', 'kubeconfig', rdNetworking)); }); - const client = this.client = new KubeClient(); + const client = this.client = kubeClient || new KubeClient(); await this.progressTracker.action( 'Waiting for services', diff --git a/pkg/rancher-desktop/backend/mock.ts b/pkg/rancher-desktop/backend/mock.ts index 2fa339d335a..8a331f2ac45 100644 --- a/pkg/rancher-desktop/backend/mock.ts +++ b/pkg/rancher-desktop/backend/mock.ts @@ -6,12 +6,20 @@ import util from 'util'; import semver from 'semver'; import { - BackendSettings, execOptions, State, RestartReasons, VMExecutor, BackendEvents, + BackendEvents, BackendSettings, execOptions, RestartReasons, State, VMExecutor, } from './backend'; import { - ContainerBasicOptions, ContainerComposeExecOptions, ContainerComposeOptions, ContainerComposePortOptions, ContainerEngineClient, ContainerRunClientOptions, ContainerRunOptions, ContainerStopOptions, ReadableProcess, + ContainerBasicOptions, + ContainerComposeExecOptions, + ContainerComposeOptions, + ContainerComposePortOptions, + ContainerEngineClient, + ContainerRunClientOptions, + ContainerRunOptions, + ContainerStopOptions, + ReadableProcess, } from './containerClient'; -import { KubernetesBackend, KubernetesError, KubernetesBackendEvents } from './k8s'; +import { KubernetesBackend, KubernetesBackendEvents, KubernetesError } from './k8s'; import ProgressTracker from './progressTracker'; import K3sHelper from '@pkg/backend/k3sHelper'; diff --git a/pkg/rancher-desktop/backend/mock_screenshots.ts b/pkg/rancher-desktop/backend/mock_screenshots.ts new file mode 100644 index 00000000000..b7cc78da44b --- /dev/null +++ b/pkg/rancher-desktop/backend/mock_screenshots.ts @@ -0,0 +1,40 @@ +import semver from 'semver'; + +import { BackendSettings } from '@pkg/backend/backend'; +import { KubeClient, ServiceEntry } from '@pkg/backend/kube/client'; +import LimaKubernetesBackend from '@pkg/backend/kube/lima'; +import WSLKubernetesBackend from '@pkg/backend/kube/wsl'; + +export class LimaKubernetesBackendMock extends LimaKubernetesBackend { + start(config_: BackendSettings, kubernetesVersion: semver.SemVer): Promise { + return super.start(config_, kubernetesVersion, new KubeClientMock()); + } +} + +export class WSLKubernetesBackendMock extends WSLKubernetesBackend { + start(config_: BackendSettings, kubernetesVersion: semver.SemVer): Promise { + return super.start(config_, kubernetesVersion, new KubeClientMock()); + } +} + +class KubeClientMock extends KubeClient { + listServices(namespace: string | undefined = undefined): ServiceEntry[] { + return [{ + namespace: 'default', + name: 'nginx', + portName: 'http', + port: 8080, + listenPort: 30001, + }, { + namespace: 'default', + name: 'wordpress', + portName: 'http', + port: 8080, + }, { + namespace: 'default', + name: 'wordpress', + portName: 'https', + port: 443, + }]; + } +} diff --git a/screenshots/screenshots.e2e.spec.ts b/screenshots/screenshots.e2e.spec.ts index 28817ea4c71..dd440e86504 100644 --- a/screenshots/screenshots.e2e.spec.ts +++ b/screenshots/screenshots.e2e.spec.ts @@ -108,6 +108,9 @@ test.describe.serial('Main App Test', () => { }); test('PortForwarding Page', async({ colorScheme }) => { + const portForwardingPage = await navPage.navigateTo('PortForwarding'); + + await expect(portForwardingPage.page.getByRole('row')).toHaveCount(4); await screenshot.take('PortForwarding', navPage); });