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
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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.


### https://raw.github.com/github/gitignore/abad92dac5a4306f72242dae3bca6e277bce3615/Global/Vim.gitignore

Expand Down
36 changes: 0 additions & 36 deletions .kitchen.yml

This file was deleted.

21 changes: 18 additions & 3 deletions Dockerfile
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"
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.

FROM microsoft/terraform-test:${BUILD_TERRAFORM_VERSION}

ARG MODULE_NAME="terraform-azurerm-compute"
Expand All @@ -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
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.

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

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

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"]
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ group :test do
git 'https://github.com/Azure/terramodtest.git' do
gem 'terramodtest', :tag => 'v0.2.0'
end
gem 'test-kitchen', '1.16.0'
gem 'kitchen-terraform', '3.0.0'
end
50 changes: 32 additions & 18 deletions README.md
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)
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.


## Deploys 1+ Virtual Machines to your provided VNet

This Terraform module deploys Virtual Machines in Azure with the following characteristics:

Expand All @@ -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.

Expand Down Expand Up @@ -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.

Expand All @@ -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"
Expand Down Expand Up @@ -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.

Expand All @@ -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

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

Choose a reason for hiding this comment

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

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 I will put Gopkg.* in root.

$ dep ensure
$ cd ../..
```

Finally simply run it in local shell:

```sh
$ bundle install
$ rake build
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't you make make dep into task of rake build system?

Copy link
Contributor Author

@foreverXZC foreverXZC Jul 26, 2018

Choose a reason for hiding this comment

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

Expand All @@ -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)
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.


- [Docker](https://www.docker.com/community-edition#/download)

Expand All @@ -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)
25 changes: 6 additions & 19 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ require 'rspec/core/rake_task'
require 'bundler/setup'
require 'terramodtest'

namespace :presteps do
Copy link
Contributor

Choose a reason for hiding this comment

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

You may still keep presteps task to run dep restore.

Copy link
Contributor Author

@foreverXZC foreverXZC Jul 26, 2018

Choose a reason for hiding this comment

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

Unluckily there is no command named dep restore. Only dep init and dep ensure are available.

task :clean_up do
clean_up_kitchen
clean_up_terraform
end
end

namespace :static do
task :style do
style_tf
Expand All @@ -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")
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.

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' ]

Expand All @@ -54,4 +41,4 @@ task :e2e => [ 'integration:test' ]

task :default => [ 'build' ]

task :full => [ 'build', 'unit', 'e2e']
task :full => [ 'build', 'unit', 'e2e' ]
61 changes: 61 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -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.


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/)
Loading