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") 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 return err
} }
@ -183,23 +183,18 @@ func (c *Client) createSubnet(ctx context.Context, name, cidr, network, secondar
return c.subnetworksAPI.Insert(ctx, req) return c.subnetworksAPI.Insert(ctx, req)
} }
func (c *Client) terminateSubnets(ctx context.Context) error { func (c *Client) terminateSubnet(ctx context.Context) error {
var op Operation if c.subnetwork == "" {
var err error return nil
if c.subnetwork != "" {
op, err = c.terminateSubnet(ctx, c.subnetwork)
if err != nil {
return err
}
} }
return c.waitForOperations(ctx, []Operation{op})
}
func (c *Client) terminateSubnet(ctx context.Context, name string) (Operation, error) {
req := &computepb.DeleteSubnetworkRequest{ req := &computepb.DeleteSubnetworkRequest{
Project: c.project, Project: c.project,
Region: c.region, 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 networksAPI networksAPI
subnetworksAPI subnetworksAPI subnetworksAPI subnetworksAPI
firewalls []string firewalls []string
subnetwork string
wantErr bool wantErr bool
}{ }{
"successful terminate": { "successful terminate": {
@ -104,6 +105,14 @@ func TestTerminateVPCs(t *testing.T) {
operationRegionAPI: stubOperationRegionAPI{}, operationRegionAPI: stubOperationRegionAPI{},
networksAPI: stubNetworksAPI{}, networksAPI: stubNetworksAPI{},
subnetworksAPI: stubSubnetworksAPI{}, subnetworksAPI: stubSubnetworksAPI{},
subnetwork: "subnetwork-id-1",
},
"subnetwork empty": {
operationGlobalAPI: stubOperationGlobalAPI{},
operationRegionAPI: stubOperationRegionAPI{},
networksAPI: stubNetworksAPI{},
subnetworksAPI: stubSubnetworksAPI{},
subnetwork: "",
}, },
"failed wait global op": { "failed wait global op": {
operationGlobalAPI: stubOperationGlobalAPI{waitErr: someErr}, operationGlobalAPI: stubOperationGlobalAPI{waitErr: someErr},
@ -111,6 +120,7 @@ func TestTerminateVPCs(t *testing.T) {
networksAPI: stubNetworksAPI{}, networksAPI: stubNetworksAPI{},
subnetworksAPI: stubSubnetworksAPI{}, subnetworksAPI: stubSubnetworksAPI{},
wantErr: true, wantErr: true,
subnetwork: "subnetwork-id-1",
}, },
"failed delete networks": { "failed delete networks": {
operationGlobalAPI: stubOperationGlobalAPI{}, operationGlobalAPI: stubOperationGlobalAPI{},
@ -118,6 +128,7 @@ func TestTerminateVPCs(t *testing.T) {
networksAPI: stubNetworksAPI{deleteErr: someErr}, networksAPI: stubNetworksAPI{deleteErr: someErr},
subnetworksAPI: stubSubnetworksAPI{}, subnetworksAPI: stubSubnetworksAPI{},
wantErr: true, wantErr: true,
subnetwork: "subnetwork-id-1",
}, },
"failed delete subnetworks": { "failed delete subnetworks": {
operationGlobalAPI: stubOperationGlobalAPI{}, operationGlobalAPI: stubOperationGlobalAPI{},
@ -125,6 +136,7 @@ func TestTerminateVPCs(t *testing.T) {
networksAPI: stubNetworksAPI{}, networksAPI: stubNetworksAPI{},
subnetworksAPI: stubSubnetworksAPI{deleteErr: someErr}, subnetworksAPI: stubSubnetworksAPI{deleteErr: someErr},
wantErr: true, wantErr: true,
subnetwork: "subnetwork-id-1",
}, },
"must delete firewalls first": { "must delete firewalls first": {
firewalls: []string{"firewall-1", "firewall-2"}, firewalls: []string{"firewall-1", "firewall-2"},
@ -133,6 +145,7 @@ func TestTerminateVPCs(t *testing.T) {
networksAPI: stubNetworksAPI{}, networksAPI: stubNetworksAPI{},
subnetworksAPI: stubSubnetworksAPI{}, subnetworksAPI: stubSubnetworksAPI{},
wantErr: true, wantErr: true,
subnetwork: "subnetwork-id-1",
}, },
} }
@ -152,7 +165,7 @@ func TestTerminateVPCs(t *testing.T) {
subnetworksAPI: tc.subnetworksAPI, subnetworksAPI: tc.subnetworksAPI,
firewalls: tc.firewalls, firewalls: tc.firewalls,
network: "network-id-1", network: "network-id-1",
subnetwork: "subnetwork-id-1", subnetwork: tc.subnetwork,
} }
if tc.wantErr { if tc.wantErr {

View File

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