Skip to content

Commit f8915b7

Browse files
committed
test: add test for ListWatch labelSelector query param
1 parent b9a013e commit f8915b7

File tree

1 file changed

+87
-38
lines changed

1 file changed

+87
-38
lines changed

src/cache_test.ts

Lines changed: 87 additions & 38 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';
12-
import { ListPromise } from './informer';
13+
import { KubeConfig } from './config';
14+
import { Cluster, Context, User } from './config_types';
15+
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);
@@ -1024,52 +1053,73 @@ describe('ListWatchCache', () => {
10241053
).once();
10251054
expect(errorEmitted).to.equal(true);
10261055
});
1027-
});
10281056

1029-
describe('delete items', () => {
1030-
it('should remove correctly', () => {
1031-
const listA: V1Pod[] = [
1057+
it('should send label selector', async () => {
1058+
const APP_LABEL_SELECTOR = 'app=foo';
1059+
1060+
const list: V1Namespace[] = [
10321061
{
10331062
metadata: {
10341063
name: 'name1',
1035-
namespace: 'ns1',
1064+
labels: {
1065+
app: 'foo',
1066+
},
10361067
} as V1ObjectMeta,
1037-
} as V1Pod,
1068+
} as V1Namespace,
10381069
{
10391070
metadata: {
10401071
name: 'name2',
1041-
namespace: 'ns2',
1042-
} as V1ObjectMeta,
1043-
} as V1Pod,
1044-
];
1045-
const listB: V1Pod[] = [
1046-
{
1047-
metadata: {
1048-
name: 'name1',
1049-
namespace: 'ns1',
1050-
} as V1ObjectMeta,
1051-
} as V1Pod,
1052-
{
1053-
metadata: {
1054-
name: 'name3',
1055-
namespace: 'ns3',
1072+
labels: {
1073+
app: 'foo',
1074+
},
10561075
} as V1ObjectMeta,
1057-
} as V1Pod,
1058-
];
1059-
const expected: V1Pod[] = [
1060-
{
1061-
metadata: {
1062-
name: 'name1',
1063-
namespace: 'ns1',
1064-
} as V1ObjectMeta,
1065-
} as V1Pod,
1076+
} as V1Namespace,
10661077
];
1078+
const listObj = {
1079+
metadata: {
1080+
resourceVersion: '12345',
1081+
} as V1ListMeta,
1082+
items: list,
1083+
} as V1NamespaceList;
10671084

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

1072-
it('should callback correctly', () => {
1121+
describe('delete items', () => {
1122+
it('should remove correctly', () => {
10731123
const listA: V1Pod[] = [
10741124
{
10751125
metadata: {
@@ -1101,12 +1151,11 @@ describe('delete items', () => {
11011151
const expected: V1Pod[] = [
11021152
{
11031153
metadata: {
1104-
name: 'name2',
1105-
namespace: 'ns2',
1154+
name: 'name1',
1155+
namespace: 'ns1',
11061156
} as V1ObjectMeta,
11071157
} as V1Pod,
11081158
];
1109-
const pods: V1Pod[] = [];
11101159

11111160
deleteItems(listA, listB, [(obj?: V1Pod) => pods.push(obj!)]);
11121161
expect(pods).to.deep.equal(expected);

0 commit comments

Comments
 (0)