Skip to content

Commit

Permalink
Add Terraform and Helm tests with Terratest
Browse files Browse the repository at this point in the history
Useful to check if Terraform configuration is valid and verify different
variables used by helm templates.
  • Loading branch information
aLekSer committed Apr 20, 2020
1 parent 3d22c68 commit 2d75daa
Show file tree
Hide file tree
Showing 4 changed files with 782 additions and 0 deletions.
7 changes: 7 additions & 0 deletions build/includes/terraform.mk
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,10 @@ endif
gcloud-terraform-destroy-cluster:
$(DOCKER_RUN) bash -c 'cd $(mount_path)/build/terraform/gke && \
terraform destroy -target module.helm_agones.helm_release.agones -auto-approve && sleep 60 && terraform destroy -auto-approve'

terraform-test: $(ensure-build-image)
ifndef GCP_PROJECT
$(eval GCP_PROJECT=$(shell sh -c "gcloud config get-value project 2> /dev/null"))
endif
$(DOCKER_RUN) bash -c 'cd $(mount_path)/test/terraform && GOPATH="$(PWD)/.." go test \
-timeout 1h -project $(GCP_PROJECT) $(ARGS)'
60 changes: 60 additions & 0 deletions test/terraform/gke_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2020 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package test

import (
"flag"
"testing"
"time"

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

var project string

func TestTerraformGKEInstallConfig(t *testing.T) {
t.Logf("Using project %s\n", project)
terraformOptions := &terraform.Options{
TerraformDir: "../../build/terraform/gke/",
Vars: map[string]interface{}{
"project": project,
"name": "terratest-cluster",
"values_file": "",
},
}

defer destroy(t, terraformOptions)

terraform.InitAndApply(t, terraformOptions)

output := terraform.Output(t, terraformOptions, "host")
assert.Contains(t, output, "https://")
}

func destroy(t *testing.T, options *terraform.Options) {
options.Targets = []string{"module.helm_agones.helm_release.agones"}
terraform.Destroy(t, options)
time.Sleep(30 * time.Second)
options.Targets = []string{}
terraform.Destroy(t, options)
}

func TestMain(m *testing.M) {
projectFlag := flag.String("project", "agones", "name of the proejct")
flag.Parse()
project = *projectFlag
m.Run()
}
10 changes: 10 additions & 0 deletions test/terraform/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/agones/agones/test/terraform

go 1.13

require (
github.com/gruntwork-io/terratest v0.26.6
github.com/stretchr/testify v1.5.1
k8s.io/api v0.17.0
k8s.io/apiextensions-apiserver v0.15.7
)
Loading

0 comments on commit 2d75daa

Please sign in to comment.