Skip to content

Commit

Permalink
e2e: add cleanup logic to kserve tests to ensure no KnativeServing is…
Browse files Browse the repository at this point in the history
… present in the cluster (#1517)

* e2e: add cleanup logic to kserve tests to ensure no KnativeServing is present in the cluster

* Fix findings

* lint
  • Loading branch information
lburgazzoli authored Jan 18, 2025
1 parent 9b062f6 commit 72443c5
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion tests/e2e/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package e2e_test

import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
Expand All @@ -16,6 +17,7 @@ import (
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
k8serr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -28,10 +30,12 @@ import (
infrav1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/infrastructure/v1"
serviceApi "github.com/opendatahub-io/opendatahub-operator/v2/apis/services/v1alpha1"
modelregistryctrl "github.com/opendatahub-io/opendatahub-operator/v2/controllers/components/modelregistry"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
)

const (
knativeServingNamespace = "knative-serving"
servicemeshNamespace = "openshift-operators"
servicemeshOpName = "servicemeshoperator"
serverlessOpName = "serverless-operator"
Expand Down Expand Up @@ -456,6 +460,43 @@ func ensureServicemeshOperators(t *testing.T, tc *testContext) error { //nolint:
return errors.ErrorOrNil()
}

//nolint:thelper
func (tc *testContext) setUpServerless(t *testing.T) error {
ksl := unstructured.UnstructuredList{}
ksl.SetGroupVersionKind(gvk.KnativeServing)

if err := tc.customClient.List(tc.ctx, &ksl, client.InNamespace(knativeServingNamespace)); err != nil {
return fmt.Errorf("error listing Knative Serving objects: %w", err)
}

if len(ksl.Items) != 0 {
t.Logf("Detected %d Knative Serving objects in namespace %s", len(ksl.Items), knativeServingNamespace)
}

for _, obj := range ksl.Items {
data, err := json.Marshal(obj)
if err != nil {
return fmt.Errorf("error marshalling Knative Serving object: %w", err)
}

t.Logf("Deleting Knative Serving %s in namespace %s: %s", obj.GetName(), obj.GetNamespace(), string(data))

if err := tc.customClient.Delete(tc.ctx, &obj); err != nil && !k8serr.IsNotFound(err) {
return fmt.Errorf("error deleting Knative Serving object: %w", err)
}
}

return nil
}

func (tc *testContext) setUp(t *testing.T) error { //nolint: thelper
return ensureServicemeshOperators(t, tc)
if err := ensureServicemeshOperators(t, tc); err != nil {
return err
}

if err := tc.setUpServerless(t); err != nil {
return err
}

return nil
}

0 comments on commit 72443c5

Please sign in to comment.