diff --git a/internal/services/containerapps/validate/validate.go b/internal/services/containerapps/validate/validate.go index 11d83de9a796..db9eed11e7b0 100644 --- a/internal/services/containerapps/validate/validate.go +++ b/internal/services/containerapps/validate/validate.go @@ -74,7 +74,7 @@ func ContainerAppName(i interface{}, k string) (warnings []string, errors []erro return } - if matched := regexp.MustCompile(`^([a-z])[a-z0-9-]{0,58}[a-z0-9]?$`).Match([]byte(v)); !matched || strings.HasSuffix(v, "-") || strings.Contains(v, "--") { + if matched := regexp.MustCompile(`^([a-z])[a-z0-9-]{0,30}[a-z0-9]?$`).Match([]byte(v)); !matched || strings.HasSuffix(v, "-") || strings.Contains(v, "--") { errors = append(errors, fmt.Errorf("%q must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character and cannot have '--'. The length must not be more than 32 characters", k)) return } @@ -118,7 +118,7 @@ func ContainerAppContainerName(i interface{}, k string) (warnings []string, erro return } - if matched := regexp.MustCompile(`^([a-zA-Z0-9])[a-zA-Z0-9-.]{0,254}[a-z]?$`).Match([]byte(v)); !matched || strings.HasSuffix(v, "-") { + if matched := regexp.MustCompile(`^([a-z])[a-z0-9-.]{0,58}[a-z]$`).Match([]byte(v)); !matched || strings.HasSuffix(v, "-") { errors = append(errors, fmt.Errorf("%q must consist of lower case alphanumeric characters, '-', or '.', start with an alphabetic character, and end with an alphanumeric character. The length must not be more than 60 characters", k)) } diff --git a/internal/services/containerapps/validate/validate_test.go b/internal/services/containerapps/validate/validate_test.go index 784fd34a5823..338d4f09470f 100644 --- a/internal/services/containerapps/validate/validate_test.go +++ b/internal/services/containerapps/validate/validate_test.go @@ -324,3 +324,104 @@ func TestContainerAppScaleRuleConcurrentRequests(t *testing.T) { } } } + +func TestValidateContainerAppName(t *testing.T) { + cases := []struct { + Input string + Valid bool + }{ + { + Input: "", + }, + { + Input: "-", + }, + { + Input: "9", + }, + { + Input: "a-", + }, + { + Input: "a.", + }, + { + Input: "a--a", + }, + { + Input: "Cannothavecapitals", + }, + { + Input: "a-a", + Valid: true, + }, + { + Input: "val-1-id", + Valid: true, + }, + { + Input: "invalid12345678901234567890123456", + }, + } + + for _, tc := range cases { + t.Logf("[DEBUG] Testing Value %s", tc.Input) + _, errors := ContainerAppName(tc.Input, "test") + valid := len(errors) == 0 + + if tc.Valid != valid { + t.Fatalf("Expected %t but got %t for %s: %+v", tc.Valid, valid, tc.Input, errors) + } + } +} + +func TestValidateContainerAppContainerName(t *testing.T) { + cases := []struct { + Input string + Valid bool + }{ + { + Input: "", + }, + { + Input: "-", + }, + { + Input: "9", + }, + { + Input: "a-", + }, + { + Input: "a.", + }, + { + Input: "a--a", + Valid: true, + }, + { + Input: "Cannothavecapitals", + }, + { + Input: "a-a", + Valid: true, + }, + { + Input: "val-1-id", + Valid: true, + }, + { + Input: "invalid12345678901234567890123456", + }, + } + + for _, tc := range cases { + t.Logf("[DEBUG] Testing Value %s", tc.Input) + _, errors := ContainerAppContainerName(tc.Input, "test") + valid := len(errors) == 0 + + if tc.Valid != valid { + t.Fatalf("Expected %t but got %t for %s: %+v", tc.Valid, valid, tc.Input, errors) + } + } +}