2018-02-14 17:02:57 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 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 util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-08-02 12:10:03 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2018-02-14 17:02:57 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLookupContainerPortNumberByServicePort(t *testing.T) {
|
2018-05-12 09:24:28 +00:00
|
|
|
tests := []struct {
|
2018-02-14 17:02:57 +00:00
|
|
|
name string
|
2018-08-02 12:10:03 +00:00
|
|
|
svc v1.Service
|
|
|
|
pod v1.Pod
|
2018-02-14 17:02:57 +00:00
|
|
|
port int32
|
|
|
|
containerPort int32
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test success 1 (int port)",
|
2018-08-02 12:10:03 +00:00
|
|
|
svc: v1.Service{
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
Ports: []v1.ServicePort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: intstr.FromInt(8080),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-08-02 12:10:03 +00:00
|
|
|
pod: v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
2018-08-02 12:10:03 +00:00
|
|
|
Ports: []v1.ContainerPort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
ContainerPort: int32(8080)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
port: 80,
|
|
|
|
containerPort: 8080,
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test success 2 (clusterIP: None)",
|
2018-08-02 12:10:03 +00:00
|
|
|
svc: v1.Service{
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
ClusterIP: v1.ClusterIPNone,
|
|
|
|
Ports: []v1.ServicePort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: intstr.FromInt(8080),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-08-02 12:10:03 +00:00
|
|
|
pod: v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
2018-08-02 12:10:03 +00:00
|
|
|
Ports: []v1.ContainerPort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
ContainerPort: int32(8080)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
port: 80,
|
|
|
|
containerPort: 80,
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test success 3 (named port)",
|
2018-08-02 12:10:03 +00:00
|
|
|
svc: v1.Service{
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
Ports: []v1.ServicePort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: intstr.FromString("http"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-08-02 12:10:03 +00:00
|
|
|
pod: v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
2018-08-02 12:10:03 +00:00
|
|
|
Ports: []v1.ContainerPort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
ContainerPort: int32(8080)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
port: 80,
|
|
|
|
containerPort: 8080,
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test success (targetPort omitted)",
|
2018-08-02 12:10:03 +00:00
|
|
|
svc: v1.Service{
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
Ports: []v1.ServicePort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-08-02 12:10:03 +00:00
|
|
|
pod: v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
2018-08-02 12:10:03 +00:00
|
|
|
Ports: []v1.ContainerPort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
ContainerPort: int32(80)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
port: 80,
|
|
|
|
containerPort: 80,
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test failure 1 (cannot find a matching named port)",
|
2018-08-02 12:10:03 +00:00
|
|
|
svc: v1.Service{
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
Ports: []v1.ServicePort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: intstr.FromString("http"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-08-02 12:10:03 +00:00
|
|
|
pod: v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
2018-08-02 12:10:03 +00:00
|
|
|
Ports: []v1.ContainerPort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Name: "https",
|
|
|
|
ContainerPort: int32(443)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
port: 80,
|
|
|
|
containerPort: -1,
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test failure 2 (cannot find a matching service port)",
|
2018-08-02 12:10:03 +00:00
|
|
|
svc: v1.Service{
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
Ports: []v1.ServicePort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: intstr.FromString("http"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-08-02 12:10:03 +00:00
|
|
|
pod: v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
2018-08-02 12:10:03 +00:00
|
|
|
Ports: []v1.ContainerPort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Name: "https",
|
|
|
|
ContainerPort: int32(443)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
port: 443,
|
|
|
|
containerPort: 443,
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test failure 2 (cannot find a matching service port, but ClusterIP: None)",
|
2018-08-02 12:10:03 +00:00
|
|
|
svc: v1.Service{
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
ClusterIP: v1.ClusterIPNone,
|
|
|
|
Ports: []v1.ServicePort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: intstr.FromString("http"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-08-02 12:10:03 +00:00
|
|
|
pod: v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
2018-08-02 12:10:03 +00:00
|
|
|
Ports: []v1.ContainerPort{
|
2018-02-14 17:02:57 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
ContainerPort: int32(80)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
port: 443,
|
|
|
|
containerPort: 443,
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-05-12 09:24:28 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
containerPort, err := LookupContainerPortNumberByServicePort(tt.svc, tt.pod, tt.port)
|
|
|
|
if err != nil {
|
|
|
|
if tt.err {
|
|
|
|
if containerPort != tt.containerPort {
|
|
|
|
t.Errorf("%v: expected port %v; got %v", tt.name, tt.containerPort, containerPort)
|
|
|
|
}
|
|
|
|
return
|
2018-02-14 17:02:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 09:24:28 +00:00
|
|
|
t.Errorf("%v: unexpected error: %v", tt.name, err)
|
|
|
|
return
|
|
|
|
}
|
2018-02-14 17:02:57 +00:00
|
|
|
|
2018-05-12 09:24:28 +00:00
|
|
|
if tt.err {
|
|
|
|
t.Errorf("%v: unexpected success", tt.name)
|
|
|
|
return
|
|
|
|
}
|
2018-02-14 17:02:57 +00:00
|
|
|
|
2018-05-12 09:24:28 +00:00
|
|
|
if containerPort != tt.containerPort {
|
|
|
|
t.Errorf("%v: expected port %v; got %v", tt.name, tt.containerPort, containerPort)
|
|
|
|
}
|
|
|
|
})
|
2018-02-14 17:02:57 +00:00
|
|
|
}
|
|
|
|
}
|