Skip to content

Commit 0ea7835

Browse files
committed
fix: remove unused request dependency
1 parent 92ec46c commit 0ea7835

File tree

2 files changed

+86
-50
lines changed

2 files changed

+86
-50
lines changed

src/cache_test.ts

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
import { expect, use } from 'chai';
2-
import * as request from 'request';
32
import chaiAsPromised = require('chai-as-promised');
43

54
import * as mock from 'ts-mockito';
65

76
import http = require('http');
8-
import { Duplex } from 'stream';
9-
import { EventEmitter } from 'ws';
107

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';
1310
import { KubeConfig } from './config';
1411
import { Cluster, Context, User } from './config_types';
15-
import { ADD, UPDATE, DELETE, ERROR, ListPromise, CHANGE } from './informer';
12+
import { ListPromise } from './informer';
1613

1714
use(chaiAsPromised);
1815

19-
import { DefaultRequest, RequestResult, Watch } from './watch';
16+
import AbortController from 'abort-controller';
17+
import nock = require('nock');
18+
import { Watch } from './watch';
2019

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';
2821

2922
const fakeConfig: {
3023
clusters: Cluster[];
@@ -115,7 +108,7 @@ describe('ListWatchCache', () => {
115108
],
116109
} as V1NamespaceList;
117110

118-
var calls = 0;
111+
let calls = 0;
119112
const listFn: ListPromise<V1Namespace> = function (): Promise<{
120113
response: http.IncomingMessage;
121114
body: V1NamespaceList;
@@ -134,7 +127,7 @@ describe('ListWatchCache', () => {
134127
mock.when(
135128
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
136129
).thenCall(() => {
137-
resolve(new FakeRequest());
130+
resolve(new AbortController());
138131
});
139132
});
140133
const cache = new ListWatch('/some/path', mock.instance(fakeWatch), listFn);
@@ -240,7 +233,7 @@ describe('ListWatchCache', () => {
240233
mock.when(
241234
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
242235
).thenCall(() => {
243-
resolve(new FakeRequest());
236+
resolve(new AbortController());
244237
});
245238
});
246239
const informer = new ListWatch('/some/path', mock.instance(fakeWatch), listFn);
@@ -329,7 +322,7 @@ describe('ListWatchCache', () => {
329322
mock.when(
330323
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
331324
).thenCall(() => {
332-
resolve(new FakeRequest());
325+
resolve(new AbortController());
333326
});
334327
});
335328
const informer = new ListWatch('/some/path', mock.instance(fakeWatch), listFn);
@@ -392,7 +385,7 @@ describe('ListWatchCache', () => {
392385
mock.when(
393386
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
394387
).thenCall(() => {
395-
resolve(new FakeRequest());
388+
resolve(new AbortController());
396389
});
397390
});
398391
const informer = new ListWatch('/some/path', mock.instance(fakeWatch), listFn);
@@ -461,7 +454,7 @@ describe('ListWatchCache', () => {
461454
mock.when(
462455
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
463456
).thenCall(() => {
464-
resolve(new FakeRequest());
457+
resolve(new AbortController());
465458
});
466459
});
467460
const informer = new ListWatch('/some/path', mock.instance(fakeWatch), listFn, false);
@@ -483,7 +476,7 @@ describe('ListWatchCache', () => {
483476
mock.when(
484477
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
485478
).thenCall(() => {
486-
resolve(new FakeRequest());
479+
resolve(new AbortController());
487480
});
488481
});
489482
doneHandler(null);
@@ -537,7 +530,7 @@ describe('ListWatchCache', () => {
537530
mock.when(
538531
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
539532
).thenCall(() => {
540-
resolve(new FakeRequest());
533+
resolve(new AbortController());
541534
});
542535
});
543536
const informer = new ListWatch('/some/path', mock.instance(fakeWatch), listFn, false);
@@ -560,7 +553,7 @@ describe('ListWatchCache', () => {
560553
mock.when(
561554
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
562555
).thenCall(() => {
563-
resolve(new FakeRequest());
556+
resolve(new AbortController());
564557
});
565558
});
566559
listObj.items = list2;
@@ -617,7 +610,7 @@ describe('ListWatchCache', () => {
617610
mock.when(
618611
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
619612
).thenCall(() => {
620-
resolve(new FakeRequest());
613+
resolve(new AbortController());
621614
});
622615
});
623616
const cache = new ListWatch('/some/path', mock.instance(fakeWatch), listFn);
@@ -962,11 +955,11 @@ describe('ListWatchCache', () => {
962955
},
963956
);
964957
};
965-
let promise = new Promise((resolve) => {
958+
const promise = new Promise((resolve) => {
966959
mock.when(
967960
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
968961
).thenCall(() => {
969-
resolve(new FakeRequest());
962+
resolve(new AbortController());
970963
});
971964
});
972965

@@ -1025,11 +1018,11 @@ describe('ListWatchCache', () => {
10251018
},
10261019
);
10271020
};
1028-
let promise = new Promise((resolve) => {
1021+
const promise = new Promise((resolve) => {
10291022
mock.when(
10301023
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
10311024
).thenCall(() => {
1032-
resolve(new FakeRequest());
1025+
resolve(new AbortController());
10331026
});
10341027
});
10351028

@@ -1074,7 +1067,7 @@ describe('ListWatchCache', () => {
10741067
mock.when(
10751068
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
10761069
).thenCall(() => {
1077-
resolve(new FakeRequest());
1070+
resolve(new AbortController());
10781071
});
10791072
});
10801073
const informer = new ListWatch('/some/path', mock.instance(fakeWatch), listFn, false);
@@ -1098,7 +1091,7 @@ describe('ListWatchCache', () => {
10981091
mock.when(
10991092
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
11001093
).thenCall(() => {
1101-
resolve(new FakeRequest());
1094+
resolve(new AbortController());
11021095
});
11031096
});
11041097
informer.start();
@@ -1130,7 +1123,7 @@ describe('ListWatchCache', () => {
11301123
mock.when(
11311124
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
11321125
).thenCall(() => {
1133-
resolve(new FakeRequest());
1126+
resolve(new AbortController());
11341127
});
11351128
});
11361129

@@ -1159,7 +1152,7 @@ describe('ListWatchCache', () => {
11591152
mock.when(
11601153
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
11611154
).thenCall(() => {
1162-
resolve(new FakeRequest());
1155+
resolve(new AbortController());
11631156
});
11641157
});
11651158

@@ -1179,7 +1172,7 @@ describe('ListWatchCache', () => {
11791172
expect(listCalls).to.be.equal(2);
11801173
});
11811174

1182-
it.skip('should send label selector', async () => {
1175+
it('should send label selector', async () => {
11831176
const APP_LABEL_SELECTOR = 'app=foo';
11841177

11851178
const list: V1Namespace[] = [
@@ -1220,20 +1213,57 @@ describe('ListWatchCache', () => {
12201213

12211214
const kc = new KubeConfig();
12221215
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);
12251217

1226-
const fakeRequest = new FakeRequest();
1227-
mock.when(fakeRequestor.webRequest(mock.anything())).thenReturn(fakeRequest);
1218+
const path = '/some/path';
12281219

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+
);
12301245

12311246
await informer.start();
12321247

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();
12371267
});
12381268
});
12391269

@@ -1344,11 +1374,11 @@ describe('delete items', () => {
13441374
},
13451375
);
13461376
};
1347-
let promise = new Promise((resolve) => {
1377+
const promise = new Promise((resolve) => {
13481378
mock.when(
13491379
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
13501380
).thenCall(() => {
1351-
resolve(new FakeRequest());
1381+
resolve(new AbortController());
13521382
});
13531383
});
13541384

src/watch_test.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,23 @@ describe('Watch', () => {
9090
},
9191
};
9292

93-
const path = '/some/path/to/object?watch=true&a=b';
93+
const path = '/some/path/to/object';
9494

9595
const stream = new PassThrough();
9696

9797
const [scope] = systemUnderTest();
9898

99-
const s = scope.get(path).reply(200, () => {
100-
stream.push(JSON.stringify(obj1) + '\n');
101-
stream.push(JSON.stringify(obj2) + '\n');
102-
return stream;
103-
});
99+
const s = scope
100+
.get(path)
101+
.query({
102+
watch: 'true',
103+
a: 'b',
104+
})
105+
.reply(200, () => {
106+
stream.push(JSON.stringify(obj1) + '\n');
107+
stream.push(JSON.stringify(obj2) + '\n');
108+
return stream;
109+
});
104110

105111
const receivedTypes: string[] = [];
106112
const receivedObjects: string[] = [];

0 commit comments

Comments
 (0)