Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create var file for template variables #7511

Merged
merged 9 commits into from
Jun 29, 2018

Conversation

arjgupta
Copy link
Member

No description provided.

res[key] = value
});
let content: string = JSON.stringify(res);
tl.writeFile(filePath, content);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a writeFile utility which will be useful in l0 mocking

templateVariables.forEach((value: string, key: string) => {
res[key] = value
});
let content: string = JSON.stringify(res);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test with special characters in values - space, quotes etc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this on ubuntu 18.04 and win 10. The var file created had whitespaces and quotes in its path.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also test for variable values containing special chars. They should be properly escaped when using Json.Stringify and packer should be able to consume them successfully

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@@ -84,6 +84,17 @@ export function readJsonFile(filePath: string): string {
return content;
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of random, you can also use Date.now() which is good enough

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using Date.now(), but since we create 2 separate var files one after the other, there were name collisions a few times. That is why I am using to Math.random.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@bishal-pdMSFT
Copy link
Contributor

I believe you will need to modify UTs.

@@ -1,132 +0,0 @@
import ma = require('vsts-task-lib/mock-answer');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @bishal-pdMSFT , I noticed that this file isn't being referenced anywhere. There is a typo in the filename ( notice the Linuxs ). There is another file with the correct name already present. Can you confirm if this isn't required ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is not being used anywhere, please delete it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -54,23 +54,23 @@ let a: any = <any>{
"code": 0,
"stdout": "{ \"some-key\": \"some-value\" }"
},
"packer validate -var resource_group=testrg -var storage_account=teststorage -var image_publisher=Canonical -var image_offer=UbuntuServer -var image_sku=14.04.4-LTS -var location=South India -var capture_name_prefix=Release-1 -var skip_clean=true -var script_relative_path=dir3/somedir/deploy.sh -var package_path=/tmp/dir1/somedir/dir2 -var package_name=dir2 -var script_arguments=\"subdir 1\" false -var subscription_id=sId -var client_id=spId -var client_secret=spKey -var tenant_id=tenant -var object_id= /tmp/tempdir/100/default.linux.template-fixed.json": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should modify test to ensure that these parameters are present in var file. I think test mocks Write-File to write to console. You can grep console output to ensure that this content is correct.
In this task, passing correct parameters is the most important aspect. Hence we need to test them thoroughly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added 2 tests for this. first one verifies for default template, the other one for custom template

@arjgupta arjgupta merged commit 7892a29 into master Jun 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants