Skip to content

Commit 03b6f71

Browse files
committed
test: add test for ListWatch labelSelector query param
1 parent 2013ba1 commit 03b6f71

File tree

1 file changed

+129
-37
lines changed

1 file changed

+129
-37
lines changed

src/cache_test.ts

Lines changed: 129 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, use } from 'chai';
2+
import * as request from 'request';
23
import chaiAsPromised = require('chai-as-promised');
34

45
import * as mock from 'ts-mockito';
@@ -9,18 +10,46 @@ import { EventEmitter } from 'ws';
910

1011
import { V1Namespace, V1NamespaceList, V1ObjectMeta, V1Pod, V1ListMeta } from './api';
1112
import { deleteObject, ListWatch, deleteItems } from './cache';
13+
import { KubeConfig } from './config';
14+
import { Cluster, Context, User } from './config_types';
1215
import { ADD, UPDATE, DELETE, ERROR, ListPromise, CHANGE } from './informer';
1316

1417
use(chaiAsPromised);
1518

16-
import { RequestResult, Watch } from './watch';
19+
import { DefaultRequest, RequestResult, Watch } from './watch';
1720

1821
// Object replacing real Request object in the test
1922
class FakeRequest extends EventEmitter implements RequestResult {
2023
pipe(stream: Duplex): void {}
2124
abort() {}
2225
}
2326

27+
const server = 'foo.company.com';
28+
29+
const fakeConfig: {
30+
clusters: Cluster[];
31+
contexts: Context[];
32+
users: User[];
33+
} = {
34+
clusters: [
35+
{
36+
name: 'cluster',
37+
server,
38+
} as Cluster,
39+
],
40+
contexts: [
41+
{
42+
cluster: 'cluster',
43+
user: 'user',
44+
} as Context,
45+
],
46+
users: [
47+
{
48+
name: 'user',
49+
} as User,
50+
],
51+
};
52+
2453
describe('ListWatchCache', () => {
2554
it('should throw on unknown update', () => {
2655
const fake = mock.mock(Watch);
@@ -1022,52 +1051,73 @@ describe('ListWatchCache', () => {
10221051
).once();
10231052
expect(errorEmitted).to.equal(true);
10241053
});
1025-
});
10261054

1027-
describe('delete items', () => {
1028-
it('should remove correctly', () => {
1029-
const listA: V1Pod[] = [
1055+
it('should send label selector', async () => {
1056+
const APP_LABEL_SELECTOR = 'app=foo';
1057+
1058+
const list: V1Namespace[] = [
10301059
{
10311060
metadata: {
10321061
name: 'name1',
1033-
namespace: 'ns1',
1062+
labels: {
1063+
app: 'foo',
1064+
},
10341065
} as V1ObjectMeta,
1035-
} as V1Pod,
1066+
} as V1Namespace,
10361067
{
10371068
metadata: {
10381069
name: 'name2',
1039-
namespace: 'ns2',
1070+
labels: {
1071+
app: 'foo',
1072+
},
10401073
} as V1ObjectMeta,
1041-
} as V1Pod,
1042-
];
1043-
const listB: V1Pod[] = [
1044-
{
1045-
metadata: {
1046-
name: 'name1',
1047-
namespace: 'ns1',
1048-
} as V1ObjectMeta,
1049-
} as V1Pod,
1050-
{
1051-
metadata: {
1052-
name: 'name3',
1053-
namespace: 'ns3',
1054-
} as V1ObjectMeta,
1055-
} as V1Pod,
1056-
];
1057-
const expected: V1Pod[] = [
1058-
{
1059-
metadata: {
1060-
name: 'name1',
1061-
namespace: 'ns1',
1062-
} as V1ObjectMeta,
1063-
} as V1Pod,
1074+
} as V1Namespace,
10641075
];
1076+
const listObj = {
1077+
metadata: {
1078+
resourceVersion: '12345',
1079+
} as V1ListMeta,
1080+
items: list,
1081+
} as V1NamespaceList;
10651082

1066-
const output = deleteItems(listA, listB);
1067-
expect(output).to.deep.equal(expected);
1083+
const listFn: ListPromise<V1Namespace> = function(): Promise<{
1084+
response: http.IncomingMessage;
1085+
body: V1NamespaceList;
1086+
}> {
1087+
return new Promise<{ response: http.IncomingMessage; body: V1NamespaceList }>(
1088+
(resolve, reject) => {
1089+
resolve({ response: {} as http.IncomingMessage, body: listObj });
1090+
},
1091+
);
1092+
};
1093+
1094+
const kc = new KubeConfig();
1095+
Object.assign(kc, fakeConfig);
1096+
const fakeRequestor = mock.mock(DefaultRequest);
1097+
const watch = new Watch(kc, mock.instance(fakeRequestor));
1098+
1099+
const fakeRequest = new FakeRequest();
1100+
mock.when(fakeRequestor.webRequest(mock.anything())).thenReturn(fakeRequest);
1101+
1102+
const informer = new ListWatch(
1103+
'/some/path',
1104+
watch,
1105+
listFn,
1106+
false,
1107+
APP_LABEL_SELECTOR,
1108+
);
1109+
1110+
await informer.start();
1111+
1112+
mock.verify(fakeRequestor.webRequest(mock.anything()));
1113+
const [opts] = mock.capture(fakeRequestor.webRequest).last();
1114+
const reqOpts: request.OptionsWithUri = opts as request.OptionsWithUri;
1115+
expect(reqOpts.qs.labelSelector).to.equal(APP_LABEL_SELECTOR);
10681116
});
1117+
});
10691118

1070-
it('should callback correctly', () => {
1119+
describe('delete items', () => {
1120+
it('should remove correctly', () => {
10711121
const listA: V1Pod[] = [
10721122
{
10731123
metadata: {
@@ -1099,12 +1149,54 @@ describe('delete items', () => {
10991149
const expected: V1Pod[] = [
11001150
{
11011151
metadata: {
1102-
name: 'name2',
1103-
namespace: 'ns2',
1152+
name: 'name1',
1153+
namespace: 'ns1',
11041154
} as V1ObjectMeta,
11051155
} as V1Pod,
11061156
];
1107-
const pods: V1Pod[] = [];
1157+
1158+
const output = deleteItems(listA, listB);
1159+
expect(output).to.deep.equal(expected);
1160+
});
1161+
1162+
it('should callback correctly', () => {
1163+
const listA: V1Pod[] = [
1164+
{
1165+
metadata: {
1166+
name: 'name1',
1167+
namespace: 'ns1',
1168+
} as V1ObjectMeta,
1169+
} as V1Pod,
1170+
{
1171+
metadata: {
1172+
name: 'name2',
1173+
namespace: 'ns2',
1174+
} as V1ObjectMeta,
1175+
} as V1Pod,
1176+
];
1177+
const listB: V1Pod[] = [
1178+
{
1179+
metadata: {
1180+
name: 'name1',
1181+
namespace: 'ns1',
1182+
} as V1ObjectMeta,
1183+
} as V1Pod,
1184+
{
1185+
metadata: {
1186+
name: 'name3',
1187+
namespace: 'ns3',
1188+
} as V1ObjectMeta,
1189+
} as V1Pod,
1190+
];
1191+
const expected: V1Pod[] = [
1192+
{
1193+
metadata: {
1194+
name: 'name2',
1195+
namespace: 'ns2',
1196+
} as V1ObjectMeta,
1197+
} as V1Pod,
1198+
];
1199+
const pods: V1Pod[] = [];
11081200

11091201
deleteItems(listA, listB, [(obj: V1Pod) => pods.push(obj)]);
11101202
expect(pods).to.deep.equal(expected);

0 commit comments

Comments
 (0)