Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Upgrade the test to be based on go module and add Terraform plan test #49

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ terraform.tfvars
# Compiled files
*.tfstate
*.tfstate.backup
.terraform.lock.hcl
.DS_Store
terraform.log

Expand Down Expand Up @@ -34,3 +35,4 @@ tags

# Ruby download package lock file.
Gemfile.lock
go.sum
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ ENV PATH /usr/local/go/bin:$GOPATH/bin:$PATH
RUN go get github.com/katbyte/terrafmt
RUN /bin/bash -c "curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh"

RUN ["bundle", "update", "--bundler"]
RUN ["bundle", "install", "--gemfile", "./Gemfile"]
RUN curl -sL https://aka.ms/InstallAzureCLIDeb | bash
156 changes: 0 additions & 156 deletions Gopkg.lock

This file was deleted.

34 changes: 0 additions & 34 deletions Gopkg.toml

This file was deleted.

38 changes: 29 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,26 @@ require 'bundler/setup'
require 'terramodtest'

namespace :presteps do
task :fmt do
puts "Format Terraform code.\n"
success = system('terraform fmt --recursive .')
if not success
raise "ERROR: Terraform format failed.\n".red
end
end

task :ensure do
puts "Using dep ensure to install required go packages.\n"
success = system ("dep ensure")
puts "Downloading missing go modules and remove unused ones.\n"
success = system('go get github.com/katbyte/terrafmt && go mod tidy')
if not success
raise "ERROR: Dep ensure failed!\n".red
raise "ERROR: Failed to tidy go depedencies!\n".red
end
end

task :clean do
puts "Clean out the temporary terraform files in test folder.\n"
FileUtils.rm_r(['./test/fixture/.terraform', './test/fixture/.terraform.lock.hcl'], force: true)
end
end

namespace :static do
Expand All @@ -34,26 +47,33 @@ namespace :static do
end
end

namespace :integration do
task :test do
success = system ("go test -v ./test/ -timeout 20m")
namespace :test do
task :unit do
success = system ("go test -v ./test/unit/ -timeout 20m")
if not success
raise "ERROR: Go test failed!\n".red
end
end

task :integration do
success = system ("go test -v ./test/integration -timeout 20m")
if not success
raise "ERROR: Go test failed!\n".red
end
end
end

task :prereqs => [ 'presteps:ensure' ]
task :prereqs => [ 'presteps:fmt', 'presteps:ensure', 'presteps:clean' ]

task :validate => [ 'static:style', 'static:lint', 'static:readme_style','static:fixture_style' ]

task :format => [ 'static:format' ]

task :build => [ 'prereqs', 'validate' ]

task :unit => []
task :unit => [ 'build', 'test:unit' ]

task :e2e => [ 'integration:test' ]
task :e2e => [ 'build', 'test:integration' ]

task :default => [ 'build' ]

Expand Down
17 changes: 17 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/Azure/terraform-azurerm-vnet

go 1.16

require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/google/go-cmp v0.5.2 // indirect
github.com/gruntwork-io/terratest v0.36.3
github.com/imdario/mergo v0.3.9 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/zclconf/go-cty v1.5.1 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
55 changes: 55 additions & 0 deletions test/integration/terrraform_vnet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package test

import (
"testing"

"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/gruntwork-io/terratest/modules/test-structure"
)

func TestTerraformVnet(t *testing.T) {
t.Parallel()

fixtureFolder := "../fixture"

// Deploy the example
test_structure.RunTestStage(t, "setup", func() {
terraformOptions := configureTerraformOptions(t, fixtureFolder)

// Save the options so later test stages can use them
test_structure.SaveTerraformOptions(t, fixtureFolder, terraformOptions)

// This will init and apply the resources and fail the test if there are any errors
terraform.InitAndApply(t, terraformOptions)
})

// Check whether the length of output meets the requirement
test_structure.RunTestStage(t, "validate", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, fixtureFolder)

string := terraform.Output(t, terraformOptions, "test_vnet_id")
if len(string) <= 0 {
t.Fatal("Wrong output")
}
})

// At the end of the test, clean up any resources that were created
test_structure.RunTestStage(t, "teardown", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, fixtureFolder)
terraform.Destroy(t, terraformOptions)
})

}

func configureTerraformOptions(t *testing.T, fixtureFolder string) *terraform.Options {

terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: fixtureFolder,

// Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{},
}

return terraformOptions
}
17 changes: 17 additions & 0 deletions test/unit/plan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package unit

import (
"testing"

"github.com/gruntwork-io/terratest/modules/terraform"
)

func TestPlan(t *testing.T) {
tfOptions := &terraform.Options{
TerraformDir: "../fixture",
Vars: map[string]interface{}{},
}

terraform.Init(t, tfOptions)
terraform.Plan(t, tfOptions)
}