Skip to content

Commit

Permalink
Move theCloud and related setup to e2e package and export.
Browse files Browse the repository at this point in the history
  • Loading branch information
briantkennedy committed Sep 20, 2024
1 parent 742d76d commit 6bd06ad
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 234 deletions.
18 changes: 9 additions & 9 deletions e2e/addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ func TestAddresses(t *testing.T) {

t.Logf("addr1 = %s", fmt.Sprint(addr1))

t.Cleanup(func() { theCloud.Addresses().Delete(context.Background(), addr1Key) })
t.Cleanup(func() { TheCloud.Addresses().Delete(context.Background(), addr1Key) })

// Insert
err := theCloud.Addresses().Insert(ctx, addr1Key, addr1)
err := TheCloud.Addresses().Insert(ctx, addr1Key, addr1)
if err != nil {
t.Fatalf("Addresses.Insert(addr1) = %v", err)
}

// Get
a, err := theCloud.Addresses().Get(ctx, addr1Key)
a, err := TheCloud.Addresses().Get(ctx, addr1Key)
if err != nil {
t.Fatalf("Addresses.Get(addr1) = %v", err)
}
Expand All @@ -60,7 +60,7 @@ func TestAddresses(t *testing.T) {
}

// List
al, err := theCloud.Addresses().List(ctx, regionName, nil)
al, err := TheCloud.Addresses().List(ctx, regionName, nil)
if err != nil {
t.Fatalf("Error listing Addresses: %v", err)
}
Expand All @@ -77,7 +77,7 @@ func TestAddresses(t *testing.T) {
}

// Delete
err = theCloud.Addresses().Delete(ctx, addr1Key)
err = TheCloud.Addresses().Delete(ctx, addr1Key)
if err != nil {
t.Fatalf("Addresses.Delete(addr1) = %v", err)
}
Expand All @@ -100,16 +100,16 @@ func TestGlobalAddresses(t *testing.T) {

t.Logf("addr1 = %s", fmt.Sprint(addr1))

t.Cleanup(func() { theCloud.GlobalAddresses().Delete(context.Background(), addr1Key) })
t.Cleanup(func() { TheCloud.GlobalAddresses().Delete(context.Background(), addr1Key) })

// Insert
err := theCloud.GlobalAddresses().Insert(ctx, addr1Key, addr1)
err := TheCloud.GlobalAddresses().Insert(ctx, addr1Key, addr1)
if err != nil {
t.Fatalf("GlobalAddresses.Insert(addr1) = %v", err)
}

// Get
a, err := theCloud.GlobalAddresses().Get(ctx, addr1Key)
a, err := TheCloud.GlobalAddresses().Get(ctx, addr1Key)
if err != nil {
t.Fatalf("GlobalAddresses.Get(addr1) = %v", err)
}
Expand All @@ -118,7 +118,7 @@ func TestGlobalAddresses(t *testing.T) {
}

// Delete
err = theCloud.GlobalAddresses().Delete(ctx, addr1Key)
err = TheCloud.GlobalAddresses().Delete(ctx, addr1Key)
if err != nil {
t.Fatalf("GlobalAddresses.Delete(addr1) = %v", err)
}
Expand Down
54 changes: 27 additions & 27 deletions e2e/backend_service_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func TestBackendServiceUpdate(t *testing.T) {
graphBuilder := rgraph.NewBuilder()
meshURL, meshKey := ensureMesh(ctx, t, "test-bs-mesh")
t.Cleanup(func() {
err := theCloud.Meshes().Delete(ctx, meshKey)
t.Logf("theCloud.Meshes().Delete(ctx, %s): %v", meshKey, err)
err := TheCloud.Meshes().Delete(ctx, meshKey)
t.Logf("TheCloud.Meshes().Delete(ctx, %s): %v", meshKey, err)
})

hc1ID, err := buildHealthCheck(graphBuilder, "hc1-test", 15)
Expand Down Expand Up @@ -176,12 +176,12 @@ func TestBackendServiceUpdate(t *testing.T) {
t.Fatalf("graphBuilder.Build() = %v, want nil", err)
}

result, err := plan.Do(ctx, theCloud, graph)
result, err := plan.Do(ctx, TheCloud, graph)
if err != nil {
t.Fatalf("plan.Do(_, _, _) = %v, want nil", err)
}

ex, err := exec.NewSerialExecutor(theCloud, result.Actions)
ex, err := exec.NewSerialExecutor(TheCloud, result.Actions)
if err != nil {
t.Fatalf("exec.NewSerialExecutor(_, _) err: %v", err)
return
Expand All @@ -191,30 +191,30 @@ func TestBackendServiceUpdate(t *testing.T) {
t.Errorf("ex.Run(_,_) = %v, want nil", err)
}
t.Cleanup(func() {
err := theCloud.TcpRoutes().Delete(ctx, tcpr.Key)
err := TheCloud.TcpRoutes().Delete(ctx, tcpr.Key)
if err != nil {
t.Logf("delete TCProute: %v", err)
}
err = theCloud.BackendServices().Delete(ctx, bs1ID.Key)
err = TheCloud.BackendServices().Delete(ctx, bs1ID.Key)
if err != nil {
t.Logf("delete backend service: %v", err)
}
err = theCloud.BackendServices().Delete(ctx, bs2ID.Key)
err = TheCloud.BackendServices().Delete(ctx, bs2ID.Key)
if err != nil {
t.Logf("delete backend service: %v", err)
}
err = theCloud.HealthChecks().Delete(ctx, hc1ID.Key)
t.Logf("theCloud.HealthChecks().Delete(ctx, %s): %v", hc1ID.Key, err)
err = theCloud.HealthChecks().Delete(ctx, hc2ID.Key)
t.Logf("theCloud.HealthChecks().Delete(ctx, %s): %v", hc2ID.Key, err)
err = TheCloud.HealthChecks().Delete(ctx, hc1ID.Key)
t.Logf("TheCloud.HealthChecks().Delete(ctx, %s): %v", hc1ID.Key, err)
err = TheCloud.HealthChecks().Delete(ctx, hc2ID.Key)
t.Logf("TheCloud.HealthChecks().Delete(ctx, %s): %v", hc2ID.Key, err)
})
rulesToBs := [][]string{{resourceSelfLink(bs1ID)}, {resourceSelfLink(bs2ID)}}
checkTCPRoute(t, ctx, theCloud, tcpr, rulesToBs)
checkTCPRoute(t, ctx, TheCloud, tcpr, rulesToBs)
compareLBScheme := cmp.Comparer(func(a, b *compute.BackendService) bool {
return cmp.Equal(a.LoadBalancingScheme, b.LoadBalancingScheme)
})
wantBS := &compute.BackendService{LoadBalancingScheme: "INTERNAL_SELF_MANAGED"}
checkBackendService(t, ctx, theCloud, bs1ID, wantBS, compareLBScheme)
checkBackendService(t, ctx, TheCloud, bs1ID, wantBS, compareLBScheme)

// update backend service
bs1ID, err = buildBackendServiceWithLBScheme(graphBuilder, bs1Name, hc1ID, "INTERNAL_MANAGED")
Expand All @@ -223,7 +223,7 @@ func TestBackendServiceUpdate(t *testing.T) {
if err != nil {
t.Fatalf("graphBuilder.Build() = %v, want nil", err)
}
result, err = plan.Do(ctx, theCloud, graph)
result, err = plan.Do(ctx, TheCloud, graph)
if err != nil {
t.Fatalf("plan.Do(_, _, _) = %v, want nil", err)
}
Expand All @@ -243,7 +243,7 @@ func TestBackendServiceUpdate(t *testing.T) {
processGraphAndExpectActions(t, graphBuilder, expectedActions)

wantBS = &compute.BackendService{LoadBalancingScheme: "INTERNAL_MANAGED"}
checkBackendService(t, ctx, theCloud, bs1ID, wantBS, compareLBScheme)
checkBackendService(t, ctx, TheCloud, bs1ID, wantBS, compareLBScheme)
}

func TestBackendServiceMetadataUpdate(t *testing.T) {
Expand All @@ -264,16 +264,16 @@ func TestBackendServiceMetadataUpdate(t *testing.T) {

processGraphAndExpectActions(t, graphBuilder, expectedActions)
t.Cleanup(func() {
err = theCloud.BackendServices().Delete(ctx, bsID.Key)
err = TheCloud.BackendServices().Delete(ctx, bsID.Key)
if err != nil {
t.Logf("theCloud.BackendServices().Delete(_, %s): %v", bsID.Key, err)
t.Logf("TheCloud.BackendServices().Delete(_, %s): %v", bsID.Key, err)
}
})

/* Verify BackendService was created correctly */
bs, err := theCloud.BackendServices().Get(ctx, bsID.Key)
bs, err := TheCloud.BackendServices().Get(ctx, bsID.Key)
if err != nil {
t.Fatalf("theCloud.BackendServices().Get(_, %s) = (nil, %v) wanted (_, nil)", bsID.Key, err)
t.Fatalf("TheCloud.BackendServices().Get(_, %s) = (nil, %v) wanted (_, nil)", bsID.Key, err)
}
if !maps.Equal(bs.Metadatas, wantedMetadata) {
df := cmp.Diff(bs.Metadatas, wantedMetadata)
Expand All @@ -291,9 +291,9 @@ func TestBackendServiceMetadataUpdate(t *testing.T) {
processGraphAndExpectActions(t, graphBuilder, expectedActions)

/* Verify Metadatas field was updated correctly */
bs, err = theCloud.BackendServices().Get(ctx, bsID.Key)
bs, err = TheCloud.BackendServices().Get(ctx, bsID.Key)
if err != nil {
t.Fatalf("theCloud.BackendServices().Get(_, %s) = (nil, %v) wanted (_, nil)", bsID.Key, err)
t.Fatalf("TheCloud.BackendServices().Get(_, %s) = (nil, %v) wanted (_, nil)", bsID.Key, err)
}

if !maps.Equal(bs.Metadatas, wantedMetadata) {
Expand All @@ -312,9 +312,9 @@ func TestBackendServiceMetadataUpdate(t *testing.T) {
processGraphAndExpectActions(t, graphBuilder, expectedActions)

/* Verify a key from BackendService metadatas field was removed correctly */
bs, err = theCloud.BackendServices().Get(ctx, bsID.Key)
bs, err = TheCloud.BackendServices().Get(ctx, bsID.Key)
if err != nil {
t.Fatalf("theCloud.BackendServices().Get(_, %s) = (nil, %v) wanted (_, nil)", bsID.Key, err)
t.Fatalf("TheCloud.BackendServices().Get(_, %s) = (nil, %v) wanted (_, nil)", bsID.Key, err)
}

if !maps.Equal(bs.Metadatas, wantedMetadata) {
Expand All @@ -329,7 +329,7 @@ func TestBackendServiceMetadataUpdate(t *testing.T) {
if err != nil {
t.Fatalf("graphBuilder.Build() = %v want nil", err)
}
result, err := plan.Do(ctx, theCloud, graph)
result, err := plan.Do(ctx, TheCloud, graph)
if err != nil {
t.Fatalf("plan.Do(_, _, _) = %v, want nil", err)
}
Expand Down Expand Up @@ -368,7 +368,7 @@ func TestEnsureBackendService(t *testing.T) {
t.Fatalf("graphBuilder.Build() = %v want nil", err)
}

result, err := plan.Do(ctx, theCloud, graph)
result, err := plan.Do(ctx, TheCloud, graph)
if err != nil {
t.Fatalf("plan.Do(_, _, _) = %v, want nil", err)
}
Expand All @@ -383,7 +383,7 @@ func TestEnsureBackendService(t *testing.T) {
}

processGraphAndExpectActions(t, graphBuilder, expectedActions)
checkGCEBackendService(t, ctx, theCloud, hcID, bsID, 80)
checkGCEBackendService(t, ctx, TheCloud, hcID, bsID, 80)

expectedActions = []exec.ActionMetadata{
{Type: exec.ActionTypeMeta, Name: eventName(bsID)},
Expand All @@ -395,7 +395,7 @@ func TestEnsureBackendService(t *testing.T) {
t.Fatalf("graphBuilder.Build() = %v want nil", err)
}

result, err = plan.Do(ctx, theCloud, graph)
result, err = plan.Do(ctx, TheCloud, graph)
if err != nil {
t.Fatalf("plan.Do(_, _, _) = %v, want nil", err)
}
Expand Down
32 changes: 16 additions & 16 deletions e2e/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,66 +14,66 @@ func matchTestResource(name string) bool {
}

func cleanupMeshes(ctx context.Context) {
tcprs, err := theCloud.Meshes().List(ctx, filter.None)
tcprs, err := TheCloud.Meshes().List(ctx, filter.None)
if err != nil {
log.Printf("FallbackCleanup: theCloud.Meshes().List(ctx, _): %v\n", err)
log.Printf("FallbackCleanup: TheCloud.Meshes().List(ctx, _): %v\n", err)
return
}
for _, tcpr := range tcprs {
if !matchTestResource(tcpr.Name) {
continue
}
key := meta.GlobalKey(tcpr.Name)
err = theCloud.Meshes().Delete(ctx, key)
log.Printf("FallbackCleanup: theCloud.Meshes().Delete(ctx, %s): %v\n", key, err)
err = TheCloud.Meshes().Delete(ctx, key)
log.Printf("FallbackCleanup: TheCloud.Meshes().Delete(ctx, %s): %v\n", key, err)
}
}

func cleanupTcpRoutes(ctx context.Context) {
tcprs, err := theCloud.TcpRoutes().List(ctx, filter.None)
tcprs, err := TheCloud.TcpRoutes().List(ctx, filter.None)
if err != nil {
log.Printf("FallbackCleanup: theCloud.TcpRoutes().List(ctx, _): %v\n", err)
log.Printf("FallbackCleanup: TheCloud.TcpRoutes().List(ctx, _): %v\n", err)
return
}
for _, tcpr := range tcprs {
if !matchTestResource(tcpr.Name) {
continue
}
key := meta.GlobalKey(tcpr.Name)
err = theCloud.TcpRoutes().Delete(ctx, key)
log.Printf("FallbackCleanup: theCloud.TcpRoutes().Delete(ctx, %s): %v\n", key, err)
err = TheCloud.TcpRoutes().Delete(ctx, key)
log.Printf("FallbackCleanup: TheCloud.TcpRoutes().Delete(ctx, %s): %v\n", key, err)
}
}

func cleanupBackendServices(ctx context.Context) {
bss, err := theCloud.BackendServices().List(ctx, filter.None)
bss, err := TheCloud.BackendServices().List(ctx, filter.None)
if err != nil {
log.Printf("FallbackCleanup: theCloud.BackendServices().List(ctx, _): %v\n", err)
log.Printf("FallbackCleanup: TheCloud.BackendServices().List(ctx, _): %v\n", err)
return
}
for _, bs := range bss {
if !matchTestResource(bs.Name) {
continue
}
key := meta.GlobalKey(bs.Name)
err = theCloud.BackendServices().Delete(ctx, key)
log.Printf("FallbackCleanup: theCloud.BackendServices().Delete(ctx, %s): %v\n", key, err)
err = TheCloud.BackendServices().Delete(ctx, key)
log.Printf("FallbackCleanup: TheCloud.BackendServices().Delete(ctx, %s): %v\n", key, err)
}
}

func cleanupHealthChecks(ctx context.Context) {
hcs, err := theCloud.HealthChecks().List(ctx, filter.None)
hcs, err := TheCloud.HealthChecks().List(ctx, filter.None)
if err != nil {
log.Printf("FallbackCleanup: theCloud.HealthChecks().List(ctx, _): %v\n", err)
log.Printf("FallbackCleanup: TheCloud.HealthChecks().List(ctx, _): %v\n", err)
return
}
for _, hc := range hcs {
if !matchTestResource(hc.Name) {
continue
}
key := meta.GlobalKey(hc.Name)
err = theCloud.HealthChecks().Delete(ctx, key)
log.Printf("FallbackCleanup: theCloud.HealthChecks().Delete(ctx, %s): %v\n", key, err)
err = TheCloud.HealthChecks().Delete(ctx, key)
log.Printf("FallbackCleanup: TheCloud.HealthChecks().Delete(ctx, %s): %v\n", key, err)
}
}

Expand Down
63 changes: 63 additions & 0 deletions e2e/cloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package e2e

import (
"context"
"log"
"time"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
"google.golang.org/api/impersonate"
"google.golang.org/api/option"
)

var (
// TheCloud is a global to be used in the e2e tests.
TheCloud cloud.Cloud
)

func SetupCloudOrDie(ctx context.Context) {
credentials, err := google.FindDefaultCredentials(ctx, compute.ComputeScope)
if err != nil {
log.Fatal(err)
}
ts := credentials.TokenSource

// Optionally, impersonate service account by replacing token source for http client.
if TestFlags.ServiceAccountName != "" {
ts, err = impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{
TargetPrincipal: TestFlags.ServiceAccountName,
Scopes: []string{compute.ComputeScope, compute.CloudPlatformScope},
}, option.WithCredentials(credentials))
if err != nil {
log.Fatalf("Failed to use %q credentials: %v", TestFlags.ServiceAccountName, err)
}
}
client := oauth2.NewClient(ctx, ts)

mrl := &cloud.MinimumRateLimiter{RateLimiter: &cloud.NopRateLimiter{}, Minimum: 50 * time.Millisecond}
crl := cloud.NewCompositeRateLimiter(mrl)

// The default limit is 1500 per minute. Leave 200 buffer.
computeRL := cloud.NewTickerRateLimiter(1300, time.Minute)
crl.Register("HealthChecks", "", computeRL)
crl.Register("BackendServices", "", computeRL)
crl.Register("NetworkEndpointGroups", "", computeRL)

// The default limit is 1200 per minute. Leave 200 buffer.
networkServicesRL := cloud.NewTickerRateLimiter(1000, time.Minute)
crl.Register("TcpRoutes", "", networkServicesRL)
crl.Register("Meshes", "", networkServicesRL)

// To ensure minimum time between operations, wrap the network services rate limiter.
orl := &cloud.MinimumRateLimiter{RateLimiter: networkServicesRL, Minimum: 100 * time.Millisecond}
crl.Register("Operations", "", orl)

svc, err := cloud.NewService(ctx, client, &cloud.SingleProjectRouter{ID: TestFlags.Project}, crl)
if err != nil {
log.Fatal(err)
}
TheCloud = cloud.NewGCE(svc)
}
Loading

0 comments on commit 6bd06ad

Please sign in to comment.