From 84a57de2e3c1cd32b74fb3ca493c14794268dba4 Mon Sep 17 00:00:00 2001 From: Nikola Jokic Date: Wed, 8 Jun 2022 11:23:05 +0200 Subject: [PATCH] added tests around user volume mounts for prepare job --- packages/k8s/src/k8s/utils.ts | 5 ++++- packages/k8s/tests/prepare-job-test.ts | 28 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/k8s/src/k8s/utils.ts b/packages/k8s/src/k8s/utils.ts index a0bf87f..3951c16 100644 --- a/packages/k8s/src/k8s/utils.ts +++ b/packages/k8s/src/k8s/utils.ts @@ -52,7 +52,10 @@ export function containerVolumes( 'Volume mounts outside of the work folder are not supported' ) } - sourceVolumePath = userVolume.sourceVolumePath.slice(workspacePath.length) + // sourcec volume path should be relative path + sourceVolumePath = userVolume.sourceVolumePath.slice( + workspacePath.length + 1 + ) } else { sourceVolumePath = userVolume.sourceVolumePath } diff --git a/packages/k8s/tests/prepare-job-test.ts b/packages/k8s/tests/prepare-job-test.ts index 25048d0..1992da1 100644 --- a/packages/k8s/tests/prepare-job-test.ts +++ b/packages/k8s/tests/prepare-job-test.ts @@ -40,4 +40,32 @@ describe('Prepare job', () => { const content = fs.readFileSync(prepareJobOutputFilePath) expect(() => JSON.parse(content.toString())).not.toThrow() }) + + it('should prepare job with absolute path for userVolumeMount', async () => { + prepareJobData.args.container.userMountVolumes.forEach(v => { + if (!path.isAbsolute(v.sourceVolumePath)) { + v.sourceVolumePath = path.join( + process.env.GITHUB_WORKSPACE as string, + v.sourceVolumePath + ) + } + }) + await expect( + prepareJob(prepareJobData.args, prepareJobOutputFilePath) + ).resolves.not.toThrow() + }) + + it('should throw an exception if the user volume mount is absolute path outside of GITHUB_WORKSPACE', async () => { + prepareJobData.args.container.userMountVolumes.forEach(v => { + if (!path.isAbsolute(v.sourceVolumePath)) { + v.sourceVolumePath = path.join( + '/path/outside/of/github-workspace', + v.sourceVolumePath + ) + } + }) + await expect( + prepareJob(prepareJobData.args, prepareJobOutputFilePath) + ).rejects.toThrow() + }) })