@@ -28,7 +28,9 @@ import (
2828 "cloud.google.com/go/compute/apiv1/computepb"
2929 kms "cloud.google.com/go/kms/apiv1"
3030 "cloud.google.com/go/kms/apiv1/kmspb"
31+ . "github.com/onsi/ginkgo/v2"
3132 "github.com/onsi/ginkgo/v2/dsl/core"
33+ . "github.com/onsi/gomega"
3234 "google.golang.org/api/googleapi"
3335 "google.golang.org/api/iterator"
3436 "k8s.io/apimachinery/pkg/util/wait"
@@ -37,7 +39,6 @@ import (
3739)
3840
3941type GCPAction struct {
40- t core.GinkgoTInterface
4142 projectID string
4243 network * gcpNetwork
4344
@@ -60,10 +61,7 @@ const (
6061 googleKeyName = "projects/atlasoperator/locations/global/keyRings/atlas-operator-test-key-ring/cryptoKeys/encryption-at-rest-test-key"
6162)
6263
63- func (a * GCPAction ) InitNetwork (vpcName , region string , subnets map [string ]string , cleanup bool ) (string , error ) {
64- a .t .Helper ()
65- ctx := context .Background ()
66-
64+ func (a * GCPAction ) InitNetwork (ctx context.Context , vpcName , region string , subnets map [string ]string , cleanup bool ) (string , error ) {
6765 vpc , err := a .findVPC (ctx , vpcName )
6866 if err != nil {
6967 return "" , err
@@ -77,11 +75,8 @@ func (a *GCPAction) InitNetwork(vpcName, region string, subnets map[string]strin
7775 }
7876
7977 if cleanup {
80- a .t .Cleanup (func () {
81- err = a .deleteVPC (ctx , vpcName )
82- if err != nil {
83- a .t .Error (err )
84- }
78+ DeferCleanup (func (ctx SpecContext ) error {
79+ return a .deleteVPC (ctx , vpcName )
8580 })
8681 }
8782
@@ -97,11 +92,8 @@ func (a *GCPAction) InitNetwork(vpcName, region string, subnets map[string]strin
9792 }
9893
9994 if cleanup {
100- a .t .Cleanup (func () {
101- err = a .deleteSubnet (ctx , name , region )
102- if err != nil {
103- a .t .Error (err )
104- }
95+ DeferCleanup (func (ctx SpecContext ) error {
96+ return a .deleteSubnet (ctx , name , region )
10597 })
10698 }
10799 }
@@ -116,8 +108,6 @@ func (a *GCPAction) InitNetwork(vpcName, region string, subnets map[string]strin
116108}
117109
118110func (a * GCPAction ) CreatePrivateEndpoint (ctx context.Context , name , region , subnet , target string , index int ) (string , string , error ) {
119- a .t .Helper ()
120-
121111 address := fmt .Sprintf ("%s-%s-ip-%d" , googleConnectPrefix , name , index )
122112 rule := fmt .Sprintf ("%s-%s-fr-%d" , googleConnectPrefix , name , index )
123113
@@ -126,49 +116,38 @@ func (a *GCPAction) CreatePrivateEndpoint(ctx context.Context, name, region, sub
126116 return "" , "" , err
127117 }
128118
129- a .t .Cleanup (func () {
130- err := a .deleteVirtualAddress (ctx , address , region )
131- if err != nil {
132- a .t .Error (err )
133- }
119+ DeferCleanup (func (ctx SpecContext ) error {
120+ return a .deleteVirtualAddress (ctx , address , region )
134121 })
135122
136123 err = a .createForwardRule (ctx , rule , address , region , target )
137124 if err != nil {
138125 return "" , "" , err
139126 }
140127
141- a .t .Cleanup (func () {
142- err = a .deleteForwardRule (ctx , rule , region )
143- if err != nil {
144- a .t .Error (err )
145- }
128+ DeferCleanup (func (ctx SpecContext ) error {
129+ return a .deleteForwardRule (ctx , rule , region )
146130 })
147131
148132 return rule , ipAddress , err
149133}
150134
151- func (a * GCPAction ) GetForwardingRule (name , region string , suffixIndex int ) (* computepb.ForwardingRule , error ) {
152- a .t .Helper ()
153-
135+ func (a * GCPAction ) GetForwardingRule (ctx context.Context , name , region string , suffixIndex int ) (* computepb.ForwardingRule , error ) {
154136 ruleRequest := & computepb.GetForwardingRuleRequest {
155137 Project : a .projectID ,
156138 ForwardingRule : fmt .Sprintf ("%s-%s-fr-%d" , googleConnectPrefix , name , suffixIndex ),
157139 Region : region ,
158140 }
159141
160- rule , err := a .forwardRuleClient .Get (context . Background () , ruleRequest )
142+ rule , err := a .forwardRuleClient .Get (ctx , ruleRequest )
161143 if err != nil {
162144 return nil , err
163145 }
164146
165147 return rule , nil
166148}
167149
168- func (a * GCPAction ) CreateNetworkPeering (vpcName , peerProjectID , peerVPCName string ) error {
169- a .t .Helper ()
170- ctx := context .Background ()
171-
150+ func (a * GCPAction ) CreateNetworkPeering (ctx context.Context , vpcName , peerProjectID , peerVPCName string ) error {
172151 peerName := "atlas-networking-peering"
173152 peerRequest := & computepb.AddPeeringNetworkRequest {
174153 Project : a .projectID ,
@@ -192,19 +171,14 @@ func (a *GCPAction) CreateNetworkPeering(vpcName, peerProjectID, peerVPCName str
192171 return err
193172 }
194173
195- a .t .Cleanup (func () {
196- err = a .deleteVPCPeering (ctx , vpcName , peerName )
197- if err != nil {
198- a .t .Error (err )
199- }
174+ DeferCleanup (func (ctx SpecContext ) error {
175+ return a .deleteVPCPeering (ctx , vpcName , peerName )
200176 })
201177
202178 return nil
203179}
204180
205181func (a * GCPAction ) findVPC (ctx context.Context , vpcName string ) (* computepb.Network , error ) {
206- a .t .Helper ()
207-
208182 vpcRequest := & computepb.GetNetworkRequest {
209183 Project : a .projectID ,
210184 Network : vpcName ,
@@ -223,8 +197,6 @@ func (a *GCPAction) findVPC(ctx context.Context, vpcName string) (*computepb.Net
223197}
224198
225199func (a * GCPAction ) createVPC (ctx context.Context , vpcName string ) error {
226- a .t .Helper ()
227-
228200 vpcRequest := & computepb.InsertNetworkRequest {
229201 Project : a .projectID ,
230202 NetworkResource : & computepb.Network {
@@ -248,8 +220,6 @@ func (a *GCPAction) createVPC(ctx context.Context, vpcName string) error {
248220}
249221
250222func (a * GCPAction ) deleteVPC (ctx context.Context , vpcName string ) error {
251- a .t .Helper ()
252-
253223 vpcRequest := & computepb.DeleteNetworkRequest {
254224 Project : a .projectID ,
255225 Network : vpcName ,
@@ -269,8 +239,6 @@ func (a *GCPAction) deleteVPC(ctx context.Context, vpcName string) error {
269239}
270240
271241func (a * GCPAction ) getSubnets (ctx context.Context , region string ) (map [string ]string , error ) {
272- a .t .Helper ()
273-
274242 subnetRequest := & computepb.ListSubnetworksRequest {
275243 Project : a .projectID ,
276244 Region : region ,
@@ -294,8 +262,6 @@ func (a *GCPAction) getSubnets(ctx context.Context, region string) (map[string]s
294262}
295263
296264func (a * GCPAction ) createSubnet (ctx context.Context , vpcName , subnetName , ipRange , region string ) error {
297- a .t .Helper ()
298-
299265 subnetRequest := & computepb.InsertSubnetworkRequest {
300266 Project : a .projectID ,
301267 Region : region ,
@@ -320,8 +286,6 @@ func (a *GCPAction) createSubnet(ctx context.Context, vpcName, subnetName, ipRan
320286}
321287
322288func (a * GCPAction ) deleteSubnet (ctx context.Context , subnetName , region string ) error {
323- a .t .Helper ()
324-
325289 subnetRequest := & computepb.DeleteSubnetworkRequest {
326290 Subnetwork : subnetName ,
327291 Project : a .projectID ,
@@ -374,9 +338,7 @@ func (a *GCPAction) randomIP(subnet string) string {
374338 const maxRandValue = 256
375339 for {
376340 randNumberBig , err := rand .Int (rand .Reader , big .NewInt (maxRandValue ))
377- if err != nil {
378- a .t .Error (err )
379- }
341+ Expect (err ).NotTo (HaveOccurred ())
380342 randNumber := randNumberBig .String ()
381343 ipParts [3 ] = randNumber
382344 genIP := net .ParseIP (strings .Join (ipParts , "." ))
@@ -388,8 +350,6 @@ func (a *GCPAction) randomIP(subnet string) string {
388350}
389351
390352func (a * GCPAction ) createVirtualAddress (ctx context.Context , ip , name , subnet , region string ) error {
391- a .t .Helper ()
392-
393353 addressRequest := & computepb.InsertAddressRequest {
394354 Project : a .projectID ,
395355 Region : region ,
@@ -422,8 +382,6 @@ func (a *GCPAction) createVirtualAddress(ctx context.Context, ip, name, subnet,
422382}
423383
424384func (a * GCPAction ) deleteVirtualAddress (ctx context.Context , name , region string ) error {
425- a .t .Helper ()
426-
427385 addressRequest := & computepb.DeleteAddressRequest {
428386 Address : name ,
429387 Project : a .projectID ,
@@ -444,8 +402,6 @@ func (a *GCPAction) deleteVirtualAddress(ctx context.Context, name, region strin
444402}
445403
446404func (a * GCPAction ) createForwardRule (ctx context.Context , rule , address , region , target string ) error {
447- a .t .Helper ()
448-
449405 ruleRequest := & computepb.InsertForwardingRuleRequest {
450406 Project : a .projectID ,
451407 Region : region ,
@@ -471,8 +427,6 @@ func (a *GCPAction) createForwardRule(ctx context.Context, rule, address, region
471427}
472428
473429func (a * GCPAction ) deleteForwardRule (ctx context.Context , rule , region string ) error {
474- a .t .Helper ()
475-
476430 addressRequest := & computepb.DeleteForwardingRuleRequest {
477431 ForwardingRule : rule ,
478432 Project : a .projectID ,
@@ -493,8 +447,6 @@ func (a *GCPAction) deleteForwardRule(ctx context.Context, rule, region string)
493447}
494448
495449func (a * GCPAction ) deleteVPCPeering (ctx context.Context , vpcName , peerName string ) error {
496- a .t .Helper ()
497-
498450 peerRequest := & computepb.RemovePeeringNetworkRequest {
499451 Project : a .projectID ,
500452 Network : vpcName ,
@@ -516,23 +468,16 @@ func (a *GCPAction) deleteVPCPeering(ctx context.Context, vpcName, peerName stri
516468 return nil
517469}
518470
519- func (a * GCPAction ) CreateKMS () (string , error ) {
520- a .t .Helper ()
521-
522- ctx := context .Background ()
523-
471+ func (a * GCPAction ) CreateKMS (ctx context.Context ) (string , error ) {
524472 result , err := a .keyManagementClient .CreateCryptoKeyVersion (ctx , & kmspb.CreateCryptoKeyVersionRequest {
525473 Parent : googleKeyName ,
526474 })
527475 if err != nil {
528476 return "" , err
529477 }
530478
531- a .t .Cleanup (func () {
532- err = a .deleteKMS (ctx , result .Name )
533- if err != nil {
534- a .t .Error (err )
535- }
479+ DeferCleanup (func (ctx SpecContext ) error {
480+ return a .deleteKMS (ctx , result .Name )
536481 })
537482
538483 ver := strings .Split (result .Name , "/" )
@@ -550,8 +495,6 @@ func (a *GCPAction) CreateKMS() (string, error) {
550495}
551496
552497func (a * GCPAction ) deleteKMS (ctx context.Context , keyName string ) error {
553- a .t .Helper ()
554-
555498 req := & kmspb.DestroyCryptoKeyVersionRequest {
556499 Name : keyName ,
557500 }
@@ -564,11 +507,9 @@ func (a *GCPAction) deleteKMS(ctx context.Context, keyName string) error {
564507 return nil
565508}
566509
567- func NewGCPAction (t core.GinkgoTInterface , projectID string ) (* GCPAction , error ) {
510+ func NewGCPAction (ctx context. Context , t core.GinkgoTInterface , projectID string ) (* GCPAction , error ) {
568511 t .Helper ()
569512
570- ctx := context .Background ()
571-
572513 networkClient , err := compute .NewNetworksRESTClient (ctx )
573514 if err != nil {
574515 return nil , err
@@ -595,7 +536,6 @@ func NewGCPAction(t core.GinkgoTInterface, projectID string) (*GCPAction, error)
595536 }
596537
597538 return & GCPAction {
598- t : t ,
599539 projectID : projectID ,
600540
601541 networkClient : networkClient ,
0 commit comments