Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudjardine committed Mar 28, 2019
1 parent 3086480 commit c57deb5
Showing 1 changed file with 55 additions and 9 deletions.
64 changes: 55 additions & 9 deletions pkg/minikube/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pkg/errors"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/kubernetes/typed/core/v1/fake"
Expand All @@ -36,12 +37,14 @@ import (
)

type MockClientGetter struct {
servicesMap map[string]corev1.ServiceInterface
servicesMap map[string]corev1.ServiceInterface
endpointsMap map[string]corev1.EndpointsInterface
}

func (m *MockClientGetter) GetCoreClient() (corev1.CoreV1Interface, error) {
return &MockCoreClient{
servicesMap: m.servicesMap,
servicesMap: m.servicesMap,
endpointsMap: m.endpointsMap,
}, nil
}

Expand All @@ -51,7 +54,8 @@ func (m *MockClientGetter) GetClientset(timeout time.Duration) (*kubernetes.Clie

type MockCoreClient struct {
fake.FakeCoreV1
servicesMap map[string]corev1.ServiceInterface
servicesMap map[string]corev1.ServiceInterface
endpointsMap map[string]corev1.EndpointsInterface
}

var serviceNamespaces = map[string]corev1.ServiceInterface{
Expand All @@ -68,8 +72,18 @@ var defaultNamespaceServiceInterface = &MockServiceInterface{
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
{NodePort: int32(1111)},
{NodePort: int32(2222)},
{
NodePort: int32(1111),
TargetPort: intstr.IntOrString{
IntVal: int32(11111),
},
},
{
NodePort: int32(2222),
TargetPort: intstr.IntOrString{
IntVal: int32(22222),
},
},
},
},
},
Expand All @@ -86,8 +100,14 @@ var defaultNamespaceServiceInterface = &MockServiceInterface{
},
}

var endpointNamespaces = map[string]corev1.EndpointsInterface{
"default": defaultNamespaceEndpointInterface,
}

var defaultNamespaceEndpointInterface = &MockEndpointsInterface{}

func (m *MockCoreClient) Endpoints(namespace string) corev1.EndpointsInterface {
return &MockEndpointsInterface{}
return m.endpointsMap[namespace]
}

func (m *MockCoreClient) Services(namespace string) corev1.ServiceInterface {
Expand Down Expand Up @@ -124,6 +144,22 @@ var endpointMap = map[string]*v1.Endpoints{
},
},
},
"mock-dashboard": {
Subsets: []v1.EndpointSubset{
{
Ports: []v1.EndpointPort{
{
Name: "port1",
Port: int32(11111),
},
{
Name: "port2",
Port: int32(22222),
},
},
},
},
},
}

func (e MockEndpointsInterface) Get(name string, _ metav1.GetOptions) (*v1.Endpoints, error) {
Expand Down Expand Up @@ -195,7 +231,8 @@ func TestGetServiceListFromServicesByLabel(t *testing.T) {
func TestPrintURLsForService(t *testing.T) {
defaultTemplate := template.Must(template.New("svc-template").Parse("http://{{.IP}}:{{.Port}}"))
client := &MockCoreClient{
servicesMap: serviceNamespaces,
servicesMap: serviceNamespaces,
endpointsMap: endpointNamespaces,
}
var tests = []struct {
description string
Expand All @@ -219,6 +256,13 @@ func TestPrintURLsForService(t *testing.T) {
tmpl: template.Must(template.New("svc-arbitrary-template").Parse("{{.IP}}:{{.Port}}")),
expectedOutput: []string{"127.0.0.1:1111", "127.0.0.1:2222"},
},
{
description: "should get the name of all target ports with arbitrary format",
serviceName: "mock-dashboard",
namespace: "default",
tmpl: template.Must(template.New("svc-arbitrary-template").Parse("{{.Name}}={{.IP}}:{{.Port}}")),
expectedOutput: []string{"port1=127.0.0.1:1111", "port2=127.0.0.1:2222"},
},
{
description: "empty slice for no node ports",
serviceName: "mock-dashboard-no-ports",
Expand Down Expand Up @@ -361,7 +405,8 @@ func TestGetServiceURLs(t *testing.T) {
t.Parallel()

K8s = &MockClientGetter{
servicesMap: serviceNamespaces,
servicesMap: serviceNamespaces,
endpointsMap: endpointNamespaces,
}
urls, err := GetServiceURLs(test.api, test.namespace, defaultTemplate)
if err != nil && !test.err {
Expand Down Expand Up @@ -428,7 +473,8 @@ func TestGetServiceURLsForService(t *testing.T) {
t.Run(test.description, func(t *testing.T) {
t.Parallel()
K8s = &MockClientGetter{
servicesMap: serviceNamespaces,
servicesMap: serviceNamespaces,
endpointsMap: endpointNamespaces,
}
urls, err := GetServiceURLsForService(test.api, test.namespace, test.service, defaultTemplate)
if err != nil && !test.err {
Expand Down

0 comments on commit c57deb5

Please sign in to comment.