1
1
import { expect , use } from 'chai' ;
2
- import * as request from 'request' ;
3
2
import chaiAsPromised = require( 'chai-as-promised' ) ;
4
3
5
4
import * as mock from 'ts-mockito' ;
6
5
7
6
import http = require( 'http' ) ;
8
- import { Duplex } from 'stream' ;
9
- import { EventEmitter } from 'ws' ;
10
7
11
- import { V1Namespace , V1NamespaceList , V1ObjectMeta , V1Pod , V1ListMeta } from './api' ;
12
- import { deleteObject , ListWatch , deleteItems } from './cache' ;
8
+ import { V1ListMeta , V1Namespace , V1NamespaceList , V1ObjectMeta , V1Pod } from './api' ;
9
+ import { deleteItems , deleteObject , ListWatch } from './cache' ;
13
10
import { KubeConfig } from './config' ;
14
11
import { Cluster , Context , User } from './config_types' ;
15
- import { ADD , UPDATE , DELETE , ERROR , ListPromise , CHANGE } from './informer' ;
12
+ import { ListPromise } from './informer' ;
16
13
17
14
use ( chaiAsPromised ) ;
18
15
19
- import { DefaultRequest , RequestResult , Watch } from './watch' ;
16
+ import AbortController from 'abort-controller' ;
17
+ import nock = require( 'nock' ) ;
18
+ import { Watch } from './watch' ;
20
19
21
- // Object replacing real Request object in the test
22
- class FakeRequest extends EventEmitter implements RequestResult {
23
- pipe ( stream : Duplex ) : void { }
24
- abort ( ) { }
25
- }
26
-
27
- const server = 'foo.company.com' ;
20
+ const server = 'http://foo.company.com' ;
28
21
29
22
const fakeConfig : {
30
23
clusters : Cluster [ ] ;
@@ -115,7 +108,7 @@ describe('ListWatchCache', () => {
115
108
] ,
116
109
} as V1NamespaceList ;
117
110
118
- var calls = 0 ;
111
+ let calls = 0 ;
119
112
const listFn : ListPromise < V1Namespace > = function ( ) : Promise < {
120
113
response : http . IncomingMessage ;
121
114
body : V1NamespaceList ;
@@ -134,7 +127,7 @@ describe('ListWatchCache', () => {
134
127
mock . when (
135
128
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
136
129
) . thenCall ( ( ) => {
137
- resolve ( new FakeRequest ( ) ) ;
130
+ resolve ( new AbortController ( ) ) ;
138
131
} ) ;
139
132
} ) ;
140
133
const cache = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
@@ -240,7 +233,7 @@ describe('ListWatchCache', () => {
240
233
mock . when (
241
234
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
242
235
) . thenCall ( ( ) => {
243
- resolve ( new FakeRequest ( ) ) ;
236
+ resolve ( new AbortController ( ) ) ;
244
237
} ) ;
245
238
} ) ;
246
239
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
@@ -329,7 +322,7 @@ describe('ListWatchCache', () => {
329
322
mock . when (
330
323
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
331
324
) . thenCall ( ( ) => {
332
- resolve ( new FakeRequest ( ) ) ;
325
+ resolve ( new AbortController ( ) ) ;
333
326
} ) ;
334
327
} ) ;
335
328
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
@@ -392,7 +385,7 @@ describe('ListWatchCache', () => {
392
385
mock . when (
393
386
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
394
387
) . thenCall ( ( ) => {
395
- resolve ( new FakeRequest ( ) ) ;
388
+ resolve ( new AbortController ( ) ) ;
396
389
} ) ;
397
390
} ) ;
398
391
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
@@ -461,7 +454,7 @@ describe('ListWatchCache', () => {
461
454
mock . when (
462
455
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
463
456
) . thenCall ( ( ) => {
464
- resolve ( new FakeRequest ( ) ) ;
457
+ resolve ( new AbortController ( ) ) ;
465
458
} ) ;
466
459
} ) ;
467
460
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn , false ) ;
@@ -483,7 +476,7 @@ describe('ListWatchCache', () => {
483
476
mock . when (
484
477
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
485
478
) . thenCall ( ( ) => {
486
- resolve ( new FakeRequest ( ) ) ;
479
+ resolve ( new AbortController ( ) ) ;
487
480
} ) ;
488
481
} ) ;
489
482
doneHandler ( null ) ;
@@ -537,7 +530,7 @@ describe('ListWatchCache', () => {
537
530
mock . when (
538
531
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
539
532
) . thenCall ( ( ) => {
540
- resolve ( new FakeRequest ( ) ) ;
533
+ resolve ( new AbortController ( ) ) ;
541
534
} ) ;
542
535
} ) ;
543
536
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn , false ) ;
@@ -560,7 +553,7 @@ describe('ListWatchCache', () => {
560
553
mock . when (
561
554
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
562
555
) . thenCall ( ( ) => {
563
- resolve ( new FakeRequest ( ) ) ;
556
+ resolve ( new AbortController ( ) ) ;
564
557
} ) ;
565
558
} ) ;
566
559
listObj . items = list2 ;
@@ -617,7 +610,7 @@ describe('ListWatchCache', () => {
617
610
mock . when (
618
611
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
619
612
) . thenCall ( ( ) => {
620
- resolve ( new FakeRequest ( ) ) ;
613
+ resolve ( new AbortController ( ) ) ;
621
614
} ) ;
622
615
} ) ;
623
616
const cache = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
@@ -962,11 +955,11 @@ describe('ListWatchCache', () => {
962
955
} ,
963
956
) ;
964
957
} ;
965
- let promise = new Promise ( ( resolve ) => {
958
+ const promise = new Promise ( ( resolve ) => {
966
959
mock . when (
967
960
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
968
961
) . thenCall ( ( ) => {
969
- resolve ( new FakeRequest ( ) ) ;
962
+ resolve ( new AbortController ( ) ) ;
970
963
} ) ;
971
964
} ) ;
972
965
@@ -1025,11 +1018,11 @@ describe('ListWatchCache', () => {
1025
1018
} ,
1026
1019
) ;
1027
1020
} ;
1028
- let promise = new Promise ( ( resolve ) => {
1021
+ const promise = new Promise ( ( resolve ) => {
1029
1022
mock . when (
1030
1023
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
1031
1024
) . thenCall ( ( ) => {
1032
- resolve ( new FakeRequest ( ) ) ;
1025
+ resolve ( new AbortController ( ) ) ;
1033
1026
} ) ;
1034
1027
} ) ;
1035
1028
@@ -1074,7 +1067,7 @@ describe('ListWatchCache', () => {
1074
1067
mock . when (
1075
1068
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
1076
1069
) . thenCall ( ( ) => {
1077
- resolve ( new FakeRequest ( ) ) ;
1070
+ resolve ( new AbortController ( ) ) ;
1078
1071
} ) ;
1079
1072
} ) ;
1080
1073
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn , false ) ;
@@ -1098,7 +1091,7 @@ describe('ListWatchCache', () => {
1098
1091
mock . when (
1099
1092
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
1100
1093
) . thenCall ( ( ) => {
1101
- resolve ( new FakeRequest ( ) ) ;
1094
+ resolve ( new AbortController ( ) ) ;
1102
1095
} ) ;
1103
1096
} ) ;
1104
1097
informer . start ( ) ;
@@ -1130,7 +1123,7 @@ describe('ListWatchCache', () => {
1130
1123
mock . when (
1131
1124
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
1132
1125
) . thenCall ( ( ) => {
1133
- resolve ( new FakeRequest ( ) ) ;
1126
+ resolve ( new AbortController ( ) ) ;
1134
1127
} ) ;
1135
1128
} ) ;
1136
1129
@@ -1159,7 +1152,7 @@ describe('ListWatchCache', () => {
1159
1152
mock . when (
1160
1153
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
1161
1154
) . thenCall ( ( ) => {
1162
- resolve ( new FakeRequest ( ) ) ;
1155
+ resolve ( new AbortController ( ) ) ;
1163
1156
} ) ;
1164
1157
} ) ;
1165
1158
@@ -1179,7 +1172,7 @@ describe('ListWatchCache', () => {
1179
1172
expect ( listCalls ) . to . be . equal ( 2 ) ;
1180
1173
} ) ;
1181
1174
1182
- it . skip ( 'should send label selector' , async ( ) => {
1175
+ it ( 'should send label selector' , async ( ) => {
1183
1176
const APP_LABEL_SELECTOR = 'app=foo' ;
1184
1177
1185
1178
const list : V1Namespace [ ] = [
@@ -1220,20 +1213,57 @@ describe('ListWatchCache', () => {
1220
1213
1221
1214
const kc = new KubeConfig ( ) ;
1222
1215
Object . assign ( kc , fakeConfig ) ;
1223
- const fakeRequestor = mock . mock ( DefaultRequest ) ;
1224
- const watch = new Watch ( kc , mock . instance ( fakeRequestor ) ) ;
1216
+ const watch = new Watch ( kc ) ;
1225
1217
1226
- const fakeRequest = new FakeRequest ( ) ;
1227
- mock . when ( fakeRequestor . webRequest ( mock . anything ( ) ) ) . thenReturn ( fakeRequest ) ;
1218
+ const path = '/some/path' ;
1228
1219
1229
- const informer = new ListWatch ( '/some/path' , watch , listFn , false , APP_LABEL_SELECTOR ) ;
1220
+ const informer = new ListWatch ( path , watch , listFn , false , APP_LABEL_SELECTOR ) ;
1221
+
1222
+ const scope = nock ( fakeConfig . clusters [ 0 ] . server ) ;
1223
+ const s = scope
1224
+ . get ( path )
1225
+ . query ( {
1226
+ watch : true ,
1227
+ resourceVersion : '12345' ,
1228
+ labelSelector : APP_LABEL_SELECTOR ,
1229
+ } )
1230
+ . reply (
1231
+ 200 ,
1232
+ ( ) =>
1233
+ JSON . stringify ( {
1234
+ type : 'ADDED' ,
1235
+ object : {
1236
+ metadata : {
1237
+ name : 'name3' ,
1238
+ labels : {
1239
+ app : 'foo3' ,
1240
+ } ,
1241
+ } as V1ObjectMeta ,
1242
+ } ,
1243
+ } ) + '\n' ,
1244
+ ) ;
1230
1245
1231
1246
await informer . start ( ) ;
1232
1247
1233
- mock . verify ( fakeRequestor . webRequest ( mock . anything ( ) ) ) ;
1234
- const [ opts ] = mock . capture ( fakeRequestor . webRequest ) . last ( ) ;
1235
- const reqOpts : request . OptionsWithUri = opts as request . OptionsWithUri ;
1236
- expect ( reqOpts . qs . labelSelector ) . to . equal ( APP_LABEL_SELECTOR ) ;
1248
+ let doneResolve : any ;
1249
+ const donePromise = new Promise ( ( resolve ) => {
1250
+ doneResolve = resolve ;
1251
+ } ) ;
1252
+
1253
+ informer . on ( 'add' , doneResolve ) ;
1254
+
1255
+ const value = await donePromise ;
1256
+
1257
+ expect ( value ) . to . deep . equal ( {
1258
+ metadata : {
1259
+ labels : {
1260
+ app : 'foo3' ,
1261
+ } ,
1262
+ name : 'name3' ,
1263
+ } ,
1264
+ } ) ;
1265
+
1266
+ s . done ( ) ;
1237
1267
} ) ;
1238
1268
} ) ;
1239
1269
@@ -1344,11 +1374,11 @@ describe('delete items', () => {
1344
1374
} ,
1345
1375
) ;
1346
1376
} ;
1347
- let promise = new Promise ( ( resolve ) => {
1377
+ const promise = new Promise ( ( resolve ) => {
1348
1378
mock . when (
1349
1379
fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
1350
1380
) . thenCall ( ( ) => {
1351
- resolve ( new FakeRequest ( ) ) ;
1381
+ resolve ( new AbortController ( ) ) ;
1352
1382
} ) ;
1353
1383
} ) ;
1354
1384
0 commit comments