2022-06-02 15:53:11 -04:00
|
|
|
import * as fs from 'fs'
|
|
|
|
|
import * as path from 'path'
|
|
|
|
|
import { cleanupJob } from '../src/hooks'
|
|
|
|
|
import { prepareJob } from '../src/hooks/prepare-job'
|
2022-06-06 00:21:44 -04:00
|
|
|
import { TestHelper } from './test-setup'
|
2022-06-02 15:53:11 -04:00
|
|
|
|
|
|
|
|
jest.useRealTimers()
|
|
|
|
|
|
2022-06-06 00:21:44 -04:00
|
|
|
let testHelper: TestHelper
|
2022-06-02 15:53:11 -04:00
|
|
|
|
|
|
|
|
let prepareJobData: any
|
|
|
|
|
|
|
|
|
|
let prepareJobOutputFilePath: string
|
|
|
|
|
|
|
|
|
|
describe('Prepare job', () => {
|
2022-06-06 00:21:44 -04:00
|
|
|
beforeEach(async () => {
|
|
|
|
|
testHelper = new TestHelper()
|
|
|
|
|
await testHelper.initialize()
|
2022-06-15 03:41:49 +02:00
|
|
|
prepareJobData = testHelper.getPrepareJobDefinition()
|
2022-06-06 00:21:44 -04:00
|
|
|
prepareJobOutputFilePath = testHelper.createFile('prepare-job-output.json')
|
2022-06-02 15:53:11 -04:00
|
|
|
})
|
|
|
|
|
afterEach(async () => {
|
|
|
|
|
await cleanupJob()
|
2022-06-06 00:21:44 -04:00
|
|
|
await testHelper.cleanup()
|
2022-06-02 15:53:11 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should not throw exception', async () => {
|
|
|
|
|
await expect(
|
|
|
|
|
prepareJob(prepareJobData.args, prepareJobOutputFilePath)
|
|
|
|
|
).resolves.not.toThrow()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should generate output file in JSON format', async () => {
|
|
|
|
|
await prepareJob(prepareJobData.args, prepareJobOutputFilePath)
|
|
|
|
|
const content = fs.readFileSync(prepareJobOutputFilePath)
|
|
|
|
|
expect(() => JSON.parse(content.toString())).not.toThrow()
|
|
|
|
|
})
|
2022-06-08 11:23:05 +02:00
|
|
|
|
|
|
|
|
it('should prepare job with absolute path for userVolumeMount', async () => {
|
2022-06-15 03:41:49 +02:00
|
|
|
prepareJobData.args.container.userMountVolumes = [
|
|
|
|
|
{
|
|
|
|
|
sourceVolumePath: path.join(
|
2022-06-08 11:23:05 +02:00
|
|
|
process.env.GITHUB_WORKSPACE as string,
|
2022-06-15 03:41:49 +02:00
|
|
|
'/myvolume'
|
|
|
|
|
),
|
|
|
|
|
targetVolumePath: '/volume_mount',
|
|
|
|
|
readOnly: false
|
2022-06-08 11:23:05 +02:00
|
|
|
}
|
2022-06-15 03:41:49 +02:00
|
|
|
]
|
2022-06-08 11:23:05 +02:00
|
|
|
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 () => {
|
2022-06-15 03:41:49 +02:00
|
|
|
prepareJobData.args.container.userMountVolumes = [
|
|
|
|
|
{
|
|
|
|
|
sourceVolumePath: '/somewhere/not/in/gh-workspace',
|
|
|
|
|
targetVolumePath: '/containermount',
|
|
|
|
|
readOnly: false
|
2022-06-08 11:23:05 +02:00
|
|
|
}
|
2022-06-15 03:41:49 +02:00
|
|
|
]
|
2022-06-08 11:23:05 +02:00
|
|
|
await expect(
|
|
|
|
|
prepareJob(prepareJobData.args, prepareJobOutputFilePath)
|
|
|
|
|
).rejects.toThrow()
|
|
|
|
|
})
|
2022-06-10 11:07:50 +02:00
|
|
|
|
2022-06-10 16:38:07 +02:00
|
|
|
it('should not run prepare job without the job container', async () => {
|
2022-06-10 11:07:50 +02:00
|
|
|
prepareJobData.args.container = undefined
|
|
|
|
|
await expect(
|
|
|
|
|
prepareJob(prepareJobData.args, prepareJobOutputFilePath)
|
2022-06-10 16:38:07 +02:00
|
|
|
).rejects.toThrow()
|
2022-06-10 11:07:50 +02:00
|
|
|
})
|
2022-06-02 15:53:11 -04:00
|
|
|
})
|