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 authored and yoshi-automation committed Jul 19, 2021
2 parents 8075be5 + 1110dcf commit c273438
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 6 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
2 changes: 1 addition & 1 deletion spanner/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
go.opencensus.io v0.23.0
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
google.golang.org/api v0.50.0
google.golang.org/genproto v0.0.0-20210715145939-324b959e9c22
google.golang.org/genproto v0.0.0-20210719143636-1d5a45f8e492
google.golang.org/grpc v1.39.0
google.golang.org/protobuf v1.27.1
)
7 changes: 4 additions & 3 deletions spanner/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,9 @@ golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.4 h1:cVngSRcfgyZCzys3KYOpCFa+4dqX/Oub9tAq00ttGVs=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down Expand Up @@ -462,8 +463,8 @@ google.golang.org/genproto v0.0.0-20210604141403-392c879c8b08/go.mod h1:UODoCrxH
google.golang.org/genproto v0.0.0-20210608205507-b6d2f5bf0d7d/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0=
google.golang.org/genproto v0.0.0-20210624195500-8bfb893ecb84/go.mod h1:SzzZ/N+nwJDaO1kznhnlzqS8ocJICar6hYhVyhi++24=
google.golang.org/genproto v0.0.0-20210701133433-6b8dcf568a95/go.mod h1:yiaVoXHpRzHGyxV3o4DktVWY4mSUErTKaeEOq6C3t3U=
google.golang.org/genproto v0.0.0-20210715145939-324b959e9c22 h1:jcklN/lATdu/CSsNEOLujIgfvY7IMQpRq6HNaPZuVIc=
google.golang.org/genproto v0.0.0-20210715145939-324b959e9c22/go.mod h1:AxrInvYm1dci+enl5hChSFPOmmUF1+uAa/UsgNRWd7k=
google.golang.org/genproto v0.0.0-20210719143636-1d5a45f8e492 h1:7yQQsvnwjfEahbNNEKcBHv3mR+HnB1ctGY/z1JXzx8M=
google.golang.org/genproto v0.0.0-20210719143636-1d5a45f8e492/go.mod h1:ob2IJxKrgPT52GcgX759i1sleT07tiKowYBGbczaW48=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
Expand Down
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 c273438

Please sign in to comment.