Skip to content

Commit 33c9b67

Browse files
committed
wire context to gcp tests
1 parent 04f1657 commit 33c9b67

File tree

5 files changed

+30
-90
lines changed

5 files changed

+30
-90
lines changed

test/e2e/encryption_at_rest_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func encryptionAtRestFlow(ctx context.Context, userData *model.TestDataProvider,
151151

152152
fillKMSforAWS(ctx, userData, &encAtRest, aRole.AtlasAWSAccountArn, aRole.IamAssumedRoleArn)
153153
fillVaultforAzure(userData, &encAtRest)
154-
fillKMSforGCP(userData, &encAtRest)
154+
fillKMSforGCP(ctx, userData, &encAtRest)
155155

156156
Expect(userData.K8SClient.Get(userData.Context, types.NamespacedName{
157157
Name: userData.Project.Name,
@@ -268,15 +268,15 @@ func fillVaultforAzure(userData *model.TestDataProvider, encAtRest *akov2.Encryp
268268
}
269269
}
270270

271-
func fillKMSforGCP(userData *model.TestDataProvider, encAtRest *akov2.EncryptionAtRest) {
271+
func fillKMSforGCP(ctx context.Context, userData *model.TestDataProvider, encAtRest *akov2.EncryptionAtRest) {
272272
if (encAtRest.GoogleCloudKms == akov2.GoogleCloudKms{}) {
273273
return
274274
}
275275

276-
gcpAction, err := cloud.NewGCPAction(GinkgoT(), cloud.GoogleProjectID)
276+
gcpAction, err := cloud.NewGCPAction(ctx, GinkgoT(), cloud.GoogleProjectID)
277277
Expect(err).ToNot(HaveOccurred())
278278

279-
keyID, err := gcpAction.CreateKMS()
279+
keyID, err := gcpAction.CreateKMS(ctx)
280280
Expect(err).ToNot(HaveOccurred())
281281

282282
secret := &corev1.Secret{

test/e2e/private_link_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func prepareProviderAction(ctx context.Context) (*cloud.ProviderAction, error) {
292292
return nil, err
293293
}
294294

295-
gcp, err := cloud.NewGCPAction(t, cloud.GoogleProjectID)
295+
gcp, err := cloud.NewGCPAction(ctx, t, cloud.GoogleProjectID)
296296
if err != nil {
297297
return nil, err
298298
}

test/helper/e2e/actions/cloud/gcp.go

Lines changed: 21 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3941
type 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

118110
func (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

205181
func (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

225199
func (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

250222
func (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

271241
func (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

296264
func (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

322288
func (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

390352
func (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

424384
func (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

446404
func (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

473429
func (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

495449
func (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

552497
func (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,

test/helper/e2e/actions/cloud/gcp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestCreateUsedVirtualAddress(t *testing.T) {
2929
}
3030
ctx := context.Background()
3131
gt := ginkgo.GinkgoT()
32-
ga, err := NewGCPAction(gt, GoogleProjectID)
32+
ga, err := NewGCPAction(ctx, gt, GoogleProjectID)
3333
require.NoError(t, err)
3434

3535
err = ga.createVirtualAddress(ctx, "10.0.0.155", "name1", Subnet2Name, GCPRegion)

test/helper/e2e/actions/cloud/provider.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func (a *ProviderAction) SetupNetwork(ctx context.Context, providerName provider
177177
Expect(err).To(BeNil())
178178
return id
179179
case provider.ProviderGCP:
180-
id, err := a.gcpProvider.InitNetwork(a.gcpConfig.VPC, a.gcpConfig.Region, a.gcpConfig.Subnets, a.gcpConfig.EnableCleanup)
180+
id, err := a.gcpProvider.InitNetwork(ctx, a.gcpConfig.VPC, a.gcpConfig.Region, a.gcpConfig.Subnets, a.gcpConfig.EnableCleanup)
181181
Expect(err).To(BeNil())
182182
return id
183183
case provider.ProviderAzure:
@@ -314,7 +314,7 @@ func (a *ProviderAction) ValidatePrivateEndpointStatus(ctx context.Context, prov
314314
case provider.ProviderGCP:
315315
res := true
316316
for i := 0; i < gcpNumAttachments; i++ {
317-
rule, err := a.gcpProvider.GetForwardingRule(endpoint, region, i)
317+
rule, err := a.gcpProvider.GetForwardingRule(ctx, endpoint, region, i)
318318
g.Expect(err).To(BeNil())
319319

320320
res = res && (*rule.PscConnectionStatus == "ACCEPTED")
@@ -337,7 +337,7 @@ func (a *ProviderAction) SetupNetworkPeering(ctx context.Context, providerName p
337337
case provider.ProviderAWS:
338338
Expect(a.awsProvider.AcceptVpcPeeringConnection(ctx, peerID, a.awsConfig.Region)).To(Succeed())
339339
case provider.ProviderGCP:
340-
Expect(a.gcpProvider.CreateNetworkPeering(a.gcpConfig.VPC, peerID, peerVPC)).To(Succeed())
340+
Expect(a.gcpProvider.CreateNetworkPeering(ctx, a.gcpConfig.VPC, peerID, peerVPC)).To(Succeed())
341341
}
342342
}
343343

0 commit comments

Comments
 (0)