Fix error that occured in e2e test

This commit is contained in:
katexochen 2022-04-26 13:22:57 +02:00 committed by Paul Meyer
parent 1317fc2bb2
commit d5c7bb6078
3 changed files with 27 additions and 16 deletions

View File

@ -124,7 +124,7 @@ func (c *Client) TerminateVPCs(ctx context.Context) error {
return errors.New("client has firewalls, which must be deleted first")
}
if err := c.terminateSubnets(ctx); err != nil {
if err := c.terminateSubnet(ctx); err != nil {
return err
}
@ -183,23 +183,18 @@ func (c *Client) createSubnet(ctx context.Context, name, cidr, network, secondar
return c.subnetworksAPI.Insert(ctx, req)
}
func (c *Client) terminateSubnets(ctx context.Context) error {
var op Operation
var err error
if c.subnetwork != "" {
op, err = c.terminateSubnet(ctx, c.subnetwork)
if err != nil {
return err
}
func (c *Client) terminateSubnet(ctx context.Context) error {
if c.subnetwork == "" {
return nil
}
return c.waitForOperations(ctx, []Operation{op})
}
func (c *Client) terminateSubnet(ctx context.Context, name string) (Operation, error) {
req := &computepb.DeleteSubnetworkRequest{
Project: c.project,
Region: c.region,
Subnetwork: name,
Subnetwork: c.subnetwork,
}
return c.subnetworksAPI.Delete(ctx, req)
op, err := c.subnetworksAPI.Delete(ctx, req)
if err != nil {
return err
}
return c.waitForOperations(ctx, []Operation{op})
}

View File

@ -97,6 +97,7 @@ func TestTerminateVPCs(t *testing.T) {
networksAPI networksAPI
subnetworksAPI subnetworksAPI
firewalls []string
subnetwork string
wantErr bool
}{
"successful terminate": {
@ -104,6 +105,14 @@ func TestTerminateVPCs(t *testing.T) {
operationRegionAPI: stubOperationRegionAPI{},
networksAPI: stubNetworksAPI{},
subnetworksAPI: stubSubnetworksAPI{},
subnetwork: "subnetwork-id-1",
},
"subnetwork empty": {
operationGlobalAPI: stubOperationGlobalAPI{},
operationRegionAPI: stubOperationRegionAPI{},
networksAPI: stubNetworksAPI{},
subnetworksAPI: stubSubnetworksAPI{},
subnetwork: "",
},
"failed wait global op": {
operationGlobalAPI: stubOperationGlobalAPI{waitErr: someErr},
@ -111,6 +120,7 @@ func TestTerminateVPCs(t *testing.T) {
networksAPI: stubNetworksAPI{},
subnetworksAPI: stubSubnetworksAPI{},
wantErr: true,
subnetwork: "subnetwork-id-1",
},
"failed delete networks": {
operationGlobalAPI: stubOperationGlobalAPI{},
@ -118,6 +128,7 @@ func TestTerminateVPCs(t *testing.T) {
networksAPI: stubNetworksAPI{deleteErr: someErr},
subnetworksAPI: stubSubnetworksAPI{},
wantErr: true,
subnetwork: "subnetwork-id-1",
},
"failed delete subnetworks": {
operationGlobalAPI: stubOperationGlobalAPI{},
@ -125,6 +136,7 @@ func TestTerminateVPCs(t *testing.T) {
networksAPI: stubNetworksAPI{},
subnetworksAPI: stubSubnetworksAPI{deleteErr: someErr},
wantErr: true,
subnetwork: "subnetwork-id-1",
},
"must delete firewalls first": {
firewalls: []string{"firewall-1", "firewall-2"},
@ -133,6 +145,7 @@ func TestTerminateVPCs(t *testing.T) {
networksAPI: stubNetworksAPI{},
subnetworksAPI: stubSubnetworksAPI{},
wantErr: true,
subnetwork: "subnetwork-id-1",
},
}
@ -152,7 +165,7 @@ func TestTerminateVPCs(t *testing.T) {
subnetworksAPI: tc.subnetworksAPI,
firewalls: tc.firewalls,
network: "network-id-1",
subnetwork: "subnetwork-id-1",
subnetwork: tc.subnetwork,
}
if tc.wantErr {

View File

@ -2,6 +2,7 @@ package client
import (
"context"
"errors"
"fmt"
computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
@ -12,6 +13,8 @@ import (
func (c *Client) waitForOperations(ctx context.Context, ops []Operation) error {
for _, op := range ops {
switch {
case op.Proto() == nil:
return errors.New("proto of operation is nil")
case op.Proto().Zone != nil:
if err := c.waitForZoneOperation(ctx, op); err != nil {
return err