Merge pull request #111492 from muyangren2/addtest_validation_port

add test for ValidatePort
This commit is contained in:
Kubernetes Prow Robot 2022-08-01 03:50:29 -07:00 committed by GitHub
commit ebad8c70c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,6 +18,7 @@ package validation
import (
"os"
"strings"
"testing"
"github.com/spf13/pflag"
@ -183,6 +184,34 @@ func TestValidateIPFromString(t *testing.T) {
}
}
func TestValidatePort(t *testing.T) {
var tests = []struct {
name string
port int32
expectedErr bool
}{
{"negative number port", -1234, true},
{"zero number port", 0, true},
{"minimum valid value port", 1, false},
{"valid value port", 300, false},
{"maximum valid value port", 65535, false},
{"if port greater than 65535", 65538, true},
}
for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
allErrs := ValidatePort(rt.port, nil)
if len(allErrs) > 0 {
find := strings.Contains(allErrs[0].Error(), "port number is not valid")
if find != rt.expectedErr {
t.Errorf(
"test case failed :\n\t err(s): %v\n\t", allErrs[0].Error(),
)
}
}
})
}
}
func TestValidateIPNetFromString(t *testing.T) {
var tests = []struct {
name string