2017-08-08 21:16:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-05-30 14:48:34 +00:00
|
|
|
"os"
|
2017-08-08 21:16:20 +00:00
|
|
|
"os/exec"
|
|
|
|
"testing"
|
2017-05-30 14:48:34 +00:00
|
|
|
|
|
|
|
. "github.com/franela/goblin"
|
2017-08-08 21:16:20 +00:00
|
|
|
)
|
|
|
|
|
2017-05-30 14:48:34 +00:00
|
|
|
func TestPlugin(t *testing.T) {
|
|
|
|
g := Goblin(t)
|
|
|
|
|
|
|
|
g.Describe("CopyTfEnv", func() {
|
|
|
|
g.It("Should create copies of TF_VAR_ to lowercase", func() {
|
|
|
|
// Set some initial TF_VAR_ that are uppercase
|
|
|
|
os.Setenv("TF_VAR_SOMETHING", "some value")
|
|
|
|
os.Setenv("TF_VAR_SOMETHING_ELSE", "some other value")
|
2017-09-06 14:29:04 +00:00
|
|
|
os.Setenv("TF_VAR_BASE64", "dGVzdA==")
|
2017-05-30 14:48:34 +00:00
|
|
|
|
|
|
|
CopyTfEnv()
|
|
|
|
|
|
|
|
// Make sure new env vars exist with proper values
|
|
|
|
g.Assert(os.Getenv("TF_VAR_something")).Equal("some value")
|
|
|
|
g.Assert(os.Getenv("TF_VAR_something_else")).Equal("some other value")
|
2017-09-06 14:29:04 +00:00
|
|
|
g.Assert(os.Getenv("TF_VAR_base64")).Equal("dGVzdA==")
|
2017-05-30 14:48:34 +00:00
|
|
|
})
|
|
|
|
})
|
2018-02-14 15:57:57 +00:00
|
|
|
|
|
|
|
g.Describe("tfApply", func() {
|
|
|
|
g.It("Should return correct apply commands given the arguments", func() {
|
|
|
|
type args struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *exec.Cmd
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"default",
|
|
|
|
args{config: Config{}},
|
|
|
|
exec.Command("terraform", "apply", "plan.tfout"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with parallelism",
|
|
|
|
args{config: Config{Parallelism: 5}},
|
|
|
|
exec.Command("terraform", "apply", "-parallelism=5", "plan.tfout"),
|
|
|
|
},
|
2018-07-31 18:33:44 +00:00
|
|
|
{
|
|
|
|
"with targets",
|
|
|
|
args{config: Config{Targets: []string{"target1", "target2"}}},
|
|
|
|
exec.Command("terraform", "apply", "--target", "target1", "--target", "target2", "plan.tfout"),
|
|
|
|
},
|
2018-02-14 15:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
g.Assert(tfApply(tt.args.config)).Equal(tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
g.Describe("tfDestroy", func() {
|
|
|
|
g.It("Should return correct destroy commands given the arguments", func() {
|
|
|
|
type args struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *exec.Cmd
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"default",
|
|
|
|
args{config: Config{}},
|
|
|
|
exec.Command("terraform", "destroy", "-force"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with parallelism",
|
|
|
|
args{config: Config{Parallelism: 5}},
|
|
|
|
exec.Command("terraform", "destroy", "-parallelism=5", "-force"),
|
|
|
|
},
|
2018-07-31 18:33:44 +00:00
|
|
|
{
|
|
|
|
"with targets",
|
|
|
|
args{config: Config{Targets: []string{"target1", "target2"}}},
|
|
|
|
exec.Command("terraform", "destroy", "-target=target1", "-target=target2", "-force"),
|
|
|
|
},
|
2018-07-31 18:46:41 +00:00
|
|
|
{
|
|
|
|
"with vars",
|
|
|
|
args{config: Config{Vars: map[string]string{"username": "someuser", "password": "1pass"}}},
|
|
|
|
exec.Command("terraform", "destroy", "-var", "username=someuser", "-var", "password=1pass", "-force"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with var-files",
|
|
|
|
args{config: Config{VarFiles: []string{"common.tfvars", "prod.tfvars"}}},
|
|
|
|
exec.Command("terraform", "destroy", "-var-file=common.tfvars", "-var-file=prod.tfvars", "-force"),
|
|
|
|
},
|
2018-02-14 15:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
g.Assert(tfDestroy(tt.args.config)).Equal(tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-11-09 22:26:24 +00:00
|
|
|
|
|
|
|
g.Describe("tfValidate", func() {
|
|
|
|
g.It("Should return correct validate command", func() {
|
2019-11-10 00:05:01 +00:00
|
|
|
type args struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
2019-11-09 22:26:24 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-11-10 00:05:01 +00:00
|
|
|
args args
|
2019-11-09 22:26:24 +00:00
|
|
|
want *exec.Cmd
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"default",
|
2019-11-10 00:05:01 +00:00
|
|
|
args{config: Config{VarFiles: []string{"common.tfvars", "prod.tfvars"}}},
|
|
|
|
exec.Command("terraform", "validate"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with no vars",
|
|
|
|
args{config: Config{Vars: map[string]string{"var1": "value1", "var2": "value2"}}},
|
|
|
|
exec.Command("terraform", "validate"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with no var-files",
|
|
|
|
args{config: Config{VarFiles: []string{"common.tfvars", "prod.tfvars"}}},
|
2019-11-09 22:26:24 +00:00
|
|
|
exec.Command("terraform", "validate"),
|
2019-11-09 23:50:16 +00:00
|
|
|
},
|
2019-11-09 22:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
g.Assert(tfValidate()).Equal(tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2018-02-14 15:57:57 +00:00
|
|
|
|
|
|
|
g.Describe("tfPlan", func() {
|
|
|
|
g.It("Should return correct plan commands given the arguments", func() {
|
|
|
|
type args struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2018-07-27 07:32:19 +00:00
|
|
|
name string
|
|
|
|
args args
|
2018-02-14 15:57:57 +00:00
|
|
|
destroy bool
|
2018-07-27 07:32:19 +00:00
|
|
|
want *exec.Cmd
|
2018-02-14 15:57:57 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"default",
|
|
|
|
args{config: Config{}},
|
|
|
|
false,
|
|
|
|
exec.Command("terraform", "plan", "-out=plan.tfout"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"destroy",
|
|
|
|
args{config: Config{}},
|
|
|
|
true,
|
|
|
|
exec.Command("terraform", "plan", "-destroy"),
|
|
|
|
},
|
2018-07-31 18:46:41 +00:00
|
|
|
{
|
|
|
|
"with vars",
|
|
|
|
args{config: Config{Vars: map[string]string{"username": "someuser", "password": "1pass"}}},
|
|
|
|
false,
|
|
|
|
exec.Command("terraform", "plan", "-out=plan.tfout", "-var", "username=someuser", "-var", "password=1pass"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with var-files",
|
|
|
|
args{config: Config{VarFiles: []string{"common.tfvars", "prod.tfvars"}}},
|
|
|
|
false,
|
|
|
|
exec.Command("terraform", "plan", "-out=plan.tfout", "-var-file=common.tfvars", "-var-file=prod.tfvars"),
|
|
|
|
},
|
2018-02-14 15:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
g.Assert(tfPlan(tt.args.config, tt.destroy)).Equal(tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-02-04 17:21:54 +00:00
|
|
|
g.Describe("tfFmt", func() {
|
|
|
|
g.It("Should return correct fmt commands given the arguments", func() {
|
|
|
|
type args struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
affirmative := true
|
|
|
|
negative := false
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *exec.Cmd
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"default",
|
|
|
|
args{config: Config{}},
|
|
|
|
exec.Command("terraform", "fmt"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with list",
|
|
|
|
args{config: Config{FmtOptions: FmtOptions{List: &affirmative}}},
|
|
|
|
exec.Command("terraform", "fmt", "-list=true"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with write",
|
|
|
|
args{config: Config{FmtOptions: FmtOptions{Write: &affirmative}}},
|
|
|
|
exec.Command("terraform", "fmt", "-write=true"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with diff",
|
|
|
|
args{config: Config{FmtOptions: FmtOptions{Diff: &affirmative}}},
|
|
|
|
exec.Command("terraform", "fmt", "-diff=true"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with check",
|
|
|
|
args{config: Config{FmtOptions: FmtOptions{Check: &affirmative}}},
|
|
|
|
exec.Command("terraform", "fmt", "-check=true"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with combination",
|
|
|
|
args{config: Config{FmtOptions: FmtOptions{
|
|
|
|
List: &negative,
|
|
|
|
Write: &negative,
|
|
|
|
Diff: &affirmative,
|
|
|
|
Check: &affirmative,
|
|
|
|
}}},
|
|
|
|
exec.Command("terraform", "fmt", "-list=false", "-write=false", "-diff=true", "-check=true"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
g.Assert(tfFmt(tt.args.config)).Equal(tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-07-17 13:48:56 +00:00
|
|
|
|
|
|
|
g.Describe("tfDataDir", func() {
|
|
|
|
g.It("Should override the terraform data dir environment variable when provided", func() {
|
|
|
|
type args struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-08-14 16:06:35 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *exec.Cmd
|
2019-07-17 13:48:56 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"with TerraformDataDir",
|
|
|
|
args{config: Config{TerraformDataDir: ".overriden_terraform_dir"}},
|
|
|
|
exec.Command("terraform", "apply", ".overriden_terraform_dir.plan.tfout"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with TerraformDataDir value as .terraform",
|
|
|
|
args{config: Config{TerraformDataDir: ".terraform"}},
|
|
|
|
exec.Command("terraform", "apply", "plan.tfout"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"without TerraformDataDir",
|
|
|
|
args{config: Config{}},
|
|
|
|
exec.Command("terraform", "apply", "plan.tfout"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2019-08-14 16:06:35 +00:00
|
|
|
os.Setenv("TF_DATA_DIR", tt.args.config.TerraformDataDir)
|
2019-07-17 13:48:56 +00:00
|
|
|
applied := tfApply(tt.args.config)
|
|
|
|
|
|
|
|
g.Assert(applied).Equal(tt.want)
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2017-05-30 14:48:34 +00:00
|
|
|
}
|