Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(bigtable): Clean up test clusters #9270

Merged
merged 4 commits into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to confirm, is there ever a case where we want to keep the instance and cluster?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to keep the ones created by the integration test.

The ones created by user will not be deleted.
User can specify an existing instance and a existing cluster to use in tests via environment variables:
GCLOUD_TESTS_BIGTABLE_INSTANCE
GCLOUD_TESTS_BIGTABLE_CLUSTER

There are prefix checks at line 137 and 154 to determine whether resource was created by the test

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
Loading