forked from kubernetes-client/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattach_test.ts
More file actions
107 lines (86 loc) · 4.63 KB
/
attach_test.ts
File metadata and controls
107 lines (86 loc) · 4.63 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
import { expect } from 'chai';
import WebSocket = require('isomorphic-ws');
import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers';
import { anyFunction, anything, capture, instance, mock, verify, when } from 'ts-mockito';
import { Attach } from './attach';
import { KubeConfig } from './config';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
describe('Attach', () => {
describe('basic', () => {
it('should correctly attach to a url', async () => {
const kc = new KubeConfig();
const fakeWebSocket: WebSocketInterface = mock(WebSocketHandler);
const attach = new Attach(kc, instance(fakeWebSocket));
const osStream = new WritableStreamBuffer();
const errStream = new WritableStreamBuffer();
const isStream = new ReadableStreamBuffer();
const namespace = 'somenamespace';
const pod = 'somepod';
const container = 'somecontainer';
await attach.attach(
namespace, pod, container, osStream, errStream, isStream, false);
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/attach`;
let args = `container=${container}&stderr=true&stdin=true&stdout=true&tty=false`;
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
await attach.attach(
namespace, pod, container, null, null, null, false);
args = `container=${container}&stderr=false&stdin=false&stdout=false&tty=false`;
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
await attach.attach(
namespace, pod, container, osStream, null, null, false);
args = `container=${container}&stderr=false&stdin=false&stdout=true&tty=false`;
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
await attach.attach(
namespace, pod, container, osStream, errStream, null, false);
args = `container=${container}&stderr=true&stdin=false&stdout=true&tty=false`;
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
await attach.attach(
namespace, pod, container, osStream, errStream, null, true);
args = `container=${container}&stderr=true&stdin=false&stdout=true&tty=true`;
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
});
it('should correctly attach to streams', async () => {
const kc = new KubeConfig();
const fakeWebSocket: WebSocketInterface = mock(WebSocketHandler);
const attach = new Attach(kc, instance(fakeWebSocket));
const osStream = new WritableStreamBuffer();
const errStream = new WritableStreamBuffer();
const isStream = new ReadableStreamBuffer();
const namespace = 'somenamespace';
const pod = 'somepod';
const container = 'somecontainer';
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/attach`;
const args = `container=${container}&stderr=true&stdin=true&stdout=true&tty=false`;
const fakeConn: WebSocket = mock(WebSocket);
when(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).thenResolve(fakeConn);
await attach.attach(
namespace, pod, container, osStream, errStream, isStream, false);
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;
}
let buffer = Buffer.alloc(1024, 10);
outputFn(WebSocketHandler.StdoutStream, buffer);
expect(osStream.size()).to.equal(1024);
let buff = osStream.getContents() as Buffer;
for (let i = 0; i < 1024; i++) {
expect(buff[i]).to.equal(10);
}
buffer = Buffer.alloc(1024, 20);
outputFn(WebSocketHandler.StderrStream, buffer);
expect(errStream.size()).to.equal(1024);
buff = errStream.getContents() as Buffer;
for (let i = 0; i < 1024; i++) {
expect(buff[i]).to.equal(20);
}
const msg = 'This is test data';
isStream.put(msg);
verify(fakeConn.send(msg));
isStream.stop();
verify(fakeConn.close());
});
});
});