1
1
import { expect , use } from 'chai' ;
2
+ import * as request from 'request' ;
2
3
import chaiAsPromised = require( 'chai-as-promised' ) ;
3
4
4
5
import * as mock from 'ts-mockito' ;
@@ -9,18 +10,46 @@ import { EventEmitter } from 'ws';
9
10
10
11
import { V1Namespace , V1NamespaceList , V1ObjectMeta , V1Pod , V1ListMeta } from './api' ;
11
12
import { deleteObject , ListWatch , deleteItems } from './cache' ;
13
+ import { KubeConfig } from './config' ;
14
+ import { Cluster , Context , User } from './config_types' ;
12
15
import { ADD , UPDATE , DELETE , ERROR , ListPromise , CHANGE } from './informer' ;
13
16
14
17
use ( chaiAsPromised ) ;
15
18
16
- import { RequestResult , Watch } from './watch' ;
19
+ import { DefaultRequest , RequestResult , Watch } from './watch' ;
17
20
18
21
// Object replacing real Request object in the test
19
22
class FakeRequest extends EventEmitter implements RequestResult {
20
23
pipe ( stream : Duplex ) : void { }
21
24
abort ( ) { }
22
25
}
23
26
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
+
24
53
describe ( 'ListWatchCache' , ( ) => {
25
54
it ( 'should throw on unknown update' , ( ) => {
26
55
const fake = mock . mock ( Watch ) ;
@@ -1022,52 +1051,73 @@ describe('ListWatchCache', () => {
1022
1051
) . once ( ) ;
1023
1052
expect ( errorEmitted ) . to . equal ( true ) ;
1024
1053
} ) ;
1025
- } ) ;
1026
1054
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 [ ] = [
1030
1059
{
1031
1060
metadata : {
1032
1061
name : 'name1' ,
1033
- namespace : 'ns1' ,
1062
+ labels : {
1063
+ app : 'foo' ,
1064
+ } ,
1034
1065
} as V1ObjectMeta ,
1035
- } as V1Pod ,
1066
+ } as V1Namespace ,
1036
1067
{
1037
1068
metadata : {
1038
1069
name : 'name2' ,
1039
- namespace : 'ns2' ,
1070
+ labels : {
1071
+ app : 'foo' ,
1072
+ } ,
1040
1073
} 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 ,
1064
1075
] ;
1076
+ const listObj = {
1077
+ metadata : {
1078
+ resourceVersion : '12345' ,
1079
+ } as V1ListMeta ,
1080
+ items : list ,
1081
+ } as V1NamespaceList ;
1065
1082
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 ) ;
1068
1116
} ) ;
1117
+ } ) ;
1069
1118
1070
- it ( 'should callback correctly' , ( ) => {
1119
+ describe ( 'delete items' , ( ) => {
1120
+ it ( 'should remove correctly' , ( ) => {
1071
1121
const listA : V1Pod [ ] = [
1072
1122
{
1073
1123
metadata : {
@@ -1099,12 +1149,54 @@ describe('delete items', () => {
1099
1149
const expected : V1Pod [ ] = [
1100
1150
{
1101
1151
metadata : {
1102
- name : 'name2 ' ,
1103
- namespace : 'ns2 ' ,
1152
+ name : 'name1 ' ,
1153
+ namespace : 'ns1 ' ,
1104
1154
} as V1ObjectMeta ,
1105
1155
} as V1Pod ,
1106
1156
] ;
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 [ ] = [ ] ;
1108
1200
1109
1201
deleteItems ( listA , listB , [ ( obj : V1Pod ) => pods . push ( obj ) ] ) ;
1110
1202
expect ( pods ) . to . deep . equal ( expected ) ;
0 commit comments