@@ -23,7 +23,7 @@ import (
23
23
"strings"
24
24
"testing"
25
25
26
- "k8s.io/api/core/v1"
26
+ v1 "k8s.io/api/core/v1"
27
27
apierrors "k8s.io/apimachinery/pkg/api/errors"
28
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
29
"k8s.io/apimachinery/pkg/runtime"
@@ -40,14 +40,22 @@ import (
40
40
const region = "us-central"
41
41
42
42
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
+ }
44
54
}
45
55
46
56
//Wrap newService so that you don't have to call default arguments again and again.
47
57
func defaultExternalService () * v1.Service {
48
-
49
58
return newService ("external-balancer" , types .UID ("123" ), v1 .ServiceTypeLoadBalancer )
50
-
51
59
}
52
60
53
61
func alwaysReady () bool { return true }
@@ -151,7 +159,13 @@ func TestCreateExternalLoadBalancer(t *testing.T) {
151
159
152
160
for _ , item := range table {
153
161
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 )
155
169
if ! item .expectErr && err != nil {
156
170
t .Errorf ("unexpected error: %v" , err )
157
171
} else if item .expectErr && err == nil {
@@ -185,12 +199,12 @@ func TestCreateExternalLoadBalancer(t *testing.T) {
185
199
}
186
200
actionFound := false
187
201
for _ , action := range actions {
188
- if action .GetVerb () == "update " && action .GetResource ().Resource == "services" {
202
+ if action .GetVerb () == "patch " && action .GetResource ().Resource == "services" {
189
203
actionFound = true
190
204
}
191
205
}
192
206
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 )
194
208
}
195
209
}
196
210
}
@@ -326,7 +340,7 @@ func TestGetNodeConditionPredicate(t *testing.T) {
326
340
}
327
341
328
342
func TestProcessServiceUpdate (t * testing.T ) {
329
- var controller * ServiceController
343
+ controller , _ , client := newController ()
330
344
331
345
//A pair of old and new loadbalancer IP address
332
346
oldLBIP := "192.168.1.1"
@@ -345,7 +359,6 @@ func TestProcessServiceUpdate(t *testing.T) {
345
359
svc : defaultExternalService (),
346
360
updateFn : func (svc * v1.Service ) * v1.Service {
347
361
348
- controller , _ , _ = newController ()
349
362
controller .cache .getOrCreate ("validKey" )
350
363
return svc
351
364
@@ -404,6 +417,9 @@ func TestProcessServiceUpdate(t *testing.T) {
404
417
405
418
for _ , tc := range testCases {
406
419
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
+ }
407
423
svcCache := controller .cache .getOrCreate (tc .key )
408
424
obtErr := controller .processServiceUpdate (svcCache , newSvc , tc .key )
409
425
if err := tc .expectedFn (newSvc , obtErr ); err != nil {
0 commit comments