-
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
Changes from 19 commits
ebb6cf4
32a231c
6cc3cb8
b280410
f66f021
c714ed1
cb199d0
53434c0
ae08618
625ffa2
b3a916f
0711017
9c07cf2
fdbfd38
64ba0c1
83526a0
32b085c
fd150c7
eb49a27
ce59e9c
24e50ca
8490781
7ddfd6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 commentThe 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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Great. |
||
FROM microsoft/terraform-test:${BUILD_TERRAFORM_VERSION} | ||
|
||
ARG MODULE_NAME="terraform-azurerm-compute" | ||
|
@@ -20,10 +20,25 @@ ENV ARM_TENANT_ID=${BUILD_ARM_TENANT_ID} | |
ENV ARM_TEST_LOCATION=${BUILD_ARM_TEST_LOCATION} | ||
ENV ARM_TEST_LOCATION_ALT=${BUILD_ARM_TEST_LOCATION_ALT} | ||
|
||
# Set work directory and generate ssh key | ||
RUN mkdir /usr/src/${MODULE_NAME} | ||
COPY . /usr/src/${MODULE_NAME} | ||
|
||
WORKDIR /usr/src/${MODULE_NAME} | ||
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 commentThe 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 commentThe 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 commentThe 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 commentThe 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 commentThe 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. |
||
RUN wget https://releases.hashicorp.com/terraform/0.11.7/terraform_0.11.7_linux_amd64.zip >/dev/null 2>&1 | ||
RUN unzip terraform_0.11.7_linux_amd64.zip >/dev/null | ||
RUN wget https://storage.googleapis.com/golang/go1.10.3.linux-amd64.tar.gz >/dev/null 2>&1 | ||
RUN tar -zxvf go1.10.3.linux-amd64.tar.gz -C /usr/local/ >/dev/null | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Will remove. |
||
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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. You can install |
||
RUN /bin/bash -c "go get github.com/gruntwork-io/terratest/modules/retry" | ||
RUN /bin/bash -c "go get github.com/gruntwork-io/terratest/modules/terraform" | ||
RUN /bin/bash -c "go get github.com/gruntwork-io/terratest/modules/test-structure" | ||
|
||
RUN ["bundle", "install", "--gemfile", "./Gemfile"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# terraform-azurerm-compute # | ||
[![Build Status](https://travis-ci.org/Azure/terraform-azurerm-compute.svg?branch=master)](https://travis-ci.org/Azure/terraform-azurerm-compute) | ||
# terraform-azurerm-compute | ||
|
||
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 commentThe 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 commentThe 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 commentThe 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 commentThe 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 commentThe 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. |
||
|
||
## Deploys 1+ Virtual Machines to your provided VNet | ||
|
||
This Terraform module deploys Virtual Machines in Azure with the following characteristics: | ||
|
||
|
@@ -14,8 +14,7 @@ This Terraform module deploys Virtual Machines in Azure with the following chara | |
|
||
> Note: Terraform module registry is incorrect in the number of required parameters since it only deems required based on variables with non-existent values. The actual minimum required variables depends on the configuration and is specified below in the usage. | ||
|
||
Simple Usage | ||
----- | ||
## Simple Usage | ||
|
||
This contains the bare minimum options to be configured for the VM to be provisioned. The entire code block provisions a Windows and a Linux VM, but feel free to delete one or the other and corresponding outputs. The outputs are also not necessary to provision, but included to make it convenient to know the address to connect to the VMs after provisioning completes. | ||
|
||
|
@@ -58,8 +57,7 @@ Provisions an Ubuntu Server 16.04-LTS VM and a Windows 2016 Datacenter Server VM | |
} | ||
``` | ||
|
||
Advanced Usage | ||
----- | ||
## Advanced Usage | ||
|
||
The following example illustrates some of the configuration options available to deploy a virtual machine. Feel free to remove the Linux or Windows modules and corresponding outputs. | ||
|
||
|
@@ -81,7 +79,7 @@ More specifically this provisions: | |
- Two Public IP addresses (one for each VM) | ||
- Opens up port 3389 for RDP access using the password as shown | ||
|
||
```hcl | ||
```hcl | ||
module "linuxservers" { | ||
source = "Azure/compute/azurerm" | ||
resource_group_name = "terraform-advancedvms" | ||
|
@@ -149,10 +147,10 @@ More specifically this provisions: | |
|
||
``` | ||
|
||
Test | ||
----- | ||
## Test | ||
|
||
### Configurations | ||
|
||
- [Configure Terraform for Azure](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/terraform-install-configure) | ||
- [Generate and add SSH Key](https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/) Save the key in ~/.ssh/id_rsa. This is not required for Windows deployments. | ||
|
||
|
@@ -161,9 +159,11 @@ 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) | ||
- [Terraform **(~> 0.11.7)**](https://www.terraform.io/downloads.html) | ||
- [Golang **(~> 1.10.3)**](https://golang.org/dl/) | ||
|
||
#### Quick Run | ||
|
||
|
@@ -173,7 +173,16 @@ We provide simple script to quickly set up module development environment: | |
$ curl -sSL https://raw.githubusercontent.com/Azure/terramodtest/master/tool/env_setup.sh | sudo bash | ||
``` | ||
|
||
Then simply run it in local shell: | ||
Then use dep ensure to install go packages: | ||
|
||
```sh | ||
$ cd $GOPATH/src/{directory_name}/test/compute | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need to specifically do this. All the steps should be supposed to run at the root folder of each module project. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I will put Gopkg.* in root. |
||
$ dep ensure | ||
$ cd ../.. | ||
``` | ||
|
||
Finally simply run it in local shell: | ||
|
||
```sh | ||
$ bundle install | ||
$ rake build | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't you make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will install dep in Dockerfile or in local environment and put dep into Rakefile. |
||
|
@@ -184,7 +193,7 @@ $ rake e2e | |
|
||
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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. OK I will ignore that. |
||
|
||
- [Docker](https://www.docker.com/community-edition#/download) | ||
|
||
|
@@ -208,11 +217,16 @@ This runs the end to end tests: | |
$ docker run --rm azure-compute /bin/bash -c "bundle install && rake e2e" | ||
``` | ||
|
||
Authors | ||
======= | ||
This runs the full tests: | ||
|
||
```sh | ||
$ docker run --rm azure-compute /bin/bash -c "bundle install && rake full" | ||
``` | ||
|
||
## Authors | ||
|
||
Originally created by [David Tesar](http://github.com/dtzar) | ||
|
||
License | ||
======= | ||
## License | ||
|
||
[MIT](LICENSE) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,6 @@ require 'rspec/core/rake_task' | |
require 'bundler/setup' | ||
require 'terramodtest' | ||
|
||
namespace :presteps do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may still keep presteps task to run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unluckily there is no command named |
||
task :clean_up do | ||
clean_up_kitchen | ||
clean_up_terraform | ||
end | ||
end | ||
|
||
namespace :static do | ||
task :style do | ||
style_tf | ||
|
@@ -26,21 +19,15 @@ namespace :static do | |
end | ||
|
||
namespace :integration do | ||
task :converge do | ||
kitchen_converge | ||
end | ||
task :verify do | ||
kitchen_verify | ||
end | ||
task :test do | ||
kitchen_test | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. OK I will move these go files outside. |
||
if not success | ||
raise "ERROR: Go test failed!\n".red | ||
end | ||
end | ||
end | ||
|
||
task :prereqs => [ 'presteps:clean_up' ] | ||
task :prereqs => [] | ||
|
||
task :validate => [ 'static:style', 'static:lint' ] | ||
|
||
|
@@ -54,4 +41,4 @@ task :e2e => [ 'integration:test' ] | |
|
||
task :default => [ 'build' ] | ||
|
||
task :full => [ 'build', 'unit', 'e2e'] | ||
task :full => [ 'build', 'unit', 'e2e' ] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Compute Example | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. |
||
|
||
Use terraform azure module "compute" to deploy one or more virtual machines on azure. Then use terratest to ssh to virtual machines. | ||
|
||
## Fixture | ||
|
||
These terraform files enable users to deploy one or more virtual machines on azure, as well as virtual network. To use these files, you should provide path to ssh public key file in [fixture/terraform.tfvars](/test/fixture/terraform.tfvars). You can just test the infrastructure code manually without terratest. | ||
|
||
## Compute | ||
|
||
This folder includes only one file. [compute/terraform_ssh_example_test.go](/test/compute/terraform_ssh_example_test.go) is the go test file which represents the whole process of testing the module. First, it uses terraform compute module to deploy virtual machines on azure. After that, it calls functions from terratest ssh section, so as to ssh to these virtual machines and check whether they are running properly. Next, everything will be cleaned up after validation. Of course you can write your own test code, or even take advantage of deprecated methods. Finally, in order to make this program work, you should provide your own ssh private key. | ||
|
||
## Running this module manually | ||
|
||
1. Sign up for [Azure](https://portal.azure.com/). | ||
|
||
1. Configure your Azure credentials. For instance, you may use [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli) and execute `az login`. | ||
|
||
1. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH`. | ||
|
||
1. Fill in blank of your ssh public key in [fixture/terraform.tfvars](/test/fixture/terraform.tfvars) and make sure your configuration is correct. | ||
|
||
1. Direct to folder [fixture](/test/fixture) and run `terraform init`. | ||
|
||
1. Run `terraform apply`. | ||
|
||
1. When you're done, run `terraform destroy`. | ||
|
||
## Running automated tests against this module | ||
|
||
1. Sign up for [Azure](https://portal.azure.com/). | ||
|
||
1. Configure your Azure credentials. For instance, you may use [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli) and execute `az login`. | ||
|
||
1. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH`. | ||
|
||
1. Install [Golang](https://golang.org/) and make sure this code is checked out into your `GOPATH`. | ||
|
||
1. Fill in blank of your ssh public key in [fixture/terraform.tfvars](/test/fixture/terraform.tfvars) and make sure your configuration is correct. | ||
|
||
1. Direct to folder [compute](/test/compute) and make sure all packages are installed, such as executing `go get github.com/gruntwork-io/terratest/modules/terraform`, etc. | ||
|
||
1. Run `go test -timeout timelimit -args username path/to/your/private/key`. For example, `go test -timeout 20m -args azureuser id_rsa`. Be aware that `-timeout` is set to 10 minutes by default and can be omitted, but it should be defined before `-args`. | ||
|
||
## Postscript | ||
|
||
This is a universal manual which is written based on official terratest document. To test the compute module using Mac or Linux more conveniently or to take advantage of docker, you may refer to [README.md/Test](#test). | ||
|
||
## Reference | ||
|
||
[Terraform Azure Compute Module](https://registry.terraform.io/modules/Azure/compute/azurerm/) | ||
|
||
[Terratest SSH Source Code](https://github.com/gruntwork-io/terratest/blob/master/test/terraform_ssh_example_test.go) | ||
|
||
[SSH Golang Document](https://godoc.org/golang.org/x/crypto/ssh) | ||
|
||
[SSH Client Connection Example in Golang](http://blog.ralch.com/tutorial/golang-ssh-connection/) | ||
|
||
[Azure Linux Virtual Machine Document](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/) | ||
|
||
[Azure Virtual Network Document](https://docs.microsoft.com/en-us/azure/virtual-network/) |
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.