-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tests): add sweeper to remove workspaces (#355)
* chore(tests): add sweeper to remove workspaces Adds sweepers to clean up workspaces in cases where they were not automatically cleaned up. https://developer.hashicorp.com/terraform/plugin/testing/acceptance-tests/sweepers Related to https://linear.app/prefect/issue/PLA-642/ensure-ephemeral-workspaces-are-cleaned-up-consistently * Only set workspace handle filter if provided Only sets the workspace handle filter if one is provided. Otherwise, passes in an empty filter struct. This allows the List method to return all workspaces. Before this change, the empty "Any" field was causing no matches to be returned. This will help us with the sweeper so it can return all workspaces and let us filter the ones that match the prefix we use for acceptance testing. * Change sweeper to list all workspaces The ephemeral workspaces we make during tests have random values. Because sweepers are run in a separate command from the acceptance tests, there's currently no way to persist those unique values between commands so the sweeper can delete those specific workspaces. Instead, as a first iteration, this change lists all workspaces in an account and deletes any that match the acceptance testing prefix we use. This is designed to run either by hand or by CI at a given interval, such as overnight, when other acceptance tests are not running. * Update Makefile to support sweepers * Add sweep arg in Makefile doc * Move sweeper to sweep package Follows other patterns and allows us to test this specific package instead of other packages with other tests. * Split files into sweep.go, sweep_test.go * Update testacc-dev to sweep ./internal/sweep * Add CI workflow to sweep nightly Adds a CI workflow that will run the Terraform sweepers each night. --------- Co-authored-by: Edward Park <[email protected]>
- Loading branch information
1 parent
3ac1fa6
commit 5a6386d
Showing
7 changed files
with
143 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# This workflow runs the Terraform acceptance tests sweepers every day. | ||
name: Acceptance Tests - Sweepers | ||
|
||
"on": | ||
# Run at 7 AM UTC every day (2 AM EST) | ||
schedule: | ||
- cron: '0 7 * * *' | ||
# Run manually | ||
workflow_dispatch: {} | ||
|
||
permissions: {} | ||
|
||
jobs: | ||
sweepers: | ||
permissions: | ||
contents: read | ||
name: Run Sweepers | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version-file: 'go.mod' | ||
- name: Run sweeper tests | ||
run: make testacc-sweepers | ||
env: | ||
PREFECT_API_URL: ${{ secrets.ACC_TEST_PREFECT_API_URL }} | ||
PREFECT_API_KEY: ${{ secrets.ACC_TEST_PREFECT_API_KEY }} | ||
PREFECT_CLOUD_ACCOUNT_ID: ${{ secrets.ACC_TEST_PREFECT_CLOUD_ACCOUNT_ID }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package sweep | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/google/uuid" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/prefecthq/terraform-provider-prefect/internal/testutils" | ||
) | ||
|
||
// AddWorkspaceSweeper adds a sweeper that deletes any workspaces that match | ||
// the prefix we use for ephemeral workspaces in acceptance tests. | ||
// | ||
// This is designed to run at a given interval when other acceptance tests are | ||
// not likely running. | ||
func AddWorkspaceSweeper() { | ||
resource.AddTestSweepers("workspaces", &resource.Sweeper{ | ||
Name: "workspaces", | ||
F: func(_ string) error { | ||
client, err := testutils.NewTestClient() | ||
if err != nil { | ||
return fmt.Errorf("unable to get prefect client: %w", err) | ||
} | ||
|
||
// NOTE: the accountID is inherited by the one set in the test environment | ||
workspacesClient, err := client.Workspaces(uuid.Nil) | ||
if err != nil { | ||
return fmt.Errorf("unable to get workspaces client: %w", err) | ||
} | ||
|
||
workspaces, err := workspacesClient.List(context.Background(), []string{}) | ||
if err != nil { | ||
return fmt.Errorf("unable to list workspaces: %w", err) | ||
} | ||
|
||
if len(workspaces) == 0 { | ||
return fmt.Errorf("no workspaces found for this account") | ||
} | ||
|
||
for _, workspace := range workspaces { | ||
if strings.HasPrefix(workspace.Name, testutils.TestAccPrefix) { | ||
log.Printf("found acceptance testing workspace %s, deleting...\n", workspace.Name) | ||
|
||
err := workspacesClient.Delete(context.Background(), workspace.ID) | ||
if err != nil { | ||
log.Printf("unable to delete workspaces %s during sweep: %s\n", workspace.Name, err) | ||
} | ||
} else { | ||
log.Printf("workspace %s does not match acceptance testing prefix, skipping...\n", workspace.Name) | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package sweep_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/prefecthq/terraform-provider-prefect/internal/sweep" | ||
) | ||
|
||
// TestMain adds sweeper functionality to the "go test" command. | ||
func TestMain(m *testing.M) { | ||
sweep.AddWorkspaceSweeper() | ||
|
||
resource.TestMain(m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters