Skip to content

Commit

Permalink
Add map variable to test and readme
Browse files Browse the repository at this point in the history
Signed-off-by: Reinhard Naegele <unguiculus@gmail.com>
  • Loading branch information
unguiculus committed Jan 24, 2020
1 parent 8ffd16e commit 377e3e1
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 13 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ vars:
# TF_VAR_<var>=value for commands that support them
foo: foovalue
templatedVar: "{{ .Params.param }}"
mapvar: |-
{
entry1 = {
value1 = testvalue1
value2 = true
}
entry2 = {
value1 = testvalue2
value2 = false
}
}
envs:
# Environment variables are added to the Terraform calls environment
BAR: barvalue
Expand Down Expand Up @@ -88,6 +99,17 @@ vars:
# TF_VAR_<var>=value for commands that support them
foo: foovalue
templatedVar: "myval"
mapvar: |-
{
entry1 = {
value1 = testvalue1
value2 = true
}
entry2 = {
value1 = testvalue2
value2 = false
}
}
envs:
# Environment variables are added to the Terraform calls environment
BAR: barvalue
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func Load(configFile string, params map[string]string) (*Config, error) {
}
cfg.BackendConfigs[key] = sb.String()
}

return cfg, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestLoad(t *testing.T) {
assert.NotNil(t, got)

assert.Equal(t, got.VarsFiles, []string{"testdata/testmodule/test.tfvars"})
assert.Equal(t, got.Vars, map[string]string{"foo": "foovalue", "templatedVar": "paramvalue"})
assert.Equal(t, got.Vars, map[string]string{"foo": "foovalue", "templatedVar": "paramvalue", "mapvar": "{\n value1 = \"testvalue\"\n value2 = true\n}"})
assert.Equal(t, got.Envs, map[string]string{"BAR": "barvalue", "TEMPLATED_ENV": "paramvalue"})
assert.Equal(t, got.BackendConfigs, map[string]string{
"backend_key": "be_key_foovalue_barvalue",
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/testdata/test_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ varsFiles:
vars:
foo: foovalue
templatedVar: "{{ .Params.param }}"
mapvar: |-
{
value1 = "testvalue"
value2 = true
}
envs:
BAR: barvalue
TEMPLATED_ENV: "{{ .Params.param }}"
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/testdata/testmodule/test.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ variable "foo" {}

variable "baz" {}

variable "mapvar" {
type = map(object({
value1 = string
value2 = bool
}))
}

output "foo" {
value = var.foo
}
Expand Down
29 changes: 19 additions & 10 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package integrationtest
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
Expand All @@ -38,34 +37,44 @@ func Test_e2e(t *testing.T) {

binary := buildBinary(tempDir)

output, err := runProcess(binary, "-c", "testdata/test_config.yaml", "init", "-no-color", "testdata/testmodule")
output, err := runProcess(binary, "-d", "-c", "testdata/test_config.yaml", "init", "-no-color", "testdata/testmodule")
fmt.Println(output)
assert.NoError(t, err)
assert.Contains(t, output, "Terraform has been successfully initialized!")

output, err = runProcess(binary, "-c", "testdata/test_config.yaml", "plan", "-no-color", "testdata/testmodule")
output, err = runProcess(binary, "-d", "-c", "testdata/test_config.yaml", "plan", "-no-color", "testdata/testmodule")
fmt.Println(output)
assert.NoError(t, err)
assert.Contains(t, output, "# null_resource.echo will be created")
assert.Contains(t, output, "Plan: 1 to add, 0 to change, 0 to destroy.")

output, err = runProcess(binary, "-c", "testdata/test_config.yaml", "apply", "-auto-approve", "-no-color", "testdata/testmodule")
output, err = runProcess(binary, "-d", "-c", "testdata/test_config.yaml", "apply", "-auto-approve", "-no-color", "testdata/testmodule")
fmt.Println(output)
assert.NoError(t, err)
assert.Contains(t, output, "baz = bazvalue")
assert.Contains(t, output, "foo = foovalue")
assert.Contains(t, output, `baz = bazvalue
foo = 42
mapvar = {
entry1 = {
value1 = testvalue1
value2 = true
}
entry2 = {
value1 = testvalue2
value2 = false
}
}`)
}

func buildBinary(dir string) string {
log.Println("Building application...")
fmt.Println("Building application...")
binary := filepath.Join(dir, "gotf")
output, err := runProcess("go", "build", "-o", binary, "..")
if err != nil {
log.Println(output)
fmt.Println(output)
panic(err)
}
log.Println("Build finished successfully")
log.Printf("Using binary for test: %v\n", binary)
fmt.Println("Build finished successfully")
fmt.Printf("Using binary for test: %v\n\n", binary)
return binary
}

Expand Down
15 changes: 13 additions & 2 deletions test/testdata/test_config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
varsFiles:
- testmodule/test.tfvars
vars:
foo: foovalue
foo: 42
mapvar: |-
{
entry1 = {
value1 = testvalue1
value2 = true
}
entry2 = {
value1 = testvalue2
value2 = false
}
}
envs:
bar: barvalue
BAR: barvalue
backendConfigs:
path: testdata/.tfstate/terraform.tfstate
6 changes: 6 additions & 0 deletions test/testdata/testmodule/test.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ variable "foo" {}

variable "baz" {}

variable "mapvar" {}

output "foo" {
value = var.foo
}
Expand All @@ -14,6 +16,10 @@ output "baz" {
value = var.baz
}

output "mapvar" {
value = var.mapvar
}

resource "null_resource" "echo" {
provisioner "local-exec" {
command = "echo foo=${var.foo}"
Expand Down

0 comments on commit 377e3e1

Please sign in to comment.