-
Notifications
You must be signed in to change notification settings - Fork 8
/
planner_test.go
159 lines (135 loc) · 4.36 KB
/
planner_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"os"
"reflect"
"testing"
)
func TestCreate(t *testing.T) {
dir := createDir(t)
defer os.RemoveAll(dir)
content := `resource "null_resource" "first" {}
resource "null_resource" "second" {}`
if err := os.WriteFile(dir+"/main.tf", []byte(content), 0644); err != nil {
t.Fatal(err)
}
if err := terraformExec(config{}, true, []string{}, "init"); err != nil {
t.Fatalf("terraform init failed with %s\n", err)
}
want := []ResChange{
{"null_resource.first", "null_resource", Change{[]changeAction{create}}},
{"null_resource.second", "null_resource", Change{[]changeAction{create}}},
}
got, err := changes(config{}, []string{})
if err != nil {
t.Fatalf("failed computing changes")
}
if !reflect.DeepEqual(got, want) {
t.Errorf("changes() = %q, want %q", got, want)
}
}
func TestDelete(t *testing.T) {
dir := createDir(t)
defer os.RemoveAll(dir)
content := `resource "null_resource" "first" {}
resource "null_resource" "second" {}`
prepareState(dir, content, t)
if err := os.WriteFile(dir+"/main.tf", []byte("\n"), 0644); err != nil {
t.Fatal(err)
}
want := []ResChange{
{"null_resource.first", "null_resource", Change{[]changeAction{del}}},
{"null_resource.second", "null_resource", Change{[]changeAction{del}}},
}
got, err := changes(config{}, []string{})
if err != nil {
t.Fatalf("failed computing changes")
}
if !reflect.DeepEqual(got, want) {
t.Errorf("changes() = %q, want %q", got, want)
}
}
func TestNoOp(t *testing.T) {
dir := createDir(t)
defer os.RemoveAll(dir)
content := `resource "null_resource" "first" {}
resource "null_resource" "second" {}`
prepareState(dir, content, t)
if err := os.WriteFile(dir+"/main.tf", []byte(content), 0644); err != nil {
t.Fatal(err)
}
var want []ResChange
isPre012, err := isPre012()
if err != nil {
t.Fatal(err)
}
if !isPre012 {
want = []ResChange{
{"null_resource.first", "null_resource", Change{[]changeAction{noOp}}},
{"null_resource.second", "null_resource", Change{[]changeAction{noOp}}},
}
}
got, err := changes(config{}, []string{})
if err != nil {
t.Fatalf("failed computing changes")
}
if !reflect.DeepEqual(got, want) {
t.Errorf("changes() = %q, want %q", got, want)
}
}
func TestFilter(t *testing.T) {
resources := []ResChange{
{"null_resource.create", "null_resource", Change{[]changeAction{create}}},
{"null_resource.delete", "null_resource", Change{[]changeAction{del}}},
{"null_resource.noop", "null_resource", Change{[]changeAction{noOp}}},
{"null_resource.update", "null_resource", Change{[]changeAction{update}}},
}
want := make(map[Resource]bool)
want[Resource{"null_resource.create", "null_resource"}] = true
if got := filterByAction(resources, create); !reflect.DeepEqual(got, want) {
t.Errorf("changes() = %v, want %v", got, want)
}
want = make(map[Resource]bool)
want[Resource{"null_resource.delete", "null_resource"}] = true
if got := filterByAction(resources, del); !reflect.DeepEqual(got, want) {
t.Errorf("changes() = %v, want %v", got, want)
}
}
func createDir(t *testing.T) string {
dir, err := os.MkdirTemp("", t.Name())
if err != nil {
t.Fatal(err)
}
err = os.Chdir(dir)
if err != nil {
t.Fatal(err)
}
return dir
}
func prepareState(dir string, content string, t *testing.T) {
if err := os.WriteFile(dir+"/main.tf", []byte(content), 0644); err != nil {
t.Fatal(err)
}
if err := terraformExec(config{}, true, []string{}, "init"); err != nil {
t.Fatal(err)
}
if err := terraformExec(config{}, true, []string{}, "apply", "-auto-approve"); err != nil {
t.Fatal(err)
}
}
func TestFilterByDestinationResourceTypes(t *testing.T) {
resSrc1 := Resource{Address: "null_resource.resource_alpha", Type: "null_resource"}
resSrc2 := Resource{Address: "null_resource.resource_beta", Type: "another_type"}
resDest1 := Resource{Address: "null_resource.resource_gamma", Type: "null_resource"}
resDest2 := Resource{Address: "null_resource.resource_delta", Type: "new_type"}
sourceResources := make(map[Resource]bool)
sourceResources[resSrc1] = true
sourceResources[resSrc2] = true
destResources := make(map[Resource]bool)
destResources[resDest1] = true
destResources[resDest2] = true
want := make(map[Resource]bool)
want[resSrc1] = true
if got := filterByDestinationResourceTypes(sourceResources, destResources); !reflect.DeepEqual(got, want) {
t.Errorf("filterByDestinationResourceTypes() = %v, want %v", got, want)
}
}