replicationcontrollerlist_test.go 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 main

import (
B
bryk 已提交
18
	"reflect"
19
	"testing"
M
Marcin Maciaszczyk 已提交
20 21

	"k8s.io/kubernetes/pkg/api"
22 23
)

24
func TestIsLabelSelectorMatching(t *testing.T) {
B
bryk 已提交
25
	cases := []struct {
26
		serviceSelector, replicationControllerSelector map[string]string
27
		expected                                       bool
B
bryk 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
	}{
		{nil, nil, false},
		{nil, map[string]string{}, false},
		{map[string]string{}, nil, false},
		{map[string]string{}, map[string]string{}, false},
		{map[string]string{"app": "my-name"}, map[string]string{}, false},
		{map[string]string{"app": "my-name", "version": "2"},
			map[string]string{"app": "my-name", "version": "1.1"}, false},
		{map[string]string{"app": "my-name", "env": "prod"},
			map[string]string{"app": "my-name", "version": "1.1"}, false},
		{map[string]string{"app": "my-name"}, map[string]string{"app": "my-name"}, true},
		{map[string]string{"app": "my-name", "version": "1.1"},
			map[string]string{"app": "my-name", "version": "1.1"}, true},
		{map[string]string{"app": "my-name"},
			map[string]string{"app": "my-name", "version": "1.1"}, true},
	}
	for _, c := range cases {
45
		actual := isLabelSelectorMatching(c.serviceSelector, c.replicationControllerSelector)
B
bryk 已提交
46
		if actual != c.expected {
47
			t.Errorf("isLabelSelectorMatching(%+v, %+v) == %+v, expected %+v",
48
				c.serviceSelector, c.replicationControllerSelector, actual, c.expected)
B
bryk 已提交
49 50 51 52 53 54
		}
	}
}

func TestGetMatchingServices(t *testing.T) {
	cases := []struct {
55
		services              []api.Service
56
		replicationController *api.ReplicationController
57
		expected              []api.Service
B
bryk 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	}{
		{nil, nil, nil},
		{
			[]api.Service{{Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name"}}}},
			&api.ReplicationController{
				Spec: api.ReplicationControllerSpec{Selector: map[string]string{"app": "my-name"}}},
			[]api.Service{{Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name"}}}},
		},
		{
			[]api.Service{
				{Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name"}}},
				{Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name", "ver": "2"}}},
			},
			&api.ReplicationController{
				Spec: api.ReplicationControllerSpec{Selector: map[string]string{"app": "my-name"}}},
			[]api.Service{{Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name"}}}},
		},
	}
	for _, c := range cases {
77
		actual := getMatchingServices(c.services, c.replicationController)
B
bryk 已提交
78 79
		if !reflect.DeepEqual(actual, c.expected) {
			t.Errorf("getMatchingServices(%+v, %+v) == %+v, expected %+v",
80
				c.services, c.replicationController, actual, c.expected)
B
bryk 已提交
81 82 83 84
		}
	}
}

85
func TestGetReplicationControllerList(t *testing.T) {
86 87 88 89
	getPodsErrorFnMock := func(pods []api.Pod) ([]PodEvent, error) {
		return []PodEvent{}, nil
	}

B
bryk 已提交
90
	cases := []struct {
91
		replicationControllers []api.ReplicationController
92 93 94
		services               []api.Service
		pods                   []api.Pod
		expected               *ReplicationControllerList
B
bryk 已提交
95
	}{
96
		{nil, nil, nil, &ReplicationControllerList{ReplicationControllers: []ReplicationController{}}},
B
bryk 已提交
97 98 99
		{
			[]api.ReplicationController{
				{
100 101 102 103
					ObjectMeta: api.ObjectMeta{
						Name:      "my-app-1",
						Namespace: "namespace-1",
					},
B
bryk 已提交
104 105 106 107 108 109 110 111
					Spec: api.ReplicationControllerSpec{
						Selector: map[string]string{"app": "my-name-1"},
						Template: &api.PodTemplateSpec{
							Spec: api.PodSpec{Containers: []api.Container{{Image: "my-container-image-1"}}},
						},
					},
				},
				{
112 113 114 115
					ObjectMeta: api.ObjectMeta{
						Name:      "my-app-2",
						Namespace: "namespace-2",
					},
B
bryk 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
					Spec: api.ReplicationControllerSpec{
						Selector: map[string]string{"app": "my-name-2", "ver": "2"},
						Template: &api.PodTemplateSpec{
							Spec: api.PodSpec{Containers: []api.Container{{Image: "my-container-image-2"}}},
						},
					},
				},
			},
			[]api.Service{
				{
					Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name-1"}},
					ObjectMeta: api.ObjectMeta{
						Name:      "my-app-1",
						Namespace: "namespace-1",
					},
				},
				{
					Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name-2", "ver": "2"}},
					ObjectMeta: api.ObjectMeta{
						Name:      "my-app-2",
						Namespace: "namespace-2",
					},
				},
			},
140
			[]api.Pod{
141
				{
142 143 144 145 146 147 148 149
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-1",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodFailed,
					},
				},
150
				{
151 152 153 154 155 156 157 158
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-1",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodFailed,
					},
				},
159
				{
160 161 162 163 164 165 166 167
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-1",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodPending,
					},
				},
168
				{
169 170 171 172 173 174 175 176
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-2",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodPending,
					},
				},
177
				{
178 179 180 181 182 183 184 185
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-1",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodRunning,
					},
				},
186
				{
187 188 189 190 191 192 193 194
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-1",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodSucceeded,
					},
				},
195
				{
196 197 198 199 200 201 202 203 204
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-1",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodUnknown,
					},
				},
			},
205 206
			&ReplicationControllerList{
				ReplicationControllers: []ReplicationController{
B
bryk 已提交
207
					{
208 209
						Name:              "my-app-1",
						Namespace:         "namespace-1",
B
bryk 已提交
210
						ContainerImages:   []string{"my-container-image-1"},
211
						InternalEndpoints: []Endpoint{{Host: "my-app-1.namespace-1"}},
212
						Pods: ReplicationControllerPodInfo{
213 214 215 216
							Failed:   2,
							Pending:  1,
							Running:  1,
							Warnings: []PodEvent{},
217
						},
B
bryk 已提交
218
					}, {
219 220
						Name:              "my-app-2",
						Namespace:         "namespace-2",
B
bryk 已提交
221
						ContainerImages:   []string{"my-container-image-2"},
222
						InternalEndpoints: []Endpoint{{Host: "my-app-2.namespace-2"}},
223 224 225
						Pods: ReplicationControllerPodInfo{
							Warnings: []PodEvent{},
						},
B
bryk 已提交
226 227 228 229 230 231
					},
				},
			},
		},
	}
	for _, c := range cases {
232 233
		actual, _ := getReplicationControllerList(c.replicationControllers, c.services, c.pods,
			getPodsErrorFnMock)
B
bryk 已提交
234
		if !reflect.DeepEqual(actual, c.expected) {
235 236
			t.Errorf("getReplicationControllerList(%#v, %#v) == \n%#v\nexpected \n%#v\n",
				c.replicationControllers, c.services, actual, c.expected)
B
bryk 已提交
237 238
		}
	}
239
}