Skip to content

Commit

Permalink
chore: Nuke stale objects (#2869)
Browse files Browse the repository at this point in the history
Cleaning old test objects (warehouses and databases for now)
  • Loading branch information
sfc-gh-asawicki authored Jun 17, 2024
1 parent 3c11953 commit 9c4a117
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ sweep: ## destroy the whole architecture; USE ONLY FOR DEVELOPMENT ACCOUNTS
@echo "Are you sure? [y/n]" >&2
@read -r REPLY; \
if echo "$$REPLY" | grep -qG "^[yY]$$"; then \
TEST_SF_TF_ENABLE_SWEEP=1 go test -timeout 300s -run ^TestSweepAll ./pkg/sdk -v; \
TEST_SF_TF_ENABLE_SWEEP=1 go test -timeout 300s -run "^(TestSweepAll|Test_Sweeper_NukeStaleObjects)" ./pkg/sdk -v; \
else echo "Aborting..."; \
fi;

Expand Down
95 changes: 95 additions & 0 deletions pkg/sdk/sweepers_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package sdk

import (
"context"
"fmt"
"log"
"testing"
"time"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs"
Expand Down Expand Up @@ -29,3 +33,94 @@ func TestSweepAll(t *testing.T) {
assert.NoError(t, err)
})
}

func Test_Sweeper_NukeStaleObjects(t *testing.T) {
_ = testenvs.GetOrSkipTest(t, testenvs.EnableSweep)

t.Run("sweep integration test precreated objects", func(t *testing.T) {
client := testClient(t)
secondaryClient := testSecondaryClient(t)

err := nukeWarehouses(client, "int_test_wh_%")()
assert.NoError(t, err)

err = nukeWarehouses(secondaryClient, "int_test_wh_%")()
assert.NoError(t, err)

err = nukeDatabases(client, "int_test_db_%")()
assert.NoError(t, err)

err = nukeDatabases(secondaryClient, "int_test_db_%")()
assert.NoError(t, err)
})

t.Run("sweep acceptance tests precreated objects", func(t *testing.T) {
client := testClient(t)
secondaryClient := testSecondaryClient(t)

err := nukeWarehouses(client, "acc_test_wh_%")()
assert.NoError(t, err)

err = nukeWarehouses(secondaryClient, "acc_test_wh_%")()
assert.NoError(t, err)

err = nukeDatabases(client, "acc_test_db_%")()
assert.NoError(t, err)

err = nukeDatabases(secondaryClient, "acc_test_db_%")()
assert.NoError(t, err)
})

// TODO [SNOW-955520]: nuke stale objects (e.g. created more than 2 weeks ago)
}

// TODO [SNOW-955520]: generalize nuke methods (sweepers too)
func nukeWarehouses(client *Client, prefix string) func() error {
return func() error {
log.Printf("[DEBUG] Nuking warehouses with prefix %s\n", prefix)
ctx := context.Background()

whs, err := client.Warehouses.Show(ctx, &ShowWarehouseOptions{Like: &Like{Pattern: String(prefix)}})
if err != nil {
return fmt.Errorf("sweeping warehouses ended with error, err = %w", err)
}
log.Printf("[DEBUG] Found %d warehouses matching search criteria\n", len(whs))
for idx, wh := range whs {
log.Printf("[DEBUG] Processing warehouse [%d/%d]: %s...\n", idx+1, len(whs), wh.ID().FullyQualifiedName())
if wh.Name != "SNOWFLAKE" && wh.CreatedOn.Before(time.Now().Add(-4*time.Hour)) {
log.Printf("[DEBUG] Dropping warehouse %s, created at: %s\n", wh.ID().FullyQualifiedName(), wh.CreatedOn.String())
if err := client.Warehouses.Drop(ctx, wh.ID(), &DropWarehouseOptions{IfExists: Bool(true)}); err != nil {
return fmt.Errorf("sweeping warehouse %s ended with error, err = %w", wh.ID().FullyQualifiedName(), err)
}
} else {
log.Printf("[DEBUG] Skipping warehouse %s, created at: %s\n", wh.ID().FullyQualifiedName(), wh.CreatedOn.String())
}
}
return nil
}
}

func nukeDatabases(client *Client, prefix string) func() error {
return func() error {
log.Printf("[DEBUG] Nuking databases with prefix %s\n", prefix)
ctx := context.Background()

dbs, err := client.Databases.Show(ctx, &ShowDatabasesOptions{Like: &Like{Pattern: String(prefix)}})
if err != nil {
return fmt.Errorf("sweeping databases ended with error, err = %w", err)
}
log.Printf("[DEBUG] Found %d databases matching search criteria\n", len(dbs))
for idx, db := range dbs {
log.Printf("[DEBUG] Processing database [%d/%d]: %s...\n", idx+1, len(dbs), db.ID().FullyQualifiedName())
if db.Name != "SNOWFLAKE" && db.CreatedOn.Before(time.Now().Add(-4*time.Hour)) {
log.Printf("[DEBUG] Dropping database %s, created at: %s\n", db.ID().FullyQualifiedName(), db.CreatedOn.String())
if err := client.Databases.Drop(ctx, db.ID(), &DropDatabaseOptions{IfExists: Bool(true)}); err != nil {
return fmt.Errorf("sweeping database %s ended with error, err = %w", db.ID().FullyQualifiedName(), err)
}
} else {
log.Printf("[DEBUG] Skipping database %s, created at: %s\n", db.ID().FullyQualifiedName(), db.CreatedOn.String())
}
}
return nil
}
}

0 comments on commit 9c4a117

Please sign in to comment.