mirror of
https://github.com/actions/runner-container-hooks.git
synced 2025-12-15 01:06:43 +00:00
Initial Commit
This commit is contained in:
31
packages/k8s/tests/cleanup-job-test.ts
Normal file
31
packages/k8s/tests/cleanup-job-test.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import * as path from 'path'
|
||||
import * as fs from 'fs'
|
||||
import { prepareJob, cleanupJob } from '../src/hooks'
|
||||
import { TestTempOutput } from './test-setup'
|
||||
|
||||
let testTempOutput: TestTempOutput
|
||||
|
||||
const prepareJobJsonPath = path.resolve(
|
||||
`${__dirname}/../../../examples/prepare-job.json`
|
||||
)
|
||||
|
||||
let prepareJobOutputFilePath: string
|
||||
|
||||
describe('Cleanup Job', () => {
|
||||
beforeEach(async () => {
|
||||
const prepareJobJson = fs.readFileSync(prepareJobJsonPath)
|
||||
let prepareJobData = JSON.parse(prepareJobJson.toString())
|
||||
|
||||
testTempOutput = new TestTempOutput()
|
||||
testTempOutput.initialize()
|
||||
prepareJobOutputFilePath = testTempOutput.createFile(
|
||||
'prepare-job-output.json'
|
||||
)
|
||||
await prepareJob(prepareJobData.args, prepareJobOutputFilePath)
|
||||
})
|
||||
it('should not throw', async () => {
|
||||
const outputJson = fs.readFileSync(prepareJobOutputFilePath)
|
||||
const outputData = JSON.parse(outputJson.toString())
|
||||
await expect(cleanupJob()).resolves.not.toThrow()
|
||||
})
|
||||
})
|
||||
66
packages/k8s/tests/e2e-test.ts
Normal file
66
packages/k8s/tests/e2e-test.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import {
|
||||
cleanupJob,
|
||||
prepareJob,
|
||||
runContainerStep,
|
||||
runScriptStep
|
||||
} from '../src/hooks'
|
||||
import { TestTempOutput } from './test-setup'
|
||||
|
||||
jest.useRealTimers()
|
||||
|
||||
let testTempOutput: TestTempOutput
|
||||
|
||||
const prepareJobJsonPath = path.resolve(
|
||||
`${__dirname}/../../../../examples/prepare-job.json`
|
||||
)
|
||||
const runScriptStepJsonPath = path.resolve(
|
||||
`${__dirname}/../../../../examples/run-script-step.json`
|
||||
)
|
||||
let runContainerStepJsonPath = path.resolve(
|
||||
`${__dirname}/../../../../examples/run-container-step.json`
|
||||
)
|
||||
|
||||
let prepareJobData: any
|
||||
|
||||
let prepareJobOutputFilePath: string
|
||||
describe('e2e', () => {
|
||||
beforeEach(() => {
|
||||
const prepareJobJson = fs.readFileSync(prepareJobJsonPath)
|
||||
prepareJobData = JSON.parse(prepareJobJson.toString())
|
||||
|
||||
testTempOutput = new TestTempOutput()
|
||||
testTempOutput.initialize()
|
||||
prepareJobOutputFilePath = testTempOutput.createFile(
|
||||
'prepare-job-output.json'
|
||||
)
|
||||
})
|
||||
afterEach(async () => {
|
||||
testTempOutput.cleanup()
|
||||
})
|
||||
it('should prepare job, run script step, run container step then cleanup without errors', async () => {
|
||||
await expect(
|
||||
prepareJob(prepareJobData.args, prepareJobOutputFilePath)
|
||||
).resolves.not.toThrow()
|
||||
|
||||
const scriptStepContent = fs.readFileSync(runScriptStepJsonPath)
|
||||
const scriptStepData = JSON.parse(scriptStepContent.toString())
|
||||
|
||||
const prepareJobOutputJson = fs.readFileSync(prepareJobOutputFilePath)
|
||||
const prepareJobOutputData = JSON.parse(prepareJobOutputJson.toString())
|
||||
|
||||
await expect(
|
||||
runScriptStep(scriptStepData.args, prepareJobOutputData.state, null)
|
||||
).resolves.not.toThrow()
|
||||
|
||||
const runContainerStepContent = fs.readFileSync(runContainerStepJsonPath)
|
||||
const runContainerStepData = JSON.parse(runContainerStepContent.toString())
|
||||
|
||||
await expect(
|
||||
runContainerStep(runContainerStepData.args)
|
||||
).resolves.not.toThrow()
|
||||
|
||||
await expect(cleanupJob()).resolves.not.toThrow()
|
||||
})
|
||||
})
|
||||
47
packages/k8s/tests/prepare-job-test.ts
Normal file
47
packages/k8s/tests/prepare-job-test.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { cleanupJob } from '../src/hooks'
|
||||
import { prepareJob } from '../src/hooks/prepare-job'
|
||||
import { TestTempOutput } from './test-setup'
|
||||
|
||||
jest.useRealTimers()
|
||||
|
||||
let testTempOutput: TestTempOutput
|
||||
|
||||
const prepareJobJsonPath = path.resolve(
|
||||
`${__dirname}/../../../examples/prepare-job.json`
|
||||
)
|
||||
let prepareJobData: any
|
||||
|
||||
let prepareJobOutputFilePath: string
|
||||
|
||||
describe('Prepare job', () => {
|
||||
beforeEach(() => {
|
||||
const prepareJobJson = fs.readFileSync(prepareJobJsonPath)
|
||||
prepareJobData = JSON.parse(prepareJobJson.toString())
|
||||
|
||||
testTempOutput = new TestTempOutput()
|
||||
testTempOutput.initialize()
|
||||
prepareJobOutputFilePath = testTempOutput.createFile(
|
||||
'prepare-job-output.json'
|
||||
)
|
||||
})
|
||||
afterEach(async () => {
|
||||
const outputJson = fs.readFileSync(prepareJobOutputFilePath)
|
||||
const outputData = JSON.parse(outputJson.toString())
|
||||
await cleanupJob()
|
||||
testTempOutput.cleanup()
|
||||
})
|
||||
|
||||
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()
|
||||
})
|
||||
})
|
||||
27
packages/k8s/tests/run-container-step-test.ts
Normal file
27
packages/k8s/tests/run-container-step-test.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { TestTempOutput } from './test-setup'
|
||||
import * as path from 'path'
|
||||
import { runContainerStep } from '../src/hooks'
|
||||
import * as fs from 'fs'
|
||||
|
||||
jest.useRealTimers()
|
||||
|
||||
let testTempOutput: TestTempOutput
|
||||
|
||||
let runContainerStepJsonPath = path.resolve(
|
||||
`${__dirname}/../../../examples/run-container-step.json`
|
||||
)
|
||||
|
||||
let runContainerStepData: any
|
||||
|
||||
describe('Run container step', () => {
|
||||
beforeAll(() => {
|
||||
const content = fs.readFileSync(runContainerStepJsonPath)
|
||||
runContainerStepData = JSON.parse(content.toString())
|
||||
process.env.RUNNER_NAME = 'testjob'
|
||||
})
|
||||
it('should not throw', async () => {
|
||||
await expect(
|
||||
runContainerStep(runContainerStepData.args)
|
||||
).resolves.not.toThrow()
|
||||
})
|
||||
})
|
||||
61
packages/k8s/tests/run-script-step-test.ts
Normal file
61
packages/k8s/tests/run-script-step-test.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { prepareJob, cleanupJob, runScriptStep } from '../src/hooks'
|
||||
import { TestTempOutput } from './test-setup'
|
||||
import * as path from 'path'
|
||||
import * as fs from 'fs'
|
||||
|
||||
jest.useRealTimers()
|
||||
|
||||
let testTempOutput: TestTempOutput
|
||||
|
||||
const prepareJobJsonPath = path.resolve(
|
||||
`${__dirname}/../../../examples/prepare-job.json`
|
||||
)
|
||||
let prepareJobData: any
|
||||
|
||||
let prepareJobOutputFilePath: string
|
||||
let prepareJobOutputData: any
|
||||
|
||||
describe('Run script step', () => {
|
||||
beforeEach(async () => {
|
||||
const prepareJobJson = fs.readFileSync(prepareJobJsonPath)
|
||||
prepareJobData = JSON.parse(prepareJobJson.toString())
|
||||
console.log(prepareJobData)
|
||||
|
||||
testTempOutput = new TestTempOutput()
|
||||
testTempOutput.initialize()
|
||||
prepareJobOutputFilePath = testTempOutput.createFile(
|
||||
'prepare-job-output.json'
|
||||
)
|
||||
await prepareJob(prepareJobData.args, prepareJobOutputFilePath)
|
||||
const outputContent = fs.readFileSync(prepareJobOutputFilePath)
|
||||
prepareJobOutputData = JSON.parse(outputContent.toString())
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await cleanupJob()
|
||||
testTempOutput.cleanup()
|
||||
})
|
||||
|
||||
// NOTE: To use this test, do kubectl apply -f podspec.yaml (from podspec examples)
|
||||
// then change the name of the file to 'run-script-step-test.ts' and do
|
||||
// npm run test run-script-step
|
||||
|
||||
it('should not throw an exception', async () => {
|
||||
const args = {
|
||||
entryPointArgs: ['echo "test"'],
|
||||
entryPoint: '/bin/bash',
|
||||
environmentVariables: {
|
||||
NODE_ENV: 'development'
|
||||
},
|
||||
prependPath: ['/foo/bar', 'bar/foo'],
|
||||
workingDirectory: '/__w/thboop-test2/thboop-test2'
|
||||
}
|
||||
const state = {
|
||||
jobPod: prepareJobOutputData.state.jobPod
|
||||
}
|
||||
const responseFile = null
|
||||
await expect(
|
||||
runScriptStep(args, state, responseFile)
|
||||
).resolves.not.toThrow()
|
||||
})
|
||||
})
|
||||
28
packages/k8s/tests/test-setup.ts
Normal file
28
packages/k8s/tests/test-setup.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import * as fs from 'fs'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
export class TestTempOutput {
|
||||
private tempDirPath: string
|
||||
constructor() {
|
||||
this.tempDirPath = `${__dirname}/_temp/${uuidv4()}`
|
||||
}
|
||||
|
||||
public initialize(): void {
|
||||
fs.mkdirSync(this.tempDirPath, { recursive: true })
|
||||
}
|
||||
|
||||
public cleanup(): void {
|
||||
fs.rmSync(this.tempDirPath, { recursive: true })
|
||||
}
|
||||
|
||||
public createFile(fileName?: string): string {
|
||||
const filePath = `${this.tempDirPath}/${fileName || uuidv4()}`
|
||||
fs.writeFileSync(filePath, '')
|
||||
return filePath
|
||||
}
|
||||
|
||||
public removeFile(fileName: string): void {
|
||||
const filePath = `${this.tempDirPath}/${fileName}`
|
||||
fs.rmSync(filePath)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user