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

@@ -1,11 +1,12 @@
import * as fs from 'fs'
import { v4 as uuidv4 } from 'uuid'
import { env } from 'process'
import { Mount } from 'hooklib'
import { env } from 'process'
import { v4 as uuidv4 } from 'uuid'
export default class TestSetup {
private testdir: string
private runnerMockDir: string
private runnerMockSubdirs = {
work: '_work',
externals: 'externals',
@@ -15,36 +16,20 @@ export default class TestSetup {
githubHome: '_work/_temp/_github_home',
githubWorkflow: '_work/_temp/_github_workflow'
}
private readonly projectName = 'example'
private readonly projectName = 'test'
constructor() {
this.testdir = `${__dirname}/_temp/${uuidv4()}`
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 {
for (const dir of this.allTestDirectories) {
fs.mkdirSync(dir, { recursive: true })
}
env['RUNNER_NAME'] = 'test'
env[
'RUNNER_TEMP'
] = `${this.runnerMockDir}/${this.runnerMockSubdirs.workTemp}`
env.RUNNER_NAME = 'test'
env.RUNNER_TEMP = `${this.runnerMockDir}/${this.runnerMockSubdirs.workTemp}`
env.GITHUB_WORKSPACE = this.runnerProjectWorkDir
}
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[] {
return [
{
@@ -106,7 +109,32 @@ export default class TestSetup {
]
}
public get workingDirectory(): string {
public get containerWorkingDirectory(): string {
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)
}
}