|
| 1 | +import assert from 'node:assert'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import os from 'node:os'; |
| 5 | +import { setTimeout } from 'node:timers/promises'; |
| 6 | +import { CoreV1Api, KubeConfig, V1Pod } from '../../index.js'; |
| 7 | +import { Cp } from '../../cp.js'; |
| 8 | +import { generateName } from './name.js'; |
| 9 | + |
| 10 | +export default async function cpFromPod() { |
| 11 | + const kc = new KubeConfig(); |
| 12 | + kc.loadFromDefault(); |
| 13 | + |
| 14 | + const coreV1Client = kc.makeApiClient(CoreV1Api); |
| 15 | + const cp = new Cp(kc); |
| 16 | + |
| 17 | + const testPodName = generateName('cp-test-pod'); |
| 18 | + const namespace = 'default'; |
| 19 | + |
| 20 | + const pod = new V1Pod(); |
| 21 | + pod.metadata = { name: testPodName }; |
| 22 | + pod.spec = { |
| 23 | + containers: [ |
| 24 | + { |
| 25 | + name: 'test-container', |
| 26 | + image: 'busybox', |
| 27 | + command: ['sh', '-c', 'echo "test content" > /tmp/test.txt && sleep 3600'], |
| 28 | + }, |
| 29 | + ], |
| 30 | + restartPolicy: 'Never', |
| 31 | + }; |
| 32 | + |
| 33 | + console.log(`Creating pod ${testPodName}`); |
| 34 | + await coreV1Client.createNamespacedPod({ namespace, body: pod }); |
| 35 | + |
| 36 | + console.log('Waiting for pod to be ready...'); |
| 37 | + let podReady = false; |
| 38 | + for (let i = 0; i < 30; i++) { |
| 39 | + const currentPod = await coreV1Client.readNamespacedPod({ name: testPodName, namespace }); |
| 40 | + if (currentPod.status?.phase === 'Running') { |
| 41 | + podReady = true; |
| 42 | + break; |
| 43 | + } |
| 44 | + await setTimeout(1000); |
| 45 | + } |
| 46 | + |
| 47 | + assert.strictEqual(podReady, true, 'Pod did not become ready in time'); |
| 48 | + console.log('Pod is ready'); |
| 49 | + |
| 50 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'k8s-cp-test-')); |
| 51 | + |
| 52 | + try { |
| 53 | + console.log('Copying file from pod...'); |
| 54 | + |
| 55 | + cp.cpFromPod(namespace, testPodName, 'test-container', 'test.txt', tempDir, '/tmp'); |
| 56 | + |
| 57 | + // Wait for file to appear |
| 58 | + const copiedFilePath = path.join(tempDir, 'test.txt'); |
| 59 | + let fileExists = false; |
| 60 | + for (let i = 0; i < 20; i++) { |
| 61 | + if (fs.existsSync(copiedFilePath)) { |
| 62 | + fileExists = true; |
| 63 | + break; |
| 64 | + } |
| 65 | + await setTimeout(500); |
| 66 | + } |
| 67 | + |
| 68 | + assert.strictEqual(fileExists, true, 'File was not copied'); |
| 69 | + |
| 70 | + const content = fs.readFileSync(copiedFilePath, 'utf-8'); |
| 71 | + assert.strictEqual(content.trim(), 'test content', 'File content does not match'); |
| 72 | + |
| 73 | + console.log('cpFromPod test passed!'); |
| 74 | + } finally { |
| 75 | + console.log('Cleaning up...'); |
| 76 | + await coreV1Client.deleteNamespacedPod({ name: testPodName, namespace }); |
| 77 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 78 | + } |
| 79 | +} |
0 commit comments