Skip to content

Commit

Permalink
add tests for verifying the var file content being written is correct (
Browse files Browse the repository at this point in the history
  • Loading branch information
arjgupta authored Jun 25, 2018
1 parent 6b84fb7 commit 75fb3f3
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 20 deletions.
31 changes: 29 additions & 2 deletions Tasks/PackerBuildV0/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,43 @@ describe('PackerBuild Suite', function() {
done();
});

it('Writes packer var file successfully for windows template', (done:MochaDone) => {
let tp = path.join(__dirname, 'L0Windows.js');
let tr : ttm.MockTestRunner = new ttm.MockTestRunner(tp);
let match1 = 'writing to file C:\\somefolder\\somevarfile.json content: {"subscription_id":"sId","client_id":"spId","client_secret":"spKey","tenant_id":"tenant","object_id":"oId"}';
let match2 = 'writing to file C:\\somefolder\\somevarfile.json content: {"resource_group":"testrg","storage_account":"teststorage","image_publisher":"MicrosoftWindowsServer","image_offer":"WindowsServer","image_sku":"2012-R2-Datacenter","location":"South India","capture_name_prefix":"Release-1","skip_clean":"true","script_relative_path":"dir3\\\\somedir\\\\deploy.ps1","package_path":"C:\\\\dir1\\\\somedir\\\\dir2","package_name":"dir2","script_arguments":"-target \\"subdir 1\\" -shouldFail false"}';
tr.run();

assert(tr.invokedToolCount == 4, 'should have invoked tool four times. actual: ' + tr.invokedToolCount);
assert(tr.stdout.indexOf(match1) > -1, 'correctly writes contents of var file (containing azure spn details)');
assert(tr.stdout.indexOf(match2) > -1, 'correctly writes contents of var file (containing template variables)');

done();
});

it('Runs successfully for custom template', (done:MochaDone) => {
let tp = path.join(__dirname, 'L0CustomTemplate.js');
let tr : ttm.MockTestRunner = new ttm.MockTestRunner(tp);

tr.run();

assert(tr.invokedToolCount == 4, 'should have invoked tool four 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');
done();
});

it('Writes packer var file successfully for custom template', (done:MochaDone) => {
let tp = path.join(__dirname, 'L0CustomTemplate.js');
let tr : ttm.MockTestRunner = new ttm.MockTestRunner(tp);
let match1 = 'writing to file C:\\somefolder\\somevarfile.json content: {"client_id":"abcdef","drop-location":"C:\\\\folder 1\\\\folder-2"}';
let match2 = 'writing to file C:\\somefolder\\somevarfile.json content: {}';

tr.run();

assert(tr.invokedToolCount == 4, 'should have invoked tool four times. actual: ' + tr.invokedToolCount);
assert(tr.stdout.indexOf(match1) > -1, 'correctly writes contents of var file (containing azure spn details)');
assert(tr.stdout.indexOf(match2) > -1, 'correctly writes contents of var file (containing template variables)');

done();
});
Expand Down Expand Up @@ -109,8 +138,6 @@ describe('PackerBuild Suite', function() {
done();
});



it('Should copy builtin template to temp location for windows template', (done:MochaDone) => {
let tp = path.join(__dirname, 'L0WindowsCustomImage.js');
let tr : ttm.MockTestRunner = new ttm.MockTestRunner(tp);
Expand Down
5 changes: 4 additions & 1 deletion Tasks/PackerBuildV0/Tests/L0CustomTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@ var utMock = {
console.log('copying ' + source + ' to ' + destination);
}
},
createTemplateVarFile: function (templateVariables) {
generateTemporaryFilePath: function () {
return "C:\\somefolder\\somevarfile.json";
},
getPackerVarFileContent: function(variables) {
return ut.getPackerVarFileContent(variables);
},
writeFile: function(filePath: string, content: string) {
console.log("writing to file " + filePath + " content: " + content);
},
Expand Down
5 changes: 4 additions & 1 deletion Tasks/PackerBuildV0/Tests/L0Linux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,12 @@ var utMock = {
copyFile: function(source: string, destination: string) {
console.log('copying ' + source + ' to ' + destination);
},
createTemplateVarFile: function (templateVariables) {
generateTemporaryFilePath: function () {
return "/somefolder/somevarfile.json";
},
getPackerVarFileContent: function(variables) {
return ut.getPackerVarFileContent(variables);
},
writeFile: function(filePath: string, content: string) {
console.log("writing to file " + filePath + " content: " + content);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ var utMock = {
readJsonFile: function(filePath: string) {
return "{\"builders\":[{\"type\":\"azure-arm\"}]}";
},
createTemplateVarFile: function (templateVariables) {
generateTemporaryFilePath: function () {
return "/somefolder/somevarfile.json";
},
getPackerVarFileContent: function(variables) {
return ut.getPackerVarFileContent(variables);
},
writeFile: function(filePath: string, content: string) {
console.log("writing to file " + filePath + " content: " + content);
},
Expand Down
5 changes: 4 additions & 1 deletion Tasks/PackerBuildV0/Tests/L0LinuxCustomImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,12 @@ var utMock = {
console.log('copying ' + source + ' to ' + destination);
}
},
createTemplateVarFile: function (templateVariables) {
generateTemporaryFilePath: function () {
return "/somefolder/somevarfile.json";
},
getPackerVarFileContent: function(variables) {
return ut.getPackerVarFileContent(variables);
},
writeFile: function(filePath: string, content: string) {
console.log("writing to file " + filePath + " content: " + content);
},
Expand Down
5 changes: 4 additions & 1 deletion Tasks/PackerBuildV0/Tests/L0LinuxInstallPacker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,12 @@ var utMock = {
copyFile: function(source: string, destination: string) {
console.log('copying ' + source + ' to ' + destination);
},
createTemplateVarFile: function (templateVariables) {
generateTemporaryFilePath: function () {
return "/somefolder/somevarfile.json";
},
getPackerVarFileContent: function(variables) {
return ut.getPackerVarFileContent(variables);
},
writeFile: function(filePath: string, content: string) {
console.log("writing to file " + filePath + " content: " + content);
},
Expand Down
5 changes: 4 additions & 1 deletion Tasks/PackerBuildV0/Tests/L0Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ var utMock = {
copyFile: function(source: string, destination: string) {
console.log('copying ' + source + ' to ' + destination);
},
createTemplateVarFile: function (templateVariables) {
generateTemporaryFilePath: function () {
return "C:\\somefolder\\somevarfile.json";
},
getPackerVarFileContent: function(variables) {
return ut.getPackerVarFileContent(variables);
},
writeFile: function(filePath: string, content: string) {
console.log("writing to file " + filePath + " content: " + content);
},
Expand Down
5 changes: 4 additions & 1 deletion Tasks/PackerBuildV0/Tests/L0Windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ var utMock = {
writeFile: function(filePath: string, content: string) {
console.log("writing to file " + filePath + " content: " + content);
},
createTemplateVarFile: function (templateVariables) {
generateTemporaryFilePath: function () {
return "C:\\somefolder\\somevarfile.json";
},
getPackerVarFileContent: function(variables) {
return ut.getPackerVarFileContent(variables);
},
findMatch: function(root: string, patterns: string[] | string) {
if(root === DefaultWorkingDirectory) {
return ["C:\\dir1\\somedir\\dir2"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ var utMock = {
readJsonFile: function(filePath: string) {
return "{\"builders\":[{\"type\":\"azure-arm\"}]}";
},
createTemplateVarFile: function (templateVariables) {
generateTemporaryFilePath: function () {
return "C:\\somefolder\\somevarfile.json";
},
getPackerVarFileContent: function(variables) {
return ut.getPackerVarFileContent(variables);
},
writeFile: function(filePath: string, content: string) {
console.log("writing to file " + filePath + " content: " + content);
},
Expand Down
5 changes: 4 additions & 1 deletion Tasks/PackerBuildV0/Tests/L0WindowsCustomImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ var utMock = {
console.log('copying ' + source + ' to ' + destination);
}
},
createTemplateVarFile: function (templateVariables) {
generateTemporaryFilePath: function () {
return "C:\\somefolder\\somevarfile.json";
},
getPackerVarFileContent: function(variables) {
return ut.getPackerVarFileContent(variables);
},
writeFile: function(filePath: string, content: string) {
console.log("writing to file " + filePath + " content: " + content);
},
Expand Down
5 changes: 4 additions & 1 deletion Tasks/PackerBuildV0/Tests/L0WindowsFail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ var utMock = {
copyFile: function(source: string, destination: string) {
console.log('copying ' + source + ' to ' + destination);
},
createTemplateVarFile: function (templateVariables) {
generateTemporaryFilePath: function () {
return "C:\\somefolder\\somevarfile.json";
},
getPackerVarFileContent: function(variables) {
return ut.getPackerVarFileContent(variables);
},
writeFile: function(filePath: string, content: string) {
console.log("writing to file " + filePath + " content: " + content);
},
Expand Down
5 changes: 4 additions & 1 deletion Tasks/PackerBuildV0/Tests/L0WindowsInstallPacker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ var utMock = {
copyFile: function(source: string, destination: string) {
console.log('copying ' + source + ' to ' + destination);
},
createTemplateVarFile: function (templateVariables) {
generateTemporaryFilePath: function () {
return "C:\\somefolder\\somevarfile.json";
},
getPackerVarFileContent: function(variables) {
return ut.getPackerVarFileContent(variables);
},
writeFile: function(filePath: string, content: string) {
console.log("writing to file " + filePath + " content: " + content);
},
Expand Down
6 changes: 4 additions & 2 deletions Tasks/PackerBuildV0/src/operations/packerBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ export async function run(packerHost: packerHost): Promise<any> {
var variableProviders = packerHost.getTemplateVariablesProviders();
for (var provider of variableProviders) {
var variables = await provider.getTemplateVariables(packerHost);
let varFilePath = utils.createTemplateVarFile(variables);
command.arg(util.format("%s=%s", '-var-file', varFilePath));
let filePath: string = utils.generateTemporaryFilePath();
let content: string = utils.getPackerVarFileContent(variables);
utils.writeFile(filePath, content);
command.arg(util.format("%s=%s", '-var-file', filePath));
}

command.arg(packerHost.getTemplateFileProvider().getTemplateFileLocation(packerHost));
Expand Down
6 changes: 4 additions & 2 deletions Tasks/PackerBuildV0/src/operations/packerValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export async function run(packerHost: packerHost): Promise<void> {
var variableProviders = packerHost.getTemplateVariablesProviders();
for (var provider of variableProviders) {
var variables = await provider.getTemplateVariables(packerHost);
let varFilePath = utils.createTemplateVarFile(variables);
command.arg(util.format("%s=%s", '-var-file', varFilePath));
let filePath: string = utils.generateTemporaryFilePath();
let content: string = utils.getPackerVarFileContent(variables);
utils.writeFile(filePath, content);
command.arg(util.format("%s=%s", '-var-file', filePath));
}

command.arg(packerHost.getTemplateFileProvider().getTemplateFileLocation(packerHost));
Expand Down
9 changes: 6 additions & 3 deletions Tasks/PackerBuildV0/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,18 @@ export function readJsonFile(filePath: string): string {
return content;
}

export function createTemplateVarFile (templateVariables: Map<string, string>): string {
export function generateTemporaryFilePath (): string {
let filePath: string = path.resolve(tl.getVariable('Agent.TempDirectory'), Math.random().toString(36).replace('0.', '') + '.json');
return filePath;
}

export function getPackerVarFileContent (templateVariables: Map<string, string>): string {
let res = {};
templateVariables.forEach((value: string, key: string) => {
res[key] = value
});
let content: string = JSON.stringify(res);
writeFile(filePath, content);
return filePath
return content;
}

export function writeFile(filePath: string, content: string): void {
Expand Down

0 comments on commit 75fb3f3

Please sign in to comment.