Skip to content

Commit 8ea5717

Browse files
nikola-jokicthboop
andauthored
Fix working directory and write state for appPod to be used in run-script-step (actions#8)
* added initial entrypoint script * change workingg directory working with addition to fix prepare-job state output * added prepend path * added run-script-step file generation, removed prepend path from container-step and prepare job * latest changes with testing run script step * fix the mounts real fast * cleanup * fix tests * add kind test * add kind yaml to ignore and run it during ci * fix kind option * remove gitignore * lowercase pwd * checkout first! * ignore test file in build.yaml * fixed wrong working directory and added test to run script step testing for the env * handle env's/escaping better * added single quote escape to env escapes * surounded env value with single quote * added spacing around run-container-step, changed examples to actually echo hello world * refactored tests * make sure to escape properly * set addition mounts for container steps * fixup container action mounts Co-authored-by: Thomas Boop <[email protected]> Co-authored-by: Thomas Boop <[email protected]>
1 parent 643bf36 commit 8ea5717

23 files changed

+391
-230
lines changed

Diff for: .github/workflows/build.yaml

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ jobs:
1010
build:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: helm/[email protected]
1413
- uses: actions/checkout@v3
14+
- run: sed -i "s|{{PATHTOREPO}}|$(pwd)|" packages/k8s/tests/test-kind.yaml
15+
name: Setup kind cluster yaml config
16+
- uses: helm/[email protected]
17+
with:
18+
config: packages/k8s/tests/test-kind.yaml
1519
- run: npm install
1620
name: Install dependencies
1721
- run: npm run bootstrap
@@ -22,6 +26,6 @@ jobs:
2226
- name: Check linter
2327
run: |
2428
npm run lint
25-
git diff --exit-code
29+
git diff --exit-code -- ':!packages/k8s/tests/test-kind.yaml'
2630
- name: Run tests
2731
run: npm run test

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules/
22
lib/
33
dist/
4-
**/tests/_temp/**
4+
**/tests/_temp/**
5+
packages/k8s/tests/test-kind.yaml

Diff for: examples/example-script.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
echo "Hello World"

Diff for: examples/run-container-step.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"image": "node:14.16",
1313
"dockerfile": null,
1414
"entryPointArgs": [
15-
"-c",
16-
"echo \"hello world2\""
15+
"-e",
16+
"example-script.sh"
1717
],
1818
"entryPoint": "bash",
1919
"workingDirectory": "/__w/repo/repo",

Diff for: examples/run-script-step.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
},
1111
"args": {
1212
"entryPointArgs": [
13-
"-c",
14-
"echo \"hello world\""
13+
"-e",
14+
"example-script.sh"
1515
],
1616
"entryPoint": "bash",
1717
"environmentVariables": {

Diff for: packages/docker/tests/cleanup-job-test.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as fs from 'fs'
1+
import { PrepareJobArgs } from 'hooklib/lib'
22
import { cleanupJob, prepareJob } from '../src/hooks'
33
import TestSetup from './test-setup'
44

@@ -11,22 +11,16 @@ describe('cleanup job', () => {
1111
testSetup = new TestSetup()
1212
testSetup.initialize()
1313

14-
const prepareJobDefinition = JSON.parse(
15-
fs.readFileSync(
16-
`${__dirname}/../../../examples/prepare-job.json`,
17-
'utf-8'
18-
)
19-
)
14+
const prepareJobDefinition = testSetup.getPrepareJobDefinition()
2015

2116
const prepareJobOutput = testSetup.createOutputFile(
2217
'prepare-job-output.json'
2318
)
2419

25-
prepareJobDefinition.args.container.registry = null
26-
prepareJobDefinition.args.services.forEach(s => {
27-
s.registry = null
28-
})
29-
await prepareJob(prepareJobDefinition.args, prepareJobOutput)
20+
await prepareJob(
21+
prepareJobDefinition.args as PrepareJobArgs,
22+
prepareJobOutput
23+
)
3024
})
3125

3226
afterEach(() => {

Diff for: packages/docker/tests/container-build-test.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
1-
import * as fs from 'fs'
21
import { containerBuild } from '../src/dockerCommands'
32
import TestSetup from './test-setup'
43

54
let testSetup
65
let runContainerStepDefinition
7-
const runContainerStepInputPath = `${__dirname}/../../../examples/run-container-step.json`
86

97
describe('container build', () => {
108
beforeEach(() => {
119
testSetup = new TestSetup()
1210
testSetup.initialize()
1311

14-
let runContainerStepJson = fs.readFileSync(
15-
runContainerStepInputPath,
16-
'utf8'
17-
)
18-
runContainerStepDefinition = JSON.parse(runContainerStepJson.toString())
19-
runContainerStepDefinition.image = ''
20-
const actionPath = testSetup.initializeDockerAction()
21-
runContainerStepDefinition.dockerfile = `${actionPath}/Dockerfile`
12+
runContainerStepDefinition = testSetup.getRunContainerStepDefinition()
2213
})
2314

2415
afterEach(() => {
2516
testSetup.teardown()
2617
})
2718

2819
it('should build container', async () => {
20+
runContainerStepDefinition.image = ''
21+
const actionPath = testSetup.initializeDockerAction()
22+
runContainerStepDefinition.dockerfile = `${actionPath}/Dockerfile`
2923
await expect(
3024
containerBuild(runContainerStepDefinition, 'example-test-tag')
3125
).resolves.not.toThrow()

Diff for: packages/docker/tests/e2e-test.ts

+7-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as fs from 'fs'
2-
import * as path from 'path'
32
import {
43
cleanupJob,
54
prepareJob,
@@ -8,41 +7,20 @@ import {
87
} from '../src/hooks'
98
import TestSetup from './test-setup'
109

11-
const definitions = {
12-
prepareJob: JSON.parse(
13-
fs.readFileSync(
14-
path.resolve(__dirname + '/../../../examples/prepare-job.json'),
15-
'utf8'
16-
)
17-
),
18-
19-
runContainerStep: JSON.parse(
20-
fs.readFileSync(
21-
path.resolve(__dirname + '/../../../examples/run-container-step.json'),
22-
'utf8'
23-
)
24-
),
25-
26-
runScriptStep: JSON.parse(
27-
fs.readFileSync(
28-
path.resolve(__dirname + '/../../../examples/run-script-step.json'),
29-
'utf-8'
30-
)
31-
)
32-
}
10+
let definitions
3311

3412
let testSetup: TestSetup
3513

3614
describe('e2e', () => {
3715
beforeEach(() => {
3816
testSetup = new TestSetup()
3917
testSetup.initialize()
40-
definitions.prepareJob.args.container.systemMountVolumes =
41-
testSetup.systemMountVolumes
42-
definitions.prepareJob.args.container.registry = null
43-
definitions.prepareJob.args.services.forEach(s => {
44-
s.registry = null
45-
})
18+
19+
definitions = {
20+
prepareJob: testSetup.getPrepareJobDefinition(),
21+
runScriptStep: testSetup.getRunScriptStepDefinition(),
22+
runContainerStep: testSetup.getRunContainerStepDefinition()
23+
}
4624
})
4725

4826
afterEach(() => {

Diff for: packages/docker/tests/prepare-job-test.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,15 @@ import TestSetup from './test-setup'
44

55
jest.useRealTimers()
66

7-
const prepareJobDefinition = JSON.parse(
8-
fs.readFileSync(`${__dirname}/../../../examples/prepare-job.json`, 'utf-8')
9-
)
7+
let prepareJobDefinition
108

119
let testSetup: TestSetup
1210

1311
describe('prepare job', () => {
1412
beforeEach(() => {
1513
testSetup = new TestSetup()
1614
testSetup.initialize()
17-
18-
prepareJobDefinition.args.container.systemMountVolumes =
19-
testSetup.systemMountVolumes
20-
prepareJobDefinition.args.container.workingDirectory =
21-
testSetup.workingDirectory
22-
prepareJobDefinition.args.container.registry = null
23-
prepareJobDefinition.args.services.forEach(s => {
24-
s.registry = null
25-
})
15+
prepareJobDefinition = testSetup.getPrepareJobDefinition()
2616
})
2717

2818
afterEach(() => {

Diff for: packages/docker/tests/run-script-step.ts renamed to packages/docker/tests/run-script-step-test.ts

+7-21
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,29 @@
11
import * as fs from 'fs'
22
import { PrepareJobResponse } from 'hooklib/lib'
3-
import * as path from 'path'
43
import { prepareJob, runScriptStep } from '../src/hooks'
54
import TestSetup from './test-setup'
65

76
jest.useRealTimers()
87

98
let testSetup: TestSetup
109

11-
const definitions = {
12-
prepareJob: JSON.parse(
13-
fs.readFileSync(
14-
path.resolve(__dirname + '/../../../examples/prepare-job.json'),
15-
'utf8'
16-
)
17-
),
18-
19-
runScriptStep: JSON.parse(
20-
fs.readFileSync(
21-
path.resolve(__dirname + '/../../../examples/run-script-step.json'),
22-
'utf-8'
23-
)
24-
)
25-
}
10+
let definitions
2611

2712
let prepareJobResponse: PrepareJobResponse
2813

29-
describe('run-script-step', () => {
14+
describe('run script step', () => {
3015
beforeEach(async () => {
3116
testSetup = new TestSetup()
3217
testSetup.initialize()
3318

19+
definitions = {
20+
prepareJob: testSetup.getPrepareJobDefinition(),
21+
runScriptStep: testSetup.getRunScriptStepDefinition()
22+
}
23+
3424
const prepareJobOutput = testSetup.createOutputFile(
3525
'prepare-job-output.json'
3626
)
37-
definitions.prepareJob.args.container.registry = null
38-
definitions.prepareJob.args.services.forEach(s => {
39-
s.registry = null
40-
})
4127
await prepareJob(definitions.prepareJob.args, prepareJobOutput)
4228

4329
prepareJobResponse = JSON.parse(fs.readFileSync(prepareJobOutput, 'utf-8'))

Diff for: packages/docker/tests/test-setup.ts

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from 'fs'
22
import { Mount } from 'hooklib'
3+
import { HookData } from 'hooklib/lib'
34
import * as path from 'path'
45
import { env } from 'process'
56
import { v4 as uuidv4 } from 'uuid'
@@ -51,13 +52,18 @@ export default class TestSetup {
5152
for (const dir of this.allTestDirectories) {
5253
fs.mkdirSync(dir, { recursive: true })
5354
}
55+
56+
fs.copyFileSync(
57+
path.resolve(`${__dirname}/../../../examples/example-script.sh`),
58+
`${env.RUNNER_TEMP}/example-script.sh`
59+
)
5460
}
5561

5662
public teardown(): void {
5763
fs.rmdirSync(this.testdir, { recursive: true })
5864
}
5965

60-
public get systemMountVolumes(): Mount[] {
66+
private get systemMountVolumes(): Mount[] {
6167
return [
6268
{
6369
sourceVolumePath: '/var/run/docker.sock',
@@ -140,4 +146,51 @@ echo "::set-output name=time::$time"`
140146
fs.writeFileSync(entryPointPath, content)
141147
fs.chmodSync(entryPointPath, 0o755)
142148
}
149+
150+
public getPrepareJobDefinition(): HookData {
151+
const prepareJob = JSON.parse(
152+
fs.readFileSync(
153+
path.resolve(__dirname + '/../../../examples/prepare-job.json'),
154+
'utf8'
155+
)
156+
)
157+
158+
prepareJob.args.container.systemMountVolumes = this.systemMountVolumes
159+
prepareJob.args.container.workingDirectory = this.workingDirectory
160+
prepareJob.args.container.userMountVolumes = undefined
161+
prepareJob.args.container.registry = null
162+
prepareJob.args.services.forEach(s => {
163+
s.registry = null
164+
})
165+
166+
return prepareJob
167+
}
168+
169+
public getRunScriptStepDefinition(): HookData {
170+
const runScriptStep = JSON.parse(
171+
fs.readFileSync(
172+
path.resolve(__dirname + '/../../../examples/run-script-step.json'),
173+
'utf8'
174+
)
175+
)
176+
177+
runScriptStep.args.entryPointArgs[1] = `/__w/_temp/example-script.sh`
178+
return runScriptStep
179+
}
180+
181+
public getRunContainerStepDefinition(): HookData {
182+
const runContainerStep = JSON.parse(
183+
fs.readFileSync(
184+
path.resolve(__dirname + '/../../../examples/run-container-step.json'),
185+
'utf8'
186+
)
187+
)
188+
189+
runContainerStep.args.entryPointArgs[1] = `/__w/_temp/example-script.sh`
190+
runContainerStep.args.systemMountVolumes = this.systemMountVolumes
191+
runContainerStep.args.workingDirectory = this.workingDirectory
192+
runContainerStep.args.userMountVolumes = undefined
193+
runContainerStep.args.registry = null
194+
return runContainerStep
195+
}
143196
}

Diff for: packages/k8s/src/hooks/prepare-job.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,13 @@ function generateResponseFile(
100100
appPod: k8s.V1Pod,
101101
isAlpine
102102
): void {
103+
if (!appPod.metadata?.name) {
104+
throw new Error('app pod must have metadata.name specified')
105+
}
103106
const response = {
104-
state: {},
107+
state: {
108+
jobPod: appPod.metadata.name
109+
},
105110
context: {},
106111
isAlpine
107112
}
@@ -163,13 +168,11 @@ function createPodSpec(
163168
name: string,
164169
jobContainer = false
165170
): k8s.V1Container {
166-
if (!container.entryPointArgs) {
167-
container.entryPointArgs = DEFAULT_CONTAINER_ENTRY_POINT_ARGS
168-
}
169-
container.entryPointArgs = DEFAULT_CONTAINER_ENTRY_POINT_ARGS
170171
if (!container.entryPoint) {
171172
container.entryPoint = DEFAULT_CONTAINER_ENTRY_POINT
173+
container.entryPointArgs = DEFAULT_CONTAINER_ENTRY_POINT_ARGS
172174
}
175+
173176
const podContainer = {
174177
name,
175178
image: container.image,

0 commit comments

Comments
 (0)