forked from kubernetes-client/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portforward_test.ts
150 lines (123 loc) · 5.76 KB
/
portforward_test.ts
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
import { fail } from 'assert';
import { expect } from 'chai';
import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers';
import { anyFunction, capture, instance, mock, reset, verify } from 'ts-mockito';
import { KubeConfig } from './config';
import { PortForward } from './portforward';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
describe('PortForward', () => {
it('should correctly port-forward to a url', async () => {
const kc = new KubeConfig();
const fakeWebSocket: WebSocketInterface = mock(WebSocketHandler);
const portForward = new PortForward(kc, true, instance(fakeWebSocket));
const osStream = new WritableStreamBuffer();
const errStream = new WritableStreamBuffer();
const isStream = new ReadableStreamBuffer();
const namespace = 'somenamespace';
const pod = 'somepod';
const port = 8080;
await portForward.portForward(namespace, pod, [port], osStream, errStream, isStream);
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/portforward?ports=${port}`;
verify(fakeWebSocket.connect(path, null, anyFunction())).called();
});
it('should not disconnect if disconnectOnErr is false', async () => {
const kc = new KubeConfig();
const fakeWebSocket: WebSocketInterface = mock(WebSocketHandler);
const portForward = new PortForward(kc, false, instance(fakeWebSocket));
const osStream = new WritableStreamBuffer();
const isStream = new ReadableStreamBuffer();
const conn = await portForward.portForward('ns', 'p', [8000], osStream, null, isStream);
const [, , outputFn] = capture(fakeWebSocket.connect).last();
/* tslint:disable:no-unused-expression */
expect(outputFn).to.not.be.null;
// this is redundant but needed for the compiler, sigh...
if (!outputFn) {
return;
}
const buffer = Buffer.alloc(1024, 10);
// unknown stream shouldn't close the socket.
outputFn(2, buffer);
outputFn(0, buffer);
// first time, drop two bytes for the port number.
expect(osStream.size()).to.equal(1022);
});
it('should correctly port-forward streams if err is null', async () => {
const kc = new KubeConfig();
const fakeWebSocket: WebSocketInterface = mock(WebSocketHandler);
const portForward = new PortForward(kc, true, instance(fakeWebSocket));
const osStream = new WritableStreamBuffer();
const isStream = new ReadableStreamBuffer();
await portForward.portForward('ns', 'p', [8000], osStream, null, isStream);
const [, , outputFn] = capture(fakeWebSocket.connect).last();
/* tslint:disable:no-unused-expression */
expect(outputFn).to.not.be.null;
// this is redundant but needed for the compiler, sigh...
if (!outputFn) {
return;
}
const buffer = Buffer.alloc(1024, 10);
// error stream, drop two bytes for the port number.
outputFn(1, buffer);
// error stream is null, expect output to be dropped and nothing to change.
expect(osStream.size()).to.equal(0);
});
it('should correctly port-forward streams', async () => {
const kc = new KubeConfig();
const fakeWebSocket: WebSocketInterface = mock(WebSocketHandler);
const portForward = new PortForward(kc, true, instance(fakeWebSocket));
const osStream = new WritableStreamBuffer();
const errStream = new WritableStreamBuffer();
const isStream = new ReadableStreamBuffer();
await portForward.portForward('ns', 'p', [8000], osStream, errStream, isStream);
const [, , outputFn] = capture(fakeWebSocket.connect).last();
/* tslint:disable:no-unused-expression */
expect(outputFn).to.not.be.null;
// this is redundant but needed for the compiler, sigh...
if (!outputFn) {
return;
}
const buffer = Buffer.alloc(1024, 10);
outputFn(0, buffer);
// first time, drop two bytes for the port number.
expect(osStream.size()).to.equal(1022);
outputFn(0, buffer);
expect(osStream.size()).to.equal(2046);
// error stream, drop two bytes for the port number.
outputFn(1, buffer);
expect(errStream.size()).to.equal(1022);
outputFn(1, buffer);
expect(errStream.size()).to.equal(2046);
// unknown stream, shouldn't change anything.
outputFn(2, buffer);
expect(osStream.size()).to.equal(2046);
expect(errStream.size()).to.equal(2046);
});
it('should throw with no ports or too many', async () => {
const kc = new KubeConfig();
const portForward = new PortForward(kc);
const osStream = new WritableStreamBuffer();
const isStream = new ReadableStreamBuffer();
try {
await portForward.portForward('ns', 'pod', [], osStream, osStream, isStream);
expect(false, 'should have thrown').to.equal(true);
} catch (err) {
if (err instanceof Error) {
expect(err.toString()).to.equal('Error: You must provide at least one port to forward to.');
} else {
fail(`unknown exception: ${err}`);
}
}
try {
await portForward.portForward('ns', 'pod', [1, 2], osStream, osStream, isStream);
expect(false, 'should have thrown').to.equal(true);
} catch (err) {
if (err instanceof Error) {
expect(err.toString()).to.equal(
'Error: Only one port is currently supported for port-forward',
);
} else {
fail(`unknown exception: ${err}`);
}
}
});
});