From 836e494783a6c672f99a76c8062ec53aa977f24e Mon Sep 17 00:00:00 2001
From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com>
Date: Thu, 14 Mar 2024 21:10:31 +0000
Subject: [PATCH] Fix e2e test potential tag name race

---
 tests/e2e/requires_aws_api.go | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/tests/e2e/requires_aws_api.go b/tests/e2e/requires_aws_api.go
index 0407c55a07..88628bc4d2 100644
--- a/tests/e2e/requires_aws_api.go
+++ b/tests/e2e/requires_aws_api.go
@@ -16,6 +16,7 @@ package e2e
 
 import (
 	"fmt"
+	"github.com/google/uuid"
 
 	"github.com/aws/aws-sdk-go/aws"
 	"github.com/aws/aws-sdk-go/aws/session"
@@ -36,9 +37,14 @@ import (
 	ebscsidriver "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver"
 )
 
-const testTagName = "testTag"
+const testTagNamePrefix = "testTag"
 const testTagValue = "3.1415926"
 
+// generateTagName appends a random uuid to tag name to prevent clashes on parallel e2e test runs on shared cluster
+func generateTagName() string {
+	return testTagNamePrefix + uuid.NewString()[:8]
+}
+
 func validateEc2Snapshot(ec2Client ec2iface.EC2API, input *ec2.DescribeSnapshotsInput) *ec2.DescribeSnapshotsOutput {
 	describeResult, err := ec2Client.DescribeSnapshots(input)
 	if err != nil {
@@ -78,6 +84,7 @@ var _ = Describe("[ebs-csi-e2e] [single-az] [requires-aws-api] Dynamic Provision
 	ec2Client := ec2.New(session.Must(session.NewSession()))
 
 	It("should create a volume with additional tags", func() {
+		testTag := generateTagName()
 		pods := []testsuites.PodDetails{
 			{
 				Cmd: testsuites.PodCmdWriteToVolume("/mnt/test-1"),
@@ -86,7 +93,7 @@ var _ = Describe("[ebs-csi-e2e] [single-az] [requires-aws-api] Dynamic Provision
 						CreateVolumeParameters: map[string]string{
 							ebscsidriver.VolumeTypeKey: awscloud.VolumeTypeGP3,
 							ebscsidriver.FSTypeKey:     ebscsidriver.FSTypeExt4,
-							ebscsidriver.TagKeyPrefix:  fmt.Sprintf("%s=%s", testTagName, testTagValue),
+							ebscsidriver.TagKeyPrefix:  fmt.Sprintf("%s=%s", testTag, testTagValue),
 						},
 						ClaimSize:   driver.MinimumSizeForVolumeType(awscloud.VolumeTypeGP3),
 						VolumeMount: testsuites.DefaultGeneratedVolumeMount,
@@ -101,7 +108,7 @@ var _ = Describe("[ebs-csi-e2e] [single-az] [requires-aws-api] Dynamic Provision
 				result, err := ec2Client.DescribeVolumes(&ec2.DescribeVolumesInput{
 					Filters: []*ec2.Filter{
 						{
-							Name:   aws.String("tag:" + testTagName),
+							Name:   aws.String("tag:" + testTag),
 							Values: []*string{aws.String(testTagValue)},
 						},
 					},
@@ -119,6 +126,7 @@ var _ = Describe("[ebs-csi-e2e] [single-az] [requires-aws-api] Dynamic Provision
 	})
 
 	It("should create a snapshot with additional tags", func() {
+		testTag := generateTagName()
 		pod := testsuites.PodDetails{
 			Cmd: testsuites.PodCmdWriteToVolume("/mnt/test-1"),
 			Volumes: []testsuites.VolumeDetails{
@@ -150,13 +158,13 @@ var _ = Describe("[ebs-csi-e2e] [single-az] [requires-aws-api] Dynamic Provision
 			Pod:         pod,
 			RestoredPod: restoredPod,
 			Parameters: map[string]string{
-				ebscsidriver.TagKeyPrefix: fmt.Sprintf("%s=%s", testTagName, testTagValue),
+				ebscsidriver.TagKeyPrefix: fmt.Sprintf("%s=%s", testTag, testTagValue),
 			},
 			ValidateFunc: func(_ *volumesnapshotv1.VolumeSnapshot) {
 				validateEc2Snapshot(ec2Client, &ec2.DescribeSnapshotsInput{
 					Filters: []*ec2.Filter{
 						{
-							Name:   aws.String("tag:" + testTagName),
+							Name:   aws.String("tag:" + testTag),
 							Values: []*string{aws.String(testTagValue)},
 						},
 					},