This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nested acorn validation support on confirm-upgrade (#1726)
Signed-off-by: Oscar Ward <oscar@acorn.io>
- Loading branch information
Showing
5 changed files
with
144 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
pkg/server/registry/apigroups/acorn/apps/nested_validator.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package apps | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
kclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type nestedValidator struct{} | ||
|
||
func (s nestedValidator) ValidateName(_ context.Context, obj runtime.Object) (result field.ErrorList) { | ||
name := obj.(kclient.Object).GetName() | ||
for _, piece := range strings.Split(name, ".") { | ||
if errs := validation.IsDNS1035Label(piece); len(errs) > 0 { | ||
result = append(result, field.Invalid(field.NewPath("metadata", "name"), name, strings.Join(errs, ","))) | ||
} | ||
} | ||
return | ||
} |
102 changes: 102 additions & 0 deletions
102
pkg/server/registry/apigroups/acorn/apps/nested_validator_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package apps | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestNestedValidateAppName(t *testing.T) { | ||
validator := &nestedValidator{} | ||
|
||
tests := []struct { | ||
name string | ||
appName string | ||
expectValid bool | ||
}{ | ||
{ | ||
name: "Invalid Name: Underscore", | ||
appName: "my_app", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "Invalid Name: Uppercase", | ||
appName: "MyApp", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "Invalid Name: Starts with number", | ||
appName: "1app", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "Invalid Name: Starts with dash", | ||
appName: "-app", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "Invalid Name: Ends with dash", | ||
appName: "app-", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "Valid Name: Lowercase", | ||
appName: "myapp", | ||
expectValid: true, | ||
}, | ||
{ | ||
name: "Valid Name: Nested", | ||
appName: "myapp.nested", | ||
expectValid: true, | ||
}, | ||
{ | ||
name: "Valid Name: Multi Nested", | ||
appName: "myapp.nested.nested", | ||
expectValid: true, | ||
}, | ||
{ | ||
name: "Invalid Name: Nested Underscore", | ||
appName: "myapp.my_app", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "Invalid Name: Uppercase", | ||
appName: "myapp.MyApp", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "Invalid Name: Starts with number", | ||
appName: "myapp.1app", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "Invalid Name: Starts with dash", | ||
appName: "myapp.-app", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "Invalid Name: Ends with dash", | ||
appName: "myapp.app-", | ||
expectValid: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
app := &apiv1.App{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: tt.appName, | ||
}, | ||
} | ||
err := validator.ValidateName(context.Background(), app) | ||
if tt.expectValid && err != nil { | ||
t.Fatalf("Expected valid, got error: %v", err) | ||
} | ||
if !tt.expectValid && err == nil { | ||
t.Fatalf("Expected error, got nil") | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters