Merge pull request #490 from chentao1596/name_port_unittest
Add unit test case for named_port
This commit is contained in:
commit
e8b61b40d9
3 changed files with 195 additions and 4 deletions
|
@ -111,7 +111,7 @@ type GenericController struct {
|
|||
|
||||
// Configuration contains all the settings required by an Ingress controller
|
||||
type Configuration struct {
|
||||
Client *clientset.Clientset
|
||||
Client clientset.Interface
|
||||
|
||||
ResyncPeriod time.Duration
|
||||
DefaultService string
|
||||
|
|
|
@ -38,7 +38,7 @@ import (
|
|||
func (ic *GenericController) checkSvcForUpdate(svc *api.Service) error {
|
||||
// get the pods associated with the service
|
||||
// TODO: switch this to a watch
|
||||
pods, err := ic.cfg.Client.Pods(svc.Namespace).List(api.ListOptions{
|
||||
pods, err := ic.cfg.Client.Core().Pods(svc.Namespace).List(api.ListOptions{
|
||||
LabelSelector: labels.Set(svc.Spec.Selector).AsSelector(),
|
||||
})
|
||||
|
||||
|
@ -82,7 +82,7 @@ func (ic *GenericController) checkSvcForUpdate(svc *api.Service) error {
|
|||
if len(namedPorts) > 0 && !reflect.DeepEqual(curNamedPort, namedPorts) {
|
||||
data, _ := json.Marshal(namedPorts)
|
||||
|
||||
newSvc, err := ic.cfg.Client.Services(svc.Namespace).Get(svc.Name)
|
||||
newSvc, err := ic.cfg.Client.Core().Services(svc.Namespace).Get(svc.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting service %v/%v: %v", svc.Namespace, svc.Name, err)
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ func (ic *GenericController) checkSvcForUpdate(svc *api.Service) error {
|
|||
|
||||
newSvc.ObjectMeta.Annotations[service.NamedPortAnnotation] = string(data)
|
||||
glog.Infof("updating service %v with new named port mappings", svc.Name)
|
||||
_, err = ic.cfg.Client.Services(svc.Namespace).Update(newSvc)
|
||||
_, err = ic.cfg.Client.Core().Services(svc.Namespace).Update(newSvc)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error syncing service %v/%v: %v", svc.Namespace, svc.Name, err)
|
||||
}
|
||||
|
|
191
core/pkg/ingress/controller/named_port_test.go
Normal file
191
core/pkg/ingress/controller/named_port_test.go
Normal file
|
@ -0,0 +1,191 @@
|
|||
/*
|
||||
Copyright 2017 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 controller
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"k8s.io/ingress/core/pkg/ingress/annotations/service"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
testclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
|
||||
"k8s.io/kubernetes/pkg/util/intstr"
|
||||
)
|
||||
|
||||
func buildSimpleClientSet() *testclient.Clientset {
|
||||
return testclient.NewSimpleClientset(
|
||||
&api.PodList{Items: []api.Pod{
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo1",
|
||||
Namespace: api.NamespaceDefault,
|
||||
Labels: map[string]string{
|
||||
"lable_sig": "foo_pod",
|
||||
},
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
NodeName: "foo_node_1",
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Ports: []api.ContainerPort{
|
||||
{
|
||||
Name: "foo1_named_port_c1",
|
||||
Protocol: api.ProtocolTCP,
|
||||
ContainerPort: 80,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo1",
|
||||
Namespace: api.NamespaceSystem,
|
||||
Labels: map[string]string{
|
||||
"lable_sig": "foo_pod",
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
&api.ServiceList{Items: []api.Service{
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: api.NamespaceDefault,
|
||||
Name: "named_port_test_service",
|
||||
},
|
||||
},
|
||||
}},
|
||||
)
|
||||
}
|
||||
|
||||
func buildGenericController() *GenericController {
|
||||
return &GenericController{
|
||||
cfg: &Configuration{
|
||||
Client: buildSimpleClientSet(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func buildService() *api.Service {
|
||||
return &api.Service{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: api.NamespaceSystem,
|
||||
Name: "named_port_test_service",
|
||||
},
|
||||
|
||||
Spec: api.ServiceSpec{
|
||||
ClusterIP: "10.10.10.10",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckSvcForUpdate(t *testing.T) {
|
||||
foos := []struct {
|
||||
n string
|
||||
ns string
|
||||
sps []api.ServicePort
|
||||
sl map[string]string
|
||||
er string
|
||||
}{
|
||||
{
|
||||
"pods_have_not_been_found_in_this_namespace",
|
||||
api.NamespaceSystem,
|
||||
[]api.ServicePort{
|
||||
{Name: "foo_port_1", Port: 8080, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("foo1_named_port_c1")},
|
||||
{Name: "foo_port_2", Port: 8181, Protocol: api.ProtocolTCP, TargetPort: intstr.FromInt(81)},
|
||||
{Name: "foo_port_3", Port: 8282, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("")},
|
||||
},
|
||||
map[string]string{
|
||||
"lable_sig": "foo_pod",
|
||||
},
|
||||
"",
|
||||
},
|
||||
{
|
||||
"ports_have_not_been_found_in_this_pod",
|
||||
api.NamespaceDefault,
|
||||
[]api.ServicePort{
|
||||
{Name: "foo_port_1", Port: 8080, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("foo1_named_port_cXX")},
|
||||
{Name: "foo_port_2", Port: 8181, Protocol: api.ProtocolTCP, TargetPort: intstr.FromInt(81)},
|
||||
{Name: "foo_port_3", Port: 8282, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("")},
|
||||
},
|
||||
map[string]string{
|
||||
"lable_sig": "foo_pod",
|
||||
},
|
||||
"",
|
||||
},
|
||||
|
||||
{
|
||||
"ports_fixed",
|
||||
api.NamespaceDefault,
|
||||
[]api.ServicePort{
|
||||
{Name: "foo_port_1", Port: 8080, Protocol: api.ProtocolTCP, TargetPort: intstr.FromInt(80)},
|
||||
{Name: "foo_port_2", Port: 8181, Protocol: api.ProtocolTCP, TargetPort: intstr.FromInt(81)},
|
||||
{Name: "foo_port_3", Port: 8282, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("")},
|
||||
},
|
||||
map[string]string{
|
||||
"lable_sig": "foo_pod",
|
||||
},
|
||||
"",
|
||||
},
|
||||
{
|
||||
"nil_selector",
|
||||
api.NamespaceDefault,
|
||||
[]api.ServicePort{
|
||||
{Name: "foo_port_1", Port: 8080, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("foo1_named_port_c1")},
|
||||
{Name: "foo_port_2", Port: 8181, Protocol: api.ProtocolTCP, TargetPort: intstr.FromInt(81)},
|
||||
{Name: "foo_port_3", Port: 8282, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("")},
|
||||
},
|
||||
nil,
|
||||
"{\"foo1_named_port_c1\":\"80\"}",
|
||||
},
|
||||
{
|
||||
"normal_update",
|
||||
api.NamespaceDefault,
|
||||
[]api.ServicePort{
|
||||
{Name: "foo_port_1", Port: 8080, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("foo1_named_port_c1")},
|
||||
{Name: "foo_port_2", Port: 8181, Protocol: api.ProtocolTCP, TargetPort: intstr.FromInt(81)},
|
||||
{Name: "foo_port_3", Port: 8282, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("")},
|
||||
},
|
||||
map[string]string{
|
||||
"lable_sig": "foo_pod",
|
||||
},
|
||||
"{\"foo1_named_port_c1\":\"80\"}",
|
||||
},
|
||||
}
|
||||
|
||||
for _, foo := range foos {
|
||||
t.Run(foo.n, func(t *testing.T) {
|
||||
gc := buildGenericController()
|
||||
s := buildService()
|
||||
s.SetNamespace(foo.ns)
|
||||
s.Spec.Ports = foo.sps
|
||||
s.Spec.Selector = foo.sl
|
||||
|
||||
err := gc.checkSvcForUpdate(s)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
rs, _ := gc.cfg.Client.Core().Services(api.NamespaceDefault).Get("named_port_test_service")
|
||||
rr := rs.ObjectMeta.Annotations[service.NamedPortAnnotation]
|
||||
if !reflect.DeepEqual(rr, foo.er) {
|
||||
t.Errorf("Returned %s, but expected %s for %s", rr, foo.er, foo.n)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue