Skip to content

Commit 007b41e

Browse files
brandonjbjellandantonbabenko
authored andcommitted
Adding tests for vpc, subnets, and route tables (terraform-aws-modules#31)
* Adding base-level tests for simple-example * gitignoring test kitchen internals * incorporating feedback from newcontext folks * comment clean up before PR * upgrading to kt 3.1.x * test repaired for kt 3 compat * removing the gemfile lock * making md linter happy and adjusting content to match test fixture * PR feedback and rubocop compliance
1 parent 9abb08d commit 007b41e

File tree

10 files changed

+140
-1
lines changed

10 files changed

+140
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.terraform
2-
terraform.tfstate
32
*.tfstate*
3+
.kitchen
4+
terraform.tfstate
45
terraform.tfvars
6+
Gemfile.lock

.kitchen.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
driver:
3+
name: "terraform"
4+
root_module_directory: "examples/test_fixture"
5+
6+
provisioner:
7+
name: "terraform"
8+
9+
platforms:
10+
- name: "aws"
11+
12+
verifier:
13+
name: "awspec"
14+
15+
suites:
16+
- name: "default"
17+
verifier:
18+
name: "awspec"
19+
patterns:
20+
- "test/integration/default/test_vpc.rb"

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.4.2

Gemfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
ruby '2.4.2'
4+
5+
source 'https://rubygems.org/' do
6+
gem 'aws-sdk', '~> 3.0.1'
7+
gem 'awspec', '~> 1.4.0'
8+
gem 'kitchen-terraform', '~> 3.1'
9+
gem 'kitchen-verifier-awspec', '~> 0.1.1'
10+
gem 'rhcl', '~> 0.1.0'
11+
end

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ Examples
9797
* [Complete VPC](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/complete-vpc)
9898
* Few tests and edge cases examples: [#46](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/issue-46-no-private-subnets), [#44](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/issue-44-asymmetric-private-subnets)
9999

100+
101+
Tests
102+
-------
103+
104+
This module has been packaged with [awspec](https://github.com/k1LoW/awspec) tests through test kitchen. To run them:
105+
106+
1. Install [rvm](https://rvm.io/rvm/install) and the ruby version specified in the [Gemfile](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/Gemfile).
107+
2. Install bundler and the gems from our Gemfile:
108+
```
109+
gem install bundler; bundle install
110+
```
111+
3. Test using `bundle exec kitchen test` from the root of the repo.
112+
113+
100114
Authors
101115
-------
102116

examples/test_fixture/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Test fixture of simple VPC
2+
3+
Configuration in this directory creates a set of VPC resources to be tested by test kitchen.
4+
5+
There is a public and private subnet created per availability zone in addition to single NAT Gateway shared between 2 availability zones.
6+
7+
## Usage
8+
9+
To run the tests, from the repo root execute:
10+
11+
```bash
12+
$ kitchen test
13+
...
14+
Finished in 4.25 seconds (files took 2.75 seconds to load)
15+
20 examples, 0 failures
16+
17+
Finished verifying <default-aws> (0m9.03s).
18+
-----> Kitchen is finished. (0m9.40s)
19+
```
20+
21+
This will destroy any existing test resources, create the resources afresh, run the tests, report back, and destroy the resources.

examples/test_fixture/main.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
provider "aws" {
2+
region = "${var.region}"
3+
}
4+
5+
data "aws_availability_zones" "available" {}
6+
7+
module "vpc" {
8+
source = "../.."
9+
name = "test-example"
10+
cidr = "10.0.0.0/16"
11+
azs = ["${data.aws_availability_zones.available.names[0]}", "${data.aws_availability_zones.available.names[1]}"]
12+
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
13+
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
14+
enable_nat_gateway = true
15+
single_nat_gateway = true
16+
17+
tags = {
18+
Owner = "user"
19+
Environment = "dev"
20+
}
21+
}

examples/test_fixture/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "region" {
2+
description = "Region we created the resources in."
3+
value = "${var.region}"
4+
}

examples/test_fixture/variables.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
variable "region" {
2+
default = "eu-west-1"
3+
}

test/integration/default/test_vpc.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
require 'awspec'
4+
require 'aws-sdk'
5+
require 'rhcl'
6+
7+
# should strive to randomize the region for more robust testing
8+
example_main = Rhcl.parse(File.open('examples/test_fixture/main.tf'))
9+
vpc_name = example_main['module']['vpc']['name']
10+
user_tag = example_main['module']['vpc']['tags']['Owner']
11+
environment_tag = example_main['module']['vpc']['tags']['Environment']
12+
state_file = 'terraform.tfstate.d/kitchen-terraform-default-aws/terraform.tfstate'
13+
tf_state = JSON.parse(File.open(state_file).read)
14+
region = tf_state['modules'][0]['outputs']['region']['value']
15+
ENV['AWS_REGION'] = region
16+
17+
ec2 = Aws::EC2::Client.new(region: region)
18+
azs = ec2.describe_availability_zones
19+
zone_names = azs.to_h[:availability_zones].first(2).map { |az| az[:zone_name] }
20+
21+
describe vpc(vpc_name.to_s) do
22+
it { should exist }
23+
it { should be_available }
24+
it { should have_tag('Name').value(vpc_name.to_s) }
25+
it { should have_tag('Owner').value(user_tag.to_s) }
26+
it { should have_tag('Environment').value(environment_tag.to_s) }
27+
it { should have_route_table("#{vpc_name}-public") }
28+
zone_names.each do |az|
29+
it { should have_route_table("#{vpc_name}-private-#{az}") }
30+
end
31+
end
32+
33+
zone_names.each do |az|
34+
describe subnet("#{vpc_name}-public-#{az}") do
35+
it { should exist }
36+
it { should be_available }
37+
it { should belong_to_vpc(vpc_name.to_s) }
38+
it { should have_tag('Name').value("#{vpc_name}-public-#{az}") }
39+
it { should have_tag('Owner').value(user_tag.to_s) }
40+
it { should have_tag('Environment').value(environment_tag.to_s) }
41+
end
42+
end

0 commit comments

Comments
 (0)