Skip to content

Commit 86fcd39

Browse files
committed
Add tests
1 parent d57e40e commit 86fcd39

File tree

2 files changed

+83
-19
lines changed

2 files changed

+83
-19
lines changed

packages/k8s/src/hooks/prepare-job.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export async function prepareJob(
144144
generateResponseFile(responseFile, args, createdPod, isAlpine)
145145
}
146146

147-
function processServiceContainers(
147+
export function processServiceContainers(
148148
services?: ServiceContainerInfo[],
149149
container?: k8s.V1Container,
150150
extension?: k8s.V1PodTemplateSpec
@@ -157,7 +157,7 @@ function processServiceContainers(
157157
core.debug(`Adding service '${service.image}' to pod definition`)
158158
return createContainerSpec(
159159
service,
160-
generateContainerName(service.image),
160+
service.name,
161161
false,
162162
extension,
163163
service.createOptions
@@ -188,6 +188,7 @@ function processServiceContainers(
188188
container.resources.limits &&
189189
container.resources.limits['google.com/tpu']
190190
) {
191+
core.debug('removing tpu from main container resource limits')
191192
delete container.resources.limits['google.com/tpu']
192193
}
193194
}
@@ -399,11 +400,20 @@ export function createContainerSpec(
399400
}
400401

401402
podContainer.env = []
402-
for (const [key, value] of Object.entries(
403-
container['environmentVariables']
404-
)) {
405-
if (value && key !== 'HOME') {
406-
podContainer.env.push({ name: key, value: value as string })
403+
if (container['environmentVariables']) {
404+
for (const [key, value] of Object.entries(
405+
container['environmentVariables']
406+
)) {
407+
if (value && key !== 'HOME') {
408+
podContainer.env.push({ name: key, value: value as string })
409+
}
410+
}
411+
412+
if (!('CI' in container['environmentVariables'])) {
413+
podContainer.env.push({
414+
name: 'CI',
415+
value: 'true'
416+
})
407417
}
408418
}
409419

@@ -412,13 +422,6 @@ export function createContainerSpec(
412422
value: 'true'
413423
})
414424

415-
if (!('CI' in container['environmentVariables'])) {
416-
podContainer.env.push({
417-
name: 'CI',
418-
value: 'true'
419-
})
420-
}
421-
422425
podContainer.volumeMounts = containerVolumes(
423426
container.userMountVolumes,
424427
jobContainer

packages/k8s/tests/prepare-job-test.ts

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import * as fs from 'fs'
22
import * as path from 'path'
33
import { cleanupJob } from '../src/hooks'
4-
import { createContainerSpec, prepareJob } from '../src/hooks/prepare-job'
4+
import {
5+
createContainerSpec,
6+
prepareJob,
7+
processServiceContainers
8+
} from '../src/hooks/prepare-job'
59
import { TestHelper } from './test-setup'
610
import {
711
ENV_HOOK_TEMPLATE_PATH,
812
ENV_NUMBER_OF_HOSTS,
913
ENV_USE_KUBE_SCHEDULER,
10-
generateContainerName,
11-
readExtensionFromFile
14+
generateContainerName
1215
} from '../src/k8s/utils'
13-
import { getEvents, getPodByName } from '../src/k8s'
16+
import { getPodByName } from '../src/k8s'
1417
import { V1Container } from '@kubernetes/client-node'
15-
import * as yaml from 'js-yaml'
1618
import { JOB_CONTAINER_NAME } from '../src/hooks/constants'
1719

1820
jest.useRealTimers()
@@ -324,3 +326,62 @@ describe('Prepare job', () => {
324326
}
325327
)
326328
})
329+
330+
describe('processServiceContainers', () => {
331+
it('generate names for service containers', () => {
332+
expect(
333+
processServiceContainers(
334+
[
335+
{
336+
image: 'gcr.io/server'
337+
},
338+
{
339+
image: 'gcr.io/server'
340+
}
341+
],
342+
{
343+
name: 'nginx',
344+
image: 'nginx:latest',
345+
imagePullPolicy: 'IfNotPresent'
346+
} as V1Container
347+
)
348+
).toEqual(
349+
expect.arrayContaining([
350+
expect.objectContaining({ name: 'server' }),
351+
expect.objectContaining({ name: 'server-1' })
352+
])
353+
)
354+
})
355+
356+
it('generate TPU request for service containers', () => {
357+
expect(
358+
processServiceContainers(
359+
[
360+
{
361+
image: 'gcr.io/server',
362+
createOptions: '--tpu=4'
363+
}
364+
],
365+
{
366+
name: 'nginx',
367+
image: 'nginx:latest',
368+
imagePullPolicy: 'IfNotPresent'
369+
} as V1Container
370+
)
371+
).toEqual(
372+
expect.arrayContaining([
373+
expect.objectContaining({
374+
name: 'server',
375+
resources: {
376+
limits: {
377+
'google.com/tpu': '4'
378+
},
379+
requests: {
380+
'google.com/tpu': '4'
381+
}
382+
}
383+
})
384+
])
385+
)
386+
})
387+
})

0 commit comments

Comments
 (0)