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' ;
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' ;
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 ) ;
@@ -1024,52 +1053,73 @@ describe('ListWatchCache', () => {
1024
1053
) . once ( ) ;
1025
1054
expect ( errorEmitted ) . to . equal ( true ) ;
1026
1055
} ) ;
1027
- } ) ;
1028
1056
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 [ ] = [
1032
1061
{
1033
1062
metadata : {
1034
1063
name : 'name1' ,
1035
- namespace : 'ns1' ,
1064
+ labels : {
1065
+ app : 'foo' ,
1066
+ } ,
1036
1067
} as V1ObjectMeta ,
1037
- } as V1Pod ,
1068
+ } as V1Namespace ,
1038
1069
{
1039
1070
metadata : {
1040
1071
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
+ } ,
1056
1075
} 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 ,
1066
1077
] ;
1078
+ const listObj = {
1079
+ metadata : {
1080
+ resourceVersion : '12345' ,
1081
+ } as V1ListMeta ,
1082
+ items : list ,
1083
+ } as V1NamespaceList ;
1067
1084
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 ) ;
1070
1118
} ) ;
1119
+ } ) ;
1071
1120
1072
- it ( 'should callback correctly' , ( ) => {
1121
+ describe ( 'delete items' , ( ) => {
1122
+ it ( 'should remove correctly' , ( ) => {
1073
1123
const listA : V1Pod [ ] = [
1074
1124
{
1075
1125
metadata : {
@@ -1101,12 +1151,11 @@ describe('delete items', () => {
1101
1151
const expected : V1Pod [ ] = [
1102
1152
{
1103
1153
metadata : {
1104
- name : 'name2 ' ,
1105
- namespace : 'ns2 ' ,
1154
+ name : 'name1 ' ,
1155
+ namespace : 'ns1 ' ,
1106
1156
} as V1ObjectMeta ,
1107
1157
} as V1Pod ,
1108
1158
] ;
1109
- const pods : V1Pod [ ] = [ ] ;
1110
1159
1111
1160
deleteItems ( listA , listB , [ ( obj ?: V1Pod ) => pods . push ( obj ! ) ] ) ;
1112
1161
expect ( pods ) . to . deep . equal ( expected ) ;
0 commit comments