mirror of https://github.com/k3s-io/k3s
112 lines
3.0 KiB
Go
112 lines
3.0 KiB
Go
/*
|
|
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 util
|
|
|
|
import (
|
|
"testing"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
api "k8s.io/kubernetes/pkg/apis/core"
|
|
)
|
|
|
|
func TestShouldSkipService(t *testing.T) {
|
|
testCases := []struct {
|
|
service *api.Service
|
|
svcName types.NamespacedName
|
|
shouldSkip bool
|
|
}{
|
|
{
|
|
// Cluster IP is None
|
|
service: &api.Service{
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"},
|
|
Spec: api.ServiceSpec{
|
|
ClusterIP: api.ClusterIPNone,
|
|
},
|
|
},
|
|
svcName: types.NamespacedName{Namespace: "foo", Name: "bar"},
|
|
shouldSkip: true,
|
|
},
|
|
{
|
|
// Cluster IP is empty
|
|
service: &api.Service{
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"},
|
|
Spec: api.ServiceSpec{
|
|
ClusterIP: "",
|
|
},
|
|
},
|
|
svcName: types.NamespacedName{Namespace: "foo", Name: "bar"},
|
|
shouldSkip: true,
|
|
},
|
|
{
|
|
// ExternalName type service
|
|
service: &api.Service{
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"},
|
|
Spec: api.ServiceSpec{
|
|
ClusterIP: "1.2.3.4",
|
|
Type: api.ServiceTypeExternalName,
|
|
},
|
|
},
|
|
svcName: types.NamespacedName{Namespace: "foo", Name: "bar"},
|
|
shouldSkip: true,
|
|
},
|
|
{
|
|
// ClusterIP type service with ClusterIP set
|
|
service: &api.Service{
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"},
|
|
Spec: api.ServiceSpec{
|
|
ClusterIP: "1.2.3.4",
|
|
Type: api.ServiceTypeClusterIP,
|
|
},
|
|
},
|
|
svcName: types.NamespacedName{Namespace: "foo", Name: "bar"},
|
|
shouldSkip: false,
|
|
},
|
|
{
|
|
// NodePort type service with ClusterIP set
|
|
service: &api.Service{
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"},
|
|
Spec: api.ServiceSpec{
|
|
ClusterIP: "1.2.3.4",
|
|
Type: api.ServiceTypeNodePort,
|
|
},
|
|
},
|
|
svcName: types.NamespacedName{Namespace: "foo", Name: "bar"},
|
|
shouldSkip: false,
|
|
},
|
|
{
|
|
// LoadBalancer type service with ClusterIP set
|
|
service: &api.Service{
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"},
|
|
Spec: api.ServiceSpec{
|
|
ClusterIP: "1.2.3.4",
|
|
Type: api.ServiceTypeLoadBalancer,
|
|
},
|
|
},
|
|
svcName: types.NamespacedName{Namespace: "foo", Name: "bar"},
|
|
shouldSkip: false,
|
|
},
|
|
}
|
|
|
|
for i := range testCases {
|
|
skip := ShouldSkipService(testCases[i].svcName, testCases[i].service)
|
|
if skip != testCases[i].shouldSkip {
|
|
t.Errorf("case %d: expect %v, got %v", i, testCases[i].shouldSkip, skip)
|
|
}
|
|
}
|
|
}
|