-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_test.go
More file actions
390 lines (353 loc) · 8.9 KB
/
Copy pathparser_test.go
File metadata and controls
390 lines (353 loc) · 8.9 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package diffy
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/zclconf/go-cty/cty"
)
func TestNormalizeSource(t *testing.T) {
tests := []struct {
name string
source string
want string
}{
{
name: "full source path",
source: "registry.terraform.io/hashicorp/azurerm",
want: "registry.terraform.io/hashicorp/azurerm",
},
{
name: "short source path",
source: "hashicorp/azurerm",
want: "registry.terraform.io/hashicorp/azurerm",
},
{
name: "single name without slash",
source: "azurerm",
want: "azurerm", // No slash, returns as-is
},
{
name: "custom registry with slash",
source: "custom.registry.io/myorg/myprovider",
want: "registry.terraform.io/custom.registry.io/myorg/myprovider", // Has slash, gets prefixed
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NormalizeSource(tt.source)
if got != tt.want {
t.Errorf("NormalizeSource(%q) = %q, want %q", tt.source, got, tt.want)
}
})
}
}
func TestFindSubmodules(t *testing.T) {
tests := []struct {
name string
structure map[string]bool // path -> isDir
wantCount int
wantErr bool
}{
{
name: "single submodule",
structure: map[string]bool{
"network": true,
"network/main.tf": false,
},
wantCount: 1,
wantErr: false,
},
{
name: "multiple submodules",
structure: map[string]bool{
"network": true,
"network/main.tf": false,
"storage": true,
"storage/main.tf": false,
},
wantCount: 2,
wantErr: false,
},
{
name: "no submodules",
structure: map[string]bool{},
wantCount: 0,
wantErr: false, // FindSubmodules returns nil error even if directory doesn't exist
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
for path, isDir := range tt.structure {
fullPath := filepath.Join(tmpDir, path)
if isDir {
if err := os.MkdirAll(fullPath, 0755); err != nil {
t.Fatalf("Failed to create directory %s: %v", path, err)
}
} else {
dir := filepath.Dir(fullPath)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("Failed to create parent directory for %s: %v", path, err)
}
if err := os.WriteFile(fullPath, []byte("# test file"), 0644); err != nil {
t.Fatalf("Failed to create file %s: %v", path, err)
}
}
}
got, err := FindSubmodules(tmpDir)
if (err != nil) != tt.wantErr {
t.Errorf("FindSubmodules() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && len(got) != tt.wantCount {
t.Errorf("FindSubmodules() returned %d submodules, want %d", len(got), tt.wantCount)
}
})
}
}
func TestExtractIgnoreChangesFromValue(t *testing.T) {
val := cty.ListVal([]cty.Value{
cty.StringVal("name"),
cty.StringVal("all"),
cty.StringVal("tags"),
})
got := extractIgnoreChangesFromValue(val)
if len(got) != 1 || got[0] != "*all*" {
t.Fatalf("extractIgnoreChangesFromValue returned %v, want [*all*]", got)
}
}
func TestParseProviderRequirements(t *testing.T) {
tests := []struct {
name string
tfContent string
wantErr bool
wantCount int
checkResult func(*testing.T, map[string]ProviderConfig)
}{
{
name: "single provider",
tfContent: `
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 3.0.0"
}
}
}
`,
wantErr: false,
wantCount: 1,
checkResult: func(t *testing.T, providers map[string]ProviderConfig) {
if provider, ok := providers["azurerm"]; ok {
if provider.Source != "registry.terraform.io/hashicorp/azurerm" {
t.Errorf("Source = %s, want registry.terraform.io/hashicorp/azurerm", provider.Source)
}
if provider.Version != ">= 3.0.0" {
t.Errorf("Version = %s, want >= 3.0.0", provider.Version)
}
} else {
t.Error("azurerm provider not found")
}
},
},
{
name: "multiple providers",
tfContent: `
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.1"
}
}
}
`,
wantErr: false,
wantCount: 2,
checkResult: func(t *testing.T, providers map[string]ProviderConfig) {
if len(providers) != 2 {
t.Errorf("Got %d providers, want 2", len(providers))
}
},
},
{
name: "no terraform block",
tfContent: `
resource "azurerm_virtual_network" "test" {
name = "test"
}
`,
wantErr: false,
wantCount: 0,
checkResult: func(t *testing.T, providers map[string]ProviderConfig) {
if len(providers) != 0 {
t.Errorf("Got %d providers, want 0", len(providers))
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
tfFile := filepath.Join(tmpDir, "main.tf")
if err := os.WriteFile(tfFile, []byte(tt.tfContent), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
parser := NewHCLParser()
ctx := context.Background()
got, err := parser.ParseProviderRequirements(ctx, tfFile)
if (err != nil) != tt.wantErr {
t.Errorf("ParseProviderRequirements() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if len(got) != tt.wantCount {
t.Errorf("Got %d providers, want %d", len(got), tt.wantCount)
}
if tt.checkResult != nil {
tt.checkResult(t, got)
}
}
})
}
}
func TestParseTerraformFiles(t *testing.T) {
tests := []struct {
name string
files map[string]string
wantResources int
wantDataSources int
wantErr bool
}{
{
name: "single resource",
files: map[string]string{
"main.tf": `
resource "azurerm_virtual_network" "test" {
name = "test-vnet"
resource_group_name = "test-rg"
location = "westeurope"
address_space = ["10.0.0.0/16"]
}
`,
},
wantResources: 1,
wantDataSources: 0,
wantErr: false,
},
{
name: "resources and data sources",
files: map[string]string{
"main.tf": `
resource "azurerm_virtual_network" "test" {
name = "test"
}
data "azurerm_resource_group" "existing" {
name = "existing-rg"
}
`,
},
wantResources: 1,
wantDataSources: 1,
wantErr: false,
},
{
name: "multiple files",
files: map[string]string{
"main.tf": `
resource "azurerm_virtual_network" "test1" {
name = "test1"
}
`,
"data.tf": `
data "azurerm_resource_group" "existing" {
name = "existing"
}
`,
},
wantResources: 1,
wantDataSources: 1,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
var fileList []string
for filename, content := range tt.files {
tfFile := filepath.Join(tmpDir, filename)
if err := os.WriteFile(tfFile, []byte(content), 0644); err != nil {
t.Fatalf("Failed to create test file %s: %v", filename, err)
}
fileList = append(fileList, tfFile)
}
parser := NewHCLParser()
ctx := context.Background()
resources, dataSources, err := parser.ParseTerraformFiles(ctx, fileList)
if (err != nil) != tt.wantErr {
t.Errorf("ParseTerraformFiles() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if len(resources) != tt.wantResources {
t.Errorf("Got %d resources, want %d", len(resources), tt.wantResources)
}
if len(dataSources) != tt.wantDataSources {
t.Errorf("Got %d data sources, want %d", len(dataSources), tt.wantDataSources)
}
}
})
}
}
func TestParseMainFile(t *testing.T) {
tmpDir := t.TempDir()
tfFile := filepath.Join(tmpDir, "main.tf")
content := `
resource "azurerm_virtual_network" "test" {
name = "test"
}
`
if err := os.WriteFile(tfFile, []byte(content), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
parser := NewHCLParser()
ctx := context.Background()
resources, dataSources, err := parser.ParseMainFile(ctx, tfFile)
if err != nil {
t.Errorf("ParseMainFile() error = %v", err)
}
if len(resources) != 1 {
t.Errorf("Got %d resources, want 1", len(resources))
}
if len(dataSources) != 0 {
t.Errorf("Got %d data sources, want 0", len(dataSources))
}
}
func TestParseHCLFile_InvalidFile(t *testing.T) {
parser := NewHCLParser()
ctx := context.Background()
_, _, err := parser.ParseMainFile(ctx, "/non/existent/file.tf")
if err == nil {
t.Error("ParseMainFile() should return error for non-existent file")
}
}
func TestParseHCLFile_InvalidSyntax(t *testing.T) {
tmpDir := t.TempDir()
tfFile := filepath.Join(tmpDir, "invalid.tf")
content := `resource "test" {`
if err := os.WriteFile(tfFile, []byte(content), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
parser := NewHCLParser()
ctx := context.Background()
_, _, err := parser.ParseMainFile(ctx, tfFile)
if err == nil {
t.Error("ParseMainFile() should return error for invalid HCL syntax")
}
}