From bfe9db3e27bc52da0e78586b73b41442cb8ccfb9 Mon Sep 17 00:00:00 2001 From: ivinokur Date: Tue, 17 Dec 2024 18:11:56 +0200 Subject: [PATCH] add test --- .../__tests__/fixtures/kubeconfig.yaml | 20 ++++++++++++++++ .../services/__tests__/kubeConfigApi.spec.ts | 23 ++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 packages/dashboard-backend/src/devworkspaceClient/services/__tests__/fixtures/kubeconfig.yaml diff --git a/packages/dashboard-backend/src/devworkspaceClient/services/__tests__/fixtures/kubeconfig.yaml b/packages/dashboard-backend/src/devworkspaceClient/services/__tests__/fixtures/kubeconfig.yaml new file mode 100644 index 000000000..82c81f531 --- /dev/null +++ b/packages/dashboard-backend/src/devworkspaceClient/services/__tests__/fixtures/kubeconfig.yaml @@ -0,0 +1,20 @@ +apiVersion: v1 +kind: Config +clusters: + - name: inCluster + cluster: + server: https://0.0.0.0:443 + certificate-authority: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt + insecure-skip-tls-verify: false +users: + - name: developer + user: + token: token +contexts: + - name: logged-user + context: + user: developer + cluster: inCluster + name: logged-user +preferences: {} +current-context: logged-user diff --git a/packages/dashboard-backend/src/devworkspaceClient/services/__tests__/kubeConfigApi.spec.ts b/packages/dashboard-backend/src/devworkspaceClient/services/__tests__/kubeConfigApi.spec.ts index 19a950b3e..554f0789a 100644 --- a/packages/dashboard-backend/src/devworkspaceClient/services/__tests__/kubeConfigApi.spec.ts +++ b/packages/dashboard-backend/src/devworkspaceClient/services/__tests__/kubeConfigApi.spec.ts @@ -14,6 +14,7 @@ import * as mockClient from '@kubernetes/client-node'; import { CoreV1Api, V1PodList } from '@kubernetes/client-node'; +import fs from 'fs'; import * as helper from '@/devworkspaceClient/services/helpers/exec'; import { KubeConfigApiService } from '@/devworkspaceClient/services/kubeConfigApi'; @@ -28,7 +29,7 @@ const mockExecPrintenvHome = jest.fn().mockReturnValue({ stdError: '', }); -const mockExecCatKubeConfig = jest.fn().mockReturnValue({ +let mockExecCatKubeConfig = jest.fn().mockReturnValue({ stdOut: '', stdError: '', }); @@ -65,6 +66,9 @@ const config = JSON.stringify({ apiVersion: 'v1', kind: 'Config', 'current-context': 'logged-user', + contexts: [], + clusters: [], + users: [], }); describe('Kubernetes Config API Service', () => { @@ -146,6 +150,23 @@ describe('Kubernetes Config API Service', () => { expect.anything(), ); }); + test('should merge configs', async () => { + const configContent = fs.readFileSync(__dirname + '/fixtures/kubeconfig.yaml', 'utf-8'); + mockExecCatKubeConfig = jest.fn().mockReturnValue({ + stdOut: configContent, + stdError: '', + }); + await kubeConfigService.injectKubeConfig(namespace, 'wksp-id'); + + expect(spyExec).toHaveBeenNthCalledWith( + 5, + workspaceName, + namespace, + containerName, + ['sh', '-c', `echo '${configContent}' > ${kubeConfigDir}/config`], + expect.anything(), + ); + }); }); function buildListNamespacedPod(): { body: V1PodList } {