@@ -878,7 +878,8 @@ func TestParameterValidationEnforcement(t *testing.T) {
878878 Validation * provider.Validation
879879 OutputValue string
880880 Optional bool
881- Error * regexp.Regexp
881+ CreateError * regexp.Regexp
882+ ImportError * regexp.Regexp
882883 }
883884
884885 rows := make ([]row , 0 )
@@ -909,6 +910,19 @@ func TestParameterValidationEnforcement(t *testing.T) {
909910 t .Fatalf ("failed to parse error column %q: %v" , columns [9 ], err )
910911 }
911912 }
913+
914+ var imerr * regexp.Regexp
915+ if columns [10 ] != "" {
916+ if columns [10 ] == "=" {
917+ imerr = rerr
918+ } else {
919+ imerr , err = regexp .Compile (columns [10 ])
920+ if err != nil {
921+ t .Fatalf ("failed to parse error column %q: %v" , columns [10 ], err )
922+ }
923+ }
924+ }
925+
912926 var options []string
913927 if columns [4 ] != "" {
914928 options = strings .Split (columns [4 ], "," )
@@ -955,7 +969,8 @@ func TestParameterValidationEnforcement(t *testing.T) {
955969 Validation : validation ,
956970 OutputValue : columns [7 ],
957971 Optional : optional ,
958- Error : rerr ,
972+ CreateError : rerr ,
973+ ImportError : imerr ,
959974 })
960975 }
961976
@@ -974,9 +989,9 @@ func TestParameterValidationEnforcement(t *testing.T) {
974989 t .Setenv (provider .ParameterEnvironmentVariable ("parameter" ), row .InputValue )
975990 }
976991
977- if row .Error != nil {
992+ if row .CreateError != nil && row . ImportError != nil {
978993 if row .OutputValue != "" {
979- t .Errorf ("output value %q should not be set if error is set" , row .OutputValue )
994+ t .Errorf ("output value %q should not be set if both errors are set" , row .OutputValue )
980995 }
981996 }
982997
@@ -1020,42 +1035,56 @@ func TestParameterValidationEnforcement(t *testing.T) {
10201035
10211036 cfg .WriteString ("}\n " )
10221037
1023- resource . Test ( t , resource. TestCase {
1024- ProviderFactories : coderFactory (),
1025- IsUnitTest : true ,
1026- Steps : []resource. TestStep {{
1027- Config : cfg . String (),
1028- ExpectError : row . Error ,
1029- Check : func ( state * terraform. State ) error {
1030- require . Len ( t , state . Modules , 1 )
1031- require . Len ( t , state . Modules [ 0 ]. Resources , 1 )
1032- param := state . Modules [ 0 ]. Resources [ "data.coder_parameter.parameter" ]
1033- require . NotNil ( t , param )
1038+ for _ , mode := range []provider. ValidationMode { provider . ValidationModeDefault , provider . ValidationModeTemplateImport } {
1039+ name := string ( mode )
1040+ if mode == provider . ValidationModeDefault {
1041+ name = "create"
1042+ }
1043+ t . Run ( name , func ( t * testing. T ) {
1044+ t . Setenv ( "CODER_VALIDATION_MODE" , string ( mode ))
1045+ rerr := row . CreateError
1046+ if mode == provider . ValidationModeTemplateImport {
1047+ rerr = row . ImportError
1048+ }
10341049
1035- if row .Default == "" {
1036- _ , ok := param .Primary .Attributes ["default" ]
1037- require .False (t , ok , "default should not be set" )
1038- } else {
1039- require .Equal (t , strings .Trim (row .Default , `"` ), param .Primary .Attributes ["default" ])
1040- }
1050+ resource .Test (t , resource.TestCase {
1051+ ProviderFactories : coderFactory (),
1052+ IsUnitTest : true ,
1053+ Steps : []resource.TestStep {{
1054+ Config : cfg .String (),
1055+ ExpectError : rerr ,
1056+ Check : func (state * terraform.State ) error {
1057+ require .Len (t , state .Modules , 1 )
1058+ require .Len (t , state .Modules [0 ].Resources , 1 )
1059+ param := state .Modules [0 ].Resources ["data.coder_parameter.parameter" ]
1060+ require .NotNil (t , param )
10411061
1042- if row .OutputValue == "" {
1043- _ , ok := param .Primary .Attributes ["value " ]
1044- require .False (t , ok , "output value should not be set" )
1045- } else {
1046- require .Equal (t , strings .Trim (row .OutputValue , `"` ), param .Primary .Attributes ["value " ])
1047- }
1062+ if row .Default == "" {
1063+ _ , ok := param .Primary .Attributes ["default " ]
1064+ require .False (t , ok , "default should not be set" )
1065+ } else {
1066+ require .Equal (t , strings .Trim (row .Default , `"` ), param .Primary .Attributes ["default " ])
1067+ }
10481068
1049- for key , expected := range map [string ]string {
1050- "optional" : strconv .FormatBool (row .Optional ),
1051- } {
1052- require .Equal (t , expected , param .Primary .Attributes [key ], "optional" )
1053- }
1069+ if row .OutputValue == "" {
1070+ _ , ok := param .Primary .Attributes ["value" ]
1071+ require .False (t , ok , "output value should not be set" )
1072+ } else {
1073+ require .Equal (t , strings .Trim (row .OutputValue , `"` ), param .Primary .Attributes ["value" ])
1074+ }
10541075
1055- return nil
1056- },
1057- }},
1058- })
1076+ for key , expected := range map [string ]string {
1077+ "optional" : strconv .FormatBool (row .Optional ),
1078+ } {
1079+ require .Equal (t , expected , param .Primary .Attributes [key ], "optional" )
1080+ }
1081+
1082+ return nil
1083+ },
1084+ }},
1085+ })
1086+ })
1087+ }
10591088 })
10601089 }
10611090 }
0 commit comments