Skip to content

Commit

Permalink
Allow overriding memory limit on docker build/run (#6345)
Browse files Browse the repository at this point in the history
Fixes #6342
  • Loading branch information
heaths authored and jikuma committed Feb 8, 2018
1 parent 51045bf commit 8496b96
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
"loc.input.help.enforceDockerNamingConvention": "If enabled docker image name will be modified to follow Docker naming convention. Converts upper case character to lower case and removes spaces in image name.",
"loc.input.label.cwd": "Working Directory",
"loc.input.help.cwd": "Working directory for the Docker command.",
"loc.input.label.memory": "Memory limit",
"loc.input.help.memory": "The maximum amount of memory available to the container as a integer with optional suffixes like '2GB'.",
"loc.messages.ContainerPatternFound": "Pattern found in docker filepath parameter",
"loc.messages.ContainerPatternNotFound": "No pattern found in docker filepath parameter",
"loc.messages.ContainerDockerFileNotFound": "No Docker file matching %s was found.",
Expand Down
31 changes: 31 additions & 0 deletions Tasks/Docker/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('Docker Suite', function() {
delete process.env[shared.TestEnvVars.imageName];
delete process.env[shared.TestEnvVars.additionalImageTags];
delete process.env[shared.TestEnvVars.enforceDockerNamingConvention];
delete process.env[shared.TestEnvVars.memory];
});
after(function () {
});
Expand All @@ -37,6 +38,21 @@ describe('Docker Suite', function() {
done();
});

it('Runs successfully for docker build with memory limit', (done:MochaDone) => {
let tp = path.join(__dirname, 'TestSetup.js');
let tr : ttm.MockTestRunner = new ttm.MockTestRunner(tp);
process.env[shared.TestEnvVars.action] = shared.ActionTypes.buildImage;
process.env[shared.TestEnvVars.memory] = "2GB";
tr.run();

assert(tr.invokedToolCount == 1, 'should have invoked tool one times. actual: ' + tr.invokedToolCount);
assert(tr.stderr.length == 0 || tr.errorIssues.length, 'should not have written to stderr');
assert(tr.succeeded, 'task should have succeeded');
assert(tr.stdout.indexOf(`[command]docker build -f ${shared.formatPath("dir1/DockerFile")} -t test/test:2 -m 2GB`) != -1, "docker build should run");
console.log(tr.stderr);
done();
});

it('Runs successfully for docker build for invalid image name', (done:MochaDone) => {
let tp = path.join(__dirname, 'TestSetup.js');
let tr : ttm.MockTestRunner = new ttm.MockTestRunner(tp);
Expand Down Expand Up @@ -116,6 +132,21 @@ describe('Docker Suite', function() {
done();
});

it('Runs successfully for docker run image with memory limit', (done:MochaDone) => {
let tp = path.join(__dirname, 'TestSetup.js');
let tr : ttm.MockTestRunner = new ttm.MockTestRunner(tp);
process.env[shared.TestEnvVars.action] = shared.ActionTypes.runImage;
process.env[shared.TestEnvVars.memory] = "2GB";
tr.run();

assert(tr.invokedToolCount == 1, 'should have invoked tool one times. actual: ' + tr.invokedToolCount);
assert(tr.stderr.length == 0 || tr.errorIssues.length, 'should not have written to stderr');
assert(tr.succeeded, 'task should have succeeded');
assert(tr.stdout.indexOf("[command]docker run --rm -m 2GB test/test:2") != -1, "docker run should run");
console.log(tr.stderr);
done();
});

it('Runs successfully for docker tag image from image names file', (done:MochaDone) => {
let tp = path.join(__dirname, 'TestSetup.js');
let tr : ttm.MockTestRunner = new ttm.MockTestRunner(tp);
Expand Down
9 changes: 9 additions & 0 deletions Tasks/Docker/Tests/TestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tr.setInput('azureSubscriptionEndpoint', 'AzureRMSpn');
tr.setInput('azureContainerRegistry', '{"loginServer":"ajgtestacr1.azurecr.io", "id" : "/subscriptions/c00d16c7-6c1f-4c03-9be1-6934a4c49682/resourcegroups/ajgtestacr1rg/providers/Microsoft.ContainerRegistry/registries/ajgtestacr1"}')
tr.setInput('additionalImageTags', process.env[shared.TestEnvVars.additionalImageTags] || '');
tr.setInput('enforceDockerNamingConvention', process.env[shared.TestEnvVars.enforceDockerNamingConvention]);
tr.setInput('memory', process.env[shared.TestEnvVars.memory] || '');

console.log("Inputs have been set");

Expand Down Expand Up @@ -60,6 +61,10 @@ let a = {
"code": 0,
"stdout": "successfully ran test/test:2 image"
},
"docker run --rm -m 2GB test/test:2": {
"code": 0,
"stdout": "successfully ran test/test:2 image"
},
"docker pull test/test:2": {
"code": 0,
"stdout": "successfully pulled test/test:2 image"
Expand All @@ -74,6 +79,10 @@ a.exec[`docker build -f ${DockerFilePath} -t test/test:2`] = {
"code": 0,
"stdout": "successfully build test/test:2 image"
};
a.exec[`docker build -f ${DockerFilePath} -t test/test:2 -m 2GB`] = {
"code": 0,
"stdout": "successfully build test/test:2 image"
};
a.exec[`docker build -f ${DockerFilePath} -t test/Te st:2`] = {
"code": 1,
"stdout": "test/Te st:2 not valid imagename"
Expand Down
3 changes: 2 additions & 1 deletion Tasks/Docker/Tests/TestShared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export let TestEnvVars = {
includeLatestTag: "__includeLatestTag__",
imageName: "__imageName__",
additionalImageTags: "__additionalImageTags__",
enforceDockerNamingConvention: "__enforceDockerNamingConvention__"
enforceDockerNamingConvention: "__enforceDockerNamingConvention__",
memory: "__memory__"
};

export let OperatingSystems = {
Expand Down
5 changes: 5 additions & 0 deletions Tasks/Docker/containerbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export function run(connection: ContainerConnection): any {
command.arg(["-t", baseImageName]);
}

var memory = tl.getInput("memory");
if (memory) {
command.arg(["-m", memory]);
}

var context: string;
var defaultContext = tl.getBoolInput("defaultContext");
if (defaultContext) {
Expand Down
7 changes: 6 additions & 1 deletion Tasks/Docker/containerrun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ export function run(connection: ContainerConnection): any {
if (workDir) {
command.arg(["-w", workDir]);
}


var memory = tl.getInput("memory");
if (memory) {
command.arg(["-m", memory]);
}

var imageName = utils.getImageName();
var qualifyImageName = tl.getBoolInput("qualifyImageName");
if (qualifyImageName) {
Expand Down
10 changes: 9 additions & 1 deletion Tasks/Docker/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 0,
"Minor": 3,
"Patch": 6
"Patch": 7
},
"demands": [],
"preview": "false",
Expand Down Expand Up @@ -294,6 +294,14 @@
"defaultValue": "$(System.DefaultWorkingDirectory)",
"helpMarkDown": "Working directory for the Docker command.",
"groupName": "advanced"
},
{
"name": "memory",
"type": "string",
"label": "Memory limit",
"visibleRule": "action = Build an image || action = Run an image",
"helpMarkDown": "The maximum amount of memory available to the container as a integer with optional suffixes like '2GB'.",
"groupName": "advanced"
}
],
"dataSourceBindings": [
Expand Down
10 changes: 9 additions & 1 deletion Tasks/Docker/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 0,
"Minor": 3,
"Patch": 6
"Patch": 7
},
"demands": [],
"preview": "false",
Expand Down Expand Up @@ -300,6 +300,14 @@
"defaultValue": "$(System.DefaultWorkingDirectory)",
"helpMarkDown": "ms-resource:loc.input.help.cwd",
"groupName": "advanced"
},
{
"name": "memory",
"type": "string",
"label": "ms-resource:loc.input.label.memory",
"visibleRule": "action = Build an image || action = Run an image",
"helpMarkDown": "ms-resource:loc.input.help.memory",
"groupName": "advanced"
}
],
"dataSourceBindings": [
Expand Down

0 comments on commit 8496b96

Please sign in to comment.