@@ -2,6 +2,7 @@ package terraform_test
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
5
6
"os"
6
7
"path/filepath"
7
8
"runtime"
@@ -584,6 +585,69 @@ func TestAppSlugValidation(t *testing.T) {
584
585
require .ErrorContains (t , err , "duplicate app slug" )
585
586
}
586
587
588
+ func TestParameterValidation (t * testing.T ) {
589
+ t .Parallel ()
590
+
591
+ // nolint:dogsled
592
+ _ , filename , _ , _ := runtime .Caller (0 )
593
+
594
+ // Load the rich-parameters state file and edit it.
595
+ dir := filepath .Join (filepath .Dir (filename ), "testdata" , "rich-parameters" )
596
+ tfPlanRaw , err := os .ReadFile (filepath .Join (dir , "rich-parameters.tfplan.json" ))
597
+ require .NoError (t , err )
598
+ var tfPlan tfjson.Plan
599
+ err = json .Unmarshal (tfPlanRaw , & tfPlan )
600
+ require .NoError (t , err )
601
+ tfPlanGraph , err := os .ReadFile (filepath .Join (dir , "rich-parameters.tfplan.dot" ))
602
+ require .NoError (t , err )
603
+
604
+ // Change all names to be identical.
605
+ var names []string
606
+ for _ , resource := range tfPlan .PriorState .Values .RootModule .Resources {
607
+ if resource .Type == "coder_parameter" {
608
+ resource .AttributeValues ["name" ] = "identical"
609
+ names = append (names , resource .Name )
610
+ }
611
+ }
612
+
613
+ state , err := terraform .ConvertState ([]* tfjson.StateModule {tfPlan .PriorState .Values .RootModule }, string (tfPlanGraph ), names )
614
+ require .Nil (t , state )
615
+ require .Error (t , err )
616
+ require .ErrorContains (t , err , "coder_parameter names must be unique but \" identical\" appears multiple times" )
617
+
618
+ // Make two sets of identical names.
619
+ count := 0
620
+ names = nil
621
+ for _ , resource := range tfPlan .PriorState .Values .RootModule .Resources {
622
+ if resource .Type == "coder_parameter" {
623
+ resource .AttributeValues ["name" ] = fmt .Sprintf ("identical-%d" , count % 2 )
624
+ names = append (names , resource .Name )
625
+ count ++
626
+ }
627
+ }
628
+
629
+ state , err = terraform .ConvertState ([]* tfjson.StateModule {tfPlan .PriorState .Values .RootModule }, string (tfPlanGraph ), names )
630
+ require .Nil (t , state )
631
+ require .Error (t , err )
632
+ require .ErrorContains (t , err , "coder_parameter names must be unique but \" identical-0\" and \" identical-1\" appear multiple times" )
633
+
634
+ // Once more with three sets.
635
+ count = 0
636
+ names = nil
637
+ for _ , resource := range tfPlan .PriorState .Values .RootModule .Resources {
638
+ if resource .Type == "coder_parameter" {
639
+ resource .AttributeValues ["name" ] = fmt .Sprintf ("identical-%d" , count % 3 )
640
+ names = append (names , resource .Name )
641
+ count ++
642
+ }
643
+ }
644
+
645
+ state , err = terraform .ConvertState ([]* tfjson.StateModule {tfPlan .PriorState .Values .RootModule }, string (tfPlanGraph ), names )
646
+ require .Nil (t , state )
647
+ require .Error (t , err )
648
+ require .ErrorContains (t , err , "coder_parameter names must be unique but \" identical-0\" , \" identical-1\" and \" identical-2\" appear multiple times" )
649
+ }
650
+
587
651
func TestInstanceTypeAssociation (t * testing.T ) {
588
652
t .Parallel ()
589
653
type tc struct {
0 commit comments