123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- /*
- Copyright 2015 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 pod
- import (
- "testing"
- "k8s.io/kubernetes/pkg/api"
- "k8s.io/kubernetes/pkg/util/intstr"
- )
- func TestFindPort(t *testing.T) {
- testCases := []struct {
- name string
- containers []api.Container
- port intstr.IntOrString
- expected int
- pass bool
- }{{
- name: "valid int, no ports",
- containers: []api.Container{{}},
- port: intstr.FromInt(93),
- expected: 93,
- pass: true,
- }, {
- name: "valid int, with ports",
- containers: []api.Container{{Ports: []api.ContainerPort{{
- Name: "",
- ContainerPort: 11,
- Protocol: "TCP",
- }, {
- Name: "p",
- ContainerPort: 22,
- Protocol: "TCP",
- }}}},
- port: intstr.FromInt(93),
- expected: 93,
- pass: true,
- }, {
- name: "valid str, no ports",
- containers: []api.Container{{}},
- port: intstr.FromString("p"),
- expected: 0,
- pass: false,
- }, {
- name: "valid str, one ctr with ports",
- containers: []api.Container{{Ports: []api.ContainerPort{{
- Name: "",
- ContainerPort: 11,
- Protocol: "UDP",
- }, {
- Name: "p",
- ContainerPort: 22,
- Protocol: "TCP",
- }, {
- Name: "q",
- ContainerPort: 33,
- Protocol: "TCP",
- }}}},
- port: intstr.FromString("q"),
- expected: 33,
- pass: true,
- }, {
- name: "valid str, two ctr with ports",
- containers: []api.Container{{}, {Ports: []api.ContainerPort{{
- Name: "",
- ContainerPort: 11,
- Protocol: "UDP",
- }, {
- Name: "p",
- ContainerPort: 22,
- Protocol: "TCP",
- }, {
- Name: "q",
- ContainerPort: 33,
- Protocol: "TCP",
- }}}},
- port: intstr.FromString("q"),
- expected: 33,
- pass: true,
- }, {
- name: "valid str, two ctr with same port",
- containers: []api.Container{{}, {Ports: []api.ContainerPort{{
- Name: "",
- ContainerPort: 11,
- Protocol: "UDP",
- }, {
- Name: "p",
- ContainerPort: 22,
- Protocol: "TCP",
- }, {
- Name: "q",
- ContainerPort: 22,
- Protocol: "TCP",
- }}}},
- port: intstr.FromString("q"),
- expected: 22,
- pass: true,
- }, {
- name: "valid str, invalid protocol",
- containers: []api.Container{{}, {Ports: []api.ContainerPort{{
- Name: "a",
- ContainerPort: 11,
- Protocol: "snmp",
- },
- }}},
- port: intstr.FromString("a"),
- expected: 0,
- pass: false,
- }, {
- name: "valid hostPort",
- containers: []api.Container{{}, {Ports: []api.ContainerPort{{
- Name: "a",
- ContainerPort: 11,
- HostPort: 81,
- Protocol: "TCP",
- },
- }}},
- port: intstr.FromString("a"),
- expected: 11,
- pass: true,
- },
- {
- name: "invalid hostPort",
- containers: []api.Container{{}, {Ports: []api.ContainerPort{{
- Name: "a",
- ContainerPort: 11,
- HostPort: -1,
- Protocol: "TCP",
- },
- }}},
- port: intstr.FromString("a"),
- expected: 11,
- pass: true,
- //this should fail but passes.
- },
- {
- name: "invalid ContainerPort",
- containers: []api.Container{{}, {Ports: []api.ContainerPort{{
- Name: "a",
- ContainerPort: -1,
- Protocol: "TCP",
- },
- }}},
- port: intstr.FromString("a"),
- expected: -1,
- pass: true,
- //this should fail but passes
- },
- {
- name: "HostIP Address",
- containers: []api.Container{{}, {Ports: []api.ContainerPort{{
- Name: "a",
- ContainerPort: 11,
- HostIP: "192.168.1.1",
- Protocol: "TCP",
- },
- }}},
- port: intstr.FromString("a"),
- expected: 11,
- pass: true,
- },
- }
- for _, tc := range testCases {
- port, err := FindPort(&api.Pod{Spec: api.PodSpec{Containers: tc.containers}},
- &api.ServicePort{Protocol: "TCP", TargetPort: tc.port})
- if err != nil && tc.pass {
- t.Errorf("unexpected error for %s: %v", tc.name, err)
- }
- if err == nil && !tc.pass {
- t.Errorf("unexpected non-error for %s: %d", tc.name, port)
- }
- if port != tc.expected {
- t.Errorf("wrong result for %s: expected %d, got %d", tc.name, tc.expected, port)
- }
- }
- }
|