mirror of
https://github.com/actions/runner-container-hooks.git
synced 2025-12-14 08:36:45 +00:00
refactored tests to be easier to follow
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import {
|
||||
cleanupJob,
|
||||
prepareJob,
|
||||
@@ -9,94 +8,83 @@ import {
|
||||
} from '../src/hooks'
|
||||
import TestSetup from './test-setup'
|
||||
|
||||
const prepareJobJson = fs.readFileSync(
|
||||
path.resolve(__dirname + '/../../../examples/prepare-job.json'),
|
||||
'utf8'
|
||||
)
|
||||
const definitions = {
|
||||
prepareJob: JSON.parse(
|
||||
fs.readFileSync(
|
||||
path.resolve(__dirname + '/../../../examples/prepare-job.json'),
|
||||
'utf8'
|
||||
)
|
||||
),
|
||||
|
||||
const containerStepJson = fs.readFileSync(
|
||||
path.resolve(__dirname + '/../../../examples/run-container-step.json'),
|
||||
'utf8'
|
||||
)
|
||||
runContainerStep: JSON.parse(
|
||||
fs.readFileSync(
|
||||
path.resolve(__dirname + '/../../../examples/run-container-step.json'),
|
||||
'utf8'
|
||||
)
|
||||
),
|
||||
|
||||
const tmpOutputDir = `${__dirname}/_temp/${uuidv4()}`
|
||||
|
||||
let prepareJobData: any
|
||||
let scriptStepJson: any
|
||||
let scriptStepData: any
|
||||
let containerStepData: any
|
||||
|
||||
let prepareJobOutputFilePath: string
|
||||
runScriptStep: JSON.parse(
|
||||
fs.readFileSync(
|
||||
path.resolve(__dirname + '/../../../examples/run-script-step.json'),
|
||||
'utf-8'
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
let testSetup: TestSetup
|
||||
|
||||
describe('e2e', () => {
|
||||
beforeAll(() => {
|
||||
fs.mkdirSync(tmpOutputDir, { recursive: true })
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
fs.rmSync(tmpOutputDir, { recursive: true })
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
// init dirs
|
||||
testSetup = new TestSetup()
|
||||
testSetup.initialize()
|
||||
|
||||
prepareJobData = JSON.parse(prepareJobJson)
|
||||
prepareJobData.args.container.userMountVolumes = testSetup.userMountVolumes
|
||||
prepareJobData.args.container.systemMountVolumes =
|
||||
definitions.prepareJob.args.container.systemMountVolumes =
|
||||
testSetup.systemMountVolumes
|
||||
prepareJobData.args.container.workingDirectory = testSetup.workingDirectory
|
||||
|
||||
scriptStepJson = fs.readFileSync(
|
||||
path.resolve(__dirname + '/../../../examples/run-script-step.json'),
|
||||
'utf8'
|
||||
)
|
||||
scriptStepData = JSON.parse(scriptStepJson)
|
||||
scriptStepData.args.workingDirectory = testSetup.workingDirectory
|
||||
|
||||
containerStepData = JSON.parse(containerStepJson)
|
||||
containerStepData.args.workingDirectory = testSetup.workingDirectory
|
||||
containerStepData.args.userMountVolumes = testSetup.userMountVolumes
|
||||
containerStepData.args.systemMountVolumes = testSetup.systemMountVolumes
|
||||
|
||||
prepareJobOutputFilePath = `${tmpOutputDir}/prepare-job-output-${uuidv4()}.json`
|
||||
fs.writeFileSync(prepareJobOutputFilePath, '')
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
fs.rmSync(prepareJobOutputFilePath, { force: true })
|
||||
testSetup.teardown()
|
||||
})
|
||||
|
||||
it('should prepare job, then run script step, then run container step then cleanup', async () => {
|
||||
const prepareJobOutput = testSetup.createOutputFile(
|
||||
'prepare-job-output.json'
|
||||
)
|
||||
|
||||
await expect(
|
||||
prepareJob(prepareJobData.args, prepareJobOutputFilePath)
|
||||
prepareJob(definitions.prepareJob.args, prepareJobOutput)
|
||||
).resolves.not.toThrow()
|
||||
let rawState = fs.readFileSync(prepareJobOutputFilePath, 'utf-8')
|
||||
|
||||
let rawState = fs.readFileSync(prepareJobOutput, 'utf-8')
|
||||
let resp = JSON.parse(rawState)
|
||||
|
||||
await expect(
|
||||
runScriptStep(scriptStepData.args, resp.state)
|
||||
runScriptStep(definitions.runScriptStep.args, resp.state)
|
||||
).resolves.not.toThrow()
|
||||
|
||||
await expect(
|
||||
runContainerStep(containerStepData.args, resp.state)
|
||||
runContainerStep(definitions.runContainerStep.args, resp.state)
|
||||
).resolves.not.toThrow()
|
||||
|
||||
await expect(cleanupJob()).resolves.not.toThrow()
|
||||
})
|
||||
|
||||
it('should prepare job, then run script step, then run container step with Dockerfile then cleanup', async () => {
|
||||
const prepareJobOutput = testSetup.createOutputFile(
|
||||
'prepare-job-output.json'
|
||||
)
|
||||
|
||||
await expect(
|
||||
prepareJob(prepareJobData.args, prepareJobOutputFilePath)
|
||||
).resolves.not.toThrow()
|
||||
let rawState = fs.readFileSync(prepareJobOutputFilePath, 'utf-8')
|
||||
let resp = JSON.parse(rawState)
|
||||
await expect(
|
||||
runScriptStep(scriptStepData.args, resp.state)
|
||||
prepareJob(definitions.prepareJob.args, prepareJobOutput)
|
||||
).resolves.not.toThrow()
|
||||
|
||||
const dockerfilePath = `${tmpOutputDir}/Dockerfile`
|
||||
let rawState = fs.readFileSync(prepareJobOutput, 'utf-8')
|
||||
let resp = JSON.parse(rawState)
|
||||
|
||||
await expect(
|
||||
runScriptStep(definitions.runScriptStep.args, resp.state)
|
||||
).resolves.not.toThrow()
|
||||
|
||||
const dockerfilePath = `${testSetup.workingDirectory}/Dockerfile`
|
||||
fs.writeFileSync(
|
||||
dockerfilePath,
|
||||
`FROM ubuntu:latest
|
||||
@@ -104,13 +92,17 @@ ENV TEST=test
|
||||
ENTRYPOINT [ "tail", "-f", "/dev/null" ]
|
||||
`
|
||||
)
|
||||
const containerStepDataCopy = JSON.parse(JSON.stringify(containerStepData))
|
||||
process.env.GITHUB_WORKSPACE = tmpOutputDir
|
||||
|
||||
const containerStepDataCopy = JSON.parse(
|
||||
JSON.stringify(definitions.runContainerStep)
|
||||
)
|
||||
|
||||
containerStepDataCopy.args.dockerfile = 'Dockerfile'
|
||||
containerStepDataCopy.args.context = '.'
|
||||
|
||||
await expect(
|
||||
runContainerStep(containerStepDataCopy.args, resp.state)
|
||||
).resolves.not.toThrow()
|
||||
|
||||
await expect(cleanupJob()).resolves.not.toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user