Skip to content

Commit cda73eb

Browse files
MrHohnjhorwit2
andcommitted
Fixes service controller unit test for patching
Co-authored-by: Josh Horwitz <[email protected]>
1 parent bff5f08 commit cda73eb

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

pkg/controller/service/service_controller_test.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"strings"
2424
"testing"
2525

26-
"k8s.io/api/core/v1"
26+
v1 "k8s.io/api/core/v1"
2727
apierrors "k8s.io/apimachinery/pkg/api/errors"
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/apimachinery/pkg/runtime"
@@ -40,14 +40,22 @@ import (
4040
const region = "us-central"
4141

4242
func newService(name string, uid types.UID, serviceType v1.ServiceType) *v1.Service {
43-
return &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "default", UID: uid, SelfLink: testapi.Default.SelfLink("services", name)}, Spec: v1.ServiceSpec{Type: serviceType}}
43+
return &v1.Service{
44+
ObjectMeta: metav1.ObjectMeta{
45+
Name: name,
46+
Namespace: "default",
47+
UID: uid,
48+
SelfLink: testapi.Default.SelfLink("services", name),
49+
},
50+
Spec: v1.ServiceSpec{
51+
Type: serviceType,
52+
},
53+
}
4454
}
4555

4656
//Wrap newService so that you don't have to call default arguments again and again.
4757
func defaultExternalService() *v1.Service {
48-
4958
return newService("external-balancer", types.UID("123"), v1.ServiceTypeLoadBalancer)
50-
5159
}
5260

5361
func alwaysReady() bool { return true }
@@ -151,7 +159,13 @@ func TestCreateExternalLoadBalancer(t *testing.T) {
151159

152160
for _, item := range table {
153161
controller, cloud, client := newController()
154-
err := controller.syncLoadBalancerIfNeeded("foo/bar", item.service)
162+
key := fmt.Sprintf("%s/%s", item.service.Namespace, item.service.Name)
163+
if _, err := client.CoreV1().Services(item.service.Namespace).Create(item.service); err != nil {
164+
t.Errorf("Failed to prepare service %s for testing: %v", key, err)
165+
continue
166+
}
167+
client.ClearActions()
168+
err := controller.syncLoadBalancerIfNeeded(key, item.service)
155169
if !item.expectErr && err != nil {
156170
t.Errorf("unexpected error: %v", err)
157171
} else if item.expectErr && err == nil {
@@ -185,12 +199,12 @@ func TestCreateExternalLoadBalancer(t *testing.T) {
185199
}
186200
actionFound := false
187201
for _, action := range actions {
188-
if action.GetVerb() == "update" && action.GetResource().Resource == "services" {
202+
if action.GetVerb() == "patch" && action.GetResource().Resource == "services" {
189203
actionFound = true
190204
}
191205
}
192206
if !actionFound {
193-
t.Errorf("expected updated service to be sent to client, got these actions instead: %v", actions)
207+
t.Errorf("expected patch service to be sent to client, got these actions instead: %v", actions)
194208
}
195209
}
196210
}
@@ -326,7 +340,7 @@ func TestGetNodeConditionPredicate(t *testing.T) {
326340
}
327341

328342
func TestProcessServiceUpdate(t *testing.T) {
329-
var controller *ServiceController
343+
controller, _, client := newController()
330344

331345
//A pair of old and new loadbalancer IP address
332346
oldLBIP := "192.168.1.1"
@@ -345,7 +359,6 @@ func TestProcessServiceUpdate(t *testing.T) {
345359
svc: defaultExternalService(),
346360
updateFn: func(svc *v1.Service) *v1.Service {
347361

348-
controller, _, _ = newController()
349362
controller.cache.getOrCreate("validKey")
350363
return svc
351364

@@ -404,6 +417,9 @@ func TestProcessServiceUpdate(t *testing.T) {
404417

405418
for _, tc := range testCases {
406419
newSvc := tc.updateFn(tc.svc)
420+
if _, err := client.CoreV1().Services(tc.svc.Namespace).Create(tc.svc); err != nil {
421+
t.Fatalf("Failed to prepare service %s for testing: %v", tc.key, err)
422+
}
407423
svcCache := controller.cache.getOrCreate(tc.key)
408424
obtErr := controller.processServiceUpdate(svcCache, newSvc, tc.key)
409425
if err := tc.expectedFn(newSvc, obtErr); err != nil {

0 commit comments

Comments
 (0)