Browse Source

Refactor all tests to utilize goblin

pull/58/head
Jacob McCann 7 years ago
parent
commit
3e6f75540f
  1. 85
      plugin_test.go

85
plugin_test.go

@ -3,16 +3,36 @@ package main
import ( import (
"os" "os"
"os/exec" "os/exec"
"reflect"
"testing" "testing"
. "github.com/franela/goblin" . "github.com/franela/goblin"
) )
func Test_destroyCommand(t *testing.T) { 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")
os.Setenv("TF_VAR_BASE64", "dGVzdA==")
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")
g.Assert(os.Getenv("TF_VAR_base64")).Equal("dGVzdA==")
})
})
g.Describe("tfApply", func() {
g.It("Should return correct apply commands given the arguments", func() {
type args struct { type args struct {
config Config config Config
} }
tests := []struct { tests := []struct {
name string name string
args args args args
@ -21,32 +41,32 @@ func Test_destroyCommand(t *testing.T) {
{ {
"default", "default",
args{config: Config{}}, args{config: Config{}},
exec.Command("terraform", "destroy", "-force"), exec.Command("terraform", "apply", "plan.tfout"),
}, },
{ {
"with targets", "with targets",
args{config: Config{Targets: []string{"target1", "target2"}}}, args{config: Config{Targets: []string{"target1", "target2"}}},
exec.Command("terraform", "destroy", "-target=target1", "-target=target2", "-force"), exec.Command("terraform", "apply", "--target", "target1", "--target", "target2", "plan.tfout"),
}, },
{ {
"with parallelism", "with parallelism",
args{config: Config{Parallelism: 5}}, args{config: Config{Parallelism: 5}},
exec.Command("terraform", "destroy", "-parallelism=5", "-force"), exec.Command("terraform", "apply", "-parallelism=5", "plan.tfout"),
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { g.Assert(tfApply(tt.args.config)).Equal(tt.want)
if got := tfDestroy(tt.args.config); !reflect.DeepEqual(got, tt.want) {
t.Errorf("destroyCommand() = %v, want %v", got, tt.want)
} }
}) })
} })
}
func Test_applyCommand(t *testing.T) { g.Describe("tfDestroy", func() {
g.It("Should return correct destroy commands given the arguments", func() {
type args struct { type args struct {
config Config config Config
} }
tests := []struct { tests := []struct {
name string name string
args args args args
@ -55,32 +75,32 @@ func Test_applyCommand(t *testing.T) {
{ {
"default", "default",
args{config: Config{}}, args{config: Config{}},
exec.Command("terraform", "apply", "plan.tfout"), exec.Command("terraform", "destroy", "-force"),
}, },
{ {
"with targets", "with targets",
args{config: Config{Targets: []string{"target1", "target2"}}}, args{config: Config{Targets: []string{"target1", "target2"}}},
exec.Command("terraform", "apply", "--target", "target1", "--target", "target2", "plan.tfout"), exec.Command("terraform", "destroy", "-target=target1", "-target=target2", "-force"),
}, },
{ {
"with parallelism", "with parallelism",
args{config: Config{Parallelism: 5}}, args{config: Config{Parallelism: 5}},
exec.Command("terraform", "apply", "-parallelism=5", "plan.tfout"), exec.Command("terraform", "destroy", "-parallelism=5", "-force"),
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { g.Assert(tfDestroy(tt.args.config)).Equal(tt.want)
if got := tfApply(tt.args.config); !reflect.DeepEqual(got, tt.want) {
t.Errorf("applyCommand() = %v, want %v", got, tt.want)
} }
}) })
} })
}
func Test_planCommand(t *testing.T) { g.Describe("tfPlan", func() {
g.It("Should return correct plan commands given the arguments", func() {
type args struct { type args struct {
config Config config Config
} }
tests := []struct { tests := []struct {
name string name string
args args args args
@ -100,31 +120,10 @@ func Test_planCommand(t *testing.T) {
exec.Command("terraform", "plan", "-destroy"), exec.Command("terraform", "plan", "-destroy"),
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { g.Assert(tfPlan(tt.args.config, tt.destroy)).Equal(tt.want)
if got := tfPlan(tt.args.config, tt.destroy); !reflect.DeepEqual(got, tt.want) {
t.Errorf("planCommand() = %v, want %v", got, tt.want)
}
})
}
} }
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")
os.Setenv("TF_VAR_BASE64", "dGVzdA==")
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")
g.Assert(os.Getenv("TF_VAR_base64")).Equal("dGVzdA==")
}) })
}) })
} }

Loading…
Cancel
Save