Skip to content
Open
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.terraform*
terraform.tfstate*
terraform.tfvars
!examples/**/terraform.tfvars
*.tfvars
!examples/**/test_terraform.tfvars
*.conf
29 changes: 23 additions & 6 deletions examples/ex4-multiple-clusters/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,37 @@ provider "aws" {
region = var.region
}

module "network" {
source = "weibeld/kubeadm/aws//modules/network"
version = "~> 0.2"
tags = merge(local.additional_tags, {"Name" = "terraform-kubeadm"})
}

module "cluster_1" {
source = "weibeld/kubeadm/aws"
version = "~> 0.2"
source = "../.."
cluster_name = var.cluster_names[0]
vpc_id = module.network.vpc_id
subnet_id = module.network.subnet_id
tags = merge(local.additional_tags, {"Name" = var.cluster_names[0]})
}

module "cluster_2" {
source = "weibeld/kubeadm/aws"
version = "~> 0.2"
source = "../.."
cluster_name = var.cluster_names[1]
vpc_id = module.network.vpc_id
subnet_id = module.network.subnet_id
tags = merge(local.additional_tags, {"Name" = var.cluster_names[1]})
}

module "cluster_3" {
source = "weibeld/kubeadm/aws"
version = "~> 0.2"
source = "../.."
cluster_name = var.cluster_names[2]
vpc_id = module.network.vpc_id
subnet_id = module.network.subnet_id
tags = merge(local.additional_tags, {"Name" = var.cluster_names[2]})

}

locals {
additional_tags = {"Environment" = terraform.workspace}
}
66 changes: 66 additions & 0 deletions examples/ex4-multiple-clusters/test/aws_kubeadm_plan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package test

import (
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
"io/ioutil"
"path/filepath"
"testing"
)

// This approach compares the entire plan json output to a static json file in the test directory.
// The file needs to be updated as the configuration changes to pass the test. An improvement would
// be to get targeted plan output for specific resources and use jsonpath to test those resources
// exist in the plan.
func TestKubeAdmClusterConfig(t *testing.T) {
t.Parallel()

planFilePath := filepath.Join(".", "plan.out")

terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "..",
PlanFilePath: planFilePath,

// Variables to pass to our Terraform code using -var-file options
VarFiles: []string{"test_terraform.tfvars"},

// Disable colors in Terraform commands so its easier to parse stdout/stderr
NoColor: true,
// Disable logging
Logger: logger.New(logger.Discard),
})

// At the end of the test, run `terraform destroy` to clean up any resources that were created.
defer terraform.Destroy(t, terraformOptions)

// This will run `terraform init` and `terraform apply` and `terraform show` and return the plan
// json at `PlanFilePath` and fail the test if there are any errors.
planJson := terraform.InitAndPlanAndShow(t, terraformOptions)

// Use jsonpath to extract the expected json nodes on the instance from the plan. You can alternatively
// use https://github.com/hashicorp/terraform-json to get a concrete struct with all the types resolved.
var actualConfig []map[string]interface{}
var expectedConfig []map[string]interface{}

k8s.UnmarshalJSONPath(
t,
[]byte(planJson),
"{ }",
&actualConfig,
)

expectedBuf, _ := ioutil.ReadFile(filepath.Join(".", "plan_config.json"))

k8s.UnmarshalJSONPath(
t,
expectedBuf,
"{ }",
&expectedConfig,
)

assert.Equal(t, expectedConfig, actualConfig)

}
8 changes: 8 additions & 0 deletions examples/ex4-multiple-clusters/test/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/christopinka/terraform-aws-kubeadm/examples/ex4-multiple-clusters/test

go 1.15

require (
github.com/gruntwork-io/terratest v0.30.23
github.com/stretchr/testify v1.6.1
)
625 changes: 625 additions & 0 deletions examples/ex4-multiple-clusters/test/go.sum

Large diffs are not rendered by default.

Loading