Skip to content

Commit

Permalink
Merge branch 'master' into regen_gocloud
Browse files Browse the repository at this point in the history
  • Loading branch information
codyoss committed Jul 19, 2021
2 parents 8075be5 + 1110dcf commit 6520718
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 2 deletions.
256 changes: 256 additions & 0 deletions compute/apiv1/smoke_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
// Copyright 2021 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.

// To run these tests, set GCLOUD_TESTS_GOLANG_PROJECT_ID env var to your GCP projectID

package compute

import (
"context"
"fmt"
"testing"

"cloud.google.com/go/internal/testutil"
"cloud.google.com/go/internal/uid"
computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
"google.golang.org/protobuf/proto"
)

var projectId = testutil.ProjID()
var defaultZone = "us-central1-a"

func TestCreateGetListInstance(t *testing.T) {
if testing.Short() {
t.Skip("skipping smoke test in short mode")
}
space := uid.NewSpace("gogapic", nil)
name := space.New()
ctx := context.Background()
c, err := NewInstancesRESTClient(ctx)
if err != nil {
t.Fatal(err)
}
zonesClient, err := NewZoneOperationsRESTClient(ctx)
if err != nil {
t.Fatal(err)
}
createRequest := &computepb.InsertInstanceRequest{
Project: projectId,
Zone: defaultZone,
InstanceResource: &computepb.Instance{
Name: &name,
Description: proto.String("тест"),
MachineType: proto.String(fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/n1-standard-1", projectId, defaultZone)),
Disks: []*computepb.AttachedDisk{
{
AutoDelete: proto.Bool(true),
Boot: proto.Bool(true),
Type: computepb.AttachedDisk_PERSISTENT.Enum(),
InitializeParams: &computepb.AttachedDiskInitializeParams{
SourceImage: proto.String("projects/debian-cloud/global/images/family/debian-10"),
},
},
},
NetworkInterfaces: []*computepb.NetworkInterface{
{
AccessConfigs: []*computepb.AccessConfig{
{
Name: proto.String("default"),
},
},
},
},
},
}

insert, err := c.Insert(ctx, createRequest)
if err != nil {
t.Fatal(err)
}

waitZonalRequest := &computepb.WaitZoneOperationRequest{
Project: projectId,
Zone: defaultZone,
Operation: insert.GetName(),
}
_, err = zonesClient.Wait(ctx, waitZonalRequest)
if err != nil {
t.Error(err)
}
defer ForceDeleteInstance(ctx, name, c)

getRequest := &computepb.GetInstanceRequest{
Project: projectId,
Zone: defaultZone,
Instance: name,
}
get, err := c.Get(ctx, getRequest)
if err != nil {
t.Error(err)
}
if get.GetName() != name {
t.Fatal(fmt.Sprintf("expected instance name: %s, got: %s", name, get.GetName()))
}
if get.GetDescription() != "тест" {
t.Fatal(fmt.Sprintf("expected instance description: %s, got: %s", "тест", get.GetDescription()))
}
listRequest := &computepb.ListInstancesRequest{
Project: projectId,
Zone: defaultZone,
}

list, err := c.List(ctx, listRequest)
if err != nil {
t.Error(err)
}
items := list.GetItems()
found := false
for _, element := range items {
if element.GetName() == name {
found = true
}
}
if !found {
t.Error("Couldn't find the instance in list response")
}

deleteInstanceRequest := &computepb.DeleteInstanceRequest{
Project: projectId,
Zone: defaultZone,
Instance: name,
}
_, err = c.Delete(ctx, deleteInstanceRequest)
if err != nil {
t.Error(err)
}
}

func ForceDeleteInstance(ctx context.Context, name string, client *InstancesClient) {
deleteInstanceRequest := &computepb.DeleteInstanceRequest{
Project: projectId,
Zone: defaultZone,
Instance: name,
}
client.Delete(ctx, deleteInstanceRequest)
}

func TestCreateGetRemoveSecurityPolicies(t *testing.T) {
if testing.Short() {
t.Skip("skipping smoke test in short mode")
}
space := uid.NewSpace("gogapic", nil)
name := space.New()
ctx := context.Background()
c, err := NewSecurityPoliciesRESTClient(ctx)
if err != nil {
t.Fatal(err)
}
globalCLient, err := NewGlobalOperationsRESTClient(ctx)
if err != nil {
t.Fatal(err)
}
action := "allow"
matcher := &computepb.SecurityPolicyRuleMatcher{
Config: &computepb.SecurityPolicyRuleMatcherConfig{
SrcIpRanges: []string{
"*",
},
},
VersionedExpr: computepb.SecurityPolicyRuleMatcher_SRC_IPS_V1.Enum(),
}
securityPolicyRule := &computepb.SecurityPolicyRule{
Action: &action,
Priority: proto.Int32(0),
Description: proto.String("test rule"),
Match: matcher,
}
securityPolicyRuleDefault := &computepb.SecurityPolicyRule{
Action: &action,
Priority: proto.Int32(2147483647),
Description: proto.String("default rule"),
Match: matcher,
}
insertRequest := &computepb.InsertSecurityPolicyRequest{
Project: projectId,
SecurityPolicyResource: &computepb.SecurityPolicy{
Name: &name,
Rules: []*computepb.SecurityPolicyRule{
securityPolicyRule,
securityPolicyRuleDefault,
},
},
}
insert, err := c.Insert(ctx, insertRequest)
if err != nil {
t.Fatal(err)
}

waitGlobalRequest := &computepb.WaitGlobalOperationRequest{
Project: projectId,
Operation: insert.GetName(),
}
_, err = globalCLient.Wait(ctx, waitGlobalRequest)
if err != nil {
t.Error(err)
}
defer ForceDeleteSecurityPolicy(ctx, name, c)

removeRuleRequest := &computepb.RemoveRuleSecurityPolicyRequest{
Priority: proto.Int32(0),
Project: projectId,
SecurityPolicy: name,
}

rule, err := c.RemoveRule(ctx, removeRuleRequest)
if err != nil {
t.Error(err)
}
waitGlobalRequestRemove := &computepb.WaitGlobalOperationRequest{
Project: projectId,
Operation: rule.GetName(),
}
_, err = globalCLient.Wait(ctx, waitGlobalRequestRemove)
if err != nil {
t.Error(err)
}

getRequest := &computepb.GetSecurityPolicyRequest{
Project: projectId,
SecurityPolicy: name,
}
get, err := c.Get(ctx, getRequest)
if err != nil {
t.Error(err)
}
if len(get.GetRules()) != 1 {
t.Fatal(fmt.Sprintf("expected count for rules: %d, got: %d", 1, len(get.GetRules())))
}

deleteRequest := &computepb.DeleteSecurityPolicyRequest{
Project: projectId,
SecurityPolicy: name,
}
_, err = c.Delete(ctx, deleteRequest)
if err != nil {
t.Error(err)
}
}

func ForceDeleteSecurityPolicy(ctx context.Context, name string, client *SecurityPoliciesClient) {
deleteRequest := &computepb.DeleteSecurityPolicyRequest{
Project: projectId,
SecurityPolicy: name,
}
client.Delete(ctx, deleteRequest)
}
2 changes: 1 addition & 1 deletion spanner/emulator_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ function cleanup() {
}
trap cleanup EXIT

go test -v -timeout 10m ./... -run '^TestIntegration_' 2>&1 | tee sponge_log.log
go test -v -timeout 10m ./... -run '^TestIntegration_' 2>&1 | tee -a sponge_log.log
10 changes: 9 additions & 1 deletion spanner/row_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package spanner

import (
"encoding/base64"
"fmt"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -417,6 +418,9 @@ func TestColumnTypeErr(t *testing.T) {
etc = f.Type.ArrayElementType.Code
}
wantErr := errDecodeColumn(i, errTypeMismatch(tc, etc, badDst))
if strings.Contains(f.Name, "STRUCT_ARRAY") {
wantErr = errDecodeColumn(i, fmt.Errorf("the container is not a slice of struct pointers: %v", errTypeMismatch(tc, etc, badDst)))
}
if gotErr := row.Column(i, badDst); !testEqual(gotErr, wantErr) {
t.Errorf("Column(%v): decoding into destination with wrong type %T returns error %v, want %v",
i, badDst, gotErr, wantErr)
Expand Down Expand Up @@ -1685,7 +1689,11 @@ func TestRowToString(t *testing.T) {
}
got := r.String()
want := `{fields: [name:"F1" type:{code:STRING} name:"F2" type:{code:STRING}], values: [string_value:"v1" string_value:"v2"]}`
if !testEqual(r.String(), want) {
// In protobuf-go, the encoder will add an additional space based on a
// deterministically random boolean value.
wantWithTwoSpaces := `{fields: [name:"F1" type:{code:STRING} name:"F2" type:{code:STRING}], values: [string_value:"v1" string_value:"v2"]}`

if !testEqual(r.String(), want) && !testEqual(r.String(), wantWithTwoSpaces) {
t.Errorf("got %+v, want %+v", got, want)
}
}
Expand Down

0 comments on commit 6520718

Please sign in to comment.