14
14
15
15
import * as mockClient from '@kubernetes/client-node' ;
16
16
import { CoreV1Api , V1PodList } from '@kubernetes/client-node' ;
17
+ import fs from 'fs' ;
18
+ import { parse , stringify } from 'yaml' ;
17
19
18
20
import * as helper from '@/devworkspaceClient/services/helpers/exec' ;
19
21
import { KubeConfigApiService } from '@/devworkspaceClient/services/kubeConfigApi' ;
@@ -28,7 +30,7 @@ const mockExecPrintenvHome = jest.fn().mockReturnValue({
28
30
stdError : '' ,
29
31
} ) ;
30
32
31
- const mockExecCatKubeConfig = jest . fn ( ) . mockReturnValue ( {
33
+ let mockExecCatKubeConfig = jest . fn ( ) . mockReturnValue ( {
32
34
stdOut : '' ,
33
35
stdError : '' ,
34
36
} ) ;
@@ -61,11 +63,14 @@ const spyExec = jest
61
63
const namespace = 'user-che' ;
62
64
const workspaceName = 'workspace-1' ;
63
65
const containerName = 'container-1' ;
64
- const config = JSON . stringify ( {
66
+ const config = {
65
67
apiVersion : 'v1' ,
66
68
kind : 'Config' ,
67
69
'current-context' : 'logged-user' ,
68
- } ) ;
70
+ contexts : [ ] ,
71
+ clusters : [ ] ,
72
+ users : [ ] ,
73
+ } ;
69
74
70
75
describe ( 'Kubernetes Config API Service' , ( ) => {
71
76
let kubeConfigService : KubeConfigApiService ;
@@ -81,7 +86,7 @@ describe('Kubernetes Config API Service', () => {
81
86
} ,
82
87
} as CoreV1Api ;
83
88
} ) ;
84
- kubeConfig . exportConfig = jest . fn ( ) . mockReturnValue ( config ) ;
89
+ kubeConfig . exportConfig = jest . fn ( ) . mockReturnValue ( JSON . stringify ( config ) ) ;
85
90
kubeConfig . getCurrentCluster = jest . fn ( ) . mockReturnValue ( '' ) ;
86
91
kubeConfig . applyToRequest = jest . fn ( ) ;
87
92
@@ -142,7 +147,24 @@ describe('Kubernetes Config API Service', () => {
142
147
workspaceName ,
143
148
namespace ,
144
149
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` ] ,
146
168
expect . anything ( ) ,
147
169
) ;
148
170
} ) ;
0 commit comments