Skip to content

Commit

Permalink
test(bigtable): Clean up test clusters (#9270)
Browse files Browse the repository at this point in the history
* test(bigtable): Clean up test clusters
  • Loading branch information
bhshkh committed Jan 23, 2024
1 parent a5a1604 commit 3213b45
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions bigtable/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
directPathIPV4Prefix = "34.126"
timeUntilResourceCleanup = time.Hour * 12 // 12 hours
prefixOfInstanceResources = "bt-it-"
prefixOfClusterResources = "bt-c-"
)

var (
Expand All @@ -62,6 +63,7 @@ var (
"j§adams": {"gwashington", "tjefferson"},
}

clusterUIDSpace = uid.NewSpace(prefixOfClusterResources, &uid.Options{Short: true})
tableNameSpace = uid.NewSpace("cbt-test", &uid.Options{Short: true})
myTableName = fmt.Sprintf("mytable-%d", time.Now().Unix())
myOtherTableName = fmt.Sprintf("myothertable-%d", time.Now().Unix())
Expand All @@ -81,11 +83,15 @@ func populatePresidentsGraph(table *Table) error {
return nil
}

func generateNewInstanceName() string {
return fmt.Sprintf("%v%d", prefixOfInstanceResources, time.Now().Unix())
}

var instanceToCreate string

func init() {
if runCreateInstanceTests {
instanceToCreate = fmt.Sprintf("bt-it-%d", time.Now().Unix())
instanceToCreate = generateNewInstanceName()
}
}

Expand Down Expand Up @@ -127,16 +133,27 @@ func cleanup(c IntegrationTestConfig) error {
return err
}

for _, info := range instances {
if strings.HasPrefix(info.Name, prefixOfInstanceResources) {
timestamp := info.Name[len(prefixOfInstanceResources):]
for _, instanceInfo := range instances {
if strings.HasPrefix(instanceInfo.Name, prefixOfInstanceResources) {
timestamp := instanceInfo.Name[len(prefixOfInstanceResources):]
t, err := strconv.ParseInt(timestamp, 10, 64)
if err != nil {
return err
}
uT := time.Unix(t, 0)
if time.Now().After(uT.Add(timeUntilResourceCleanup)) {
iac.DeleteInstance(ctx, info.Name)
iac.DeleteInstance(ctx, instanceInfo.Name)
}
} else {
// Delete clusters created in existing instances
clusters, err := iac.Clusters(ctx, instanceInfo.Name)
if err != nil {
return err
}
for _, clusterInfo := range clusters {
if strings.HasPrefix(clusterInfo.Name, prefixOfClusterResources) {
iac.DeleteCluster(ctx, instanceInfo.Name, clusterInfo.Name)
}
}
}
}
Expand Down Expand Up @@ -2378,7 +2395,7 @@ func TestIntegration_AdminUpdateInstanceAndSyncClusters(t *testing.T) {
}
defer iAdminClient.Close()

clusterID := instanceToCreate + "-cluster"
clusterID := clusterUIDSpace.New()

// Create a development instance
conf := &InstanceConf{
Expand Down Expand Up @@ -2457,7 +2474,7 @@ func TestIntegration_AdminUpdateInstanceAndSyncClusters(t *testing.T) {

// Now add a second cluster as the only change. The first cluster must also be provided so
// it is not removed.
clusterID2 := clusterID + "-2"
clusterID2 := clusterUIDSpace.New()
confWithClusters = &InstanceWithClustersConfig{
InstanceID: instanceToCreate,
Clusters: []ClusterConfig{
Expand Down Expand Up @@ -3003,9 +3020,8 @@ func TestIntegration_InstanceUpdate(t *testing.T) {
}

func createInstance(ctx context.Context, testEnv IntegrationEnv, iAdminClient *InstanceAdminClient) (string, string, error) {
diffInstanceId := uid.NewSpace(prefixOfInstanceResources, &uid.Options{Short: true})
diffInstance := diffInstanceId.New()
diffCluster := testEnv.Config().Cluster + "-d"
diffInstance := generateNewInstanceName()
diffCluster := clusterUIDSpace.New()
conf := &InstanceConf{
InstanceId: diffInstance,
ClusterId: diffCluster,
Expand Down Expand Up @@ -3077,8 +3093,7 @@ func TestIntegration_AdminCopyBackup(t *testing.T) {
destIAdminClient1 := srcIAdminClient

// Create a 2nd cluster in 1st destination project
clusterUID := uid.NewSpace("c-", &uid.Options{Short: true})
destProj1Inst1Cl2 := clusterUID.New()
destProj1Inst1Cl2 := clusterUIDSpace.New()
defer func() {
testutil.Retry(t, 3, 2*time.Second, func(r *testutil.R) {
err := destIAdminClient1.DeleteCluster(ctx, destProj1Inst1, destProj1Inst1Cl2)
Expand Down

0 comments on commit 3213b45

Please sign in to comment.