-
Notifications
You must be signed in to change notification settings - Fork 19
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
chore(tests): add sweeper to remove workspaces #355
Conversation
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
Hitting some snags here. The sweeper docs are pretty short and vague, which led someone to create hashicorp/terraform-plugin-testing#378 with very fair questions. It's a few months old now with no response, so will keep an eye on that. Haven't found many other sweeper-related issues logged yet. I also noticed that even if I put an
Note that In general, I'm a bit hesitant to pursue this much further because:
I'll dig into this a bit more, but if I don't make much progress (assuming no developments in the sweeper docs) we can use the logic in this function and just call it directly after the tests run, rather than using Terraform's sweeper facilities. FYI @parkedwards (no action required) |
scripts/testacc-dev
Outdated
@@ -33,4 +33,5 @@ TF_ACC=1 \ | |||
PREFECT_API_URL=$(op read "${vault_entry}/PREFECT_API_URL") \ | |||
PREFECT_API_KEY=$(op read "${vault_entry}/PREFECT_API_KEY") \ | |||
PREFECT_CLOUD_ACCOUNT_ID=$(op read "${vault_entry}/PREFECT_CLOUD_ACCOUNT_ID") \ | |||
gotestsum --max-fails=50 ./... -count=1 -v ${run_arg} | |||
go test ./... -v ${run_arg} -sweep=foo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im guessing -sweep=all
didn't work? or is all
supposed to be an arbitrary selector?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like sweep
specifies the provider to sweep, and sweep-run
lets you specify specific sweepers. Also learned that providing the sweep
flag only runs the sweepers, and that it's often recommended to:
- Run the sweepers (for a clean slate)
- Run the acceptance tests
- Run the sweepers again
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.
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.
b6e2b3e
to
b762450
Compare
// 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() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More context in a4d16f7
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.
Welcoming feedback here. If we want to do the more traditional implementation, we'll need to find a way to persist the unique names of the ephemeral workspaces between running the acceptance tests and then running the sweepers.
Also, the nature of us using an entirely separate (ephemeral) workspace for each test is not reflected in the basic scenario shown in the documentation. It would be more fitting if we had a static prefix for the resource in the test, and could then turn around and delete all of those specific resources matching that prefix.
I think there's a few ways we can approach this, so now that I understand sweepers a bit better, I'm very open to making adjustments here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put another way: we're not fully following the true spirit of sweepers here, in the sense that we're just making a sweeper separated from any specific resource test. That's because of our approach with making ephemeral workspaces as part of each test, so it's most effective to just go clean up those ephemeral workspaces.
But it gets the ball rolling because we can start moving in that direction if it makes sense, and we implemented the cleanup following (loosely) a Terraform framework rather than writing external scripts/etc to do the work.
It even makes me wonder if ephemeral workspaces are as vital as long as we use ephemeral name prefixes for each resource, and then use sweepers to make sure they're cleaned up. Both seem to have their own pros and cons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im OK with this. maybe I had misunderstood the original intention of implementing sweepers, because I had always imagined them running independently / being idempotent based off of that resource name prefix we use
@@ -50,7 +51,7 @@ testacc: | |||
.PHONY: testacc | |||
|
|||
testacc-dev: | |||
./scripts/testacc-dev $(TESTS) $(LOG_LEVEL) | |||
./scripts/testacc-dev $(TESTS) $(LOG_LEVEL) $(SWEEP) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: pending the approach we land on, we'll want to add a scheduled CI action to run the sweeper at a given interval (such as overnight when things are quiet).
Makefile
Outdated
@@ -17,7 +18,7 @@ help: | |||
@echo " lint - run static code analysis" | |||
@echo " test - run automated unit tests" | |||
@echo " testacc - run automated acceptance tests" | |||
@echo " testacc-dev - run automated acceptance tests from a local machine (args TESTS=<tests> LOG_LEVEL=<level>)" | |||
@echo " testacc-dev - run automated acceptance tests from a local machine (args: TESTS=<tests or empty> LOG_LEVEL=<level> SWEEP=<yes or empty>)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should SWEEP be a bool?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh i see, the shell script checks for any value
Follows other patterns and allows us to test this specific package instead of other packages with other tests.
930f84c
to
4eb557f
Compare
Adds a CI workflow that will run the Terraform sweepers each night.
4eb557f
to
ebbe68b
Compare
Summary
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
Testing
Initially I want to disable the actual deletion so we can maintain some dangling workspaces in the account for testing purposes. So I made a small change:
And ran the following: