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

Clean up resources even though AWS resources are not used #4612

Merged
merged 12 commits into from
Nov 11, 2022
63 changes: 42 additions & 21 deletions test/functional/corerp/corerptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/discovery"
Expand Down Expand Up @@ -59,7 +60,8 @@ type CoreRPTest struct {
PostDeleteVerify func(ctx context.Context, t *testing.T, ct CoreRPTest)

// Object Name => map of secret keys and values
Secrets map[string]map[string]string
Secrets map[string]map[string]string
SkipSecretDeletion bool
}

type TestOptions struct {
Expand All @@ -73,12 +75,13 @@ func NewTestOptions(t *testing.T) TestOptions {

func NewCoreRPTest(t *testing.T, name string, steps []TestStep, secrets map[string]map[string]string, initialResources ...unstructured.Unstructured) CoreRPTest {
return CoreRPTest{
Options: NewCoreRPTestOptions(t),
Name: name,
Description: name,
Steps: steps,
Secrets: secrets,
InitialResources: initialResources,
Options: NewCoreRPTestOptions(t),
Name: name,
Description: name,
Steps: steps,
Secrets: secrets,
InitialResources: initialResources,
SkipSecretDeletion: false,
}
}

Expand Down Expand Up @@ -130,7 +133,7 @@ func (ct CoreRPTest) CreateSecrets(ctx context.Context) error {
Data: data,
}, v1.CreateOptions{})
if err != nil {
return fmt.Errorf("failed to create secret %s", err.Error())
return err
}
}
}
Expand Down Expand Up @@ -207,12 +210,23 @@ func (ct CoreRPTest) Test(t *testing.T) {
}
})

t.Logf("Creating secrets if provided")
Copy link
Author

@youngbupark youngbupark Nov 10, 2022

Choose a reason for hiding this comment

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

lol.. this code had been deleted for some reason...

err := ct.CreateSecrets(ctx)
if err != nil {
if errors.IsAlreadyExists(err) {
// Do not stop the test if the same secret exists
t.Logf("the secret already exists %v", err)
} else {
t.Errorf("failed to create secrets %v", err)
}
}

// Inside the integration test code we rely on the context for timeout/cancellation functionality.
// We expect the caller to wire this out to the test timeout system, or a stricter timeout if desired.

require.GreaterOrEqual(t, len(ct.Steps), 1, "at least one step is required")
defer ct.CleanUpExtensionResources(ct.InitialResources)
err := ct.CreateInitialResources(ctx)
err = ct.CreateInitialResources(ctx)
require.NoError(t, err, "failed to create initial resources")

success := true
Expand Down Expand Up @@ -275,18 +289,17 @@ func (ct CoreRPTest) Test(t *testing.T) {
// Cleanup code here will run regardless of pass/fail of subtests
for _, step := range ct.Steps {
// Delete AWS resources if they were created
if step.AWSResources == nil || len(step.AWSResources.Resources) == 0 {
continue
}
for _, resource := range step.AWSResources.Resources {
t.Logf("deleting %s", resource.Name)
err := validation.DeleteAWSResource(ctx, t, &resource, ct.Options.AWSClient)
require.NoErrorf(t, err, "failed to delete %s", resource.Name)
t.Logf("finished deleting %s", ct.Description)

t.Logf("validating deletion of AWS resource for %s", ct.Description)
validation.ValidateNoAWSResource(ctx, t, &resource, ct.Options.AWSClient)
t.Logf("finished validation of deletion of AWS resource %s for %s", resource.Name, ct.Description)
if step.AWSResources != nil && len(step.AWSResources.Resources) > 0 {
for _, resource := range step.AWSResources.Resources {
t.Logf("deleting %s", resource.Name)
err := validation.DeleteAWSResource(ctx, t, &resource, ct.Options.AWSClient)
require.NoErrorf(t, err, "failed to delete %s", resource.Name)
t.Logf("finished deleting %s", ct.Description)

t.Logf("validating deletion of AWS resource for %s", ct.Description)
validation.ValidateNoAWSResource(ctx, t, &resource, ct.Options.AWSClient)
t.Logf("finished validation of deletion of AWS resource %s for %s", resource.Name, ct.Description)
}
}

if (step.CoreRPResources == nil && step.SkipKubernetesOutputResourceValidation) || step.SkipResourceDeletion {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we remove the continue here and change the logic in this line so that we do not add similar regressions if some cleanup gets added after this?

Copy link
Author

@youngbupark youngbupark Nov 11, 2022

Choose a reason for hiding this comment

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

Ah this condition is true only if CoreRPResource is empty and skipped explicitly. So generally, continue will not be executed.

Expand All @@ -309,6 +322,14 @@ func (ct CoreRPTest) Test(t *testing.T) {
}
}

if !ct.SkipSecretDeletion {
t.Logf("Deleting secrets")
err = ct.DeleteSecrets(ctx)
if err != nil {
t.Errorf("failed to delete secrets %v", err)
}
}

// Custom verification is expected to use `t` to trigger its own assertions
if ct.PostDeleteVerify != nil {
t.Logf("running post-delete verification for %s", ct.Description)
Expand Down