Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/cp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ export class Cp {
* @param {string} containerName - The name of the container in the pod to exec the command inside.
* @param {string} srcPath - The source path in the pod
* @param {string} tgtPath - The target path in local
* @param {string} [cwd] - The directory that is used as the parent in the pod when downloading
*/
public async cpFromPod(
namespace: string,
podName: string,
containerName: string,
srcPath: string,
tgtPath: string,
cwd?: string,
): Promise<void> {
const command = ['tar', 'zcf', '-', srcPath];
const command = ['tar', 'zcf', '-'];
if (cwd) {
command.push('-C', cwd);
}
command.push(srcPath);
const writerStream = tar.extract(tgtPath);
const errStream = new WritableStreamBuffer();
this.execInstance.exec(
Expand Down
29 changes: 29 additions & 0 deletions src/cp_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,35 @@ describe('Cp', () => {
await cp.cpFromPod(namespace, pod, container, srcPath, tgtPath);
verify(fakeWebSocket.connect(`${path}?${queryStr}`, null, anyFunction())).called();
});

it('should run create tar command to a url with cwd', 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 cwd = '/abc';
const cmdArray = ['tar', 'zcf', '-', '-C', cwd, 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, cwd);
verify(fakeWebSocket.connect(`${path}?${queryStr}`, null, anyFunction())).called();
});
});

describe('cpToPod', () => {
Expand Down