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

Add Terratest Example for Compute Module #66

Merged
merged 23 commits into from
Jul 28, 2018
Merged

Conversation

foreverXZC
Copy link
Contributor

This example uses Terratest to test compute module. All introduction and usages are included in Terratest/README.md.

@JunyiYi JunyiYi added the review label Jul 9, 2018
@metacpp metacpp self-requested a review July 10, 2018 00:44
* build test

* build test

* build test

* build test

* build test

* build test

* Add Terratest

* Add Terratest

* Add Terratest
@foreverXZC
Copy link
Contributor Author

Removed kitchen terraform and added terratest as travis CI testing.
Trivial things still need to be modified, including readme as well as some pieces of code and configuration.

@foreverXZC
Copy link
Contributor Author

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.

Copy link
Contributor

@metacpp metacpp left a 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} .
Copy link
Contributor

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.

Copy link
Contributor Author

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"
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will remove.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you remove them?

Copy link
Contributor Author

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,

Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will change.

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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)
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

0.11.7

Copy link
Contributor Author

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Go SDK.

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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")
Copy link
Contributor

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?

Copy link
Contributor Author

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...

@@ -0,0 +1,57 @@
# Compute Example
Copy link
Contributor

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.

Copy link
Contributor Author

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,

@@ -0,0 +1,41 @@
provider "random" {
Copy link
Contributor

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

Copy link
Contributor Author

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"
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@metacpp
Copy link
Contributor

metacpp commented Jul 19, 2018

@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"
Copy link
Contributor

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
Copy link
Contributor

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
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 remove the steps contained in base image.

Copy link
Contributor Author

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"
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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)
Copy link
Contributor

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")
Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@@ -0,0 +1,158 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@@ -0,0 +1,158 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.


Copy link
Contributor

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.

Copy link
Contributor Author

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/
Copy link
Contributor

Choose a reason for hiding this comment

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

Missed vendor folder.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will add.

Copy link
Contributor

@metacpp metacpp left a comment

Choose a reason for hiding this comment

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

LGTM.

@metacpp
Copy link
Contributor

metacpp commented Jul 27, 2018

@foreverXZC let me know if e2e validation is successful.

@foreverXZC foreverXZC merged commit 627c6c2 into Azure:master Jul 28, 2018
@foreverXZC
Copy link
Contributor Author

After I merged, the build passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants