Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds global OperationsUseWait which switches Operation.Get to Operation.Wait; manual e2e testing #85

Merged
merged 4 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ build: gen

.PHONY: test
test:
go test ./...
# Test only the library. e2e must be run in a special environment,
# so is skipped.
go test ./pkg/...
# We cannot use golint currently due to errors in the GCP API naming.
# golint ./...
go vet ./...
Expand Down
125 changes: 125 additions & 0 deletions e2e/addresses_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
Copyright 2023 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e

import (
"context"
"fmt"
"testing"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
"github.com/kr/pretty"
"google.golang.org/api/compute/v1"
)

func TestAddresses(t *testing.T) {
t.Parallel()

ctx := context.Background()

const regionName = "us-central1"
addr1 := &compute.Address{
AddressType: "EXTERNAL",
Description: "k8s-cloud-provider-test",
Name: resourceName("addr1"),
NetworkTier: "STANDARD",
Region: regionName,
}
addr1Key := meta.RegionalKey(addr1.Name, regionName)

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

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

// Insert
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)
if err != nil {
t.Fatalf("Addresses.Get(addr1) = %v", err)
}
if a.Name != addr1.Name || a.Description != addr1.Description {
t.Fatalf("Addresses.Get() did not match, got %s\nwant %s", pretty.Sprint(a), pretty.Sprint(addr1))
}

// List
al, err := theCloud.Addresses().List(ctx, regionName, nil)
if err != nil {
t.Fatalf("Error listing Addresses: %v", err)
}

var found bool
for _, a := range al {
if a.Name == addr1.Name {
found = true
}
}

if !found {
t.Fatalf("Expected to find Address %q but it didn't exist", addr1.Name)
}

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

// AggregatedList
}

func TestGlobalAddresses(t *testing.T) {
t.Parallel()

ctx := context.Background()

addr1 := &compute.Address{
AddressType: "EXTERNAL",
Description: "k8s-cloud-provider-test",
Name: resourceName("addr1"),
NetworkTier: "PREMIUM",
}
addr1Key := meta.GlobalKey(addr1.Name)

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

t.Cleanup(func() { theCloud.GlobalAddresses().Delete(context.Background(), addr1Key) })
bowei marked this conversation as resolved.
Show resolved Hide resolved

// Insert
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)
if err != nil {
t.Fatalf("GlobalAddresses.Get(addr1) = %v", err)
}
if a.Name != addr1.Name || a.Description != addr1.Description {
t.Fatalf("GlobalAddresses.Get() did not match, got %s\nwant %s", pretty.Sprint(a), pretty.Sprint(addr1))
}

// Delete
err = theCloud.GlobalAddresses().Delete(ctx, addr1Key)
if err != nil {
t.Fatalf("GlobalAddresses.Delete(addr1) = %v", err)
}
}
28 changes: 28 additions & 0 deletions e2e/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2023 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package e2e tests the functionality of cloud adaptor against actual GCP.
// For this test to work, you must have a valid credential and access to the
// APIs tested:
//
// $ gcloud auth application-default login
// $ go test ./e2e
//
// Run with coverage:
//
// $ go test -coverpkg ./pkg/cloud -coverprofile cov.out ./e2e ./pkg/cloud
// $ go tool cover -html cov.out
package e2e
79 changes: 79 additions & 0 deletions e2e/observe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2023 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"context"
"reflect"
"testing"
"time"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
)

type testObserver struct {
name string
start, end time.Time
startCK, endCK cloud.CallContextKey
err error
}

func (o *testObserver) Start(ctx context.Context, ck *cloud.CallContextKey) {
o.start = time.Now()
o.startCK = *ck
}
func (o *testObserver) End(ctx context.Context, ck *cloud.CallContextKey, err error) {
o.end = time.Now()
o.endCK = *ck
o.err = err
}

func TestObserve(t *testing.T) {
t.Parallel()

ctx := context.Background()
o := &testObserver{}
ctx = cloud.WithCallObserver(ctx, o)

const invalidZoneName = "moonbase1-b"
theCloud.Zones().Get(ctx, meta.GlobalKey(invalidZoneName))
if o.start.IsZero() || o.end.IsZero() {
t.Fatalf("testObserver start, end was not modified (testObserver = %+v)", o)
}

wantCK := cloud.CallContextKey{
Operation: "Get",
Version: meta.VersionGA,
Service: "Zones",
}

// Ignore differences in ProjectID as they could change when run manually
// running manually.
o.startCK.ProjectID = ""
o.endCK.ProjectID = ""

if !reflect.DeepEqual(o.startCK, wantCK) {
t.Fatalf("o.startCK = %+v, want %+v", o.startCK, wantCK)
}
if !reflect.DeepEqual(o.endCK, wantCK) {
t.Fatalf("o.endCK = %+v, want %+v", o.endCK, wantCK)
}
if o.err == nil {
t.Fatal("testObserver.err = nil, want err")
}
}
58 changes: 58 additions & 0 deletions e2e/regions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2023 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"context"
"testing"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
)

func TestRegions(t *testing.T) {
t.Parallel()

ctx := context.Background()

regions, err := theCloud.Regions().List(ctx, nil)
if err != nil {
t.Fatalf("Error listing Regions: %v", err)
}

const regionName = "us-central1"

t.Logf("Got %d Regions", len(regions))

var found bool
for _, z := range regions {
if z.Name == regionName {
found = true
}
}
if !found {
t.Fatalf("%q was not in the list of Regions", regionName)
}

_, err = theCloud.Regions().Get(ctx, meta.GlobalKey(regionName))
if err != nil {
t.Fatalf("Get(%q) = _, %v; want _, nil", regionName, err)
}

const invalidZone = "moonlab1"
_, err = theCloud.Regions().Get(ctx, meta.GlobalKey(invalidZone))
checkErrCode(t, err, 404, "Regions.Get()")
}
Loading