-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform_writer_test.go
182 lines (161 loc) · 4.16 KB
/
terraform_writer_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package core_test
import (
"errors"
"fmt"
"os"
"path"
"testing"
"github.com/andreyvit/diff"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/yoanm/go-github-tf/core"
"github.com/yoanm/go-tfsig/testutils"
)
func TestGenerateHclRepoFiles(t *testing.T) {
t.Parallel()
cases := map[string]struct {
value []*core.GhRepoConfig
expectedFiles map[string]string
}{
"Full": {
[]*core.GhRepoConfig{GetFullConfig(1), GetFullConfig(2)},
map[string]string{
"repo.repo1.tf": "repo1.full",
"repo.repo2.tf": "repo2.full",
},
},
"nil": {
nil,
nil,
},
"empty": {
[]*core.GhRepoConfig{},
nil,
},
}
for tcname, tc := range cases {
t.Run(
tcname,
func(t *testing.T) {
t.Parallel()
files, err := core.GenerateHclRepoFiles(tc.value)
if err != nil {
t.Errorf("Case %q: %s", tcname, err)
} else {
for fname, goldenfile := range tc.expectedFiles {
tffile, exists := files[fname]
if !exists {
t.Errorf("Case %q: expected file %s doesn't exist !", tcname, fname)
} else {
if err2 := testutils.EnsureFileEqualsGoldenFile(tffile, goldenfile); err2 != nil {
t.Errorf("Case %q file %s: %v", tcname, fname, err2)
}
}
}
if !t.Failed() && len(files) != len(tc.expectedFiles) {
t.Errorf("Case %q: expected %d files, got %d", tcname, len(tc.expectedFiles), len(files))
}
}
},
)
}
}
func TestWriteTerraformFiles(t *testing.T) {
t.Parallel()
file1 := hclwrite.NewEmptyFile()
file1.Body().AppendBlock(hclwrite.NewBlock("type1", []string{"label1"}))
file2 := hclwrite.NewEmptyFile()
file2.Body().AppendBlock(hclwrite.NewBlock("type2", []string{"label2"}))
cases := map[string]struct {
value map[string]*hclwrite.File
expectedFiles map[string]string
}{
"Full": {
map[string]*hclwrite.File{
"repo.repo1.tf": file1,
"repo.repo2.tf": file2,
},
map[string]string{
"repo.repo1.tf": "type1 \"label1\" {\n}\n",
"repo.repo2.tf": "type2 \"label2\" {\n}\n",
},
},
"Nil": {
nil,
nil,
},
"Empty": {
map[string]*hclwrite.File{},
nil,
},
}
for tcname, tc := range cases {
t.Run(
tcname,
func(t *testing.T) {
t.Parallel()
root := os.TempDir()
err := core.WriteTerraformFiles(root, tc.value)
if err != nil {
t.Errorf("Case %q: %s", tcname, err)
} else {
for fname, expected := range tc.expectedFiles {
_, exists := tc.value[fname]
if !exists {
t.Errorf("Case %q: expected file %s doesn't exist !", tcname, fname)
} else {
actual, err2 := os.ReadFile(path.Join(root, fname))
if err2 != nil {
t.Errorf("Case %q: %s", tcname, err2)
} else if string(actual) != expected {
t.Errorf("Case %q:\n- expected\n+ actual\n\n%v", tcname, diff.LineDiff(expected, string(actual)))
}
}
}
}
},
)
}
}
func TestWriteTerraformFiles_onError(t *testing.T) {
t.Parallel()
file1 := hclwrite.NewEmptyFile()
file1.Body().AppendBlock(hclwrite.NewBlock("type1", []string{"label1"}))
notWritableDir := path.Join(os.TempDir(), "not_writable_dir")
err := os.MkdirAll(notWritableDir, os.FileMode(0))
if err != nil {
t.Fatalf(err.Error())
}
cases := map[string]struct {
root string
value map[string]*hclwrite.File
error error
}{
"Unknown dir": {
"/an_unknown_dir/somewhere",
map[string]*hclwrite.File{
"repo.repo1.tf": file1,
},
errors.New("error while writing terraform files:\n\t - workspace path doesn't exist: /an_unknown_dir/somewhere"),
},
"Unable to write": {
notWritableDir,
map[string]*hclwrite.File{
"repo.repo1.tf": file1,
},
fmt.Errorf("error while writing terraform files:\n\t - open %s/repo.repo1.tf: permission denied", notWritableDir),
},
}
for tcname, tc := range cases {
t.Run(
tcname,
func(t *testing.T) {
t.Parallel()
if err2 := core.WriteTerraformFiles(tc.root, tc.value); err2 == nil {
t.Errorf("Case %q: expected an error but everything went well", tcname)
} else if err2.Error() != tc.error.Error() {
t.Errorf("Case %q:\n- expected\n+ actual\n\n%v", tcname, diff.LineDiff(tc.error.Error(), err2.Error()))
}
},
)
}
}