list_test.go 4.8 KB
Newer Older
1
// Copyright 2017 The Kubernetes Authors.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
// 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 service

import (
18 19 20
	"reflect"
	"testing"

21 22 23
	"github.com/kubernetes/dashboard/src/app/backend/resource/endpoint"
	"github.com/kubernetes/dashboard/src/app/backend/resource/pod"

24
	"github.com/kubernetes/dashboard/src/app/backend/api"
25 26
	"github.com/kubernetes/dashboard/src/app/backend/resource/common"
	"github.com/kubernetes/dashboard/src/app/backend/resource/dataselect"
27
	v1 "k8s.io/api/core/v1"
S
Sebastian Florek 已提交
28
	metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
	"k8s.io/client-go/kubernetes/fake"
S
Sebastian Florek 已提交
30
)
31 32

func TestGetServiceList(t *testing.T) {
33
	cases := []struct {
34
		serviceList     *v1.ServiceList
35 36 37 38
		expectedActions []string
		expected        *ServiceList
	}{
		{
39 40
			serviceList: &v1.ServiceList{
				Items: []v1.Service{
S
Sebastian Florek 已提交
41
					{ObjectMeta: metaV1.ObjectMeta{
42 43 44 45 46 47
						Name: "svc-1", Namespace: "ns-1",
						Labels: map[string]string{},
					}},
				}},
			expectedActions: []string{"list"},
			expected: &ServiceList{
48
				ListMeta: api.ListMeta{TotalItems: 1},
49 50
				Services: []Service{
					{
51
						ObjectMeta: api.ObjectMeta{
52 53 54 55
							Name:      "svc-1",
							Namespace: "ns-1",
							Labels:    map[string]string{},
						},
56 57 58
						TypeMeta:          api.TypeMeta{Kind: api.ResourceKindService},
						InternalEndpoint:  common.Endpoint{Host: "svc-1.ns-1"},
						ExternalEndpoints: []common.Endpoint{},
59 60
					},
				},
61
				Errors: []error{},
62 63 64 65 66 67 68 69
			},
		},
	}

	for _, c := range cases {
		fakeClient := fake.NewSimpleClientset(c.serviceList)
		actual, _ := GetServiceList(fakeClient, common.NewNamespaceQuery(nil), dataselect.NoDataSelect)
		actions := fakeClient.Actions()
70

71 72 73 74 75 76 77 78 79 80 81 82
		if len(actions) != len(c.expectedActions) {
			t.Errorf("Unexpected actions: %v, expected %d actions got %d", actions,
				len(c.expectedActions), len(actions))
			continue
		}

		for i, verb := range c.expectedActions {
			if actions[i].GetVerb() != verb {
				t.Errorf("Unexpected action: %+v, expected %s",
					actions[i], verb)
			}
		}
83

84 85 86 87
		if !reflect.DeepEqual(actual, c.expected) {
			t.Errorf("GetServiceList(client) == got\n%#v, expected\n %#v", actual, c.expected)
		}
	}
88
}
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

func TestToServiceDetail(t *testing.T) {
	cases := []struct {
		service      *v1.Service
		eventList    common.EventList
		podList      pod.PodList
		endpointList endpoint.EndpointList
		expected     ServiceDetail
	}{
		{
			service:      &v1.Service{},
			eventList:    common.EventList{},
			podList:      pod.PodList{},
			endpointList: endpoint.EndpointList{},
			expected: ServiceDetail{
				Service: Service{
					TypeMeta:          api.TypeMeta{Kind: api.ResourceKindService},
					ExternalEndpoints: []common.Endpoint{},
				},
			},
		}, {
			service: &v1.Service{
				ObjectMeta: metaV1.ObjectMeta{
					Name: "test-service", Namespace: "test-namespace",
				}},
			expected: ServiceDetail{
				Service: Service{
					ObjectMeta: api.ObjectMeta{
						Name:      "test-service",
						Namespace: "test-namespace",
					},
					TypeMeta:          api.TypeMeta{Kind: api.ResourceKindService},
					InternalEndpoint:  common.Endpoint{Host: "test-service.test-namespace"},
					ExternalEndpoints: []common.Endpoint{},
				},
			},
		},
	}

	for _, c := range cases {
		actual := toServiceDetail(c.service, c.endpointList, nil)

		if !reflect.DeepEqual(actual, c.expected) {
			t.Errorf("ToServiceDetail(%#v) == \ngot %#v, \nexpected %#v", c.service, actual,
				c.expected)
		}
	}
}

func TestToService(t *testing.T) {
	cases := []struct {
		service  *v1.Service
		expected Service
	}{
		{
			service: &v1.Service{}, expected: Service{
				TypeMeta:          api.TypeMeta{Kind: api.ResourceKindService},
				ExternalEndpoints: []common.Endpoint{},
			},
		}, {
			service: &v1.Service{
				ObjectMeta: metaV1.ObjectMeta{
					Name: "test-service", Namespace: "test-namespace",
				}},
			expected: Service{
				ObjectMeta: api.ObjectMeta{
					Name:      "test-service",
					Namespace: "test-namespace",
				},
				TypeMeta:          api.TypeMeta{Kind: api.ResourceKindService},
				InternalEndpoint:  common.Endpoint{Host: "test-service.test-namespace"},
				ExternalEndpoints: []common.Endpoint{},
			},
		},
	}

	for _, c := range cases {
		actual := toService(c.service)

		if !reflect.DeepEqual(actual, c.expected) {
			t.Errorf("ToService(%#v) == \ngot %#v, \nexpected %#v", c.service, actual,
				c.expected)
		}
	}
}