@@ -6,11 +6,11 @@ import (
6
6
"context"
7
7
"io"
8
8
"log"
9
- "strings"
10
9
"testing"
11
10
12
11
"github.com/spf13/afero"
13
12
"github.com/spf13/afero/zipfs"
13
+ "github.com/stretchr/testify/assert"
14
14
"github.com/stretchr/testify/require"
15
15
16
16
"cdr.dev/slog"
@@ -25,26 +25,30 @@ import (
25
25
func Test_WorkspaceTagDefaultsFromFile (t * testing.T ) {
26
26
t .Parallel ()
27
27
28
+ const (
29
+ unknownTag = "Tag value is not known"
30
+ invalidValueType = "Invalid value type for tag"
31
+ )
32
+
28
33
for _ ,tc := range []struct {
29
- name string
30
- files map [string ]string
31
- expectTags map [string ]string
32
- expectError string
34
+ name string
35
+ files map [string ]string
36
+ expectTags map [string ]string
37
+ expectedFailedTags map [ string ] string
33
38
}{
34
- {
35
- name :"empty" ,
36
- files :map [string ]string {},
37
- expectTags :map [string ]string {},
38
- expectError :"" ,
39
- },
39
+ // {
40
+ // name: "empty",
41
+ // files: map[string]string{},
42
+ // expectTags: map[string]string{},
43
+ // expectError: "",
44
+ // },
40
45
{
41
46
name :"single text file" ,
42
47
files :map [string ]string {
43
48
"file.txt" :`
44
49
hello world` ,
45
50
},
46
- expectTags :map [string ]string {},
47
- expectError :"" ,
51
+ expectTags :map [string ]string {},
48
52
},
49
53
{
50
54
name :"main.tf with no workspace_tags" ,
@@ -67,8 +71,7 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
67
71
default = "a"
68
72
}` ,
69
73
},
70
- expectTags :map [string ]string {},
71
- expectError :"" ,
74
+ expectTags :map [string ]string {},
72
75
},
73
76
{
74
77
name :"main.tf with empty workspace tags" ,
@@ -126,8 +129,7 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
126
129
}
127
130
}` ,
128
131
},
129
- expectTags :map [string ]string {"platform" :"kubernetes" ,"cluster" :"developers" ,"region" :"us" ,"az" :"a" },
130
- expectError :"" ,
132
+ expectTags :map [string ]string {"platform" :"kubernetes" ,"cluster" :"developers" ,"region" :"us" ,"az" :"a" },
131
133
},
132
134
{
133
135
name :"main.tf with parameter that has default value from dynamic value" ,
@@ -162,8 +164,7 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
162
164
}
163
165
}` ,
164
166
},
165
- expectTags :map [string ]string {"platform" :"kubernetes" ,"cluster" :"developers" ,"region" :"us" ,"az" :"a" },
166
- expectError :"" ,
167
+ expectTags :map [string ]string {"platform" :"kubernetes" ,"cluster" :"developers" ,"region" :"us" ,"az" :"a" },
167
168
},
168
169
{
169
170
name :"main.tf with parameter that has default value from another parameter" ,
@@ -199,7 +200,12 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
199
200
}
200
201
}` ,
201
202
},
202
- expectError :"Unknown variable; There is no variable named\" data\" ." ,
203
+ expectTags :map [string ]string {
204
+ "platform" :"kubernetes" ,
205
+ "cluster" :"developers" ,
206
+ "region" :"us" ,
207
+ "az" :"a" ,
208
+ },
203
209
},
204
210
{
205
211
name :"main.tf with multiple valid workspace tags" ,
@@ -244,8 +250,7 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
244
250
}
245
251
}` ,
246
252
},
247
- expectTags :map [string ]string {"platform" :"kubernetes" ,"cluster" :"developers" ,"region" :"us" ,"az" :"a" ,"foo" :"bar" },
248
- expectError :"" ,
253
+ expectTags :map [string ]string {"platform" :"kubernetes" ,"cluster" :"developers" ,"region" :"us" ,"az" :"a" ,"foo" :"bar" },
249
254
},
250
255
{
251
256
name :"main.tf with missing parameter default value for workspace tags" ,
@@ -275,7 +280,10 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
275
280
}
276
281
}` ,
277
282
},
278
- expectTags :map [string ]string {"cluster" :"developers" ,"az" :"" ,"platform" :"kubernetes" ,"region" :"us" },
283
+ expectTags :map [string ]string {"cluster" :"developers" ,"platform" :"kubernetes" ,"region" :"us" },
284
+ expectedFailedTags :map [string ]string {
285
+ "az" :"Tag value is not known, it likely refers to a variable that is not set or has no default." ,
286
+ },
279
287
},
280
288
{
281
289
name :"main.tf with missing parameter default value outside workspace tags" ,
@@ -310,8 +318,7 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
310
318
}
311
319
}` ,
312
320
},
313
- expectTags :map [string ]string {"platform" :"kubernetes" ,"cluster" :"developers" ,"region" :"us" ,"az" :"a" },
314
- expectError :`` ,
321
+ expectTags :map [string ]string {"platform" :"kubernetes" ,"cluster" :"developers" ,"region" :"us" ,"az" :"a" },
315
322
},
316
323
{
317
324
name :"main.tf with missing variable default value outside workspace tags" ,
@@ -345,8 +352,7 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
345
352
}
346
353
}` ,
347
354
},
348
- expectTags :map [string ]string {"platform" :"kubernetes" ,"cluster" :"developers" ,"region" :"us" ,"az" :"a" },
349
- expectError :`` ,
355
+ expectTags :map [string ]string {"platform" :"kubernetes" ,"cluster" :"developers" ,"region" :"us" ,"az" :"a" },
350
356
},
351
357
{
352
358
name :"main.tf with disallowed data source for workspace tags" ,
@@ -383,8 +389,15 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
383
389
}
384
390
}` ,
385
391
},
386
- expectTags :nil ,
387
- expectError :`invalid workspace tag value "data.local_file.hostname.content": only the "coder_parameter" data source is supported here` ,
392
+ expectTags :map [string ]string {
393
+ "platform" :"kubernetes" ,
394
+ "cluster" :"developers" ,
395
+ "region" :"us" ,
396
+ "az" :"a" ,
397
+ },
398
+ expectedFailedTags :map [string ]string {
399
+ "hostname" :unknownTag ,
400
+ },
388
401
},
389
402
{
390
403
name :"main.tf with disallowed resource for workspace tags" ,
@@ -418,9 +431,13 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
418
431
}
419
432
}` ,
420
433
},
421
- expectTags :nil ,
422
- // TODO: this error isn't great, but it has the desired effect.
423
- expectError :`There is no variable named "foo_bar"` ,
434
+ expectTags :map [string ]string {
435
+ "platform" :"kubernetes" ,
436
+ "cluster" :"developers" ,
437
+ "region" :"us" ,
438
+ "az" :"a" ,
439
+ "foobarbaz" :"foobar" ,
440
+ },
424
441
},
425
442
{
426
443
name :"main.tf with allowed functions in workspace tags" ,
@@ -493,8 +510,15 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
493
510
}
494
511
}` ,
495
512
},
496
- expectTags :nil ,
497
- expectError :`function "pathexpand" may not be used here` ,
513
+ expectTags :map [string ]string {
514
+ "platform" :"kubernetes" ,
515
+ "cluster" :"developers" ,
516
+ "region" :"us" ,
517
+ "az" :"a" ,
518
+ },
519
+ expectedFailedTags :map [string ]string {
520
+ "some_path" :unknownTag ,
521
+ },
498
522
},
499
523
{
500
524
name :"supported types" ,
@@ -558,14 +582,17 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
558
582
"stringvar" :"a" ,
559
583
"numvar" :"1" ,
560
584
"boolvar" :"true" ,
561
- "listvar" :`["a"]` ,
562
- "mapvar" :`{"a":"b"}` ,
563
585
"stringparam" :"a" ,
564
586
"numparam" :"1" ,
565
587
"boolparam" :"true" ,
566
- "listparam" :`["a", "b"]` ,
588
+ "listparam" :`["a", "b"]` ,// OK because params are cast to strings
589
+ //"listvar": `["a"]`,
590
+ //"mapvar": `{"a":"b"}`,
591
+ },
592
+ expectedFailedTags :map [string ]string {
593
+ "listvar" :invalidValueType ,
594
+ "mapvar" :invalidValueType ,
567
595
},
568
- expectError :`` ,
569
596
},
570
597
{
571
598
name :"overlapping var name" ,
@@ -606,21 +633,12 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
606
633
tags := output .WorkspaceTags
607
634
tagMap := tags .Tags ()
608
635
failedTags := tags .UnusableTags ()
609
- if tc .expectError != "" {
610
- found := false
611
- require .True (t ,len (failedTags )> 0 ,"Expected unusable tags" )
612
- for _ ,tag := range failedTags {
613
- verr := tfparse .TagValidationResponse (tag )
614
- if strings .Contains (verr .Error (),tc .expectError ) {
615
- found = true
616
- break
617
- }
618
- }
619
-
620
- require .True (t ,found ,"Expected error to contain: " + tc .expectError )
621
- }else {
622
- require .True (t ,len (failedTags )== 0 ,"Expected no unusable tags" )
623
- require .Equal (t ,tc .expectTags ,tagMap )
636
+ assert .Equal (t ,tc .expectTags ,tagMap ,"expected tags to match, must always provide something" )
637
+ for _ ,tag := range failedTags {
638
+ verr := tfparse .TagValidationResponse (tag )
639
+ expectedErr ,ok := tc .expectedFailedTags [tag .KeyString ()]
640
+ require .Truef (t ,ok ,"assertion for failed tag required: %s, %s" ,tag .KeyString (),verr .Error ())
641
+ require .Contains (t ,verr .Error (),expectedErr )
624
642
}
625
643
})
626
644
@@ -638,21 +656,14 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
638
656
require .False (t ,diags .HasErrors (),diags .Error ())
639
657
640
658
tags := output .WorkspaceTags
641
- if tc .expectError != "" {
642
- found := false
643
- require .True (t ,len (tags .UnusableTags ())> 0 ,"Expected unusable tags" )
644
- for _ ,tag := range tags .UnusableTags () {
645
- verr := tfparse .TagValidationResponse (tag )
646
- if strings .Contains (verr .Error (),tc .expectError ) {
647
- found = true
648
- break
649
- }
650
- }
651
-
652
- require .True (t ,found ,"Expected error to contain: " + tc .expectError )
653
- }else {
654
- require .True (t ,len (tags .UnusableTags ())== 0 ,"Expected no unusable tags" )
655
- require .Equal (t ,tc .expectTags ,tags .Tags ())
659
+ tagMap := tags .Tags ()
660
+ failedTags := tags .UnusableTags ()
661
+ assert .Equal (t ,tc .expectTags ,tagMap ,"expected tags to match, must always provide something" )
662
+ for _ ,tag := range failedTags {
663
+ verr := tfparse .TagValidationResponse (tag )
664
+ expectedErr ,ok := tc .expectedFailedTags [tag .KeyString ()]
665
+ assert .Truef (t ,ok ,"assertion for failed tag required: %s, %s" ,tag .KeyString (),verr .Error ())
666
+ assert .Contains (t ,verr .Error (),expectedErr )
656
667
}
657
668
})
658
669
}