add validation test for a scheduler-estimator and refactor the validation of scheduler-estimator

Signed-off-by: jameszhangyukun <1481815567@qq.com>

add validation test for a scheduler-estimator and refactor the validation of scheduler-estimator
This commit is contained in:
jameszhangyukun 2021-11-14 15:43:33 +08:00
parent 7cfb7e663f
commit 20483de82f
3 changed files with 116 additions and 4 deletions

View File

@ -0,0 +1,31 @@
package options
import (
"net"
"k8s.io/apimachinery/pkg/util/validation/field"
)
// Validate checks Options and return a slice of found errs.
func (o *Options) Validate() field.ErrorList {
errs := field.ErrorList{}
newPath := field.NewPath("Options")
if len(o.ClusterName) == 0 {
errs = append(errs, field.Invalid(newPath.Child("ClusterName"), o.ClusterName, "clusterName cannot be empty"))
}
if net.ParseIP(o.BindAddress) == nil {
errs = append(errs, field.Invalid(newPath.Child("BindAddress"), o.BindAddress, "not a valid textual representation of an IP address"))
}
if o.ServerPort < 0 || o.ServerPort > 65535 {
errs = append(errs, field.Invalid(newPath.Child("ServerPort"), o.ServerPort, "must be a valid port between 0 and 65535 inclusive"))
}
if o.SecurePort < 0 || o.SecurePort > 65535 {
errs = append(errs, field.Invalid(newPath.Child("SecurePort"), o.SecurePort, "must be a valid port between 0 and 65535 inclusive"))
}
return errs
}

View File

@ -0,0 +1,77 @@
package options
import (
"testing"
"k8s.io/apimachinery/pkg/util/validation/field"
)
func TestValidateKarmadaSchedulerEstimator(t *testing.T) {
successCases := []Options{
{
ClusterName: "testCluster",
BindAddress: "0.0.0.0",
SecurePort: 10100,
ServerPort: 8088,
},
}
for _, successCase := range successCases {
if errs := successCase.Validate(); len(errs) != 0 {
t.Errorf("expected success: %v", errs)
}
}
newPath := field.NewPath("Options")
testCases := map[string]struct {
opt Options
expectedErrs field.ErrorList
}{
"invalid ClusterName": {
opt: Options{
ClusterName: "",
BindAddress: "127.0.0.1",
SecurePort: 10100,
ServerPort: 8088,
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("ClusterName"), "", "clusterName cannot be empty")},
},
"invalid BindAddress": {
opt: Options{
ClusterName: "testCluster",
BindAddress: "127.0.0.1:8082",
SecurePort: 10100,
ServerPort: 8088,
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("BindAddress"), "127.0.0.1:8082", "not a valid textual representation of an IP address")},
},
"invalid SecurePort": {
opt: Options{
ClusterName: "testCluster",
BindAddress: "127.0.0.1",
SecurePort: 908188,
ServerPort: 8088,
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("SecurePort"), 908188, "must be a valid port between 0 and 65535 inclusive")},
},
"invalid ServerPort": {
opt: Options{
ClusterName: "testCluster",
BindAddress: "127.0.0.1",
SecurePort: 9089,
ServerPort: 80888,
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("ServerPort"), 80888, "must be a valid port between 0 and 65535 inclusive")},
},
}
for _, testCase := range testCases {
errs := testCase.opt.Validate()
if len(testCase.expectedErrs) != len(errs) {
t.Fatalf("Expected %d errors, got %d errors: %v", len(testCase.expectedErrs), len(errs), errs)
}
for i, err := range errs {
if err.Error() != testCase.expectedErrs[i].Error() {
t.Fatalf("Expected error: %s, got %s", testCase.expectedErrs[i], err.Error())
}
}
}
}

View File

@ -25,11 +25,15 @@ func NewSchedulerEstimatorCommand(ctx context.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "karmada-scheduler-estimator",
Long: `The karmada scheduler estimator runs an accurate scheduler estimator of a cluster`,
Run: func(cmd *cobra.Command, args []string) {
if err := run(ctx, opts); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
RunE: func(cmd *cobra.Command, args []string) error {
// validate options
if err := opts.Validate(); err != nil {
return err.ToAggregate()
}
if err := run(ctx, opts); err != nil {
return err
}
return nil
},
}