2016-06-29 10:32:35 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package kubectl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2018-03-16 01:54:52 +00:00
|
|
|
"strings"
|
2016-06-29 10:32:35 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-10-28 01:31:42 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-01-17 03:38:19 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-27 20:42:17 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
2016-06-29 10:32:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServiceBasicGenerate(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2017-10-28 01:31:42 +00:00
|
|
|
serviceType v1.ServiceType
|
2016-06-29 10:32:35 +00:00
|
|
|
tcp []string
|
|
|
|
clusterip string
|
2017-10-28 01:31:42 +00:00
|
|
|
expected *v1.Service
|
2016-06-29 10:32:35 +00:00
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "clusterip-ok",
|
|
|
|
tcp: []string{"456", "321:908"},
|
|
|
|
clusterip: "",
|
2017-10-28 01:31:42 +00:00
|
|
|
serviceType: v1.ServiceTypeClusterIP,
|
|
|
|
expected: &v1.Service{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-06-29 10:32:35 +00:00
|
|
|
Name: "clusterip-ok",
|
|
|
|
Labels: map[string]string{"app": "clusterip-ok"},
|
|
|
|
},
|
2017-10-28 01:31:42 +00:00
|
|
|
Spec: v1.ServiceSpec{Type: "ClusterIP",
|
|
|
|
Ports: []v1.ServicePort{{Name: "456", Protocol: "TCP", Port: 456, TargetPort: intstr.IntOrString{Type: 0, IntVal: 456, StrVal: ""}, NodePort: 0},
|
2016-06-29 10:32:35 +00:00
|
|
|
{Name: "321-908", Protocol: "TCP", Port: 321, TargetPort: intstr.IntOrString{Type: 0, IntVal: 908, StrVal: ""}, NodePort: 0}},
|
|
|
|
Selector: map[string]string{"app": "clusterip-ok"},
|
|
|
|
ClusterIP: "", ExternalIPs: []string(nil), LoadBalancerIP: ""},
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "clusterip-missing",
|
2017-10-28 01:31:42 +00:00
|
|
|
serviceType: v1.ServiceTypeClusterIP,
|
2016-06-29 10:32:35 +00:00
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "clusterip-none-wrong-type",
|
|
|
|
tcp: []string{},
|
|
|
|
clusterip: "None",
|
2017-10-28 01:31:42 +00:00
|
|
|
serviceType: v1.ServiceTypeNodePort,
|
2016-06-29 10:32:35 +00:00
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "clusterip-none-ok",
|
|
|
|
tcp: []string{},
|
|
|
|
clusterip: "None",
|
2017-10-28 01:31:42 +00:00
|
|
|
serviceType: v1.ServiceTypeClusterIP,
|
|
|
|
expected: &v1.Service{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-06-29 10:32:35 +00:00
|
|
|
Name: "clusterip-none-ok",
|
|
|
|
Labels: map[string]string{"app": "clusterip-none-ok"},
|
|
|
|
},
|
2017-10-28 01:31:42 +00:00
|
|
|
Spec: v1.ServiceSpec{Type: "ClusterIP",
|
|
|
|
Ports: []v1.ServicePort{},
|
2016-06-29 10:32:35 +00:00
|
|
|
Selector: map[string]string{"app": "clusterip-none-ok"},
|
|
|
|
ClusterIP: "None", ExternalIPs: []string(nil), LoadBalancerIP: ""},
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
},
|
2017-06-09 15:22:37 +00:00
|
|
|
{
|
|
|
|
name: "clusterip-none-and-port-mapping",
|
|
|
|
tcp: []string{"456:9898"},
|
|
|
|
clusterip: "None",
|
2017-10-28 01:31:42 +00:00
|
|
|
serviceType: v1.ServiceTypeClusterIP,
|
|
|
|
expected: &v1.Service{
|
2017-06-09 15:22:37 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "clusterip-none-and-port-mapping",
|
|
|
|
Labels: map[string]string{"app": "clusterip-none-and-port-mapping"},
|
|
|
|
},
|
2017-10-28 01:31:42 +00:00
|
|
|
Spec: v1.ServiceSpec{Type: "ClusterIP",
|
|
|
|
Ports: []v1.ServicePort{{Name: "456-9898", Protocol: "TCP", Port: 456, TargetPort: intstr.IntOrString{Type: 0, IntVal: 9898, StrVal: ""}, NodePort: 0}},
|
2017-06-09 15:22:37 +00:00
|
|
|
Selector: map[string]string{"app": "clusterip-none-and-port-mapping"},
|
|
|
|
ClusterIP: "None", ExternalIPs: []string(nil), LoadBalancerIP: ""},
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
},
|
2016-06-29 10:32:35 +00:00
|
|
|
{
|
|
|
|
name: "loadbalancer-ok",
|
|
|
|
tcp: []string{"456:9898"},
|
|
|
|
clusterip: "",
|
2017-10-28 01:31:42 +00:00
|
|
|
serviceType: v1.ServiceTypeLoadBalancer,
|
|
|
|
expected: &v1.Service{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-06-29 10:32:35 +00:00
|
|
|
Name: "loadbalancer-ok",
|
|
|
|
Labels: map[string]string{"app": "loadbalancer-ok"},
|
|
|
|
},
|
2017-10-28 01:31:42 +00:00
|
|
|
Spec: v1.ServiceSpec{Type: "LoadBalancer",
|
|
|
|
Ports: []v1.ServicePort{{Name: "456-9898", Protocol: "TCP", Port: 456, TargetPort: intstr.IntOrString{Type: 0, IntVal: 9898, StrVal: ""}, NodePort: 0}},
|
2016-06-29 10:32:35 +00:00
|
|
|
Selector: map[string]string{"app": "loadbalancer-ok"},
|
|
|
|
ClusterIP: "", ExternalIPs: []string(nil), LoadBalancerIP: ""},
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
},
|
2017-11-13 08:51:30 +00:00
|
|
|
{
|
|
|
|
name: "invalid-port",
|
|
|
|
tcp: []string{"65536"},
|
|
|
|
clusterip: "None",
|
|
|
|
serviceType: v1.ServiceTypeClusterIP,
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid-port-mapping",
|
|
|
|
tcp: []string{"8080:-abc"},
|
|
|
|
clusterip: "None",
|
|
|
|
serviceType: v1.ServiceTypeClusterIP,
|
|
|
|
expectErr: true,
|
|
|
|
},
|
2016-06-29 10:32:35 +00:00
|
|
|
{
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
generator := ServiceCommonGeneratorV1{
|
|
|
|
Name: test.name,
|
|
|
|
TCP: test.tcp,
|
|
|
|
Type: test.serviceType,
|
|
|
|
ClusterIP: test.clusterip,
|
|
|
|
}
|
|
|
|
obj, err := generator.StructuredGenerate()
|
|
|
|
if !test.expectErr && err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if test.expectErr && err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2017-10-28 01:31:42 +00:00
|
|
|
if !reflect.DeepEqual(obj.(*v1.Service), test.expected) {
|
|
|
|
t.Errorf("test: %v\nexpected:\n%#v\nsaw:\n%#v", test.name, test.expected, obj.(*v1.Service))
|
2016-06-29 10:32:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 01:54:52 +00:00
|
|
|
|
|
|
|
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 sucess, Got err: %v", test.name, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|