Skip to content

Commit e661e29

Browse files
committed
enhanced k8 copyFrom and its testCase
Signed-off-by: Jeromy Cannon <[email protected]>
1 parent 93b6a6f commit e661e29

File tree

3 files changed

+67
-55
lines changed

3 files changed

+67
-55
lines changed

src/core/k8.mjs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -567,12 +567,14 @@ export class K8 {
567567
const destPath = path.join(destDir, srcFile)
568568

569569
// download the tar file to a temp location
570-
const tmpFile = `${this._tempFileFor(srcFile)}.tar`
570+
// const tmpFile = `${this._tempFileFor(srcFile)}.tar`
571+
const tmpFile = this._tempFileFor(srcFile)
571572

572573
const self = this
573574
return new Promise((resolve, reject) => {
574575
const execInstance = new k8s.Exec(this.kubeConfig)
575-
const command = ['tar', 'cf', '-', '-C', srcDir, srcFile]
576+
// const command = ['tar', '-cf', '-', '-C', srcDir, srcFile]
577+
const command = ['cat', `${srcDir}/${srcFile}`]
576578
const outputFileStream = fs.createWriteStream(tmpFile)
577579
const outputPassthroughStream = new stream.PassThrough({ highWaterMark: 10 * 1024 * 1024 })
578580
const errStream = new stream.PassThrough()
@@ -605,7 +607,7 @@ export class K8 {
605607
errStream,
606608
null,
607609
false,
608-
async ({ status }) => {
610+
({ status }) => {
609611
if (status === 'Failure') {
610612
self._deleteTempFile(tmpFile)
611613
const errorMessage = `tar command failed with status Failure while copying from ${podName}:${srcDir}/${srcFile} to ${destPath}`
@@ -630,48 +632,50 @@ export class K8 {
630632
}
631633

632634
outputFileStream.end()
633-
let rejection
634635
outputFileStream.close(() => {
635636
this.logger.debug(`finished closing writerStream copying from ${podName}:${srcDir}/${srcFile} to ${destPath}`)
636637

637638
try {
639+
fs.copyFileSync(tmpFile, destPath)
638640
// extract the downloaded file
639-
tar.x({
640-
file: tmpFile,
641-
cwd: destDir
642-
}).then(() => {
643-
self._deleteTempFile(tmpFile)
644-
645-
const stat = fs.statSync(destPath)
646-
if (stat && stat.size === srcFileSize) {
647-
this.logger.info(`Finished successfully copying from ${podName}:${srcDir}/${srcFile} to ${destPath}`)
641+
// tar.x({
642+
// file: tmpFile,
643+
// cwd: destDir
644+
// }).then(() => {
645+
// self._deleteTempFile(tmpFile)
646+
this.logger.debug(`tmpFile=${tmpFile}`)
647+
648+
const stat = fs.statSync(destPath)
649+
let rejection
650+
if (stat && stat.size === srcFileSize) {
651+
this.logger.info(`Finished successfully copying from ${podName}:${srcDir}/${srcFile} to ${destPath}`)
652+
} else {
653+
rejection = true
654+
if (!stat) {
655+
additionalErrorMessageDetail = ', statSync returned no file status for the destination file'
648656
} else {
649-
rejection = true
650-
if (!stat) {
651-
additionalErrorMessageDetail = ', statSync returned no file status for the destination file'
652-
} else {
653-
additionalErrorMessageDetail = `, stat.size=${stat.size} != srcFileSize=${srcFileSize}`
654-
}
657+
additionalErrorMessageDetail = `, stat.size=${stat.size} != srcFileSize=${srcFileSize}`
655658
}
656-
}).catch((err) => {
657-
this.logger.error(`failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} to download file completely: ${destPath}${additionalErrorMessageDetail}`, err)
658-
rejection = true
659-
})
659+
}
660+
661+
if (rejection) {
662+
const errorMessage = `failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} to download file completely: ${destPath}${additionalErrorMessageDetail}`
663+
this.logger.error(errorMessage)
664+
return reject(new FullstackTestingError(errorMessage))
665+
} else {
666+
return resolve(true)
667+
}
668+
// }).catch((err) => {
669+
// const errorMessage = `failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} to download file completely: ${destPath}${additionalErrorMessageDetail}`
670+
// this.logger.error(errorMessage, err)
671+
// return reject(new FullstackTestingError(errorMessage, err))
672+
// })
660673
} catch (e) {
661674
const errorMessage = `failed to complete copying from ${podName}:${srcDir}/${srcFile} to ${destPath} to extract file: ${destPath}`
662675
this.logger.error(errorMessage, e)
663-
rejection = true
664-
throw new FullstackTestingError(errorMessage, e)
676+
return reject(new FullstackTestingError(errorMessage, e))
665677
}
666678
})
667-
668-
if (rejection) {
669-
const errorMessage = `failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} to download file completely: ${destPath}${additionalErrorMessageDetail}`
670-
this.logger.error(errorMessage)
671-
return reject(new FullstackTestingError(errorMessage))
672-
} else {
673-
return resolve(true)
674-
}
675679
})
676680
})
677681

test/data/build-v0.54.0-alpha.4.zip

87.5 MB
Binary file not shown.

test/e2e/core/k8_e2e.test.mjs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -173,32 +173,40 @@ describe('K8', () => {
173173
await expect(k8.hasDir(podName, containerName, '/tmp')).resolves.toBeTruthy()
174174
}, defaultTimeout)
175175

176-
it('should be able to copy a file to and from a container', async () => {
177-
const pods = await k8.waitForPodReady([`app=${podLabelValue}`], 1, 20)
178-
expect(pods.length).toStrictEqual(1)
179-
const localTmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'k8-test'))
180-
const remoteTmpDir = '/tmp'
181-
const localFilePath = 'test/data/pem/keys/a-private-node0.pem'
182-
const fileName = path.basename(localFilePath)
183-
const remoteFilePath = `${remoteTmpDir}/a-private-node0.pem`
184-
const originalFileHash = crypto.createHash('sha384').update(localFilePath).digest().toString()
185-
const originalStat = fs.statSync(localFilePath)
176+
describe.each([
177+
{ localFilePath: 'test/data/pem/keys/a-private-node0.pem' },
178+
{ localFilePath: 'test/data/build-v0.54.0-alpha.4.zip' }
179+
])('test copyTo and copyFrom', (input) => {
180+
it('should be able to copy a file to and from a container', async () => {
181+
const pods = await k8.waitForPodReady([`app=${podLabelValue}`], 1, 20)
182+
expect(pods.length).toStrictEqual(1)
183+
const localTmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'k8-test'))
184+
const remoteTmpDir = '/tmp'
185+
const localFilePath = input.localFilePath
186+
const fileName = path.basename(localFilePath)
187+
const remoteFilePath = `${remoteTmpDir}/${fileName}`
188+
const originalFileData = fs.readFileSync(localFilePath)
189+
const originalFileHash = crypto.createHash('sha384').update(originalFileData).digest('hex')
190+
const originalStat = fs.statSync(localFilePath)
186191

187-
// upload the file
188-
await expect(k8.copyTo(podName, containerName, localFilePath, remoteTmpDir)).resolves.toBeTruthy()
192+
// upload the file
193+
await expect(k8.copyTo(podName, containerName, localFilePath, remoteTmpDir)).resolves.toBeTruthy()
189194

190-
// download the same file
191-
await expect(k8.copyFrom(podName, containerName, remoteFilePath, localTmpDir)).resolves.toBeTruthy()
192-
const downloadedFilePath = path.join(localTmpDir, fileName)
193-
const downloadedFileHash = crypto.createHash('sha384').update(downloadedFilePath).digest().toString()
194-
const downloadedStat = fs.statSync(downloadedFilePath)
195+
// download the same file
196+
await expect(k8.copyFrom(podName, containerName, remoteFilePath, localTmpDir)).resolves.toBeTruthy()
197+
const downloadedFilePath = path.join(localTmpDir, fileName)
198+
const downloadedFileData = fs.readFileSync(downloadedFilePath)
199+
const downloadedFileHash = crypto.createHash('sha384').update(downloadedFileData).digest('hex')
200+
const downloadedStat = fs.statSync(downloadedFilePath)
195201

196-
expect(downloadedFileHash, 'downloaded file hash should match original file hash').toEqual(originalFileHash)
197-
// rm file inside the container
198-
await expect(k8.execContainer(podName, containerName, ['rm', '-f', remoteFilePath])).resolves
202+
expect(downloadedStat.size, 'downloaded file size should match original file size').toEqual(originalStat.size)
203+
expect(downloadedFileHash, 'downloaded file hash should match original file hash').toEqual(originalFileHash)
204+
// rm file inside the container
205+
await expect(k8.execContainer(podName, containerName, ['rm', '-f', remoteFilePath])).resolves
199206

200-
fs.rmdirSync(localTmpDir, { recursive: true })
201-
}, defaultTimeout)
207+
fs.rmdirSync(localTmpDir, { recursive: true })
208+
}, defaultTimeout)
209+
})
202210

203211
it('should be able to port forward gossip port', (done) => {
204212
const podName = Templates.renderNetworkPodName('node1')

0 commit comments

Comments
 (0)