Skip to content

Commit 283aaab

Browse files
committed
reorg and add tests for optional attrs
1 parent 019bf40 commit 283aaab

File tree

8 files changed

+146
-17
lines changed

8 files changed

+146
-17
lines changed

examples/basic/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ provider "awscc" {
55
module "labels" {
66
source = "../.."
77

8-
name = "measurements"
9-
namespace = "link"
8+
name = var.name
9+
namespace = var.namespace
1010

11-
env = "sandbox"
12-
account = "sandbox001"
11+
env = var.env
12+
account = var.account
1313

1414
region = var.region
1515
tags = [

examples/basic/outputs.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
output "tags" {
22
value = module.labels.tags
3-
}
3+
}
4+
5+
output "id" {
6+
value = module.labels.id
7+
}

examples/basic/variables.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,27 @@ variable "region" {
33
default = "eu-west-1"
44
description = "What AWS region to deploy resources in."
55
}
6+
7+
variable "namespace" {
8+
type = string
9+
description = "namespace, which could be your organization name, e.g. amazon"
10+
default = "link"
11+
}
12+
13+
variable "env" {
14+
type = string
15+
description = "environment, e.g. 'sit', 'uat', 'prod' etc"
16+
default = "sandbox"
17+
}
18+
19+
variable "account" {
20+
type = string
21+
description = "account, which could be AWS Account Name or Number"
22+
default = "sandbox001"
23+
}
24+
25+
variable "name" {
26+
type = string
27+
description = "stack name"
28+
default = "measurements"
29+
}

locals.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
locals {
22
vars = {
3-
name = var.name
43
namespace = var.namespace
5-
env = var.env
64
account = var.account
5+
env = var.env
6+
name = var.name
77
}
88
non_empty_vars = [for k, v in local.vars : k if v != null]
99
non_empty_vars_map = { for k in local.non_empty_vars : k => local.vars[k] }

output.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
output "tags" {
32
value = local.tags
43
}

test/examples_basic_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gruntwork-io/terratest/modules/terraform"
7+
)
8+
9+
func Validate(t *testing.T, terraformOptions *terraform.Options) {
10+
defer terraform.Destroy(t, terraformOptions)
11+
terraform.InitAndApply(t, terraformOptions)
12+
}
13+
14+
func TestBasic(t *testing.T) {
15+
t.Parallel()
16+
17+
terraformOptions := &terraform.Options{
18+
TerraformDir: "../examples/basic",
19+
}
20+
21+
Validate(t, terraformOptions)
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gruntwork-io/terratest/modules/terraform"
7+
)
8+
9+
func TestFormattedTags(t *testing.T) {
10+
t.Parallel()
11+
12+
terraformOptions := &terraform.Options{
13+
TerraformDir: "../examples/formatted_tags",
14+
}
15+
16+
Validate(t, terraformOptions)
17+
}

test/label_test.go

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,95 @@
11
package test
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/gruntwork-io/terratest/modules/terraform"
8+
"github.com/stretchr/testify/assert"
79
)
810

9-
func validate(t *testing.T, terraformOptions *terraform.Options) {
11+
const (
12+
namespace_value = "mynamespace"
13+
account_value = "myaccount"
14+
env_value = "myenv"
15+
name_value = "myname"
16+
delimiter = "-"
17+
module_dir = "../"
18+
)
19+
20+
func TestAllVarsInId(t *testing.T) {
21+
all_vars_expected := []string{namespace_value, account_value, env_value, name_value}
22+
all_vars_expected_id := strings.Join(all_vars_expected, delimiter)
23+
24+
terraformOptions := &terraform.Options{
25+
TerraformDir: module_dir,
26+
Vars: map[string]interface{}{
27+
"namespace": namespace_value,
28+
"account": account_value,
29+
"env": env_value,
30+
"name": name_value,
31+
},
32+
}
1033
defer terraform.Destroy(t, terraformOptions)
1134
terraform.InitAndApply(t, terraformOptions)
35+
36+
// Validate `id` only made of optionally passed vars, env-name
37+
output_id := terraform.Output(t, terraformOptions, "id")
38+
assert.Equal(t, all_vars_expected_id, output_id)
39+
40+
// Validate `tags` only made of optionally passed vars, env-name
41+
output_tags := terraform.OutputListOfObjects(t, terraformOptions, "tags")
42+
assert.Equal(t, len(all_vars_expected), len(output_tags))
43+
44+
// Validate `tags_aws` made of all passed, namespace-account-env-name
45+
output_tags_aws := terraform.OutputMap(t, terraformOptions, "tags_aws")
46+
assert.Equal(t, len(all_vars_expected), len(output_tags_aws))
1247
}
1348

14-
func TestBasic(t *testing.T) {
15-
t.Parallel()
49+
func TestOptionalVarsInId(t *testing.T) {
50+
opt_vars_expected := []string{env_value, name_value}
51+
opt_vars_expected_id := strings.Join(opt_vars_expected, delimiter)
1652

1753
terraformOptions := &terraform.Options{
18-
TerraformDir: "../examples/basic",
54+
TerraformDir: module_dir,
55+
Vars: map[string]interface{}{
56+
"env": env_value,
57+
"name": name_value,
58+
},
1959
}
60+
defer terraform.Destroy(t, terraformOptions)
61+
terraform.InitAndApply(t, terraformOptions)
62+
63+
// Validate `id` only made of optionally passed vars, env-name
64+
output_id := terraform.Output(t, terraformOptions, "id")
65+
assert.Equal(t, opt_vars_expected_id, output_id)
2066

21-
validate(t, terraformOptions)
67+
// Validate `tags` only made of optionally passed vars, env-name
68+
output_tags := terraform.OutputListOfObjects(t, terraformOptions, "tags")
69+
assert.Equal(t, len(opt_vars_expected), len(output_tags))
70+
71+
// Validate `tags_aws` only made of optionally passed vars, env-name
72+
output_tags_aws := terraform.OutputMap(t, terraformOptions, "tags_aws")
73+
assert.Equal(t, len(opt_vars_expected), len(output_tags_aws))
2274
}
2375

24-
func TestFormattedTags(t *testing.T) {
25-
t.Parallel()
76+
func TestOptionalVarsInIdReordered(t *testing.T) {
77+
reordered_opt_vars_expected := []string{name_value, env_value}
78+
reordered_opt_vars_expected_id := strings.Join(reordered_opt_vars_expected, delimiter)
79+
reordered := []string{"name", "env"}
2680

2781
terraformOptions := &terraform.Options{
28-
TerraformDir: "../examples/formatted_tags",
82+
TerraformDir: module_dir,
83+
Vars: map[string]interface{}{
84+
"env": env_value,
85+
"name": name_value,
86+
"id_order": reordered,
87+
},
2988
}
89+
defer terraform.Destroy(t, terraformOptions)
90+
terraform.InitAndApply(t, terraformOptions)
3091

31-
validate(t, terraformOptions)
92+
// Validate `id` only made of optionally passed vars, env-name
93+
output_id_reordered := terraform.Output(t, terraformOptions, "id")
94+
assert.Equal(t, reordered_opt_vars_expected_id, output_id_reordered)
3295
}

0 commit comments

Comments
 (0)