Skip to content

Commit

Permalink
feat(1040): resolve job image alias with template images (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimgrund authored Jun 7, 2018
1 parent a77b5c2 commit e275838
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/phase/flatten.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ function mergeTemplateIntoJob(jobName, jobConfig, newJobs, templateFactory) {

merge(newJob, oldJob, true);
delete newJob.template;

// If template.images contains a label match for the image defined in the job
// set the job image to the respective template image
if (typeof template.images !== 'undefined' &&
typeof template.images[newJob.image] !== 'undefined') {
newJob.image = template.images[newJob.image];
}

newJobs[jobName] = newJob;

return null;
Expand Down
62 changes: 62 additions & 0 deletions test/data/basic-job-with-images.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"annotations": {},
"jobs": {
"main": [
{
"annotations": {},
"commands": [
{
"command": "npm install",
"name": "install"
},
{
"command": "echo hello world",
"name": "hello"
}
],
"environment": {
"SD_TEMPLATE_FULLNAME": "ImagesTestNamespace/imagestemplate",
"SD_TEMPLATE_NAME": "imagestemplate",
"SD_TEMPLATE_NAMESPACE": "ImagesTestNamespace",
"SD_TEMPLATE_VERSION": "2"
},
"image": "node:5",
"secrets": [],
"settings": {}
}
],
"publish": [
{
"annotations": {},
"commands": [
{
"command": "npm publish",
"name": "publish"
}
],
"description": "This is the publish description",
"environment": {},
"image": "node:4",
"secrets": [],
"settings": {}
}
]
},
"workflowGraph": {
"edges": [],
"nodes": [
{
"name": "~pr"
},
{
"name": "~commit"
},
{
"name": "publish"
},
{
"name": "main"
}
]
}
}
10 changes: 10 additions & 0 deletions test/data/basic-job-with-images.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
jobs:
main:
image: latest-image
template: ImagesTestNamespace/imagestemplate@2
description: "This is the main description"
publish:
image: node:4
description: "This is the publish description"
steps:
- publish: "npm publish"
19 changes: 19 additions & 0 deletions test/data/templateWithImages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"namespace": "ImagesTestNamespace",
"name": "imagestemplate",
"version": "2",
"description": "test template",
"maintainer": "[email protected]",
"labels": [],
"images": {
"stable-image": "node:4",
"latest-image": "node:5"
},
"config": {
"image": "node:4",
"steps": [
{ "install": "npm install" },
{ "hello": "echo hello world" }
]
}
}
10 changes: 10 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,22 @@ describe('config parser', () => {
let secondTemplate;
let namespaceTemplate;
let defaultTemplate;
let imagesTemplate;

beforeEach(() => {
firstTemplate = JSON.parse(loadData('template.json'));
secondTemplate = JSON.parse(loadData('template-2.json'));
namespaceTemplate = JSON.parse(loadData('templateWithNamespace.json'));
defaultTemplate = JSON.parse(loadData('templateWithDefaultNamespace.json'));
imagesTemplate = JSON.parse(loadData('templateWithImages.json'));
templateFactoryMock.getTemplate.withArgs('[email protected]')
.resolves(firstTemplate);
templateFactoryMock.getTemplate.withArgs('yourtemplate@2')
.resolves(secondTemplate);
templateFactoryMock.getTemplate.withArgs('mynamespace/[email protected]')
.resolves(namespaceTemplate);
templateFactoryMock.getTemplate.withArgs('ImagesTestNamespace/imagestemplate@2')
.resolves(imagesTemplate);
});

it('flattens templates successfully', () =>
Expand Down Expand Up @@ -269,6 +273,12 @@ describe('config parser', () => {
data, JSON.parse(loadData('basic-job-with-template-override-steps.json'))))
);

it('flattens templates with images', () =>
parser(loadData('basic-job-with-images.yaml'), templateFactoryMock)
.then(data => assert.deepEqual(
data, JSON.parse(loadData('basic-job-with-images.json'))))
);

it('returns error if template does not exist', () => {
templateFactoryMock.getTemplate.withArgs('[email protected]').resolves(null);

Expand Down

0 comments on commit e275838

Please sign in to comment.