Skip to content

Commit f242115

Browse files
authored
Merge pull request kubernetes#125714 from googs1025/add_defaultSelector_ut
chore: add DefaultSelector method ut
2 parents 4c44efe + 8ce056d commit f242115

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

pkg/scheduler/framework/plugins/helper/spread_test.go

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ import (
2222
"time"
2323

2424
"github.com/google/go-cmp/cmp"
25+
appsv1 "k8s.io/api/apps/v1"
2526
v1 "k8s.io/api/core/v1"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/apimachinery/pkg/labels"
2729
"k8s.io/client-go/informers"
2830
"k8s.io/client-go/kubernetes/fake"
2931
st "k8s.io/kubernetes/pkg/scheduler/testing"
@@ -99,3 +101,169 @@ func TestGetPodServices(t *testing.T) {
99101
})
100102
}
101103
}
104+
105+
func TestDefaultSelector(t *testing.T) {
106+
const (
107+
namespace = "test"
108+
podName = "test-pod"
109+
serviceName = "test-service"
110+
replicaSetName = "test-replicaset"
111+
replicationControllerName = "test-replicationcontroller"
112+
statefulSetName = "test-statefulset"
113+
114+
podLabelKey = "podLabelKey"
115+
podLabelVal = "podLabelVal"
116+
117+
replicaSetLabelKey = "replicaSetLabelKey"
118+
replicaSetLabelVal = "replicaSetLabelVal"
119+
120+
replicationLabelKey = "replicationLabelKey"
121+
replicationLabelVal = "replicationLabelVal"
122+
123+
statefulSetLabelKey = "statefulSetLabelKey"
124+
statefulSetLabelVal = "statefulSetLabelVal"
125+
)
126+
127+
fakeInformerFactory := informers.NewSharedInformerFactory(&fake.Clientset{}, 0*time.Second)
128+
129+
// Create fake service
130+
addFakeService := func() error {
131+
// Create fake service
132+
service := &v1.Service{
133+
ObjectMeta: metav1.ObjectMeta{
134+
Name: serviceName,
135+
Namespace: namespace,
136+
},
137+
Spec: v1.ServiceSpec{
138+
Selector: map[string]string{
139+
podLabelKey: podLabelVal,
140+
},
141+
},
142+
}
143+
return fakeInformerFactory.Core().V1().Services().Informer().GetStore().Add(service)
144+
}
145+
146+
// Create fake ReplicaSet
147+
addFakeReplicaSet := func() error {
148+
replicaSet := &appsv1.ReplicaSet{
149+
ObjectMeta: metav1.ObjectMeta{
150+
Name: replicaSetName,
151+
Namespace: namespace,
152+
},
153+
Spec: appsv1.ReplicaSetSpec{
154+
Selector: &metav1.LabelSelector{
155+
MatchLabels: map[string]string{
156+
replicaSetLabelKey: replicaSetLabelVal,
157+
},
158+
},
159+
},
160+
}
161+
return fakeInformerFactory.Apps().V1().ReplicaSets().Informer().GetStore().Add(replicaSet)
162+
}
163+
164+
// Create fake ReplicationController
165+
addFakeReplicationController := func() error {
166+
replicationController := &v1.ReplicationController{
167+
ObjectMeta: metav1.ObjectMeta{
168+
Name: replicationControllerName,
169+
Namespace: namespace,
170+
},
171+
Spec: v1.ReplicationControllerSpec{
172+
Selector: map[string]string{
173+
replicationLabelKey: replicationLabelVal,
174+
},
175+
},
176+
}
177+
return fakeInformerFactory.Core().V1().ReplicationControllers().Informer().GetStore().Add(replicationController)
178+
}
179+
180+
// Create fake StatefulSet
181+
addFakeStatefulSet := func() error {
182+
statefulSet := &appsv1.StatefulSet{
183+
ObjectMeta: metav1.ObjectMeta{
184+
Name: statefulSetName,
185+
Namespace: namespace,
186+
},
187+
Spec: appsv1.StatefulSetSpec{
188+
Selector: &metav1.LabelSelector{
189+
MatchLabels: map[string]string{
190+
statefulSetLabelKey: statefulSetLabelVal,
191+
},
192+
},
193+
},
194+
}
195+
return fakeInformerFactory.Apps().V1().StatefulSets().Informer().GetStore().Add(statefulSet)
196+
}
197+
198+
tests := []struct {
199+
name string
200+
pod *v1.Pod
201+
expect labels.Set
202+
addFakeResourceList []func() error
203+
}{
204+
{
205+
name: "DefaultSelector for default case",
206+
pod: st.MakePod().Name(podName).
207+
Namespace(namespace).Label(podLabelKey, podLabelVal).
208+
Obj(),
209+
expect: labels.Set{},
210+
addFakeResourceList: nil,
211+
},
212+
{
213+
name: "DefaultSelector for no OwnerReference pod case",
214+
pod: st.MakePod().Name(podName).
215+
Namespace(namespace).Label(podLabelKey, podLabelVal).
216+
Obj(),
217+
expect: labels.Set{podLabelKey: podLabelVal},
218+
addFakeResourceList: []func() error{addFakeService},
219+
},
220+
{
221+
name: "DefaultSelector for ReplicaSet OwnerReference pod case",
222+
pod: st.MakePod().Name(podName).
223+
Namespace(namespace).Label(podLabelKey, podLabelVal).
224+
OwnerReference(replicaSetName, appsv1.SchemeGroupVersion.WithKind("ReplicaSet")).Obj(),
225+
expect: labels.Set{podLabelKey: podLabelVal, replicaSetLabelKey: replicaSetLabelVal},
226+
addFakeResourceList: []func() error{addFakeService, addFakeReplicaSet},
227+
},
228+
{
229+
name: "DefaultSelector for ReplicationController OwnerReference pod case",
230+
pod: st.MakePod().Name(podName).
231+
Namespace(namespace).Label(podLabelKey, podLabelVal).
232+
OwnerReference(replicationControllerName, v1.SchemeGroupVersion.WithKind("ReplicationController")).Obj(),
233+
expect: labels.Set{podLabelKey: podLabelVal, replicationLabelKey: replicationLabelVal},
234+
addFakeResourceList: []func() error{addFakeService, addFakeReplicationController},
235+
},
236+
{
237+
name: "DefaultSelector for StatefulSet OwnerReference pod case",
238+
pod: st.MakePod().Name(podName).
239+
Namespace(namespace).Label(podLabelKey, podLabelVal).
240+
OwnerReference(statefulSetName, appsv1.SchemeGroupVersion.WithKind("StatefulSet")).Obj(),
241+
expect: labels.Set{podLabelKey: podLabelVal, statefulSetLabelKey: statefulSetLabelVal},
242+
addFakeResourceList: []func() error{addFakeService, addFakeStatefulSet},
243+
},
244+
}
245+
for _, test := range tests {
246+
t.Run(test.name, func(t *testing.T) {
247+
// Add fake resources if needed.
248+
if test.addFakeResourceList != nil {
249+
for _, addFakeResource := range test.addFakeResourceList {
250+
err := addFakeResource()
251+
if err != nil {
252+
t.Fatalf("failed to add fake resource: %v", err)
253+
}
254+
}
255+
}
256+
257+
get := DefaultSelector(test.pod,
258+
fakeInformerFactory.Core().V1().Services().Lister(),
259+
fakeInformerFactory.Core().V1().ReplicationControllers().Lister(),
260+
fakeInformerFactory.Apps().V1().ReplicaSets().Lister(),
261+
fakeInformerFactory.Apps().V1().StatefulSets().Lister())
262+
diff := cmp.Diff(test.expect.String(), get.String())
263+
if diff != "" {
264+
t.Errorf("Unexpected services (-want, +got):\n%s", diff)
265+
}
266+
267+
})
268+
}
269+
}

0 commit comments

Comments
 (0)