From 54f92280289da04e46138ef4379032fa34a7d979 Mon Sep 17 00:00:00 2001 From: Georgy Ekkert Date: Mon, 12 Jul 2021 13:52:50 -0700 Subject: [PATCH 01/11] tests: add go integartion tests --- compute/apiv1/smoke_test.go | 290 ++++++++++++++++++++++++++++++++++++ 1 file changed, 290 insertions(+) create mode 100644 compute/apiv1/smoke_test.go diff --git a/compute/apiv1/smoke_test.go b/compute/apiv1/smoke_test.go new file mode 100644 index 00000000000..42f2d9583dd --- /dev/null +++ b/compute/apiv1/smoke_test.go @@ -0,0 +1,290 @@ +// 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 ( + "cloud.google.com/go/internal/testutil" + "context" + "fmt" + "github.com/stretchr/testify/assert" + computepb "google.golang.org/genproto/googleapis/cloud/compute/v1" + "google.golang.org/protobuf/proto" + "math/rand" + "testing" + "time" +) + +var projectId = testutil.ProjID() +var defaultZone = "us-central1-a" + +func TestCreateGetListInstance(t *testing.T){ + rand.Seed(time.Now().UTC().UnixNano()) + name := fmt.Sprintf("gotest%d", rand.Int()) + description := "тест" + machineType := fmt.Sprintf( + "https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/n1-standard-1", + projectId, defaultZone) + ctx := context.Background() + c, err := NewInstancesRESTClient(ctx) + if err != nil { + t.Fatal(err) + } + zonesClient, err := NewZoneOperationsRESTClient(ctx) + if err != nil { + t.Fatal(err) + } + configName := "default" + accessConfig := computepb.AccessConfig{ + Name: &configName, + } + configs := []*computepb.AccessConfig { + &accessConfig, + } + networkInterface := computepb.NetworkInterface{ + AccessConfigs: configs, + } + interfaces := []*computepb.NetworkInterface{ + &networkInterface, + } + sourceImage := "projects/debian-cloud/global/images/family/debian-10" + initializeParams := &computepb.AttachedDiskInitializeParams{ + SourceImage: &sourceImage, + } + diskType := computepb.AttachedDisk_PERSISTENT + disk := computepb.AttachedDisk{ + AutoDelete: proto.Bool(true), + Boot: proto.Bool(true), + Type: &diskType, + InitializeParams: initializeParams, + } + disks := []*computepb.AttachedDisk { + &disk, + } + instance := &computepb.Instance{ + Name: &name, + Description: &description, + MachineType: &machineType, + Disks: disks, + NetworkInterfaces: interfaces, + } + + createRequest := &computepb.InsertInstanceRequest{ + Project: projectId, + Zone: defaultZone, + InstanceResource: instance, + } + + insert, err := c.Insert(ctx, createRequest) + if err != nil { + fmt.Println(err) + t.Error(err) + } + + waitZonalRequest := &computepb.WaitZoneOperationRequest{ + Project: projectId, + Zone: defaultZone, + Operation: insert.GetName(), + } + _, err = zonesClient.Wait(ctx, waitZonalRequest) + if err != nil { + return + } + fmt.Printf("Inserted instance named %s\n", name) + defer ForceDeleteInstance(name, ctx, c) + + getRequest := &computepb.GetInstanceRequest{ + Project: projectId, + Zone: defaultZone, + Instance: name, + } + get, err := c.Get(ctx, getRequest) + if err != nil { + fmt.Println(err) + t.Error(err) + } + assert.Equal(t, name, get.GetName()) + assert.Equal(t, "тест", get.GetDescription()) + + listRequest := &computepb.ListInstancesRequest{ + Project: projectId, + Zone: defaultZone, + } + + list, err := c.List(ctx, listRequest) + if err != nil { + fmt.Println(err) + t.Error(err) + } + items := list.GetItems() + found := false + for _, element := range items { + if element.GetName() == name { + found = true + } + } + if found == false{ + 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 { + fmt.Println(err) + t.Error(err) + } +} + +func ForceDeleteInstance(name string, ctx context.Context, client *InstancesClient){ + deleteInstanceRequest := &computepb.DeleteInstanceRequest{ + Project: projectId, + Zone: defaultZone, + Instance: name, + } + _, err := client.Delete(ctx, deleteInstanceRequest) + if err != nil {} + +} + +func TestCreateGetRemoveSecurityPolicies(t *testing.T){ + rand.Seed(time.Now().UTC().UnixNano()) + name := fmt.Sprintf("gotest%d", rand.Int()) + ctx := context.Background() + c, err := NewSecurityPoliciesRESTClient(ctx) + if err != nil { + t.Fatal(err) + } + globalCLient, err := NewGlobalOperationsRESTClient(ctx) + if err != nil { + t.Fatal(err) + } + defaultDescription := "default rule" + description := "test rule" + defaultPriority := int32(2147483647) + priority := int32(0) + action := "allow" + + srcIpRanges := []string{ + "*", + } + config := &computepb.SecurityPolicyRuleMatcherConfig{ + SrcIpRanges: srcIpRanges, + } + versionExpr := computepb.SecurityPolicyRuleMatcher_SRC_IPS_V1 + matcher := &computepb.SecurityPolicyRuleMatcher{ + Config: config, + VersionedExpr: &versionExpr, + } + securityPolicyRule := &computepb.SecurityPolicyRule{ + Action: &action, + Priority: &priority, + Description: &description, + Match: matcher, + } + securityPolicyRuleDefault := &computepb.SecurityPolicyRule{ + Action: &action, + Priority: &defaultPriority, + Description: &defaultDescription, + Match: matcher, + } + + rules := []*computepb.SecurityPolicyRule{ + securityPolicyRule, + securityPolicyRuleDefault, + } + + securityPolicy := &computepb.SecurityPolicy{ + Name: &name, + Rules: rules, + } + + insertRequest := &computepb.InsertSecurityPolicyRequest{ + Project: projectId, + SecurityPolicyResource: securityPolicy, + } + insert, err := c.Insert(ctx, insertRequest) + if err != nil { + fmt.Println(err) + t.Error(err) + } + + waitGlobalRequest := &computepb.WaitGlobalOperationRequest{ + Project: projectId, + Operation: insert.GetName(), + } + _, err = globalCLient.Wait(ctx, waitGlobalRequest) + if err != nil { + fmt.Println(err) + t.Error(err) + } + fmt.Printf("Inserted security policy named %s\n", name) + defer ForceDeleteSecurityPolicy(name, ctx, c) + + removeRuleRequest := &computepb.RemoveRuleSecurityPolicyRequest{ + Priority: &priority, + Project: projectId, + SecurityPolicy: name, + } + + rule, err := c.RemoveRule(ctx, removeRuleRequest) + if err != nil { + fmt.Println(err) + t.Error(err) + } + waitGlobalRequestRemove := &computepb.WaitGlobalOperationRequest{ + Project: projectId, + Operation: rule.GetName(), + } + _, err = globalCLient.Wait(ctx, waitGlobalRequestRemove) + if err != nil { + fmt.Println(err) + t.Error(err) + } + + getRequest := &computepb.GetSecurityPolicyRequest{ + Project: projectId, + SecurityPolicy: name, + } + get, err := c.Get(ctx, getRequest) + if err != nil { + fmt.Println(err) + t.Error(err) + } + assert.Equal(t, 1, len(get.GetRules())) + + deleteRequest := &computepb.DeleteSecurityPolicyRequest{ + Project: projectId, + SecurityPolicy: name, + } + _, err = c.Delete(ctx, deleteRequest) + if err != nil { + fmt.Println(err) + t.Error(err) + } +} + +func ForceDeleteSecurityPolicy(name string, ctx context.Context, client *SecurityPoliciesClient){ + deleteRequest := &computepb.DeleteSecurityPolicyRequest{ + Project: projectId, + SecurityPolicy: name, + } + _, err := client.Delete(ctx, deleteRequest) + if err != nil {} +} From 5a30666740aa7ee849f12721e49ae68952f141ef Mon Sep 17 00:00:00 2001 From: Georgy Ekkert Date: Mon, 12 Jul 2021 14:46:13 -0700 Subject: [PATCH 02/11] tests: review fixes + formatting --- compute/apiv1/smoke_test.go | 113 ++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 64 deletions(-) diff --git a/compute/apiv1/smoke_test.go b/compute/apiv1/smoke_test.go index 42f2d9583dd..aa85db7f7ef 100644 --- a/compute/apiv1/smoke_test.go +++ b/compute/apiv1/smoke_test.go @@ -20,7 +20,6 @@ import ( "cloud.google.com/go/internal/testutil" "context" "fmt" - "github.com/stretchr/testify/assert" computepb "google.golang.org/genproto/googleapis/cloud/compute/v1" "google.golang.org/protobuf/proto" "math/rand" @@ -31,7 +30,7 @@ import ( var projectId = testutil.ProjID() var defaultZone = "us-central1-a" -func TestCreateGetListInstance(t *testing.T){ +func TestCreateGetListInstance(t *testing.T) { rand.Seed(time.Now().UTC().UnixNano()) name := fmt.Sprintf("gotest%d", rand.Int()) description := "тест" @@ -51,7 +50,7 @@ func TestCreateGetListInstance(t *testing.T){ accessConfig := computepb.AccessConfig{ Name: &configName, } - configs := []*computepb.AccessConfig { + configs := []*computepb.AccessConfig{ &accessConfig, } networkInterface := computepb.NetworkInterface{ @@ -66,37 +65,36 @@ func TestCreateGetListInstance(t *testing.T){ } diskType := computepb.AttachedDisk_PERSISTENT disk := computepb.AttachedDisk{ - AutoDelete: proto.Bool(true), - Boot: proto.Bool(true), - Type: &diskType, + AutoDelete: proto.Bool(true), + Boot: proto.Bool(true), + Type: &diskType, InitializeParams: initializeParams, } - disks := []*computepb.AttachedDisk { + disks := []*computepb.AttachedDisk{ &disk, } instance := &computepb.Instance{ - Name: &name, - Description: &description, - MachineType: &machineType, - Disks: disks, + Name: &name, + Description: &description, + MachineType: &machineType, + Disks: disks, NetworkInterfaces: interfaces, } createRequest := &computepb.InsertInstanceRequest{ - Project: projectId, - Zone: defaultZone, + Project: projectId, + Zone: defaultZone, InstanceResource: instance, } insert, err := c.Insert(ctx, createRequest) if err != nil { - fmt.Println(err) t.Error(err) } waitZonalRequest := &computepb.WaitZoneOperationRequest{ - Project: projectId, - Zone: defaultZone, + Project: projectId, + Zone: defaultZone, Operation: insert.GetName(), } _, err = zonesClient.Wait(ctx, waitZonalRequest) @@ -104,29 +102,26 @@ func TestCreateGetListInstance(t *testing.T){ return } fmt.Printf("Inserted instance named %s\n", name) - defer ForceDeleteInstance(name, ctx, c) + defer ForceDeleteInstance(ctx, name, c) getRequest := &computepb.GetInstanceRequest{ - Project: projectId, - Zone: defaultZone, + Project: projectId, + Zone: defaultZone, Instance: name, } get, err := c.Get(ctx, getRequest) if err != nil { - fmt.Println(err) t.Error(err) } - assert.Equal(t, name, get.GetName()) - assert.Equal(t, "тест", get.GetDescription()) - + testutil.Equal(name, get.GetName()) + testutil.Equal("тест", get.GetDescription()) listRequest := &computepb.ListInstancesRequest{ Project: projectId, - Zone: defaultZone, + Zone: defaultZone, } list, err := c.List(ctx, listRequest) if err != nil { - fmt.Println(err) t.Error(err) } items := list.GetItems() @@ -136,34 +131,31 @@ func TestCreateGetListInstance(t *testing.T){ found = true } } - if found == false{ + if !found { t.Error("Couldn't find the instance in list response") } deleteInstanceRequest := &computepb.DeleteInstanceRequest{ - Project: projectId, - Zone: defaultZone, + Project: projectId, + Zone: defaultZone, Instance: name, } _, err = c.Delete(ctx, deleteInstanceRequest) if err != nil { - fmt.Println(err) t.Error(err) } } -func ForceDeleteInstance(name string, ctx context.Context, client *InstancesClient){ +func ForceDeleteInstance(ctx context.Context, name string, client *InstancesClient) { deleteInstanceRequest := &computepb.DeleteInstanceRequest{ - Project: projectId, - Zone: defaultZone, + Project: projectId, + Zone: defaultZone, Instance: name, } - _, err := client.Delete(ctx, deleteInstanceRequest) - if err != nil {} - + client.Delete(ctx, deleteInstanceRequest) } -func TestCreateGetRemoveSecurityPolicies(t *testing.T){ +func TestCreateGetRemoveSecurityPolicies(t *testing.T) { rand.Seed(time.Now().UTC().UnixNano()) name := fmt.Sprintf("gotest%d", rand.Int()) ctx := context.Background() @@ -177,7 +169,7 @@ func TestCreateGetRemoveSecurityPolicies(t *testing.T){ } defaultDescription := "default rule" description := "test rule" - defaultPriority := int32(2147483647) + defaultPriority := int32(2147483647) priority := int32(0) action := "allow" @@ -189,20 +181,20 @@ func TestCreateGetRemoveSecurityPolicies(t *testing.T){ } versionExpr := computepb.SecurityPolicyRuleMatcher_SRC_IPS_V1 matcher := &computepb.SecurityPolicyRuleMatcher{ - Config: config, + Config: config, VersionedExpr: &versionExpr, } securityPolicyRule := &computepb.SecurityPolicyRule{ - Action: &action, - Priority: &priority, + Action: &action, + Priority: &priority, Description: &description, - Match: matcher, + Match: matcher, } securityPolicyRuleDefault := &computepb.SecurityPolicyRule{ - Action: &action, - Priority: &defaultPriority, + Action: &action, + Priority: &defaultPriority, Description: &defaultDescription, - Match: matcher, + Match: matcher, } rules := []*computepb.SecurityPolicyRule{ @@ -211,80 +203,73 @@ func TestCreateGetRemoveSecurityPolicies(t *testing.T){ } securityPolicy := &computepb.SecurityPolicy{ - Name: &name, + Name: &name, Rules: rules, } insertRequest := &computepb.InsertSecurityPolicyRequest{ - Project: projectId, + Project: projectId, SecurityPolicyResource: securityPolicy, } insert, err := c.Insert(ctx, insertRequest) if err != nil { - fmt.Println(err) t.Error(err) } waitGlobalRequest := &computepb.WaitGlobalOperationRequest{ - Project: projectId, + Project: projectId, Operation: insert.GetName(), } _, err = globalCLient.Wait(ctx, waitGlobalRequest) if err != nil { - fmt.Println(err) t.Error(err) } fmt.Printf("Inserted security policy named %s\n", name) - defer ForceDeleteSecurityPolicy(name, ctx, c) + defer ForceDeleteSecurityPolicy(ctx, name, c) removeRuleRequest := &computepb.RemoveRuleSecurityPolicyRequest{ - Priority: &priority, - Project: projectId, + Priority: &priority, + Project: projectId, SecurityPolicy: name, } rule, err := c.RemoveRule(ctx, removeRuleRequest) if err != nil { - fmt.Println(err) t.Error(err) } waitGlobalRequestRemove := &computepb.WaitGlobalOperationRequest{ - Project: projectId, + Project: projectId, Operation: rule.GetName(), } _, err = globalCLient.Wait(ctx, waitGlobalRequestRemove) if err != nil { - fmt.Println(err) t.Error(err) } getRequest := &computepb.GetSecurityPolicyRequest{ - Project: projectId, + Project: projectId, SecurityPolicy: name, } get, err := c.Get(ctx, getRequest) if err != nil { - fmt.Println(err) t.Error(err) } - assert.Equal(t, 1, len(get.GetRules())) + testutil.Equal(1, len(get.GetRules())) deleteRequest := &computepb.DeleteSecurityPolicyRequest{ - Project: projectId, + Project: projectId, SecurityPolicy: name, } _, err = c.Delete(ctx, deleteRequest) if err != nil { - fmt.Println(err) t.Error(err) } } -func ForceDeleteSecurityPolicy(name string, ctx context.Context, client *SecurityPoliciesClient){ +func ForceDeleteSecurityPolicy(ctx context.Context, name string, client *SecurityPoliciesClient) { deleteRequest := &computepb.DeleteSecurityPolicyRequest{ - Project: projectId, + Project: projectId, SecurityPolicy: name, } - _, err := client.Delete(ctx, deleteRequest) - if err != nil {} + client.Delete(ctx, deleteRequest) } From 478eefdef169e303b48470468833783cd4831478 Mon Sep 17 00:00:00 2001 From: Georgy Ekkert Date: Mon, 12 Jul 2021 14:48:52 -0700 Subject: [PATCH 03/11] tests: review fixes + formatting --- compute/apiv1/smoke_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compute/apiv1/smoke_test.go b/compute/apiv1/smoke_test.go index aa85db7f7ef..fbf26a64fbb 100644 --- a/compute/apiv1/smoke_test.go +++ b/compute/apiv1/smoke_test.go @@ -99,7 +99,7 @@ func TestCreateGetListInstance(t *testing.T) { } _, err = zonesClient.Wait(ctx, waitZonalRequest) if err != nil { - return + t.Error(err) } fmt.Printf("Inserted instance named %s\n", name) defer ForceDeleteInstance(ctx, name, c) From bf08f74b04ccb73a62ec34ee937d086ebe9c0659 Mon Sep 17 00:00:00 2001 From: Georgy Ekkert Date: Mon, 12 Jul 2021 15:11:51 -0700 Subject: [PATCH 04/11] tests: review fixes --- compute/apiv1/smoke_test.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/compute/apiv1/smoke_test.go b/compute/apiv1/smoke_test.go index fbf26a64fbb..ef39e911534 100644 --- a/compute/apiv1/smoke_test.go +++ b/compute/apiv1/smoke_test.go @@ -18,21 +18,23 @@ package compute import ( "cloud.google.com/go/internal/testutil" + "cloud.google.com/go/internal/uid" "context" "fmt" computepb "google.golang.org/genproto/googleapis/cloud/compute/v1" "google.golang.org/protobuf/proto" - "math/rand" "testing" - "time" ) var projectId = testutil.ProjID() var defaultZone = "us-central1-a" func TestCreateGetListInstance(t *testing.T) { - rand.Seed(time.Now().UTC().UnixNano()) - name := fmt.Sprintf("gotest%d", rand.Int()) + if testing.Short() { + t.Skip("skipping smoke test in short mode") + } + space := uid.NewSpace("gogapic", nil) + name := space.New() description := "тест" machineType := fmt.Sprintf( "https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/n1-standard-1", @@ -156,8 +158,11 @@ func ForceDeleteInstance(ctx context.Context, name string, client *InstancesClie } func TestCreateGetRemoveSecurityPolicies(t *testing.T) { - rand.Seed(time.Now().UTC().UnixNano()) - name := fmt.Sprintf("gotest%d", rand.Int()) + 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 { From 059c905026acb0b7c66dd6a28df49add180a4bac Mon Sep 17 00:00:00 2001 From: Georgy Ekkert Date: Tue, 13 Jul 2021 13:12:25 -0700 Subject: [PATCH 05/11] tests: review fixes --- compute/apiv1/smoke_test.go | 97 ++++++++++++++----------------------- 1 file changed, 37 insertions(+), 60 deletions(-) diff --git a/compute/apiv1/smoke_test.go b/compute/apiv1/smoke_test.go index ef39e911534..667ac0adb38 100644 --- a/compute/apiv1/smoke_test.go +++ b/compute/apiv1/smoke_test.go @@ -35,10 +35,6 @@ func TestCreateGetListInstance(t *testing.T) { } space := uid.NewSpace("gogapic", nil) name := space.New() - description := "тест" - machineType := fmt.Sprintf( - "https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/n1-standard-1", - projectId, defaultZone) ctx := context.Background() c, err := NewInstancesRESTClient(ctx) if err != nil { @@ -48,41 +44,31 @@ func TestCreateGetListInstance(t *testing.T) { if err != nil { t.Fatal(err) } - configName := "default" - accessConfig := computepb.AccessConfig{ - Name: &configName, - } - configs := []*computepb.AccessConfig{ - &accessConfig, - } - networkInterface := computepb.NetworkInterface{ - AccessConfigs: configs, - } - interfaces := []*computepb.NetworkInterface{ - &networkInterface, - } - sourceImage := "projects/debian-cloud/global/images/family/debian-10" - initializeParams := &computepb.AttachedDiskInitializeParams{ - SourceImage: &sourceImage, - } diskType := computepb.AttachedDisk_PERSISTENT - disk := computepb.AttachedDisk{ - AutoDelete: proto.Bool(true), - Boot: proto.Bool(true), - Type: &diskType, - InitializeParams: initializeParams, - } - disks := []*computepb.AttachedDisk{ - &disk, - } instance := &computepb.Instance{ - Name: &name, - Description: &description, - MachineType: &machineType, - Disks: disks, - NetworkInterfaces: interfaces, + 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: &diskType, + InitializeParams: &computepb.AttachedDiskInitializeParams{ + SourceImage: proto.String("projects/debian-cloud/global/images/family/debian-10"), + }, + }, + }, + NetworkInterfaces: []*computepb.NetworkInterface{ + { + AccessConfigs: []*computepb.AccessConfig{ + { + Name: proto.String("default"), + }, + }, + }, + }, } - createRequest := &computepb.InsertInstanceRequest{ Project: projectId, Zone: defaultZone, @@ -172,44 +158,35 @@ func TestCreateGetRemoveSecurityPolicies(t *testing.T) { if err != nil { t.Fatal(err) } - defaultDescription := "default rule" - description := "test rule" - defaultPriority := int32(2147483647) - priority := int32(0) action := "allow" - - srcIpRanges := []string{ - "*", - } - config := &computepb.SecurityPolicyRuleMatcherConfig{ - SrcIpRanges: srcIpRanges, - } versionExpr := computepb.SecurityPolicyRuleMatcher_SRC_IPS_V1 matcher := &computepb.SecurityPolicyRuleMatcher{ - Config: config, + Config: &computepb.SecurityPolicyRuleMatcherConfig{ + SrcIpRanges: []string{ + "*", + }, + }, VersionedExpr: &versionExpr, } securityPolicyRule := &computepb.SecurityPolicyRule{ Action: &action, - Priority: &priority, - Description: &description, + Priority: proto.Int32(0), + Description: proto.String("test rule"), Match: matcher, } securityPolicyRuleDefault := &computepb.SecurityPolicyRule{ Action: &action, - Priority: &defaultPriority, - Description: &defaultDescription, + Priority: proto.Int32(2147483647), + Description: proto.String("default rule"), Match: matcher, } - rules := []*computepb.SecurityPolicyRule{ - securityPolicyRule, - securityPolicyRuleDefault, - } - securityPolicy := &computepb.SecurityPolicy{ - Name: &name, - Rules: rules, + Name: &name, + Rules: []*computepb.SecurityPolicyRule{ + securityPolicyRule, + securityPolicyRuleDefault, + }, } insertRequest := &computepb.InsertSecurityPolicyRequest{ @@ -233,7 +210,7 @@ func TestCreateGetRemoveSecurityPolicies(t *testing.T) { defer ForceDeleteSecurityPolicy(ctx, name, c) removeRuleRequest := &computepb.RemoveRuleSecurityPolicyRequest{ - Priority: &priority, + Priority: proto.Int32(0), Project: projectId, SecurityPolicy: name, } From 4b71c2c5815ef41b965bd54571a2fc18a051769d Mon Sep 17 00:00:00 2001 From: Georgy Ekkert Date: Wed, 14 Jul 2021 13:13:54 -0700 Subject: [PATCH 06/11] tests: review fixes --- compute/apiv1/smoke_test.go | 58 ++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/compute/apiv1/smoke_test.go b/compute/apiv1/smoke_test.go index 667ac0adb38..8fcbeee36bb 100644 --- a/compute/apiv1/smoke_test.go +++ b/compute/apiv1/smoke_test.go @@ -44,40 +44,38 @@ func TestCreateGetListInstance(t *testing.T) { if err != nil { t.Fatal(err) } - diskType := computepb.AttachedDisk_PERSISTENT - instance := &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: &diskType, - InitializeParams: &computepb.AttachedDiskInitializeParams{ - SourceImage: proto.String("projects/debian-cloud/global/images/family/debian-10"), + 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"), + NetworkInterfaces: []*computepb.NetworkInterface{ + { + AccessConfigs: []*computepb.AccessConfig{ + { + Name: proto.String("default"), + }, }, }, }, }, } - createRequest := &computepb.InsertInstanceRequest{ - Project: projectId, - Zone: defaultZone, - InstanceResource: instance, - } insert, err := c.Insert(ctx, createRequest) if err != nil { - t.Error(err) + t.Fatal(err) } waitZonalRequest := &computepb.WaitZoneOperationRequest{ @@ -89,7 +87,6 @@ func TestCreateGetListInstance(t *testing.T) { if err != nil { t.Error(err) } - fmt.Printf("Inserted instance named %s\n", name) defer ForceDeleteInstance(ctx, name, c) getRequest := &computepb.GetInstanceRequest{ @@ -101,8 +98,12 @@ func TestCreateGetListInstance(t *testing.T) { if err != nil { t.Error(err) } - testutil.Equal(name, get.GetName()) - testutil.Equal("тест", get.GetDescription()) + if name != get.GetName() { + 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, @@ -236,6 +237,9 @@ func TestCreateGetRemoveSecurityPolicies(t *testing.T) { 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()))) + } testutil.Equal(1, len(get.GetRules())) deleteRequest := &computepb.DeleteSecurityPolicyRequest{ From 3c52d7a5dbdae5f8a95c74fa8067d949a8be7897 Mon Sep 17 00:00:00 2001 From: Georgy Ekkert Date: Wed, 14 Jul 2021 13:16:43 -0700 Subject: [PATCH 07/11] tests: review fixes --- compute/apiv1/smoke_test.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/compute/apiv1/smoke_test.go b/compute/apiv1/smoke_test.go index 8fcbeee36bb..63c61e1a652 100644 --- a/compute/apiv1/smoke_test.go +++ b/compute/apiv1/smoke_test.go @@ -160,14 +160,13 @@ func TestCreateGetRemoveSecurityPolicies(t *testing.T) { t.Fatal(err) } action := "allow" - versionExpr := computepb.SecurityPolicyRuleMatcher_SRC_IPS_V1 matcher := &computepb.SecurityPolicyRuleMatcher{ Config: &computepb.SecurityPolicyRuleMatcherConfig{ SrcIpRanges: []string{ "*", }, }, - VersionedExpr: &versionExpr, + VersionedExpr: computepb.SecurityPolicyRuleMatcher_SRC_IPS_V1.Enum(), } securityPolicyRule := &computepb.SecurityPolicyRule{ Action: &action, @@ -181,18 +180,15 @@ func TestCreateGetRemoveSecurityPolicies(t *testing.T) { Description: proto.String("default rule"), Match: matcher, } - - securityPolicy := &computepb.SecurityPolicy{ - Name: &name, - Rules: []*computepb.SecurityPolicyRule{ - securityPolicyRule, - securityPolicyRuleDefault, - }, - } - insertRequest := &computepb.InsertSecurityPolicyRequest{ Project: projectId, - SecurityPolicyResource: securityPolicy, + SecurityPolicyResource: &computepb.SecurityPolicy{ + Name: &name, + Rules: []*computepb.SecurityPolicyRule{ + securityPolicyRule, + securityPolicyRuleDefault, + }, + }, } insert, err := c.Insert(ctx, insertRequest) if err != nil { @@ -207,7 +203,6 @@ func TestCreateGetRemoveSecurityPolicies(t *testing.T) { if err != nil { t.Error(err) } - fmt.Printf("Inserted security policy named %s\n", name) defer ForceDeleteSecurityPolicy(ctx, name, c) removeRuleRequest := &computepb.RemoveRuleSecurityPolicyRequest{ @@ -240,7 +235,6 @@ func TestCreateGetRemoveSecurityPolicies(t *testing.T) { if len(get.GetRules()) != 1 { t.Fatal(fmt.Sprintf("expected count for rules: %d, got: %d", 1, len(get.GetRules()))) } - testutil.Equal(1, len(get.GetRules())) deleteRequest := &computepb.DeleteSecurityPolicyRequest{ Project: projectId, From 2a400b6dbb2326c9c157e81b7badaeb95208a367 Mon Sep 17 00:00:00 2001 From: Georgy Ekkert Date: Wed, 14 Jul 2021 13:18:18 -0700 Subject: [PATCH 08/11] tests: review fixes --- compute/apiv1/smoke_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compute/apiv1/smoke_test.go b/compute/apiv1/smoke_test.go index 63c61e1a652..d0621585591 100644 --- a/compute/apiv1/smoke_test.go +++ b/compute/apiv1/smoke_test.go @@ -192,7 +192,7 @@ func TestCreateGetRemoveSecurityPolicies(t *testing.T) { } insert, err := c.Insert(ctx, insertRequest) if err != nil { - t.Error(err) + t.Fatal(err) } waitGlobalRequest := &computepb.WaitGlobalOperationRequest{ From 3333a2c132febe8c67b0c6230582cac2ad702d92 Mon Sep 17 00:00:00 2001 From: Georgy Ekkert Date: Thu, 15 Jul 2021 10:52:00 -0700 Subject: [PATCH 09/11] tests: formatting --- compute/apiv1/smoke_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compute/apiv1/smoke_test.go b/compute/apiv1/smoke_test.go index d0621585591..93352889c9f 100644 --- a/compute/apiv1/smoke_test.go +++ b/compute/apiv1/smoke_test.go @@ -181,7 +181,7 @@ func TestCreateGetRemoveSecurityPolicies(t *testing.T) { Match: matcher, } insertRequest := &computepb.InsertSecurityPolicyRequest{ - Project: projectId, + Project: projectId, SecurityPolicyResource: &computepb.SecurityPolicy{ Name: &name, Rules: []*computepb.SecurityPolicyRule{ From 4850139506cb1aa26d24452b3481b4893a0fe5af Mon Sep 17 00:00:00 2001 From: Georgy Ekkert Date: Thu, 15 Jul 2021 13:34:23 -0700 Subject: [PATCH 10/11] tests: goimports --- compute/apiv1/smoke_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/compute/apiv1/smoke_test.go b/compute/apiv1/smoke_test.go index 93352889c9f..24c175319d7 100644 --- a/compute/apiv1/smoke_test.go +++ b/compute/apiv1/smoke_test.go @@ -17,13 +17,14 @@ package compute import ( - "cloud.google.com/go/internal/testutil" - "cloud.google.com/go/internal/uid" "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" - "testing" ) var projectId = testutil.ProjID() From 428e119e724e6267cf7073be471512b659a014fd Mon Sep 17 00:00:00 2001 From: Georgy Ekkert Date: Thu, 15 Jul 2021 14:03:32 -0700 Subject: [PATCH 11/11] tests: remove yoda condition --- compute/apiv1/smoke_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compute/apiv1/smoke_test.go b/compute/apiv1/smoke_test.go index 24c175319d7..ddde5a96d60 100644 --- a/compute/apiv1/smoke_test.go +++ b/compute/apiv1/smoke_test.go @@ -99,10 +99,10 @@ func TestCreateGetListInstance(t *testing.T) { if err != nil { t.Error(err) } - if name != get.GetName() { + if get.GetName() != name { t.Fatal(fmt.Sprintf("expected instance name: %s, got: %s", name, get.GetName())) } - if "тест" != get.GetDescription() { + if get.GetDescription() != "тест" { t.Fatal(fmt.Sprintf("expected instance description: %s, got: %s", "тест", get.GetDescription())) } listRequest := &computepb.ListInstancesRequest{