tests/int: Add separate resource cleanup step
Introduce a destroy-only mode in the test runner to run terraform destroy for the respective cloud provider configurations. This can be used to destroy cloud resources without going through the whole provision-test process. Add a new step in github actions workflow to run the test binary in destoy-only mode at the very end irrespective of the result of the previous steps. This ensures that the infrastructure is always destroyed, even if the CI job is cancelled. Signed-off-by: Sunny <darkowlzz@protonmail.com>
This commit is contained in:
@@ -26,6 +26,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/terraform-exec/tfexec"
|
||||
tfjson "github.com/hashicorp/terraform-json"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
@@ -197,6 +198,20 @@ func TestMain(m *testing.M) {
|
||||
log.Fatalf("Failed to get provider config for %q", infraOpts.Provider)
|
||||
}
|
||||
|
||||
// Run destroy-only mode if enabled.
|
||||
if infraOpts.DestroyOnly {
|
||||
log.Println("Running in destroy-only mode...")
|
||||
envOpts := []tftestenv.EnvironmentOption{
|
||||
tftestenv.WithVerbose(infraOpts.Verbose),
|
||||
// Ignore any state lock in destroy-only mode.
|
||||
tftestenv.WithTfDestroyOptions(tfexec.Lock(false)),
|
||||
}
|
||||
if err := tftestenv.Destroy(ctx, providerCfg.terraformPath, envOpts...); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Initialize with non-zero exit code to indicate failure by default unless
|
||||
// set by a successful test run.
|
||||
exitCode := 1
|
||||
|
||||
Reference in New Issue
Block a user