Skip to content

Commit 2b3357e

Browse files
committed
test: allow destroy options in the rtestutils.Destroy method
Sometimes tests might need to destroy resources created with some owner. Signed-off-by: Artem Chernyshev <[email protected]>
1 parent 693f08e commit 2b3357e

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

pkg/resource/rtestutils/destroy.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import (
1919
)
2020

2121
// DestroyAll performs graceful teardown/destroy sequence for all resources of type.
22-
func DestroyAll[R ResourceWithRD](ctx context.Context, t *testing.T, st state.State) {
22+
func DestroyAll[R ResourceWithRD](ctx context.Context, t *testing.T, st state.State, opts ...state.DestroyOption) {
2323
Destroy[R](ctx, t, st, ResourceIDsWithOwner[R](ctx, t, st, pointer.To("")))
2424
}
2525

2626
// Destroy performs graceful teardown/destroy sequence for specified IDs.
27-
func Destroy[R ResourceWithRD](ctx context.Context, t *testing.T, st state.State, ids []string) {
27+
func Destroy[R ResourceWithRD](ctx context.Context, t *testing.T, st state.State, ids []string, opts ...state.DestroyOption) {
2828
var r R
2929

3030
rds := r.ResourceDefinition()
@@ -37,7 +37,19 @@ func Destroy[R ResourceWithRD](ctx context.Context, t *testing.T, st state.State
3737
// start watching before tearing down, so that we don't lose events
3838
require.NoError(t, safe.StateWatchKind(ctx, st, resource.NewMetadata(rds.DefaultNamespace, rds.Type, "", resource.VersionUndefined), watchCh, state.WithBootstrapContents(true)))
3939

40-
ids = Teardown[R](ctx, t, st, ids)
40+
var destroyOptions state.DestroyOptions
41+
42+
for _, opt := range opts {
43+
opt(&destroyOptions)
44+
}
45+
46+
var teardownOptions []state.TeardownOption
47+
48+
if destroyOptions.Owner != "" {
49+
teardownOptions = append(teardownOptions, state.WithTeardownOwner(destroyOptions.Owner))
50+
}
51+
52+
ids = Teardown[R](ctx, t, st, ids, teardownOptions...)
4153
idMap := xslices.ToSet(ids)
4254

4355
for len(idMap) > 0 {
@@ -68,7 +80,7 @@ func Destroy[R ResourceWithRD](ctx context.Context, t *testing.T, st state.State
6880

6981
if r.Metadata().Phase() == resource.PhaseTearingDown && r.Metadata().Finalizers().Empty() {
7082
// time to destroy
71-
require.NoError(t, ignoreNonCriticalErrors(st.Destroy(ctx, r.Metadata())))
83+
require.NoError(t, ignoreNonCriticalErrors(st.Destroy(ctx, r.Metadata(), opts...)))
7284

7385
t.Logf("cleaned up %s ID %q", rds.Type, r.Metadata().ID())
7486
}
@@ -83,21 +95,21 @@ func Destroy[R ResourceWithRD](ctx context.Context, t *testing.T, st state.State
8395
// Teardown moves provided resources to the PhaseTearingDown.
8496
//
8597
// Teardown ignores not found resources and returns a list of resources that were actually torn down.
86-
func Teardown[R ResourceWithRD](ctx context.Context, t *testing.T, st state.State, ids []string) []string {
98+
func Teardown[R ResourceWithRD](ctx context.Context, t *testing.T, st state.State, ids []string, opts ...state.TeardownOption) []string {
8799
var r R
88100

89-
torndown, err := teardown(ctx, st, ids, r.ResourceDefinition())
101+
torndown, err := teardown(ctx, st, ids, r.ResourceDefinition(), opts...)
90102

91103
require.NoError(t, err)
92104

93105
return torndown
94106
}
95107

96-
func teardown(ctx context.Context, st state.State, ids []string, rds meta.ResourceDefinitionSpec) ([]string, error) {
108+
func teardown(ctx context.Context, st state.State, ids []string, rds meta.ResourceDefinitionSpec, opts ...state.TeardownOption) ([]string, error) {
97109
tornDown := make([]string, 0, len(ids))
98110

99111
for _, id := range ids {
100-
if _, err := st.Teardown(ctx, resource.NewMetadata(rds.DefaultNamespace, rds.Type, id, resource.VersionUndefined)); err == nil {
112+
if _, err := st.Teardown(ctx, resource.NewMetadata(rds.DefaultNamespace, rds.Type, id, resource.VersionUndefined), opts...); err == nil {
101113
tornDown = append(tornDown, id)
102114
} else if ignoreNonCriticalErrors(err) != nil {
103115
return nil, err

0 commit comments

Comments
 (0)