mirror of https://github.com/k3s-io/k3s
Merge pull request #61216 from hanxiaoshuai/fixtodo0315
Automatic merge from submit-queue (batch tested with PRs 59285, 61216). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. add unit test for func parsePorts and validate **What this PR does / why we need it**: add unit test for func parsePorts and validate **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes # **Special notes for your reviewer**: **Release note**: ```release-note NONE ```pull/8/head
commit
07d1a8cb0c
|
@ -18,6 +18,7 @@ package kubectl
|
|||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
|
@ -152,3 +153,169 @@ func TestServiceBasicGenerate(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsePorts(t *testing.T) {
|
||||
tests := []struct {
|
||||
portString string
|
||||
expectPort int32
|
||||
expectTargetPort intstr.IntOrString
|
||||
expectErr string
|
||||
}{
|
||||
{
|
||||
portString: "3232",
|
||||
expectPort: 3232,
|
||||
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 3232},
|
||||
},
|
||||
{
|
||||
portString: "1:65535",
|
||||
expectPort: 1,
|
||||
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 65535},
|
||||
},
|
||||
{
|
||||
portString: "-5:1234",
|
||||
expectPort: 0,
|
||||
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
|
||||
expectErr: "must be between 1 and 65535, inclusive",
|
||||
},
|
||||
{
|
||||
portString: "5:65536",
|
||||
expectPort: 0,
|
||||
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
|
||||
expectErr: "must be between 1 and 65535, inclusive",
|
||||
},
|
||||
{
|
||||
portString: "test-5:443",
|
||||
expectPort: 0,
|
||||
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
|
||||
expectErr: "invalid syntax",
|
||||
},
|
||||
{
|
||||
portString: "5:test-443",
|
||||
expectPort: 5,
|
||||
expectTargetPort: intstr.IntOrString{Type: intstr.String, StrVal: "test-443"},
|
||||
},
|
||||
{
|
||||
portString: "5:test*443",
|
||||
expectPort: 0,
|
||||
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
|
||||
expectErr: "must contain only alpha-numeric characters (a-z, 0-9), and hyphens (-)",
|
||||
},
|
||||
{
|
||||
portString: "5:",
|
||||
expectPort: 0,
|
||||
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
|
||||
expectErr: "must contain at least one letter or number (a-z, 0-9)",
|
||||
},
|
||||
{
|
||||
portString: "5:test--443",
|
||||
expectPort: 0,
|
||||
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
|
||||
expectErr: "must not contain consecutive hyphens",
|
||||
},
|
||||
{
|
||||
portString: "5:test443-",
|
||||
expectPort: 0,
|
||||
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
|
||||
expectErr: "must not begin or end with a hyphen",
|
||||
},
|
||||
{
|
||||
portString: "3232:1234:4567",
|
||||
expectPort: 3232,
|
||||
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 1234},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.portString, func(t *testing.T) {
|
||||
port, targetPort, err := parsePorts(test.portString)
|
||||
if len(test.expectErr) != 0 {
|
||||
if !strings.Contains(err.Error(), test.expectErr) {
|
||||
t.Errorf("parse ports string: %s. Expected err: %s, Got err: %v.", test.portString, test.expectErr, err)
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(targetPort, test.expectTargetPort) || port != test.expectPort {
|
||||
t.Errorf("parse ports string: %s. Expected port:%d, targetPort:%v, Got port:%d, targetPort:%v.", test.portString, test.expectPort, test.expectTargetPort, port, targetPort)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateServiceCommonGeneratorV1(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
s ServiceCommonGeneratorV1
|
||||
expectErr string
|
||||
}{
|
||||
{
|
||||
name: "validate-ok",
|
||||
s: ServiceCommonGeneratorV1{
|
||||
Name: "validate-ok",
|
||||
Type: v1.ServiceTypeClusterIP,
|
||||
TCP: []string{"123", "234:1234"},
|
||||
ClusterIP: "",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Name-none",
|
||||
s: ServiceCommonGeneratorV1{
|
||||
Type: v1.ServiceTypeClusterIP,
|
||||
TCP: []string{"123", "234:1234"},
|
||||
ClusterIP: "",
|
||||
},
|
||||
expectErr: "name must be specified",
|
||||
},
|
||||
{
|
||||
name: "Type-none",
|
||||
s: ServiceCommonGeneratorV1{
|
||||
Name: "validate-ok",
|
||||
TCP: []string{"123", "234:1234"},
|
||||
ClusterIP: "",
|
||||
},
|
||||
expectErr: "type must be specified",
|
||||
},
|
||||
{
|
||||
name: "invalid-ClusterIPNone",
|
||||
s: ServiceCommonGeneratorV1{
|
||||
Name: "validate-ok",
|
||||
Type: v1.ServiceTypeNodePort,
|
||||
TCP: []string{"123", "234:1234"},
|
||||
ClusterIP: v1.ClusterIPNone,
|
||||
},
|
||||
expectErr: "ClusterIP=None can only be used with ClusterIP service type",
|
||||
},
|
||||
{
|
||||
name: "TCP-none",
|
||||
s: ServiceCommonGeneratorV1{
|
||||
Name: "validate-ok",
|
||||
Type: v1.ServiceTypeClusterIP,
|
||||
ClusterIP: "",
|
||||
},
|
||||
expectErr: "at least one tcp port specifier must be provided",
|
||||
},
|
||||
{
|
||||
name: "invalid-ExternalName",
|
||||
s: ServiceCommonGeneratorV1{
|
||||
Name: "validate-ok",
|
||||
Type: v1.ServiceTypeExternalName,
|
||||
TCP: []string{"123", "234:1234"},
|
||||
ClusterIP: "",
|
||||
ExternalName: "@oi:test",
|
||||
},
|
||||
expectErr: "invalid service external name",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := test.s.validate()
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), test.expectErr) {
|
||||
t.Errorf("validate:%s Expected err: %s, Got err: %v", test.name, test.expectErr, err)
|
||||
}
|
||||
}
|
||||
if err == nil && len(test.expectErr) != 0 {
|
||||
t.Errorf("validate:%s Expected success, Got err: %v", test.name, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue