aggregation: restore cluster-ip routing tests
Kubernetes-commit: 35335c09e17c3479ff660f93aaaeed041837b247
This commit is contained in:
parent
be1a712a68
commit
0b2bbf270c
|
|
@ -19,10 +19,14 @@ go_test(
|
|||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/net:go_default_library",
|
||||
"//vendor/k8s.io/client-go/listers/core/v1:go_default_library",
|
||||
"//vendor/k8s.io/client-go/pkg/api/v1:go_default_library",
|
||||
"//vendor/k8s.io/client-go/tools/cache:go_default_library",
|
||||
"//vendor/k8s.io/kube-aggregator/pkg/apis/apiregistration:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -17,103 +17,215 @@ limitations under the License.
|
|||
package proxy
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
listersv1 "k8s.io/client-go/listers/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
v1listers "k8s.io/client-go/listers/core/v1"
|
||||
"k8s.io/client-go/pkg/api/v1"
|
||||
"net/http"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
|
||||
"k8s.io/kube-aggregator/pkg/apis/apiregistration"
|
||||
)
|
||||
|
||||
type serviceListerMock struct {
|
||||
services []*v1.Service
|
||||
err error
|
||||
}
|
||||
func TestResolve(t *testing.T) {
|
||||
endpoints := []*v1.Endpoints{{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "one", Name: "alfa"},
|
||||
Subsets: []v1.EndpointSubset{{
|
||||
Addresses: []v1.EndpointAddress{{Hostname: "dummy-host", IP: "127.0.0.1"}},
|
||||
Ports: []v1.EndpointPort{{Port: 443}},
|
||||
}},
|
||||
}}
|
||||
|
||||
func (s *serviceListerMock) List(selector labels.Selector) (ret []*v1.Service, err error) {
|
||||
return s.services, err
|
||||
}
|
||||
|
||||
func (s *serviceListerMock) Services(namespace string) listersv1.ServiceNamespaceLister {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *serviceListerMock) GetPodServices(pod *v1.Pod) ([]*v1.Service, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
type endpointsListerMock struct {
|
||||
endpoints []*v1.Endpoints
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *endpointsListerMock) List(selector labels.Selector) (ret []*v1.Endpoints, err error) {
|
||||
return e.endpoints, e.err
|
||||
}
|
||||
|
||||
func (e *endpointsListerMock) Endpoints(namespace string) listersv1.EndpointsNamespaceLister {
|
||||
return endpointsNamespaceListMock{
|
||||
endpoints: e.endpoints,
|
||||
err: e.err,
|
||||
}
|
||||
}
|
||||
|
||||
type endpointsNamespaceListMock struct {
|
||||
endpoints []*v1.Endpoints
|
||||
err error
|
||||
}
|
||||
|
||||
func (e endpointsNamespaceListMock) List(selector labels.Selector) (ret []*v1.Endpoints, err error) {
|
||||
return e.endpoints, e.err
|
||||
}
|
||||
|
||||
func (e endpointsNamespaceListMock) Get(name string) (*v1.Endpoints, error) {
|
||||
if len(e.endpoints) == 0 {
|
||||
return nil, e.err
|
||||
}
|
||||
return e.endpoints[0], e.err
|
||||
}
|
||||
|
||||
func TestNoEndpointNoPort(t *testing.T) {
|
||||
services := &serviceListerMock{}
|
||||
endpoints := &endpointsListerMock{err: errors.NewNotFound(v1.Resource("endpoints"), "dummy-svc")}
|
||||
url, err := ResolveEndpoint(services, endpoints, "dummy-ns", "dummy-svc")
|
||||
if url != nil {
|
||||
t.Error("Should not have gotten back an URL")
|
||||
}
|
||||
if err == nil {
|
||||
t.Error("Should have gotten an error")
|
||||
}
|
||||
se, ok := err.(*errors.StatusError)
|
||||
if !ok {
|
||||
t.Error("Should have gotten a status error not %T", err)
|
||||
}
|
||||
if se.ErrStatus.Code != http.StatusNotFound {
|
||||
t.Error("Should have gotten a http 404 not %d", se.ErrStatus.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOneEndpointNoPort(t *testing.T) {
|
||||
services := &serviceListerMock{}
|
||||
address := v1.EndpointAddress{Hostname: "dummy-host", IP: "127.0.0.1"}
|
||||
addresses := []v1.EndpointAddress{address}
|
||||
port := v1.EndpointPort{Port: 443}
|
||||
ports := []v1.EndpointPort{port}
|
||||
endpoint := v1.EndpointSubset{Addresses: addresses, Ports: ports}
|
||||
subsets := []v1.EndpointSubset{endpoint}
|
||||
one := &v1.Endpoints{Subsets: subsets}
|
||||
slice := []*v1.Endpoints{one}
|
||||
endpoints := &endpointsListerMock{endpoints: slice}
|
||||
url, err := ResolveEndpoint(services, endpoints, "dummy-ns", "dummy-svc")
|
||||
if err != nil {
|
||||
t.Errorf("Should not have gotten error %v", err)
|
||||
}
|
||||
if url == nil {
|
||||
t.Error("Should not have gotten back an URL")
|
||||
}
|
||||
if url.Host != "127.0.0.1:443" {
|
||||
t.Error("Should have gotten back a host of dummy-host not %s", url.Host)
|
||||
type expectation struct {
|
||||
url string
|
||||
error bool
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
services []*v1.Service
|
||||
endpoints []*v1.Endpoints
|
||||
apiService *apiregistration.APIService
|
||||
|
||||
clusterMode expectation
|
||||
endpointMode expectation
|
||||
}{
|
||||
{
|
||||
name: "cluster ip without ports",
|
||||
services: []*v1.Service{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "one", Name: "alfa"},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeClusterIP,
|
||||
ClusterIP: "hit",
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoints: endpoints, // TODO: do we have endpoints without ports?
|
||||
apiService: &apiregistration.APIService{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "v1."},
|
||||
Spec: apiregistration.APIServiceSpec{
|
||||
Service: &apiregistration.ServiceReference{
|
||||
Namespace: "one",
|
||||
Name: "alfa",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
clusterMode: expectation{url: "https://hit"}, // TODO: this should be an error as well
|
||||
endpointMode: expectation{error: true},
|
||||
},
|
||||
{
|
||||
name: "cluster ip",
|
||||
services: []*v1.Service{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "one", Name: "alfa"},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeClusterIP,
|
||||
ClusterIP: "hit",
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoints: endpoints,
|
||||
apiService: &apiregistration.APIService{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "v1."},
|
||||
Spec: apiregistration.APIServiceSpec{
|
||||
Service: &apiregistration.ServiceReference{
|
||||
Namespace: "one",
|
||||
Name: "alfa",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
clusterMode: expectation{url: "https://hit"},
|
||||
endpointMode: expectation{url: "https://127.0.0.1"},
|
||||
},
|
||||
{
|
||||
name: "cluster ip without endpoints",
|
||||
services: []*v1.Service{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "one", Name: "alfa"},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeClusterIP,
|
||||
ClusterIP: "hit",
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoints: nil,
|
||||
apiService: &apiregistration.APIService{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "v1."},
|
||||
Spec: apiregistration.APIServiceSpec{
|
||||
Service: &apiregistration.ServiceReference{
|
||||
Namespace: "one",
|
||||
Name: "alfa",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
clusterMode: expectation{url: "https://hit"},
|
||||
endpointMode: expectation{error: true},
|
||||
},
|
||||
{
|
||||
name: "loadbalancer",
|
||||
services: []*v1.Service{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "one", Name: "alfa"},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeLoadBalancer,
|
||||
ClusterIP: "lb",
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoints: nil,
|
||||
apiService: &apiregistration.APIService{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "v1."},
|
||||
Spec: apiregistration.APIServiceSpec{
|
||||
Service: &apiregistration.ServiceReference{
|
||||
Namespace: "one",
|
||||
Name: "alfa",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
clusterMode: expectation{url: "https://lb"},
|
||||
endpointMode: expectation{error: true},
|
||||
},
|
||||
{
|
||||
name: "node port",
|
||||
services: []*v1.Service{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "one", Name: "alfa"},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeNodePort,
|
||||
ClusterIP: "np",
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoints: nil,
|
||||
apiService: &apiregistration.APIService{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "v1."},
|
||||
Spec: apiregistration.APIServiceSpec{
|
||||
Service: &apiregistration.ServiceReference{
|
||||
Namespace: "one",
|
||||
Name: "alfa",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
clusterMode: expectation{url: "https://np"},
|
||||
endpointMode: expectation{error: true},
|
||||
},
|
||||
{
|
||||
name: "missing service",
|
||||
services: nil,
|
||||
endpoints: nil,
|
||||
apiService: &apiregistration.APIService{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "v1."},
|
||||
Spec: apiregistration.APIServiceSpec{
|
||||
Service: &apiregistration.ServiceReference{
|
||||
Namespace: "one",
|
||||
Name: "alfa",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
clusterMode: expectation{url: "https://alfa.one.svc"}, // defaulting to 443 due to https:// prefix
|
||||
endpointMode: expectation{error: true},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
serviceCache := cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
|
||||
serviceLister := v1listers.NewServiceLister(serviceCache)
|
||||
for i := range test.services {
|
||||
serviceCache.Add(test.services[i])
|
||||
}
|
||||
|
||||
endpointCache := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{})
|
||||
endpointLister := v1listers.NewEndpointsLister(endpointCache)
|
||||
for i := range test.endpoints {
|
||||
endpointCache.Add(test.endpoints[i])
|
||||
}
|
||||
|
||||
check := func(mode string, expected expectation, url *url.URL, err error) {
|
||||
switch {
|
||||
case err == nil && expected.error:
|
||||
t.Errorf("%s in %s mode expected error, got none", test.name, mode)
|
||||
case err != nil && expected.error:
|
||||
// ignore
|
||||
case err != nil:
|
||||
t.Errorf("%s in %s mode unexpected error: %v", test.name, mode, err)
|
||||
case url.String() != expected.url:
|
||||
t.Errorf("%s in %s mode expected url %q, got %q", test.name, mode, expected.url, url.String())
|
||||
}
|
||||
}
|
||||
|
||||
clusterURL, err := ResolveCluster(serviceLister, "one", "alfa")
|
||||
check("cluster", test.clusterMode, clusterURL, err)
|
||||
|
||||
endpointURL, err := ResolveEndpoint(serviceLister, endpointLister, "one", "alfa")
|
||||
check("endpoint", test.endpointMode, endpointURL, err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue