Repository navigation
Expand file tree
/
Copy pathexec_test.ts
More file actions
200 lines (169 loc) · 8.74 KB
/
Copy pathexec_test.ts
File metadata and controls
200 lines (169 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { describe, it } from 'node:test';
import { deepStrictEqual, ok, strictEqual } from 'node:assert';
import { setTimeout as setTimeoutPromise } from 'node:timers/promises';
import WebSocket from 'isomorphic-ws';
import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers';
import { anyFunction, anything, capture, instance, mock, verify, when } from 'ts-mockito';
import { CallAwaiter, matchBuffer, ResizableWriteableStreamBuffer } from './test/index.js';
import { V1Status } from './api.js';
import { KubeConfig } from './config.js';
import { Exec } from './exec.js';
import { TerminalSize } from './terminal-size-queue.js';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler.js';
describe('Exec', () => {
describe('basic', () => {
it('should correctly exec to a url', async () => {
const kc = new KubeConfig();
const fakeWebSocket: WebSocketInterface = mock(WebSocketHandler);
const exec = new Exec(kc, instance(fakeWebSocket));
const osStream = new WritableStreamBuffer();
const errStream = new WritableStreamBuffer();
const isStream = new ReadableStreamBuffer();
const namespace = 'somenamespace';
const pod = 'somepod';
const container = 'container';
const cmd = 'command';
const cmdArray = ['command', 'arg1', 'arg2'];
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/exec`;
await exec.exec(namespace, pod, container, cmd, osStream, errStream, isStream, false);
let args = `stdout=true&stderr=true&stdin=true&tty=false&command=${cmd}&container=${container}`;
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
await exec.exec(namespace, pod, container, cmd, null, errStream, isStream, false);
args = `stdout=false&stderr=true&stdin=true&tty=false&command=${cmd}&container=${container}`;
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
await exec.exec(namespace, pod, container, cmd, null, null, isStream, false);
args = `stdout=false&stderr=false&stdin=true&tty=false&command=${cmd}&container=${container}`;
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
await exec.exec(namespace, pod, container, cmd, null, null, null, false);
args = `stdout=false&stderr=false&stdin=false&tty=false&command=${cmd}&container=${container}`;
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
await exec.exec(namespace, pod, container, cmd, null, errStream, isStream, true);
args = `stdout=false&stderr=true&stdin=true&tty=true&command=${cmd}&container=${container}`;
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
await exec.exec(namespace, pod, container, cmdArray, null, errStream, isStream, true);
args = `stdout=false&stderr=true&stdin=true&tty=true&command=${cmdArray[0]}&command=${cmdArray[1]}&command=${cmdArray[2]}&container=${container}`;
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
});
it('should correctly attach to streams', async () => {
const kc = new KubeConfig();
const fakeWebSocketInterface: WebSocketInterface = mock(WebSocketHandler);
const fakeWebSocket: WebSocket.WebSocket = mock(WebSocket);
const callAwaiter: CallAwaiter = new CallAwaiter();
const exec = new Exec(kc, instance(fakeWebSocketInterface));
const osStream = new ResizableWriteableStreamBuffer();
const errStream = new WritableStreamBuffer();
const isStream = new ReadableStreamBuffer();
const namespace = 'somenamespace';
const pod = 'somepod';
const container = 'somecontainer';
const cmd = 'command';
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/exec`;
const args = `stdout=true&stderr=true&stdin=true&tty=false&command=${cmd}&container=${container}`;
let statusOut = {} as V1Status;
const fakeConn: WebSocket.WebSocket = instance(fakeWebSocket);
when(fakeWebSocketInterface.connect(`${path}?${args}`, null, anyFunction())).thenResolve(
fakeConn,
);
when(fakeWebSocket.send(anything())).thenCall(callAwaiter.resolveCall('send'));
when(fakeWebSocket.close()).thenCall(callAwaiter.resolveCall('close'));
await exec.exec(
namespace,
pod,
container,
cmd,
osStream,
errStream,
isStream,
false,
(status: V1Status) => {
statusOut = status;
},
);
const [, , outputFn] = capture(fakeWebSocketInterface.connect).last();
strictEqual(typeof outputFn, 'function');
// this is redundant but needed for the compiler, sigh...
if (!outputFn) {
return;
}
let buffer = Buffer.alloc(1024, 10);
outputFn(WebSocketHandler.StdoutStream, buffer);
strictEqual(osStream.size(), 1024);
let buff = osStream.getContents() as Buffer;
for (let i = 0; i < 1024; i++) {
strictEqual(buff[i], 10);
}
buffer = Buffer.alloc(1024, 20);
outputFn(WebSocketHandler.StderrStream, buffer);
strictEqual(errStream.size(), 1024);
buff = errStream.getContents() as Buffer;
for (let i = 0; i < 1024; i++) {
strictEqual(buff[i], 20);
}
const initialTerminalSize: TerminalSize = { height: 0, width: 0 };
await callAwaiter.awaitCall('send');
verify(
fakeWebSocket.send(
matchBuffer(WebSocketHandler.ResizeStream, JSON.stringify(initialTerminalSize)),
),
).called();
const msg = 'This is test data';
const inputPromise = callAwaiter.awaitCall('send');
isStream.put(msg);
await inputPromise;
verify(fakeWebSocket.send(matchBuffer(WebSocketHandler.StdinStream, msg))).called();
const terminalSize: TerminalSize = { height: 80, width: 120 };
const resizePromise = callAwaiter.awaitCall('send');
osStream.rows = terminalSize.height;
osStream.columns = terminalSize.width;
osStream.emit('resize');
await resizePromise;
verify(
fakeWebSocket.send(matchBuffer(WebSocketHandler.ResizeStream, JSON.stringify(terminalSize))),
).called();
const statusIn = {
code: 100,
message: 'this is a test',
} as V1Status;
outputFn(WebSocketHandler.StatusStream, Buffer.from(JSON.stringify(statusIn)));
deepStrictEqual(statusOut, statusIn);
const closePromise = callAwaiter.awaitCall('close');
isStream.stop();
await closePromise;
verify(fakeWebSocket.close()).called();
});
it('should optionally send websocket pings', async () => {
const pingIntervalMs = 5;
const waitForPingsMs = 20;
const kc = new KubeConfig();
const pingHandlers: Record<string, () => void> = {};
let pingCount = 0;
const fakeConn = {
readyState: WebSocket.OPEN,
ping: () => {
pingCount++;
},
on: (event: string, listener: () => void) => {
pingHandlers[event] = listener;
},
} as unknown as WebSocket.WebSocket;
const ws: WebSocketInterface = {
connect: async () => fakeConn,
};
const exec = new Exec(kc, ws);
await exec.exec('ns', 'pod', 'container', 'command', null, null, null, false, undefined, {
pingIntervalMs,
});
await setTimeoutPromise(waitForPingsMs);
strictEqual(pingCount, 1);
strictEqual(typeof pingHandlers.pong, 'function');
pingHandlers.pong();
await setTimeoutPromise(waitForPingsMs);
ok(pingCount > 1);
strictEqual(typeof pingHandlers.close, 'function');
pingHandlers.close();
const pingCountAtClose = pingCount;
await setTimeoutPromise(waitForPingsMs);
strictEqual(pingCount, pingCountAtClose);
});
});
});