-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move integration test and add backend check (#5)
The integration test no longer builds and uses the binary but runs the Cobra command directly. Also, an additional check is added that makes sure the backend Terraform was initialized with matches the backend configuration of the current run. This avoids that state is accidentally pushed to the wrong backend when switching environments.
- Loading branch information
1 parent
accfc0a
commit 9a551a2
Showing
9 changed files
with
229 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package gotf | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestExecute(t *testing.T) { | ||
type testRun struct { | ||
args []string | ||
want []string | ||
wantErr bool | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
runs []testRun | ||
}{ | ||
{ | ||
name: "happy path", | ||
runs: []testRun{ | ||
{ | ||
args: []string{"-d", "-c", "testdata/test-config-prod.yaml", "-p", "env=prod", "-m", "testdata/01_testmodule", "init", "-no-color"}, | ||
want: []string{"Terraform has been successfully initialized!"}, | ||
}, | ||
{ | ||
args: []string{"-d", "-c", "testdata/test-config-prod.yaml", "-p", "env=prod", "-m", "testdata/01_testmodule", "plan", "-no-color"}, | ||
want: []string{ | ||
"# null_resource.echo will be created", | ||
"Plan: 1 to add, 0 to change, 0 to destroy.", | ||
}, | ||
}, | ||
{ | ||
args: []string{"-d", "-c", "testdata/test-config-prod.yaml", "-p", "env=prod", "-m", "testdata/01_testmodule", "apply", "-auto-approve", "-no-color"}, | ||
want: []string{`foo = 42 | ||
mapvar = { | ||
entry1 = { | ||
value1 = testvalue1 | ||
value2 = true | ||
} | ||
entry2 = { | ||
value1 = testvalue2 | ||
value2 = false | ||
} | ||
}`}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "backend check", | ||
runs: []testRun{ | ||
{ | ||
args: []string{"-d", "-c", "testdata/test-config-prod.yaml", "-p", "env=prod", "-m", "testdata/01_testmodule", "init", "-no-color"}, | ||
want: []string{"Terraform has been successfully initialized!"}, | ||
}, | ||
{ | ||
args: []string{"-d", "-c", "testdata/test-config-prod.yaml", "-p", "env=prod", "-m", "testdata/01_testmodule", "plan", "-no-color"}, | ||
want: []string{ | ||
"# null_resource.echo will be created", | ||
"Plan: 1 to add, 0 to change, 0 to destroy.", | ||
}, | ||
}, | ||
{ | ||
args: []string{"-d", "-c", "testdata/test-config-dev.yaml", "-p", "env=dev", "-m", "testdata/01_testmodule", "plan", "-no-color"}, | ||
want: []string{ | ||
"Configured backend does not match current environment", | ||
"Got: .terraform/terraform-testmodule-prod.tfstate", | ||
"Want: .terraform/terraform-testmodule-dev.tfstate", | ||
"Run terraform init -reconfigure!", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
args: []string{"-d", "-c", "testdata/test-config-dev.yaml", "-p", "env=dev", "-m", "testdata/01_testmodule", "init", "-reconfigure"}, | ||
want: []string{ | ||
"Terraform has been successfully initialized!", | ||
}, | ||
}, | ||
{ | ||
args: []string{"-d", "-c", "testdata/test-config-dev.yaml", "-p", "env=dev", "-m", "testdata/01_testmodule", "plan", "-no-color"}, | ||
want: []string{ | ||
"# null_resource.echo will be created", | ||
"Plan: 1 to add, 0 to change, 0 to destroy.", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
os.RemoveAll("testdata/01_testmodule/.terraform") | ||
|
||
tempDir, err := ioutil.TempDir("", "gotf") | ||
panicOnError(err) | ||
defer os.RemoveAll(tempDir) | ||
|
||
panicOnError(os.Setenv("XDG_CACHE_HOME", filepath.Join(tempDir, "test"))) | ||
|
||
for _, run := range tt.runs { | ||
got, err := runGotf(run.args) | ||
if run.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
for _, want := range run.want { | ||
assert.Contains(t, got, want) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func panicOnError(err error) { | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func runGotf(args []string) (string, error) { | ||
oldStdout := os.Stdout | ||
oldStderr := os.Stderr | ||
defer func() { | ||
os.Stdout = oldStdout | ||
os.Stderr = oldStderr | ||
}() | ||
|
||
command := newGotfCommand() | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
os.Stderr = w | ||
command.SetArgs(args) | ||
err := command.Execute() | ||
w.Close() | ||
bytes, _ := ioutil.ReadAll(r) | ||
output := string(bytes) | ||
println(output) | ||
return output, err | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bar = "bazvalue" |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
terraformVersion: "0.12.20" | ||
varFiles: | ||
- 01_testmodule/test-{{ .Params.env }}.tfvars | ||
vars: | ||
foo: 42 | ||
mapvar: |- | ||
{ | ||
entry1 = { | ||
value1 = testvalue1 | ||
value2 = true | ||
} | ||
entry2 = { | ||
value1 = testvalue2 | ||
value2 = false | ||
} | ||
} | ||
module_dir: "{{ base .Params.moduleDir }}" | ||
state_key: '{{ (splitn "_" 2 (base .Params.moduleDir))._1 }}' | ||
envs: | ||
BAR: barvalue | ||
backendConfigs: | ||
path: .terraform/terraform-{{ .Vars.state_key }}-prod.tfstate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.