Skip to content

Commit 4c800ce

Browse files
Sebastien CoavouxSebastien Coavoux
authored andcommitted
Add: Test for new routes
1 parent 5f1e09c commit 4c800ce

File tree

4 files changed

+563
-3
lines changed

4 files changed

+563
-3
lines changed

src/test/backend/resource/common/types_test.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package common
22

33
import (
44
"testing"
5+
6+
"k8s.io/kubernetes/pkg/api/unversioned"
57
)
68

7-
func TestIsLabelSelectorMatching(t *testing.T) {
9+
func TestIsSelectorMatching(t *testing.T) {
810
cases := []struct {
911
serviceSelector, replicationControllerSelector map[string]string
1012
expected bool
@@ -25,10 +27,49 @@ func TestIsLabelSelectorMatching(t *testing.T) {
2527
map[string]string{"app": "my-name", "version": "1.1"}, true},
2628
}
2729
for _, c := range cases {
28-
actual := IsLabelSelectorMatching(c.serviceSelector, c.replicationControllerSelector)
30+
actual := IsSelectorMatching(c.serviceSelector, c.replicationControllerSelector)
2931
if actual != c.expected {
30-
t.Errorf("isLabelSelectorMatching(%+v, %+v) == %+v, expected %+v",
32+
t.Errorf("isSelectorMatching(%+v, %+v) == %+v, expected %+v",
3133
c.serviceSelector, c.replicationControllerSelector, actual, c.expected)
3234
}
3335
}
3436
}
37+
38+
func TestIsLabelSelectorMatching(t *testing.T) {
39+
cases := []struct {
40+
serviceSelector map[string]string
41+
daemonSetselector *unversioned.LabelSelector
42+
expected bool
43+
}{
44+
{nil, nil, false},
45+
{nil, &unversioned.LabelSelector{MatchLabels: map[string]string{}}, false},
46+
{map[string]string{}, nil, false},
47+
{map[string]string{}, &unversioned.LabelSelector{MatchLabels: map[string]string{}},
48+
false},
49+
{map[string]string{"app": "my-name"},
50+
&unversioned.LabelSelector{MatchLabels: map[string]string{}},
51+
false},
52+
{map[string]string{"app": "my-name", "version": "2"},
53+
&unversioned.LabelSelector{MatchLabels: map[string]string{"app": "my-name", "version": "1.1"}},
54+
false},
55+
{map[string]string{"app": "my-name", "env": "prod"},
56+
&unversioned.LabelSelector{MatchLabels: map[string]string{"app": "my-name", "version": "1.1"}},
57+
false},
58+
{map[string]string{"app": "my-name"},
59+
&unversioned.LabelSelector{MatchLabels: map[string]string{"app": "my-name"}},
60+
true},
61+
{map[string]string{"app": "my-name", "version": "1.1"},
62+
&unversioned.LabelSelector{MatchLabels: map[string]string{"app": "my-name", "version": "1.1"}},
63+
true},
64+
{map[string]string{"app": "my-name"},
65+
&unversioned.LabelSelector{MatchLabels: map[string]string{"app": "my-name", "version": "1.1"}},
66+
true},
67+
}
68+
for _, c := range cases {
69+
actual := IsLabelSelectorMatching(c.serviceSelector, c.daemonSetselector)
70+
if actual != c.expected {
71+
t.Errorf("isLabelSelectorMatching(%+v, %+v) == %+v, expected %+v",
72+
c.serviceSelector, c.daemonSetselector, actual, c.expected)
73+
}
74+
}
75+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Copyright 2015 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package daemonset
16+
17+
import (
18+
"reflect"
19+
"testing"
20+
21+
"github.com/kubernetes/dashboard/resource/common"
22+
"k8s.io/kubernetes/pkg/api"
23+
"k8s.io/kubernetes/pkg/api/unversioned"
24+
"k8s.io/kubernetes/pkg/apis/extensions"
25+
"k8s.io/kubernetes/pkg/client/unversioned/testclient"
26+
"k8s.io/kubernetes/pkg/labels"
27+
)
28+
29+
func TestGetDaemonSetPodInfo(t *testing.T) {
30+
cases := []struct {
31+
daemonset *extensions.DaemonSet
32+
pods []api.Pod
33+
expected common.PodInfo
34+
}{
35+
{
36+
&extensions.DaemonSet{
37+
Status: extensions.DaemonSetStatus{},
38+
Spec: extensions.DaemonSetSpec{},
39+
},
40+
[]api.Pod{
41+
{
42+
Status: api.PodStatus{
43+
Phase: api.PodRunning,
44+
},
45+
},
46+
},
47+
common.PodInfo{
48+
Running: 1,
49+
Pending: 0,
50+
Failed: 0,
51+
},
52+
},
53+
}
54+
55+
for _, c := range cases {
56+
actual := getDaemonSetPodInfo(c.daemonset, c.pods)
57+
if !reflect.DeepEqual(actual, c.expected) {
58+
t.Errorf("getReplicaSetPodInfo(%#v, %#v) == \n%#v\nexpected \n%#v\n",
59+
c.daemonset, c.pods, actual, c.expected)
60+
}
61+
}
62+
}
63+
64+
func TestGetServicesForDeletionforDS(t *testing.T) {
65+
cases := []struct {
66+
labelSelector labels.Selector
67+
DaemonSetList *extensions.DaemonSetList
68+
expected *api.ServiceList
69+
expectedActions []string
70+
}{
71+
{
72+
labels.SelectorFromSet(map[string]string{"app": "test"}),
73+
&extensions.DaemonSetList{
74+
Items: []extensions.DaemonSet{
75+
{Spec: extensions.DaemonSetSpec{
76+
Selector: &unversioned.LabelSelector{
77+
MatchLabels: map[string]string{"app": "test"},
78+
},
79+
},
80+
},
81+
},
82+
},
83+
&api.ServiceList{
84+
Items: []api.Service{
85+
{Spec: api.ServiceSpec{Selector: map[string]string{"app": "test"}}},
86+
},
87+
},
88+
[]string{"list", "list"},
89+
},
90+
{
91+
labels.SelectorFromSet(map[string]string{"app": "test"}),
92+
&extensions.DaemonSetList{
93+
Items: []extensions.DaemonSet{
94+
{Spec: extensions.DaemonSetSpec{
95+
Selector: &unversioned.LabelSelector{
96+
MatchLabels: map[string]string{"app": "test"},
97+
},
98+
},
99+
},
100+
{Spec: extensions.DaemonSetSpec{
101+
Selector: &unversioned.LabelSelector{
102+
MatchLabels: map[string]string{"app": "test"},
103+
},
104+
},
105+
},
106+
},
107+
},
108+
&api.ServiceList{
109+
Items: []api.Service{
110+
{Spec: api.ServiceSpec{Selector: map[string]string{"app": "test"}}},
111+
},
112+
},
113+
[]string{"list"},
114+
},
115+
{
116+
labels.SelectorFromSet(map[string]string{"app": "test"}),
117+
&extensions.DaemonSetList{},
118+
&api.ServiceList{
119+
Items: []api.Service{
120+
{Spec: api.ServiceSpec{Selector: map[string]string{"app": "test"}}},
121+
},
122+
},
123+
[]string{"list"},
124+
},
125+
}
126+
127+
for _, c := range cases {
128+
fakeClient := testclient.NewSimpleFake(c.DaemonSetList, c.expected)
129+
130+
getServicesForDSDeletion(fakeClient, c.labelSelector, "mock")
131+
132+
actions := fakeClient.Actions()
133+
if len(actions) != len(c.expectedActions) {
134+
t.Errorf("Unexpected actions: %v, expected %d actions got %d", actions,
135+
len(c.expectedActions), len(actions))
136+
continue
137+
}
138+
139+
for i, verb := range c.expectedActions {
140+
if actions[i].GetVerb() != verb {
141+
t.Errorf("Unexpected action: %+v, expected %s",
142+
actions[i], verb)
143+
}
144+
}
145+
}
146+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2015 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package daemonset
16+
17+
import (
18+
"testing"
19+
20+
"k8s.io/kubernetes/pkg/api"
21+
"k8s.io/kubernetes/pkg/api/unversioned"
22+
"k8s.io/kubernetes/pkg/apis/extensions"
23+
"k8s.io/kubernetes/pkg/client/unversioned/testclient"
24+
)
25+
26+
func TestDeleteDaemonSetServices(t *testing.T) {
27+
cases := []struct {
28+
namespace, name string
29+
DaemonSet *extensions.DaemonSet
30+
DaemonSetList *extensions.DaemonSetList
31+
serviceList *api.ServiceList
32+
expectedActions []string
33+
}{
34+
{
35+
"test-namespace", "test-name",
36+
&extensions.DaemonSet{
37+
Spec: extensions.DaemonSetSpec{
38+
Selector: &unversioned.LabelSelector{
39+
MatchLabels: map[string]string{"app": "test"},
40+
},
41+
},
42+
},
43+
&extensions.DaemonSetList{
44+
Items: []extensions.DaemonSet{
45+
{Spec: extensions.DaemonSetSpec{
46+
Selector: &unversioned.LabelSelector{
47+
MatchLabels: map[string]string{"app": "test"},
48+
},
49+
}},
50+
},
51+
},
52+
&api.ServiceList{
53+
Items: []api.Service{
54+
{Spec: api.ServiceSpec{Selector: map[string]string{"app": "test"}}},
55+
},
56+
},
57+
[]string{"get", "list", "list", "delete"},
58+
},
59+
{
60+
"test-namespace", "test-name",
61+
&extensions.DaemonSet{
62+
Spec: extensions.DaemonSetSpec{
63+
Selector: &unversioned.LabelSelector{
64+
MatchLabels: map[string]string{"app": "test"},
65+
},
66+
}},
67+
&extensions.DaemonSetList{
68+
Items: []extensions.DaemonSet{
69+
{Spec: extensions.DaemonSetSpec{
70+
Selector: &unversioned.LabelSelector{
71+
MatchLabels: map[string]string{"app": "test"},
72+
},
73+
}},
74+
{Spec: extensions.DaemonSetSpec{
75+
Selector: &unversioned.LabelSelector{
76+
MatchLabels: map[string]string{"app": "test"},
77+
},
78+
}},
79+
},
80+
},
81+
&api.ServiceList{
82+
Items: []api.Service{
83+
{Spec: api.ServiceSpec{Selector: map[string]string{"app": "test"}}},
84+
},
85+
},
86+
[]string{"get", "list"},
87+
},
88+
{
89+
"test-namespace", "test-name",
90+
&extensions.DaemonSet{
91+
Spec: extensions.DaemonSetSpec{
92+
Selector: &unversioned.LabelSelector{
93+
MatchLabels: map[string]string{"app": "test"},
94+
},
95+
}},
96+
&extensions.DaemonSetList{
97+
Items: []extensions.DaemonSet{
98+
{Spec: extensions.DaemonSetSpec{
99+
Selector: &unversioned.LabelSelector{
100+
MatchLabels: map[string]string{"app": "test"},
101+
},
102+
}},
103+
},
104+
},
105+
&api.ServiceList{},
106+
[]string{"get", "list", "list"},
107+
},
108+
}
109+
110+
for _, c := range cases {
111+
fakeClient := testclient.NewSimpleFake(c.DaemonSet,
112+
c.DaemonSetList, c.serviceList)
113+
114+
DeleteDaemonSetServices(fakeClient, c.namespace, c.name)
115+
116+
actions := fakeClient.Actions()
117+
if len(actions) != len(c.expectedActions) {
118+
t.Errorf("Unexpected actions: %v, expected %d actions got %d", actions,
119+
len(c.expectedActions), len(actions))
120+
continue
121+
}
122+
123+
for i, verb := range c.expectedActions {
124+
if actions[i].GetVerb() != verb {
125+
t.Errorf("Unexpected action: %+v, expected %s",
126+
actions[i], verb)
127+
}
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)