@@ -7,6 +7,32 @@ import { KubeConfig } from './config';
7
7
import { Cluster , Context , User } from './config_types' ;
8
8
import { DefaultRequest , Watch } from './watch' ;
9
9
10
+ const server = 'foo.company.com' ;
11
+
12
+ const fakeConfig : {
13
+ clusters : Cluster [ ] ;
14
+ contexts : Context [ ] ;
15
+ users : User [ ] ;
16
+ } = {
17
+ clusters : [
18
+ {
19
+ name : 'cluster' ,
20
+ server,
21
+ } as Cluster ,
22
+ ] ,
23
+ contexts : [
24
+ {
25
+ cluster : 'cluster' ,
26
+ user : 'user' ,
27
+ } as Context ,
28
+ ] ,
29
+ users : [
30
+ {
31
+ name : 'user' ,
32
+ } as User ,
33
+ ] ,
34
+ } ;
35
+
10
36
describe ( 'Watch' , ( ) => {
11
37
it ( 'should construct correctly' , ( ) => {
12
38
const kc = new KubeConfig ( ) ;
@@ -15,24 +41,7 @@ describe('Watch', () => {
15
41
16
42
it ( 'should watch correctly' , ( ) => {
17
43
const kc = new KubeConfig ( ) ;
18
- const server = 'foo.company.com' ;
19
- kc . clusters = [
20
- {
21
- name : 'cluster' ,
22
- server,
23
- } as Cluster ,
24
- ] as Cluster [ ] ;
25
- kc . contexts = [
26
- {
27
- cluster : 'cluster' ,
28
- user : 'user' ,
29
- } as Context ,
30
- ] as Context [ ] ;
31
- kc . users = [
32
- {
33
- name : 'user' ,
34
- } as User ,
35
- ] ;
44
+ Object . assign ( kc , fakeConfig ) ;
36
45
const fakeRequestor = mock ( DefaultRequest ) ;
37
46
const watch = new Watch ( kc , instance ( fakeRequestor ) ) ;
38
47
@@ -102,4 +111,52 @@ describe('Watch', () => {
102
111
doneCallback ( errIn , null , null ) ;
103
112
expect ( doneErr ) . to . deep . equal ( errIn ) ;
104
113
} ) ;
114
+
115
+ it ( 'should ignore JSON parse errors' , ( ) => {
116
+ const kc = new KubeConfig ( ) ;
117
+ Object . assign ( kc , fakeConfig ) ;
118
+ const fakeRequestor = mock ( DefaultRequest ) ;
119
+ const watch = new Watch ( kc , instance ( fakeRequestor ) ) ;
120
+
121
+ const obj = {
122
+ type : 'MODIFIED' ,
123
+ object : {
124
+ baz : 'blah' ,
125
+ } ,
126
+ } ;
127
+
128
+ const fakeRequest = {
129
+ pipe : ( stream ) => {
130
+ stream . write ( JSON . stringify ( obj ) + '\n' ) ;
131
+ stream . write ( '{"truncated json\n' ) ;
132
+ } ,
133
+ } ;
134
+
135
+ when ( fakeRequestor . webRequest ( anything ( ) , anyFunction ( ) ) ) . thenReturn ( fakeRequest ) ;
136
+
137
+ const path = '/some/path/to/object' ;
138
+
139
+ const receivedTypes : string [ ] = [ ] ;
140
+ const receivedObjects : string [ ] = [ ] ;
141
+
142
+ watch . watch (
143
+ path ,
144
+ { } ,
145
+ ( recievedType : string , recievedObject : string ) => {
146
+ receivedTypes . push ( recievedType ) ;
147
+ receivedObjects . push ( recievedObject ) ;
148
+ } ,
149
+ ( ) => {
150
+ /* ignore */
151
+ } ,
152
+ ) ;
153
+
154
+ verify ( fakeRequestor . webRequest ( anything ( ) , anyFunction ( ) ) ) ;
155
+
156
+ const [ opts , doneCallback ] = capture ( fakeRequestor . webRequest ) . last ( ) ;
157
+ const reqOpts : request . OptionsWithUri = opts as request . OptionsWithUri ;
158
+
159
+ expect ( receivedTypes ) . to . deep . equal ( [ obj . type ] ) ;
160
+ expect ( receivedObjects ) . to . deep . equal ( [ obj . object ] ) ;
161
+ } ) ;
105
162
} ) ;
0 commit comments