@@ -8,7 +8,7 @@ import http = require('http');
8
8
import { Duplex } from 'stream' ;
9
9
import { EventEmitter } from 'ws' ;
10
10
11
- import { V1Namespace , V1NamespaceList , V1ObjectMeta , V1Pod , V1ListMeta } from './api' ;
11
+ import { V1Namespace , V1NamespaceList , V1ObjectMeta , V1Pod , V1PodList , V1ListMeta } from './api' ;
12
12
import { deleteObject , ListWatch , deleteItems , CacheMap , cacheMapFromList } from './cache' ;
13
13
import { KubeConfig } from './config' ;
14
14
import { Cluster , Context , User } from './config_types' ;
@@ -80,18 +80,141 @@ describe('ListWatchCache', () => {
80
80
81
81
it ( 'should perform basic caching' , async ( ) => {
82
82
const fakeWatch = mock . mock ( Watch ) ;
83
- const list : V1Namespace [ ] = [
83
+ const list : V1Pod [ ] = [
84
84
{
85
85
metadata : {
86
86
name : 'name1' ,
87
87
namespace : 'default' ,
88
88
} as V1ObjectMeta ,
89
- } as V1Namespace ,
89
+ } as V1Pod ,
90
90
{
91
91
metadata : {
92
92
name : 'name2' ,
93
93
namespace : 'default' ,
94
94
} as V1ObjectMeta ,
95
+ } as V1Pod ,
96
+ ] ;
97
+ const listObj = {
98
+ metadata : {
99
+ resourceVersion : '12345' ,
100
+ } as V1ListMeta ,
101
+ items : list ,
102
+ } as V1PodList ;
103
+
104
+ const emptyObj = {
105
+ metadata : {
106
+ resourceVersion : '123456' ,
107
+ } as V1ListMeta ,
108
+ items : [
109
+ {
110
+ metadata : {
111
+ name : 'name3' ,
112
+ namespace : 'default' ,
113
+ } as V1ObjectMeta ,
114
+ } as V1Pod ,
115
+ ] ,
116
+ } as V1PodList ;
117
+
118
+ let calls = 0 ;
119
+ const listFn : ListPromise < V1Pod > = function ( ) : Promise < {
120
+ response : http . IncomingMessage ;
121
+ body : V1PodList ;
122
+ } > {
123
+ return new Promise < { response : http . IncomingMessage ; body : V1PodList } > ( ( resolve , reject ) => {
124
+ if ( calls ++ === 0 ) {
125
+ resolve ( { response : { } as http . IncomingMessage , body : listObj } ) ;
126
+ } else {
127
+ resolve ( { response : { } as http . IncomingMessage , body : emptyObj } ) ;
128
+ }
129
+ } ) ;
130
+ } ;
131
+ const promise = new Promise ( ( resolve ) => {
132
+ mock . when (
133
+ fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
134
+ ) . thenCall ( ( ) => {
135
+ resolve ( new FakeRequest ( ) ) ;
136
+ } ) ;
137
+ } ) ;
138
+ const cache = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
139
+ await promise ;
140
+ const [ pathOut , , watchHandler , doneHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
141
+ expect ( pathOut ) . to . equal ( '/some/path' ) ;
142
+ expect ( cache . list ( ) ) . to . deep . equal ( list ) ;
143
+
144
+ expect ( cache . get ( 'name1' , 'default' ) ) . to . equal ( list [ 0 ] ) ;
145
+ expect ( cache . get ( 'name2' , 'default' ) ) . to . equal ( list [ 1 ] ) ;
146
+
147
+ expect ( cache . list ( 'default' ) ) . to . deep . equal ( list ) ;
148
+ expect ( cache . list ( 'non-existent' ) ) . to . deep . equal ( [ ] ) ;
149
+
150
+ watchHandler ( 'ADDED' , {
151
+ metadata : {
152
+ name : 'name3' ,
153
+ namespace : 'other' ,
154
+ } as V1ObjectMeta ,
155
+ } as V1Pod ) ;
156
+
157
+ expect ( cache . list ( ) . length ) . to . equal ( 3 ) ;
158
+ expect ( cache . get ( 'name3' , 'other' ) ) . to . not . equal ( null ) ;
159
+
160
+ expect ( cache . list ( 'default' ) . length ) . to . equal ( 2 ) ;
161
+ expect ( cache . list ( 'other' ) . length ) . to . equal ( 1 ) ;
162
+ expect ( cache . list ( 'non-existent' ) ) . to . deep . equal ( [ ] ) ;
163
+
164
+ watchHandler ( 'MODIFIED' , {
165
+ metadata : {
166
+ name : 'name3' ,
167
+ namespace : 'other' ,
168
+ resourceVersion : 'baz' ,
169
+ } as V1ObjectMeta ,
170
+ } as V1Pod ) ;
171
+ expect ( cache . list ( ) . length ) . to . equal ( 3 ) ;
172
+ const obj3 = cache . get ( 'name3' , 'other' ) ;
173
+ expect ( obj3 ) . to . not . equal ( null ) ;
174
+ if ( obj3 ) {
175
+ expect ( obj3 . metadata ! . name ) . to . equal ( 'name3' ) ;
176
+ expect ( obj3 . metadata ! . resourceVersion ) . to . equal ( 'baz' ) ;
177
+ }
178
+
179
+ watchHandler ( 'DELETED' , {
180
+ metadata : {
181
+ name : 'name2' ,
182
+ namespace : 'default' ,
183
+ } as V1ObjectMeta ,
184
+ } as V1Pod ) ;
185
+ expect ( cache . list ( ) . length ) . to . equal ( 2 ) ;
186
+ expect ( cache . get ( 'name2' , 'default' ) ) . to . equal ( undefined ) ;
187
+
188
+ expect ( cache . list ( 'default' ) . length ) . to . equal ( 1 ) ;
189
+ expect ( cache . list ( 'other' ) . length ) . to . equal ( 1 ) ;
190
+
191
+ watchHandler ( 'ADDED' , {
192
+ metadata : {
193
+ name : 'name2' ,
194
+ namespace : 'default' ,
195
+ } as V1ObjectMeta ,
196
+ } as V1Pod ) ;
197
+
198
+ const error = new Error ( 'Gone' ) as Error & { statusCode : number | undefined } ;
199
+ error . statusCode = 410 ;
200
+ await doneHandler ( error ) ;
201
+ expect ( cache . list ( ) . length , 'all namespace list' ) . to . equal ( 1 ) ;
202
+ expect ( cache . list ( 'default' ) . length , 'default namespace list' ) . to . equal ( 1 ) ;
203
+ expect ( cache . list ( 'other' ) , 'other namespace list' ) . to . deep . equal ( [ ] ) ;
204
+ } ) ;
205
+
206
+ it ( 'should perform basic caching of non-namespaced objects' , async ( ) => {
207
+ const fakeWatch = mock . mock ( Watch ) ;
208
+ const list : V1Namespace [ ] = [
209
+ {
210
+ metadata : {
211
+ name : 'name1' ,
212
+ } as V1ObjectMeta ,
213
+ } as V1Namespace ,
214
+ {
215
+ metadata : {
216
+ name : 'name2' ,
217
+ } as V1ObjectMeta ,
95
218
} as V1Namespace ,
96
219
] ;
97
220
const listObj = {
@@ -109,7 +232,6 @@ describe('ListWatchCache', () => {
109
232
{
110
233
metadata : {
111
234
name : 'name3' ,
112
- namespace : 'default' ,
113
235
} as V1ObjectMeta ,
114
236
} as V1Namespace ,
115
237
] ,
@@ -143,35 +265,33 @@ describe('ListWatchCache', () => {
143
265
expect ( pathOut ) . to . equal ( '/some/path' ) ;
144
266
expect ( cache . list ( ) ) . to . deep . equal ( list ) ;
145
267
146
- expect ( cache . get ( 'name1' , 'default' ) ) . to . equal ( list [ 0 ] ) ;
147
- expect ( cache . get ( 'name2' , 'default' ) ) . to . equal ( list [ 1 ] ) ;
268
+ expect ( cache . get ( 'name1' ) ) . to . equal ( list [ 0 ] ) ;
269
+ expect ( cache . get ( 'name2' ) ) . to . equal ( list [ 1 ] ) ;
148
270
149
- expect ( cache . list ( 'default' ) ) . to . deep . equal ( list ) ;
271
+ expect ( cache . list ( 'default' ) ) . to . deep . equal ( [ ] ) ;
150
272
expect ( cache . list ( 'non-existent' ) ) . to . deep . equal ( [ ] ) ;
151
273
152
274
watchHandler ( 'ADDED' , {
153
275
metadata : {
154
276
name : 'name3' ,
155
- namespace : 'other' ,
156
277
} as V1ObjectMeta ,
157
278
} as V1Namespace ) ;
158
279
159
280
expect ( cache . list ( ) . length ) . to . equal ( 3 ) ;
160
- expect ( cache . get ( 'name3' , 'default' ) ) . to . not . equal ( null ) ;
281
+ expect ( cache . get ( 'name3' ) ) . to . not . equal ( null ) ;
161
282
162
- expect ( cache . list ( 'default' ) . length ) . to . equal ( 2 ) ;
163
- expect ( cache . list ( 'other' ) . length ) . to . equal ( 1 ) ;
283
+ expect ( cache . list ( 'default' ) . length ) . to . equal ( 0 ) ;
284
+ expect ( cache . list ( 'other' ) . length ) . to . equal ( 0 ) ;
164
285
expect ( cache . list ( 'non-existent' ) ) . to . deep . equal ( [ ] ) ;
165
286
166
287
watchHandler ( 'MODIFIED' , {
167
288
metadata : {
168
289
name : 'name3' ,
169
- namespace : 'other' ,
170
290
resourceVersion : 'baz' ,
171
291
} as V1ObjectMeta ,
172
292
} as V1Namespace ) ;
173
293
expect ( cache . list ( ) . length ) . to . equal ( 3 ) ;
174
- const obj3 = cache . get ( 'name3' , 'other' ) ;
294
+ const obj3 = cache . get ( 'name3' ) ;
175
295
expect ( obj3 ) . to . not . equal ( null ) ;
176
296
if ( obj3 ) {
177
297
expect ( obj3 . metadata ! . name ) . to . equal ( 'name3' ) ;
@@ -181,28 +301,21 @@ describe('ListWatchCache', () => {
181
301
watchHandler ( 'DELETED' , {
182
302
metadata : {
183
303
name : 'name2' ,
184
- namespace : 'default' ,
185
304
} as V1ObjectMeta ,
186
305
} as V1Namespace ) ;
187
306
expect ( cache . list ( ) . length ) . to . equal ( 2 ) ;
188
- expect ( cache . get ( 'name2' , 'default' ) ) . to . equal ( undefined ) ;
189
-
190
- expect ( cache . list ( 'default' ) . length ) . to . equal ( 1 ) ;
191
- expect ( cache . list ( 'other' ) . length ) . to . equal ( 1 ) ;
307
+ expect ( cache . get ( 'name2' ) ) . to . equal ( undefined ) ;
192
308
193
309
watchHandler ( 'ADDED' , {
194
310
metadata : {
195
311
name : 'name2' ,
196
- namespace : 'default' ,
197
312
} as V1ObjectMeta ,
198
313
} as V1Namespace ) ;
199
314
200
315
const error = new Error ( 'Gone' ) as Error & { statusCode : number | undefined } ;
201
316
error . statusCode = 410 ;
202
317
await doneHandler ( error ) ;
203
318
expect ( cache . list ( ) . length , 'all namespace list' ) . to . equal ( 1 ) ;
204
- expect ( cache . list ( 'default' ) . length , 'default namespace list' ) . to . equal ( 1 ) ;
205
- expect ( cache . list ( 'other' ) , 'other namespace list' ) . to . deep . equal ( [ ] ) ;
206
319
} ) ;
207
320
208
321
it ( 'should perform work as an informer' , async ( ) => {
0 commit comments