Skip to content

Commit

Permalink
Clean up resources created by Feature tests (#5289)
Browse files Browse the repository at this point in the history
* example usage

* actually delete the resources

* rebase
  • Loading branch information
vaikas authored Apr 21, 2021
1 parent 73ce225 commit 8565fcd
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ require (
knative.dev/hack v0.0.0-20210325223819-b6ab329907d3
knative.dev/hack/schema v0.0.0-20210325223819-b6ab329907d3
knative.dev/pkg v0.0.0-20210420053235-1afd04993622
knative.dev/reconciler-test v0.0.0-20210420201836-15b4e1222865
knative.dev/reconciler-test v0.0.0-20210420215136-4598ca0f0602
sigs.k8s.io/yaml v1.2.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1122,8 +1122,8 @@ knative.dev/hack/schema v0.0.0-20210325223819-b6ab329907d3 h1:F/pVm+rB+WpyVhH9cm
knative.dev/hack/schema v0.0.0-20210325223819-b6ab329907d3/go.mod h1:ffjwmdcrH5vN3mPhO8RrF2KfNnbHeCE2C60A+2cv3U0=
knative.dev/pkg v0.0.0-20210420053235-1afd04993622 h1:wSyDPp/LuOLeDCpvUHgKXqb4DfmCEPelsaYzC0Fojm0=
knative.dev/pkg v0.0.0-20210420053235-1afd04993622/go.mod h1:UtcSLHy2XIz5blWoPTA40F87zk4O7erxkCwv+7Tsmws=
knative.dev/reconciler-test v0.0.0-20210420201836-15b4e1222865 h1:Flf/ZPKa76ynJhHltE1+RpB+0oDum44Wlfz9ednc7l4=
knative.dev/reconciler-test v0.0.0-20210420201836-15b4e1222865/go.mod h1:yZihS1XoBt7oxU6Jq+U2hMKmUvfKFEaj3vMqOMBt/tI=
knative.dev/reconciler-test v0.0.0-20210420215136-4598ca0f0602 h1:+EGg+hRVnOdgBPID/ngPjjN2BCOY1xV983BKan+SDhk=
knative.dev/reconciler-test v0.0.0-20210420215136-4598ca0f0602/go.mod h1:yZihS1XoBt7oxU6Jq+U2hMKmUvfKFEaj3vMqOMBt/tI=
pgregory.net/rapid v0.3.3 h1:jCjBsY4ln4Atz78QoBWxUEvAHaFyNDQg9+WU62aCn1U=
pgregory.net/rapid v0.3.3/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
Expand Down
25 changes: 24 additions & 1 deletion test/rekt/features/broker/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
v1 "knative.dev/eventing/pkg/apis/duck/v1"
eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"
Expand All @@ -36,7 +37,9 @@ import (
brokerresources "knative.dev/eventing/test/rekt/resources/broker"
"knative.dev/eventing/test/rekt/resources/delivery"
triggerresources "knative.dev/eventing/test/rekt/resources/trigger"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/injection/clients/dynamicclient"
"knative.dev/pkg/ptr"
"knative.dev/reconciler-test/pkg/environment"
"knative.dev/reconciler-test/pkg/eventshub"
Expand Down Expand Up @@ -364,12 +367,31 @@ func addControlPlaneDelivery(fs *feature.FeatureSet) {
t.Failed()
}
})

f.Stable("Conformance").Should(tt.name, assertExpectedEvents(prober, expectedEvents))
f.Teardown("Delete feature resources", deleteFeatureResources())
fs.Features = append(fs.Features, *f)
}
}

func deleteFeatureResources() feature.StepFn {
return func(ctx context.Context, t feature.T) {
dc := dynamicclient.Get(ctx)
f := feature.FromContext(ctx)
for _, ref := range f.References() {
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
t.Errorf("Could not parse GroupVersion for %+v", ref.APIVersion)
} else {
resource := apis.KindToResource(gv.WithKind(ref.Kind))
t.Logf("Deleting %s/%s of GVR: %+v", ref.Namespace, ref.Name, resource)
if err := dc.Resource(resource).Namespace(ref.Namespace).Delete(ctx, ref.Name, *metav1.NewDeleteOptions(0)); err != nil {
t.Logf("Failed to delete during cleanup %s/%s of GVR: %+v", ref.Namespace, ref.Name, resource)
}
}
}
}
}

func addControlPlaneEventRouting(fs *feature.FeatureSet) {
for i, tt := range []struct {
name string
Expand Down Expand Up @@ -557,6 +579,7 @@ func addControlPlaneEventRouting(fs *feature.FeatureSet) {
})

f.Stable("Conformance").Should(tt.name, assertExpectedRoutedEvents(prober, expectedEvents))
f.Teardown("Delete feature resources", deleteFeatureResources())
fs.Features = append(fs.Features, *f)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func (mr *MagicEnvironment) Test(ctx context.Context, originalT *testing.T, f *f
f.State = &state.KVStore{}
}
ctx = state.ContextWith(ctx, f.State)
ctx = feature.ContextWith(ctx, f)

steps := categorizeSteps(f.Steps)

Expand Down
39 changes: 39 additions & 0 deletions vendor/knative.dev/reconciler-test/pkg/feature/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2021 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package feature

import (
"context"
)

type envKey struct{}

// ContextWith decorates the given context with the provided Feature, and returns
// the resulting context.
func ContextWith(ctx context.Context, f *Feature) context.Context {
return context.WithValue(ctx, envKey{}, f)
}

// FromContext returns the Feature from Context, if not found FromContext will
// panic.
// TODO: revisit if we really want to panic here... likely not.
func FromContext(ctx context.Context) *Feature {
if e, ok := ctx.Value(envKey{}).(*Feature); ok {
return e
}
panic("no Feature found in context")
}
14 changes: 14 additions & 0 deletions vendor/knative.dev/reconciler-test/pkg/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"runtime"
"strings"

corev1 "k8s.io/api/core/v1"

"knative.dev/reconciler-test/pkg/state"
)

Expand All @@ -30,6 +32,8 @@ type Feature struct {
Name string
Steps []Step
State state.Store
// Contains all the resources created as part of this Feature.
refs []corev1.ObjectReference
}

// NewFeatureNamed creates a new feature with the provided name
Expand Down Expand Up @@ -83,6 +87,16 @@ func (s *Step) TestName() string {
}
}

// Reference adds references to keep track of for example, for cleaning things
// after a Feature completes.
func (f *Feature) Reference(ref ...corev1.ObjectReference) {
f.refs = append(f.refs, ref...)
}

func (f *Feature) References() []corev1.ObjectReference {
return f.refs
}

// Setup adds a step function to the feature set at the Setup timing phase.
func (f *Feature) Setup(name string, fn StepFn) {
f.AddStep(Step{
Expand Down
5 changes: 4 additions & 1 deletion vendor/knative.dev/reconciler-test/pkg/manifest/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"knative.dev/pkg/injection/clients/dynamicclient"
"knative.dev/reconciler-test/pkg/environment"
"knative.dev/reconciler-test/pkg/feature"
)

// CfgFn is the function signature of configuration mutation options.
Expand All @@ -36,6 +37,7 @@ type CfgFn func(map[string]interface{})
func InstallYaml(ctx context.Context, dir string, base map[string]interface{}) (Manifest, error) {
env := environment.FromContext(ctx)
cfg := env.TemplateConfig(base)
f := feature.FromContext(ctx)

yamls, err := ParseTemplates(dir, env.Images(), cfg)
if err != nil {
Expand All @@ -54,8 +56,9 @@ func InstallYaml(ctx context.Context, dir string, base map[string]interface{}) (
return manifest, err
}

// Save the refs.
// Save the refs to Environment and Feature
env.Reference(manifest.References()...)
f.Reference(manifest.References()...)

// Temp
refs := manifest.References()
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ knative.dev/pkg/webhook/resourcesemantics
knative.dev/pkg/webhook/resourcesemantics/conversion
knative.dev/pkg/webhook/resourcesemantics/defaulting
knative.dev/pkg/webhook/resourcesemantics/validation
# knative.dev/reconciler-test v0.0.0-20210420201836-15b4e1222865
# knative.dev/reconciler-test v0.0.0-20210420215136-4598ca0f0602
## explicit
knative.dev/reconciler-test/cmd/eventshub
knative.dev/reconciler-test/pkg/environment
Expand Down

0 comments on commit 8565fcd

Please sign in to comment.