refactored tests and added docker build test, repaired state.network

This commit is contained in:
Nikola Jokic
2022-06-03 14:10:15 +02:00
parent 8bc1fbbec5
commit 4b7efe88ef
9 changed files with 204 additions and 113 deletions

View File

@@ -7,7 +7,6 @@ import {
ServiceContainerInfo, ServiceContainerInfo,
StepContainerInfo StepContainerInfo
} from 'hooklib/lib' } from 'hooklib/lib'
import path from 'path'
import { env } from 'process' import { env } from 'process'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import { runDockerCommand, RunDockerCommandOptions } from '../utils' import { runDockerCommand, RunDockerCommandOptions } from '../utils'
@@ -146,17 +145,43 @@ export async function containerBuild(
args: RunContainerStepArgs, args: RunContainerStepArgs,
tag: string tag: string
): Promise<void> { ): Promise<void> {
const context = path.dirname(`${env.GITHUB_WORKSPACE}/${args.dockerfile}`) if (!args.dockerfile) {
throw new Error('Container build expets args.dockerfile to be set')
}
const dockerArgs: string[] = ['build'] const dockerArgs: string[] = ['build']
dockerArgs.push('-t', tag) dockerArgs.push('-t', tag)
dockerArgs.push('-f', `${env.GITHUB_WORKSPACE}/${args.dockerfile}`) dockerArgs.push('-f', args.dockerfile)
dockerArgs.push(context) dockerArgs.push(getBuildContext(args.dockerfile))
// TODO: figure out build working directory // TODO: figure out build working directory
await runDockerCommand(dockerArgs, { await runDockerCommand(dockerArgs, {
workingDir: args['buildWorkingDirectory'] workingDir: getWorkingDir(args.dockerfile)
}) })
} }
function getBuildContext(dockerfilePath: string): string {
const pathSplit = dockerfilePath.split('/')
pathSplit.splice(pathSplit.length - 1)
return pathSplit.join('/')
}
function getWorkingDir(dockerfilePath: string): string {
const workspace = env.GITHUB_WORKSPACE as string
let workingDir = workspace
if (!dockerfilePath?.includes(workspace)) {
// This is container action
const pathSplit = dockerfilePath.split('/')
const actionIndex = pathSplit?.findIndex(d => d === '_actions')
if (actionIndex) {
const actionSubdirectoryDepth = 3 // handle + repo + [branch | tag]
pathSplit.splice(actionIndex + actionSubdirectoryDepth + 1)
workingDir = pathSplit.join('/')
}
}
return workingDir
}
export async function containerLogs(id: string): Promise<void> { export async function containerLogs(id: string): Promise<void> {
const dockerArgs: string[] = ['logs'] const dockerArgs: string[] = ['logs']
dockerArgs.push('--details') dockerArgs.push('--details')
@@ -330,7 +355,7 @@ export async function containerExecStep(
export async function containerRun( export async function containerRun(
args: RunContainerStepArgs, args: RunContainerStepArgs,
name: string, name: string,
network: string network?: string
): Promise<void> { ): Promise<void> {
if (!args.image) { if (!args.image) {
throw new Error('expected image to be set') throw new Error('expected image to be set')
@@ -340,7 +365,9 @@ export async function containerRun(
dockerArgs.push('--name', name) dockerArgs.push('--name', name)
dockerArgs.push(`--workdir=${args.workingDirectory}`) dockerArgs.push(`--workdir=${args.workingDirectory}`)
dockerArgs.push(`--label=${getRunnerLabel()}`) dockerArgs.push(`--label=${getRunnerLabel()}`)
dockerArgs.push(`--network=${network}`) if (network) {
dockerArgs.push(`--network=${network}`)
}
if (args.createOptions) { if (args.createOptions) {
dockerArgs.push(...args.createOptions.split(' ')) dockerArgs.push(...args.createOptions.split(' '))

View File

@@ -1,13 +1,12 @@
import { RunContainerStepArgs } from 'hooklib/lib'
import { v4 as uuidv4 } from 'uuid'
import { import {
containerBuild, containerBuild,
registryLogin,
registryLogout,
containerPull, containerPull,
containerRun containerRun,
registryLogin,
registryLogout
} from '../dockerCommands' } from '../dockerCommands'
import { v4 as uuidv4 } from 'uuid'
import * as core from '@actions/core'
import { RunContainerStepArgs } from 'hooklib/lib'
import { getRunnerLabel } from '../dockerCommands/constants' import { getRunnerLabel } from '../dockerCommands/constants'
export async function runContainerStep( export async function runContainerStep(
@@ -15,23 +14,23 @@ export async function runContainerStep(
state state
): Promise<void> { ): Promise<void> {
const tag = generateBuildTag() // for docker build const tag = generateBuildTag() // for docker build
if (!args.image) { if (args.image) {
core.error('expected an image') const configLocation = await registryLogin(args)
} else { try {
if (args.dockerfile) { await containerPull(args.image, configLocation)
await containerBuild(args, tag) } finally {
args.image = tag await registryLogout(configLocation)
} else {
const configLocation = await registryLogin(args)
try {
await containerPull(args.image, configLocation)
} finally {
await registryLogout(configLocation)
}
} }
} else if (args.dockerfile) {
await containerBuild(args, tag)
args.image = tag
} else {
throw new Error(
'run container step should have image or dockerfile fields specified'
)
} }
// container will get pruned at the end of the job based on the label, no need to cleanup here // container will get pruned at the end of the job based on the label, no need to cleanup here
await containerRun(args, tag.split(':')[1], state.network) await containerRun(args, tag.split(':')[1], state?.network)
} }
function generateBuildTag(): string { function generateBuildTag(): string {

View File

@@ -13,6 +13,7 @@ import {
runContainerStep, runContainerStep,
runScriptStep runScriptStep
} from './hooks' } from './hooks'
import { checkEnvironment } from './utils'
async function run(): Promise<void> { async function run(): Promise<void> {
const input = await getInputFromStdin() const input = await getInputFromStdin()
@@ -23,6 +24,7 @@ async function run(): Promise<void> {
const state = input['state'] const state = input['state']
try { try {
checkEnvironment()
switch (command) { switch (command) {
case Command.PrepareJob: case Command.PrepareJob:
await prepareJob(args as PrepareJobArgs, responseFile) await prepareJob(args as PrepareJobArgs, responseFile)

View File

@@ -2,6 +2,7 @@
/* eslint-disable @typescript-eslint/no-require-imports */ /* eslint-disable @typescript-eslint/no-require-imports */
/* eslint-disable import/no-commonjs */ /* eslint-disable import/no-commonjs */
import * as core from '@actions/core' import * as core from '@actions/core'
import { env } from 'process'
// Import this way otherwise typescript has errors // Import this way otherwise typescript has errors
const exec = require('@actions/exec') const exec = require('@actions/exec')
@@ -42,6 +43,12 @@ export function sanitize(val: string): string {
return newNameBuilder.join('') return newNameBuilder.join('')
} }
export function checkEnvironment(): void {
if (!env.GITHUB_WORKSPACE) {
throw new Error('GITHUB_WORKSPACE is not set')
}
}
// isAlpha accepts single character and checks if // isAlpha accepts single character and checks if
// that character is [a-zA-Z] // that character is [a-zA-Z]
function isAlpha(val: string): boolean { function isAlpha(val: string): boolean {

View File

@@ -1,47 +1,41 @@
import { prepareJob, cleanupJob } from '../src/hooks'
import { v4 as uuidv4 } from 'uuid'
import * as fs from 'fs' import * as fs from 'fs'
import * as path from 'path' import * as path from 'path'
import { v4 as uuidv4 } from 'uuid'
import { cleanupJob, prepareJob } from '../src/hooks'
import TestSetup from './test-setup' import TestSetup from './test-setup'
const prepareJobInputPath = path.resolve( const prepareJobInputPath = path.resolve(
`${__dirname}/../../../examples/prepare-job.json` `${__dirname}/../../../examples/prepare-job.json`
) )
const tmpOutputDir = `${__dirname}/${uuidv4()}`
let prepareJobOutputPath: string let prepareJobOutputPath: string
let prepareJobData: any let prepareJobDefinition: any
let testSetup: TestSetup let testSetup: TestSetup
jest.useRealTimers() jest.useRealTimers()
describe('cleanup job', () => { describe('cleanup job', () => {
beforeAll(() => {
fs.mkdirSync(tmpOutputDir, { recursive: true })
})
afterAll(() => {
fs.rmSync(tmpOutputDir, { recursive: true })
})
beforeEach(async () => { beforeEach(async () => {
const prepareJobRawData = fs.readFileSync(prepareJobInputPath, 'utf8')
prepareJobData = JSON.parse(prepareJobRawData.toString())
prepareJobOutputPath = `${tmpOutputDir}/prepare-job-output-${uuidv4()}.json`
fs.writeFileSync(prepareJobOutputPath, '')
testSetup = new TestSetup() testSetup = new TestSetup()
testSetup.initialize() testSetup.initialize()
prepareJobData.args.container.userMountVolumes = testSetup.userMountVolumes const prepareJobRawData = fs.readFileSync(prepareJobInputPath, 'utf8')
prepareJobData.args.container.systemMountVolumes = prepareJobDefinition = JSON.parse(prepareJobRawData.toString())
testSetup.systemMountVolumes
prepareJobData.args.container.workingDirectory = testSetup.workingDirectory
await prepareJob(prepareJobData.args, prepareJobOutputPath) prepareJobOutputPath = `${
testSetup.testDir
}/prepare-job-output-${uuidv4()}.json`
fs.writeFileSync(prepareJobOutputPath, '')
prepareJobDefinition.args.container.userMountVolumes =
testSetup.userMountVolumes
prepareJobDefinition.args.container.systemMountVolumes =
testSetup.systemMountVolumes
prepareJobDefinition.args.container.workingDirectory =
testSetup.containerWorkingDirectory
await prepareJob(prepareJobDefinition.args, prepareJobOutputPath)
}) })
afterEach(() => { afterEach(() => {
@@ -56,7 +50,7 @@ describe('cleanup job', () => {
) )
const parsedPrepareJobOutput = JSON.parse(prepareJobOutputContent) const parsedPrepareJobOutput = JSON.parse(prepareJobOutputContent)
await expect( await expect(
cleanupJob(prepareJobData.args, parsedPrepareJobOutput.state, null) cleanupJob(prepareJobDefinition.args, parsedPrepareJobOutput.state, null)
).resolves.not.toThrow() ).resolves.not.toThrow()
}) })
}) })

View File

@@ -0,0 +1,33 @@
import * as fs from 'fs'
import { containerBuild } from '../src/dockerCommands'
import TestSetup from './test-setup'
let testSetup
let runContainerStepDefinition
const runContainerStepInputPath = `${__dirname}/../../../examples/run-container-step.json`
describe('container build', () => {
beforeEach(() => {
testSetup = new TestSetup()
testSetup.initialize()
let runContainerStepJson = fs.readFileSync(
runContainerStepInputPath,
'utf8'
)
runContainerStepDefinition = JSON.parse(runContainerStepJson.toString())
runContainerStepDefinition.image = ''
const actionPath = testSetup.initializeDockerAction()
runContainerStepDefinition.dockerfile = `${actionPath}/Dockerfile`
})
afterEach(() => {
testSetup.teardown()
})
it('should build container', async () => {
await expect(
containerBuild(runContainerStepDefinition, 'example-test-tag')
).resolves.not.toThrow()
})
})

View File

@@ -1,12 +1,12 @@
import {
prepareJob,
cleanupJob,
runScriptStep,
runContainerStep
} from '../src/hooks'
import * as fs from 'fs' import * as fs from 'fs'
import * as path from 'path' import * as path from 'path'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import {
cleanupJob,
prepareJob,
runContainerStep,
runScriptStep
} from '../src/hooks'
import TestSetup from './test-setup' import TestSetup from './test-setup'
const prepareJobJson = fs.readFileSync( const prepareJobJson = fs.readFileSync(
@@ -21,10 +21,9 @@ const containerStepJson = fs.readFileSync(
const tmpOutputDir = `${__dirname}/_temp/${uuidv4()}` const tmpOutputDir = `${__dirname}/_temp/${uuidv4()}`
let prepareJobData: any let prepareJobDefinition: any
let scriptStepJson: any let scriptStepDefinition: any
let scriptStepData: any let runContainerStepDefinition: any
let containerStepData: any
let prepareJobOutputFilePath: string let prepareJobOutputFilePath: string
@@ -44,23 +43,29 @@ describe('e2e', () => {
testSetup = new TestSetup() testSetup = new TestSetup()
testSetup.initialize() testSetup.initialize()
prepareJobData = JSON.parse(prepareJobJson) prepareJobDefinition = JSON.parse(prepareJobJson)
prepareJobData.args.container.userMountVolumes = testSetup.userMountVolumes prepareJobDefinition.args.container.userMountVolumes =
prepareJobData.args.container.systemMountVolumes = testSetup.userMountVolumes
prepareJobDefinition.args.container.systemMountVolumes =
testSetup.systemMountVolumes testSetup.systemMountVolumes
prepareJobData.args.container.workingDirectory = testSetup.workingDirectory prepareJobDefinition.args.container.workingDirectory =
testSetup.containerWorkingDirectory
scriptStepJson = fs.readFileSync( const scriptStepJson = fs.readFileSync(
path.resolve(__dirname + '/../../../examples/run-script-step.json'), path.resolve(__dirname + '/../../../examples/run-script-step.json'),
'utf8' 'utf8'
) )
scriptStepData = JSON.parse(scriptStepJson) scriptStepDefinition = JSON.parse(scriptStepJson)
scriptStepData.args.workingDirectory = testSetup.workingDirectory scriptStepDefinition.args.workingDirectory =
testSetup.containerWorkingDirectory
containerStepData = JSON.parse(containerStepJson) runContainerStepDefinition = JSON.parse(containerStepJson)
containerStepData.args.workingDirectory = testSetup.workingDirectory runContainerStepDefinition.args.workingDirectory =
containerStepData.args.userMountVolumes = testSetup.userMountVolumes testSetup.containerWorkingDirectory
containerStepData.args.systemMountVolumes = testSetup.systemMountVolumes runContainerStepDefinition.args.userMountVolumes =
testSetup.userMountVolumes
runContainerStepDefinition.args.systemMountVolumes =
testSetup.systemMountVolumes
prepareJobOutputFilePath = `${tmpOutputDir}/prepare-job-output-${uuidv4()}.json` prepareJobOutputFilePath = `${tmpOutputDir}/prepare-job-output-${uuidv4()}.json`
fs.writeFileSync(prepareJobOutputFilePath, '') fs.writeFileSync(prepareJobOutputFilePath, '')
@@ -73,27 +78,27 @@ describe('e2e', () => {
it('should prepare job, then run script step, then run container step then cleanup', async () => { it('should prepare job, then run script step, then run container step then cleanup', async () => {
await expect( await expect(
prepareJob(prepareJobData.args, prepareJobOutputFilePath) prepareJob(prepareJobDefinition.args, prepareJobOutputFilePath)
).resolves.not.toThrow() ).resolves.not.toThrow()
let rawState = fs.readFileSync(prepareJobOutputFilePath, 'utf-8') let rawState = fs.readFileSync(prepareJobOutputFilePath, 'utf-8')
let resp = JSON.parse(rawState) let resp = JSON.parse(rawState)
await expect( await expect(
runScriptStep(scriptStepData.args, resp.state) runScriptStep(scriptStepDefinition.args, resp.state)
).resolves.not.toThrow() ).resolves.not.toThrow()
await expect( await expect(
runContainerStep(containerStepData.args, resp.state) runContainerStep(runContainerStepDefinition.args, resp.state)
).resolves.not.toThrow() ).resolves.not.toThrow()
await expect(cleanupJob(resp, resp.state, null)).resolves.not.toThrow() await expect(cleanupJob(resp, resp.state, null)).resolves.not.toThrow()
}) })
it('should prepare job, then run script step, then run container step with Dockerfile then cleanup', async () => { it('should prepare job, then run script step, then run container step with Dockerfile then cleanup', async () => {
await expect( await expect(
prepareJob(prepareJobData.args, prepareJobOutputFilePath) prepareJob(prepareJobDefinition.args, prepareJobOutputFilePath)
).resolves.not.toThrow() ).resolves.not.toThrow()
let rawState = fs.readFileSync(prepareJobOutputFilePath, 'utf-8') let rawState = fs.readFileSync(prepareJobOutputFilePath, 'utf-8')
let resp = JSON.parse(rawState) let resp = JSON.parse(rawState)
await expect( await expect(
runScriptStep(scriptStepData.args, resp.state) runScriptStep(scriptStepDefinition.args, resp.state)
).resolves.not.toThrow() ).resolves.not.toThrow()
const dockerfilePath = `${tmpOutputDir}/Dockerfile` const dockerfilePath = `${tmpOutputDir}/Dockerfile`
@@ -104,7 +109,9 @@ ENV TEST=test
ENTRYPOINT [ "tail", "-f", "/dev/null" ] ENTRYPOINT [ "tail", "-f", "/dev/null" ]
` `
) )
const containerStepDataCopy = JSON.parse(JSON.stringify(containerStepData)) const containerStepDataCopy = JSON.parse(
JSON.stringify(runContainerStepDefinition)
)
process.env.GITHUB_WORKSPACE = tmpOutputDir process.env.GITHUB_WORKSPACE = tmpOutputDir
containerStepDataCopy.args.dockerfile = 'Dockerfile' containerStepDataCopy.args.dockerfile = 'Dockerfile'
containerStepDataCopy.args.context = '.' containerStepDataCopy.args.context = '.'

View File

@@ -7,20 +7,11 @@ jest.useRealTimers()
let prepareJobOutputPath: string let prepareJobOutputPath: string
let prepareJobData: any let prepareJobData: any
const tmpOutputDir = `${__dirname}/_temp/${uuidv4()}`
const prepareJobInputPath = `${__dirname}/../../../examples/prepare-job.json` const prepareJobInputPath = `${__dirname}/../../../examples/prepare-job.json`
let testSetup: TestSetup let testSetup: TestSetup
describe('prepare job', () => { describe('prepare job', () => {
beforeAll(() => {
fs.mkdirSync(tmpOutputDir, { recursive: true })
})
afterAll(() => {
fs.rmSync(tmpOutputDir, { recursive: true })
})
beforeEach(async () => { beforeEach(async () => {
testSetup = new TestSetup() testSetup = new TestSetup()
testSetup.initialize() testSetup.initialize()
@@ -31,9 +22,12 @@ describe('prepare job', () => {
prepareJobData.args.container.userMountVolumes = testSetup.userMountVolumes prepareJobData.args.container.userMountVolumes = testSetup.userMountVolumes
prepareJobData.args.container.systemMountVolumes = prepareJobData.args.container.systemMountVolumes =
testSetup.systemMountVolumes testSetup.systemMountVolumes
prepareJobData.args.container.workingDirectory = testSetup.workingDirectory prepareJobData.args.container.workingDirectory =
testSetup.containerWorkingDirectory
prepareJobOutputPath = `${tmpOutputDir}/prepare-job-output-${uuidv4()}.json` prepareJobOutputPath = `${
testSetup.testDir
}/prepare-job-output-${uuidv4()}.json`
fs.writeFileSync(prepareJobOutputPath, '') fs.writeFileSync(prepareJobOutputPath, '')
}) })

View File

@@ -1,11 +1,12 @@
import * as fs from 'fs' import * as fs from 'fs'
import { v4 as uuidv4 } from 'uuid'
import { env } from 'process'
import { Mount } from 'hooklib' import { Mount } from 'hooklib'
import { env } from 'process'
import { v4 as uuidv4 } from 'uuid'
export default class TestSetup { export default class TestSetup {
private testdir: string private testdir: string
private runnerMockDir: string private runnerMockDir: string
private runnerMockSubdirs = { private runnerMockSubdirs = {
work: '_work', work: '_work',
externals: 'externals', externals: 'externals',
@@ -15,36 +16,20 @@ export default class TestSetup {
githubHome: '_work/_temp/_github_home', githubHome: '_work/_temp/_github_home',
githubWorkflow: '_work/_temp/_github_workflow' githubWorkflow: '_work/_temp/_github_workflow'
} }
private readonly projectName = 'test'
private readonly projectName = 'example'
constructor() { constructor() {
this.testdir = `${__dirname}/_temp/${uuidv4()}` this.testdir = `${__dirname}/_temp/${uuidv4()}`
this.runnerMockDir = `${this.testdir}/runner/_layout` this.runnerMockDir = `${this.testdir}/runner/_layout`
} }
private get allTestDirectories() {
const resp = [this.testdir, this.runnerMockDir]
for (const [key, value] of Object.entries(this.runnerMockSubdirs)) {
resp.push(`${this.runnerMockDir}/${value}`)
}
resp.push(
`${this.runnerMockDir}/_work/${this.projectName}/${this.projectName}`
)
return resp
}
public initialize(): void { public initialize(): void {
for (const dir of this.allTestDirectories) { for (const dir of this.allTestDirectories) {
fs.mkdirSync(dir, { recursive: true }) fs.mkdirSync(dir, { recursive: true })
} }
env['RUNNER_NAME'] = 'test' env.RUNNER_NAME = 'test'
env[ env.RUNNER_TEMP = `${this.runnerMockDir}/${this.runnerMockSubdirs.workTemp}`
'RUNNER_TEMP' env.GITHUB_WORKSPACE = this.runnerProjectWorkDir
] = `${this.runnerMockDir}/${this.runnerMockSubdirs.workTemp}`
} }
public teardown(): void { public teardown(): void {
@@ -61,6 +46,24 @@ export default class TestSetup {
] ]
} }
public get runnerProjectWorkDir() {
return `${this.runnerMockDir}/_work/${this.projectName}/${this.projectName}`
}
public get testDir() {
return this.testdir
}
private get allTestDirectories() {
const resp = [this.testdir, this.runnerMockDir, this.runnerProjectWorkDir]
for (const [key, value] of Object.entries(this.runnerMockSubdirs)) {
resp.push(`${this.runnerMockDir}/${value}`)
}
return resp
}
public get systemMountVolumes(): Mount[] { public get systemMountVolumes(): Mount[] {
return [ return [
{ {
@@ -106,7 +109,32 @@ export default class TestSetup {
] ]
} }
public get workingDirectory(): string { public get containerWorkingDirectory(): string {
return `/__w/${this.projectName}/${this.projectName}` return `/__w/${this.projectName}/${this.projectName}`
} }
public initializeDockerAction(): string {
const actionPath = `${this.testdir}/_actions/example-handle/example-repo/example-branch/mock-directory`
fs.mkdirSync(actionPath, { recursive: true })
this.writeDockerfile(actionPath)
this.writeEntrypoint(actionPath)
return actionPath
}
private writeDockerfile(actionPath: string) {
const content = `FROM alpine:3.10
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]`
fs.writeFileSync(`${actionPath}/Dockerfile`, content)
}
private writeEntrypoint(actionPath) {
const content = `#!/bin/sh -l
echo "Hello $1"
time=$(date)
echo "::set-output name=time::$time"`
const entryPointPath = `${actionPath}/entrypoint.sh`
fs.writeFileSync(entryPointPath, content)
fs.chmodSync(entryPointPath, 0o755)
}
} }