-
Notifications
You must be signed in to change notification settings - Fork 101
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
Changes from 11 commits
70956a0
0c449b4
be39d07
1c75774
27d7071
a38782e
9a1c8d4
bd88aee
4a3a6cd
26a72f1
23c4a5a
3ba5527
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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 { | ||
|
@@ -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, | ||
} | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
} | ||
} | ||
|
@@ -207,12 +210,23 @@ func (ct CoreRPTest) Test(t *testing.T) { | |
} | ||
}) | ||
|
||
t.Logf("Creating secrets if provided") | ||
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 | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
|
@@ -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) | ||
|
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.
lol.. this code had been deleted for some reason...