Skip to content

Commit fed10a3

Browse files
committed
add test
1 parent c84fe3e commit fed10a3

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: v1
2+
kind: Config
3+
clusters:
4+
- name: inCluster
5+
cluster:
6+
server: https://0.0.0.0:443
7+
certificate-authority: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
8+
insecure-skip-tls-verify: false
9+
users:
10+
- name: developer
11+
user:
12+
token: token
13+
contexts:
14+
- name: logged-user
15+
context:
16+
user: developer
17+
cluster: inCluster
18+
name: logged-user
19+
preferences: {}
20+
current-context: logged-user

packages/dashboard-backend/src/devworkspaceClient/services/__tests__/kubeConfigApi.spec.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import * as mockClient from '@kubernetes/client-node';
1616
import { CoreV1Api, V1PodList } from '@kubernetes/client-node';
17+
import fs from 'fs';
18+
import { parse, stringify } from 'yaml';
1719

1820
import * as helper from '@/devworkspaceClient/services/helpers/exec';
1921
import { KubeConfigApiService } from '@/devworkspaceClient/services/kubeConfigApi';
@@ -28,7 +30,7 @@ const mockExecPrintenvHome = jest.fn().mockReturnValue({
2830
stdError: '',
2931
});
3032

31-
const mockExecCatKubeConfig = jest.fn().mockReturnValue({
33+
let mockExecCatKubeConfig = jest.fn().mockReturnValue({
3234
stdOut: '',
3335
stdError: '',
3436
});
@@ -61,11 +63,14 @@ const spyExec = jest
6163
const namespace = 'user-che';
6264
const workspaceName = 'workspace-1';
6365
const containerName = 'container-1';
64-
const config = JSON.stringify({
66+
const config = {
6567
apiVersion: 'v1',
6668
kind: 'Config',
6769
'current-context': 'logged-user',
68-
});
70+
contexts: [],
71+
clusters: [],
72+
users: [],
73+
};
6974

7075
describe('Kubernetes Config API Service', () => {
7176
let kubeConfigService: KubeConfigApiService;
@@ -81,7 +86,7 @@ describe('Kubernetes Config API Service', () => {
8186
},
8287
} as CoreV1Api;
8388
});
84-
kubeConfig.exportConfig = jest.fn().mockReturnValue(config);
89+
kubeConfig.exportConfig = jest.fn().mockReturnValue(JSON.stringify(config));
8590
kubeConfig.getCurrentCluster = jest.fn().mockReturnValue('');
8691
kubeConfig.applyToRequest = jest.fn();
8792

@@ -142,7 +147,24 @@ describe('Kubernetes Config API Service', () => {
142147
workspaceName,
143148
namespace,
144149
containerName,
145-
['sh', '-c', `echo '${config}' > ${kubeConfigDir}/config`],
150+
['sh', '-c', `echo '${stringify(config)}' > ${kubeConfigDir}/config`],
151+
expect.anything(),
152+
);
153+
});
154+
test('should merge configs', async () => {
155+
const configContent = fs.readFileSync(__dirname + '/fixtures/kubeconfig.yaml', 'utf-8');
156+
mockExecCatKubeConfig = jest.fn().mockReturnValue({
157+
stdOut: configContent,
158+
stdError: '',
159+
});
160+
await kubeConfigService.injectKubeConfig(namespace, 'wksp-id');
161+
162+
expect(spyExec).toHaveBeenNthCalledWith(
163+
5,
164+
workspaceName,
165+
namespace,
166+
containerName,
167+
['sh', '-c', `echo '${configContent}' > ${kubeConfigDir}/config`],
146168
expect.anything(),
147169
);
148170
});

0 commit comments

Comments
 (0)