1
1
import { expect } from 'chai' ;
2
2
import WebSocket = require( 'isomorphic-ws' ) ;
3
3
import { ReadableStreamBuffer , WritableStreamBuffer } from 'stream-buffers' ;
4
- import { anyFunction , capture , instance , mock , verify , when } from 'ts-mockito' ;
4
+ import { anyFunction , anything , capture , instance , mock , verify , when } from 'ts-mockito' ;
5
5
6
+ import { CallAwaiter , matchBuffer , ResizableWriteableStreamBuffer } from '../test' ;
6
7
import { V1Status } from './api' ;
7
8
import { KubeConfig } from './config' ;
8
9
import { Exec } from './exec' ;
10
+ import { TerminalSize } from './terminal-size-queue' ;
9
11
import { WebSocketHandler , WebSocketInterface } from './web-socket-handler' ;
10
12
11
13
describe ( 'Exec' , ( ) => {
@@ -55,9 +57,11 @@ describe('Exec', () => {
55
57
56
58
it ( 'should correctly attach to streams' , async ( ) => {
57
59
const kc = new KubeConfig ( ) ;
58
- const fakeWebSocket : WebSocketInterface = mock ( WebSocketHandler ) ;
59
- const exec = new Exec ( kc , instance ( fakeWebSocket ) ) ;
60
- const osStream = new WritableStreamBuffer ( ) ;
60
+ const fakeWebSocketInterface : WebSocketInterface = mock ( WebSocketHandler ) ;
61
+ const fakeWebSocket : WebSocket = mock ( WebSocket ) ;
62
+ const callAwaiter : CallAwaiter = new CallAwaiter ( ) ;
63
+ const exec = new Exec ( kc , instance ( fakeWebSocketInterface ) ) ;
64
+ const osStream = new ResizableWriteableStreamBuffer ( ) ;
61
65
const errStream = new WritableStreamBuffer ( ) ;
62
66
const isStream = new ReadableStreamBuffer ( ) ;
63
67
@@ -71,8 +75,12 @@ describe('Exec', () => {
71
75
72
76
let statusOut = { } as V1Status ;
73
77
74
- const fakeConn : WebSocket = mock ( WebSocket ) ;
75
- when ( fakeWebSocket . connect ( `${ path } ?${ args } ` , null , anyFunction ( ) ) ) . thenResolve ( fakeConn ) ;
78
+ const fakeConn : WebSocket = instance ( fakeWebSocket ) ;
79
+ when ( fakeWebSocketInterface . connect ( `${ path } ?${ args } ` , null , anyFunction ( ) ) ) . thenResolve (
80
+ fakeConn ,
81
+ ) ;
82
+ when ( fakeWebSocket . send ( anything ( ) ) ) . thenCall ( callAwaiter . resolveCall ( 'send' ) ) ;
83
+ when ( fakeWebSocket . close ( ) ) . thenCall ( callAwaiter . resolveCall ( 'close' ) ) ;
76
84
77
85
await exec . exec (
78
86
namespace ,
@@ -88,7 +96,7 @@ describe('Exec', () => {
88
96
} ,
89
97
) ;
90
98
91
- const [ , , outputFn ] = capture ( fakeWebSocket . connect ) . last ( ) ;
99
+ const [ , , outputFn ] = capture ( fakeWebSocketInterface . connect ) . last ( ) ;
92
100
93
101
/* tslint:disable:no-unused-expression */
94
102
expect ( outputFn ) . to . not . be . null ;
@@ -115,20 +123,41 @@ describe('Exec', () => {
115
123
expect ( buff [ i ] ) . to . equal ( 20 ) ;
116
124
}
117
125
126
+ const initialTerminalSize : TerminalSize = { height : 0 , width : 0 } ;
127
+ await callAwaiter . awaitCall ( 'send' ) ;
128
+ verify (
129
+ fakeWebSocket . send (
130
+ matchBuffer ( WebSocketHandler . ResizeStream , JSON . stringify ( initialTerminalSize ) ) ,
131
+ ) ,
132
+ ) . called ( ) ;
133
+
118
134
const msg = 'This is test data' ;
135
+ const inputPromise = callAwaiter . awaitCall ( 'send' ) ;
119
136
isStream . put ( msg ) ;
120
- verify ( fakeConn . send ( msg ) ) ;
137
+ await inputPromise ;
138
+ verify ( fakeWebSocket . send ( matchBuffer ( WebSocketHandler . StdinStream , msg ) ) ) . called ( ) ;
139
+
140
+ const terminalSize : TerminalSize = { height : 80 , width : 120 } ;
141
+ const resizePromise = callAwaiter . awaitCall ( 'send' ) ;
142
+ osStream . rows = terminalSize . height ;
143
+ osStream . columns = terminalSize . width ;
144
+ osStream . emit ( 'resize' ) ;
145
+ await resizePromise ;
146
+ verify (
147
+ fakeWebSocket . send ( matchBuffer ( WebSocketHandler . ResizeStream , JSON . stringify ( terminalSize ) ) ) ,
148
+ ) . called ( ) ;
121
149
122
150
const statusIn = {
123
151
code : 100 ,
124
152
message : 'this is a test' ,
125
153
} as V1Status ;
126
-
127
154
outputFn ( WebSocketHandler . StatusStream , Buffer . from ( JSON . stringify ( statusIn ) ) ) ;
128
155
expect ( statusOut ) . to . deep . equal ( statusIn ) ;
129
156
157
+ const closePromise = callAwaiter . awaitCall ( 'close' ) ;
130
158
isStream . stop ( ) ;
131
- verify ( fakeConn . close ( ) ) ;
159
+ await closePromise ;
160
+ verify ( fakeWebSocket . close ( ) ) . called ( ) ;
132
161
} ) ;
133
162
} ) ;
134
163
} ) ;
0 commit comments