-
Notifications
You must be signed in to change notification settings - Fork 304
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
Add Terratest Example for Compute Module #66
Conversation
* build test * build test * build test * build test * build test * build test * Add Terratest * Add Terratest * Add Terratest
Removed kitchen terraform and added terratest as travis CI testing. |
After hiding environment variables, the build fails because the log demonstrates that encrypted environment variables have been removed for security reasons. Although it passes in my own repo, it seems impossible to pass here because pull request from a fork is considered not trustful and thus cannot use encrypted environment variables such as subscription ID, etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please address the issues in the PR. I would like to go through the left of this PR with you to save time in typing comment.
.travis.yml
Outdated
@@ -27,5 +27,5 @@ jobs: | |||
if: type = push AND branch = master | |||
install: true | |||
script: | |||
- docker build --build-arg BUILD_TERRAFORM_VERSION=${TERRAFORM_VERSION} --build-arg BUILD_ARM_SUBSCRIPTION_ID=$ARM_SUBSCRIPTION_ID --build-arg BUILD_ARM_CLIENT_ID=$ARM_CLIENT_ID --build-arg BUILD_ARM_CLIENT_SECRET=$ARM_CLIENT_SECRET --build-arg BUILD_ARM_TENANT_ID=$ARM_TENANT_ID -t ${IMAGE_NAME} . | |||
- docker build --build-arg BUILD_TERRAFORM_VERSION=${TERRAFORM_VERSION} --build-arg BUILD_ARM_SUBSCRIPTION_ID=$ARM_SUBSCRIPTION_ID --build-arg BUILD_ARM_CLIENT_ID=$ARM_CLIENT_ID --build-arg BUILD_ARM_CLIENT_SECRET=$ARM_CLIENT_SECRET --build-arg BUILD_ARM_TENANT_ID=$ARM_TENANT_ID -t ${IMAGE_NAME} . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra white space at the end of line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will delete.
Dockerfile
Outdated
@@ -1,5 +1,5 @@ | |||
# Pull the base image with given version. | |||
ARG BUILD_TERRAFORM_VERSION="0.11.1" | |||
ARG BUILD_TERRAFORM_VERSION="0.11.3" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be 0.11.7 now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, 0.11.7 is not released in docker hub, which means if we set terraform version as 0.11.7 here, an error occurs showing that no such kind of docker image.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0.11.7 was pushed, go ahead to use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great.
Dockerfile
Outdated
RUN ssh-keygen -q -t rsa -b 4096 -f $HOME/.ssh/id_rsa | ||
|
||
WORKDIR /usr/src/${MODULE_NAME} | ||
# Install new version of terraform and golang |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's already contained in base image, we don't need it any more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will remove.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes after I guarantee that the new version of docker image runs properly,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not matter coz I will not merge it until you verified everything from E2E.
Dockerfile
Outdated
RUN mv terraform /usr/local/bin | ||
|
||
# Install required go packages | ||
ENV GOPATH $HOME/terratest/ssh |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The environment variables related to Go should be set while baking base image.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will put in base image.
README.md
Outdated
|
||
Deploys 1+ Virtual Machines to your provided VNet | ||
================================================= | ||
[![Build Status](https://travis-ci.org/foreverXZC/terraform-azurerm-compute.svg?branch=master)](https://travis-ci.org/foreverXZC/terraform-azurerm-compute) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The badge should not be yours. Please keep the original one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please change it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes after it passes the build.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validation should always happen after you make any change. And merge will never happen if you don't do any validation.
README.md
Outdated
``` | ||
|
||
### Docker | ||
|
||
We provide a Dockerfile to build a new image based `FROM` the `microsoft/terraform-test` Docker hub image which adds additional tools / packages specific for this module (see Custom Image section). Alternatively use only the `microsoft/terraform-test` Docker hub image [by using these instructions](https://github.com/Azure/terraform-test). | ||
|
||
#### Prerequisites | ||
#### Prerequisites (Docker) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's already under Docker section, don't need to have Docker any more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VS Code considers it as the same heading as the previous one. Thus it thinks the heading is duplicated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I will ignore that.
README.md
Outdated
@@ -161,6 +159,7 @@ We provide 2 ways to build, run, and test the module on a local development mach | |||
### Native (Mac/Linux) | |||
|
|||
#### Prerequisites | |||
|
|||
- [Ruby **(~> 2.3)**](https://www.ruby-lang.org/en/downloads/) | |||
- [Bundler **(~> 1.15)**](https://bundler.io/) | |||
- [Terraform **(~> 0.11.0)**](https://www.terraform.io/downloads.html) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0.11.7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will change.
README.md
Outdated
@@ -161,6 +159,7 @@ We provide 2 ways to build, run, and test the module on a local development mach | |||
### Native (Mac/Linux) | |||
|
|||
#### Prerequisites | |||
|
|||
- [Ruby **(~> 2.3)**](https://www.ruby-lang.org/en/downloads/) | |||
- [Bundler **(~> 1.15)**](https://bundler.io/) | |||
- [Terraform **(~> 0.11.0)**](https://www.terraform.io/downloads.html) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go SDK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will add.
README.md
Outdated
@@ -170,21 +169,22 @@ We provide 2 ways to build, run, and test the module on a local development mach | |||
We provide simple script to quickly set up module development environment: | |||
|
|||
```sh | |||
$ curl -sSL https://raw.githubusercontent.com/Azure/terramodtest/master/tool/env_setup.sh | sudo bash | |||
curl -sSL https://raw.githubusercontent.com/Azure/terramodtest/master/tool/env_setup.sh | sudo bash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you verify if this script needs to be updated for new testing environment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes the script should be modified if we introduce Go SDK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, "export" commands in script only affects environment variables inside the script, which means users have to set global environment variables by themselves. Will add export commands here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that's the case, you can try source
command in linux.
Rakefile
Outdated
end | ||
task :destroy do | ||
kitchen_destroy | ||
success = system ("go test -v terratest/ssh/terraform_ssh_example_test.go -timeout 20m -args azureuser ~/.ssh/id_rsa") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test_folder_name/**/*_test.go?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that this format using * does not work...
terratest/README.md
Outdated
@@ -0,0 +1,57 @@ | |||
# Compute Example |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please merge it with README in root folder, remove unnecessary content like how to install go and terraform.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes after I ensure that the script runs correctly,
terratest/compute/main.tf
Outdated
@@ -0,0 +1,41 @@ | |||
provider "random" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put all the tf code for testing into ./test/fixture
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will change.
package test | ||
|
||
import ( | ||
"io/ioutil" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use 2 whitespaces as indention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK will modify. Nevertheless, although most programming languages use a tab or four white spaces as indentation, terraform format uses two white spaces, which seems quite interesting but a bit weird.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not true. Many languages(C++, JS) uses 2 whitespaces. Anyway, let's be consistent here.
@foreverXZC please remove the .testdata folder and add it into .gitignore. |
Dockerfile
Outdated
@@ -1,5 +1,5 @@ | |||
# Pull the base image with given version. | |||
ARG BUILD_TERRAFORM_VERSION="0.11.1" | |||
ARG BUILD_TERRAFORM_VERSION="0.11.3" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0.11.7 was pushed, go ahead to use it.
Dockerfile
Outdated
RUN ssh-keygen -q -t rsa -b 4096 -f $HOME/.ssh/id_rsa | ||
|
||
WORKDIR /usr/src/${MODULE_NAME} | ||
# Install new version of terraform and golang |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove them?
Dockerfile
Outdated
RUN mv terraform /usr/local/bin | ||
|
||
# Install required go packages | ||
ENV GOPATH $HOME/go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should remove the steps contained in base image.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will remove.
Dockerfile
Outdated
# Install required go packages | ||
ENV GOPATH $HOME/go | ||
ENV PATH /usr/local/go/bin:$PATH | ||
RUN /bin/bash -c "go get github.com/gruntwork-io/terratest/modules/ssh" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these pkgs should be restored through go dep.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately we do not have go dep installed in base image, so now maybe I have to install go dep here in Dockerfile.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can install dep
in this Dockerfile, that's OK. But you should remove the Go packages in Dockerfile, they should be restored by 'dep ensure'.
README.md
Outdated
|
||
Deploys 1+ Virtual Machines to your provided VNet | ||
================================================= | ||
[![Build Status](https://travis-ci.org/foreverXZC/terraform-azurerm-compute.svg?branch=master)](https://travis-ci.org/foreverXZC/terraform-azurerm-compute) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please change it?
Rakefile
Outdated
end | ||
task :destroy do | ||
kitchen_destroy | ||
success = system ("go test -v ./test/compute/ -timeout 20m -args azureuser ~/.ssh/id_rsa") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
./test should be enough here, compute subfolder is meaningless in compute project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I will move these go files outside.
test/README.md
Outdated
@@ -0,0 +1,61 @@ | |||
# Compute Example |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you still keep this README? You should remove merge the testing related part into README.md under root folder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main README seems enough, so now I think this one can be removed.
test/compute/Gopkg.lock
Outdated
@@ -0,0 +1,158 @@ | |||
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't use compute
folder under test folder, this project is for compute only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes will move go files outside.
test/compute/Gopkg.lock
Outdated
@@ -0,0 +1,158 @@ | |||
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gopkg.lock and Gopkg.toml can be directly put under root folder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.
.gitignore
Outdated
@@ -9,7 +9,7 @@ terraform.tfvars | |||
|
|||
# Module directory | |||
.terraform/ | |||
|
|||
.test-data/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed vendor folder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will add.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
@foreverXZC let me know if e2e validation is successful. |
After I merged, the build passed. |
This example uses Terratest to test compute module. All introduction and usages are included in Terratest/README.md.