-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathgenerate_test.go
242 lines (226 loc) · 7.36 KB
/
generate_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package generate_test
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/grafana/terraform-provider-grafana/v3/internal/testutils"
"github.com/grafana/terraform-provider-grafana/v3/pkg/generate"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)
func TestAccGenerate(t *testing.T) {
if testing.Short() {
t.Skip("skipping long test")
}
testutils.CheckOSSTestsEnabled(t)
cases := []struct {
name string
config string
generateConfig func(cfg *generate.Config)
check func(t *testing.T, tempDir string)
}{
{
name: "dashboard",
config: testutils.TestAccExample(t, "resources/grafana_dashboard/resource.tf"),
check: func(t *testing.T, tempDir string) {
assertFiles(t, tempDir, "testdata/generate/dashboard", []string{
".terraform",
".terraform.lock.hcl",
})
},
},
{
name: "dashboard-json",
config: testutils.TestAccExample(t, "resources/grafana_dashboard/resource.tf"),
generateConfig: func(cfg *generate.Config) {
cfg.Format = generate.OutputFormatJSON
},
check: func(t *testing.T, tempDir string) {
assertFiles(t, tempDir, "testdata/generate/dashboard-json", []string{
".terraform",
".terraform.lock.hcl",
})
},
},
{
name: "dashboard-crossplane",
config: testutils.TestAccExample(t, "resources/grafana_dashboard/resource.tf"),
generateConfig: func(cfg *generate.Config) {
cfg.Format = generate.OutputFormatCrossplane
},
check: func(t *testing.T, tempDir string) {
assertFiles(t, tempDir, "testdata/generate/dashboard-crossplane", nil)
},
},
{
name: "dashboard-filter-strict",
config: testutils.TestAccExample(t, "resources/grafana_dashboard/resource.tf"),
generateConfig: func(cfg *generate.Config) {
cfg.IncludeResources = []string{"grafana_dashboard._1_my-dashboard-uid"}
},
check: func(t *testing.T, tempDir string) {
assertFiles(t, tempDir, "testdata/generate/dashboard-filtered", []string{
".terraform",
".terraform.lock.hcl",
})
},
},
{
name: "dashboard-filter-wildcard-on-resource-type",
config: testutils.TestAccExample(t, "resources/grafana_dashboard/resource.tf"),
generateConfig: func(cfg *generate.Config) {
cfg.IncludeResources = []string{"*._1_my-dashboard-uid"}
},
check: func(t *testing.T, tempDir string) {
assertFiles(t, tempDir, "testdata/generate/dashboard-filtered", []string{
".terraform",
".terraform.lock.hcl",
})
},
},
{
name: "dashboard-filter-wildcard-on-resource-name",
config: testutils.TestAccExample(t, "resources/grafana_dashboard/resource.tf"),
generateConfig: func(cfg *generate.Config) {
cfg.IncludeResources = []string{"grafana_dashboard.*"}
},
check: func(t *testing.T, tempDir string) {
assertFiles(t, tempDir, "testdata/generate/dashboard-filtered", []string{
".terraform",
".terraform.lock.hcl",
})
},
},
{
name: "filter-all",
config: testutils.TestAccExample(t, "resources/grafana_dashboard/resource.tf"),
generateConfig: func(cfg *generate.Config) {
cfg.IncludeResources = []string{"doesnot.exist"}
},
check: func(t *testing.T, tempDir string) {
assertFiles(t, tempDir, "testdata/generate/empty", []string{
".terraform",
".terraform.lock.hcl",
})
},
},
{
name: "alerting-in-org",
config: func() string {
content, err := os.ReadFile("testdata/generate/alerting-in-org.tf")
require.NoError(t, err)
return string(content)
}(),
generateConfig: func(cfg *generate.Config) {
// The alerting rule group sometimes also creates an annotation.
// It seems to be async so it makes the test flaky.
// We can include only the resources we care about to avoid this.x
cfg.IncludeResources = []string{
"grafana_contact_point.*",
"grafana_folder.*",
"grafana_message_template.*",
"grafana_mute_timing.*",
"grafana_notification_policy.*",
"grafana_organization.*",
"grafana_rule_group.*",
}
},
check: func(t *testing.T, tempDir string) {
assertFiles(t, tempDir, "testdata/generate/alerting-in-org", []string{
".terraform",
".terraform.lock.hcl",
})
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: tc.config,
Check: func(s *terraform.State) error {
tempDir := t.TempDir()
config := generate.Config{
OutputDir: tempDir,
Clobber: true,
Format: generate.OutputFormatHCL,
ProviderVersion: "v3.0.0",
Grafana: &generate.GrafanaConfig{
URL: "http://localhost:3000",
Auth: "admin:admin",
},
}
if tc.generateConfig != nil {
tc.generateConfig(&config)
}
require.NoError(t, generate.Generate(context.Background(), &config))
tc.check(t, tempDir)
return nil
},
},
},
})
})
}
}
// assertFiles checks that all files in the "expectedFilesDir" directory match the files in the "gotFilesDir" directory.
func assertFiles(t *testing.T, gotFilesDir, expectedFilesDir string, ignoreDirEntries []string) {
t.Helper()
assertFilesSubdir(t, gotFilesDir, expectedFilesDir, "", ignoreDirEntries)
}
func assertFilesSubdir(t *testing.T, gotFilesDir, expectedFilesDir, subdir string, ignoreDirEntries []string) {
t.Helper()
originalGotFilesDir := gotFilesDir
originalExpectedFilesDir := expectedFilesDir
if subdir != "" {
gotFilesDir = filepath.Join(gotFilesDir, subdir)
expectedFilesDir = filepath.Join(expectedFilesDir, subdir)
}
// Check that all generated files are expected (recursively)
gotFiles, err := os.ReadDir(gotFilesDir)
if err != nil {
t.Logf("folder %s was not generated as expected", subdir)
t.Fail()
return
}
for _, gotFile := range gotFiles {
relativeName := filepath.Join(subdir, gotFile.Name())
if slices.Contains(ignoreDirEntries, relativeName) {
continue
}
if gotFile.IsDir() {
assertFilesSubdir(t, originalGotFilesDir, originalExpectedFilesDir, filepath.Join(subdir, gotFile.Name()), ignoreDirEntries)
continue
}
if _, err := os.Stat(filepath.Join(expectedFilesDir, gotFile.Name())); err != nil {
t.Logf("file %s was generated but wasn't expected", relativeName)
t.Fail()
}
}
// Verify the contents of the generated files (recursively)
// All files in the expected directory should be present in the generated directory
expectedFiles, err := os.ReadDir(expectedFilesDir)
if err != nil {
t.Logf("folder %s was generated but wasn't expected", subdir)
t.Fail()
return
}
for _, expectedFile := range expectedFiles {
if expectedFile.IsDir() {
assertFilesSubdir(t, originalGotFilesDir, originalExpectedFilesDir, filepath.Join(subdir, expectedFile.Name()), ignoreDirEntries)
continue
}
expectedContent, err := os.ReadFile(filepath.Join(expectedFilesDir, expectedFile.Name()))
require.NoError(t, err)
gotContent, err := os.ReadFile(filepath.Join(gotFilesDir, expectedFile.Name()))
require.NoError(t, err)
assert.Equal(t, strings.TrimSpace(string(expectedContent)), strings.TrimSpace(string(gotContent)))
}
}