-
Notifications
You must be signed in to change notification settings - Fork 492
/
cp_test.ts
81 lines (71 loc) · 3.23 KB
/
cp_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
import { anything, anyFunction, instance, mock, verify, when } from 'ts-mockito';
import * as querystring from 'querystring';
import WebSocket = require('isomorphic-ws');
import { CallAwaiter } from '../test';
import { KubeConfig } from './config';
import { Exec } from './exec';
import { Cp } from './cp';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
describe('Cp', () => {
describe('cpFromPod', () => {
it('should run create tar command to a url', async () => {
const kc = new KubeConfig();
const fakeWebSocket: WebSocketInterface = mock(WebSocketHandler);
const exec = new Exec(kc, instance(fakeWebSocket));
const cp = new Cp(kc, exec);
const namespace = 'somenamespace';
const pod = 'somepod';
const container = 'container';
const srcPath = '/';
const tgtPath = '/';
const cmdArray = ['tar', 'zcf', '-', srcPath];
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/exec`;
const query = {
stdout: true,
stderr: true,
stdin: false,
tty: false,
command: cmdArray,
container,
};
const queryStr = querystring.stringify(query);
await cp.cpFromPod(namespace, pod, container, srcPath, tgtPath);
// tslint:disable-next-line:max-line-length
verify(fakeWebSocket.connect(`${path}?${queryStr}`, null, anyFunction())).called();
});
});
describe('cpToPod', () => {
it('should run extract tar command to a url', async () => {
const kc = new KubeConfig();
const fakeWebSocketInterface: WebSocketInterface = mock(WebSocketHandler);
const fakeWebSocket: WebSocket = mock(WebSocket) as WebSocket;
const callAwaiter: CallAwaiter = new CallAwaiter();
const exec = new Exec(kc, instance(fakeWebSocketInterface));
const cp = new Cp(kc, exec);
const namespace = 'somenamespace';
const pod = 'somepod';
const container = 'container';
const srcPath = 'testdata/archive.txt';
const tgtPath = '/';
const cmdArray = ['tar', 'xf', '-', '-C', tgtPath];
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/exec`;
const query = {
stdout: false,
stderr: true,
stdin: true,
tty: false,
command: cmdArray,
container,
};
const queryStr = querystring.stringify(query);
const fakeConn: WebSocket = instance(fakeWebSocket);
when(fakeWebSocketInterface.connect(`${path}?${queryStr}`, null, anyFunction())).thenResolve(
fakeConn,
);
when(fakeWebSocket.send(anything())).thenCall(callAwaiter.resolveCall('send'));
when(fakeWebSocket.close()).thenCall(callAwaiter.resolveCall('close'));
await cp.cpToPod(namespace, pod, container, srcPath, tgtPath);
verify(fakeWebSocketInterface.connect(`${path}?${queryStr}`, null, anyFunction())).called();
});
});
});