Skip to content

Commit

Permalink
feat: add basic test for delete resource action
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Oct 15, 2024
1 parent 424d79c commit e0ac99e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
109 changes: 109 additions & 0 deletions controllers/components/base_reconciler_actions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package components_test

import (
"context"
"github.com/onsi/gomega/gstruct"
dscv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/datasciencecluster/v1"
dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/controllers/components"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
"github.com/rs/xid"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
ctrlClient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"testing"

. "github.com/onsi/gomega"
)

func NewFakeClient(scheme *runtime.Scheme, objs ...ctrlClient.Object) ctrlClient.WithWatch {
fakeMapper := meta.NewDefaultRESTMapper(scheme.PreferredVersionAllGroups())
for gvk := range scheme.AllKnownTypes() {
switch {

Check failure on line 28 in controllers/components/base_reconciler_actions_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

singleCaseSwitch: found switch with default case only (gocritic)
// TODO: add cases for cluster scoped
default:
fakeMapper.Add(gvk, meta.RESTScopeNamespace)
}
}

return fake.NewClientBuilder().
WithScheme(scheme).
WithRESTMapper(fakeMapper).
WithObjects(objs...).
Build()
}

func TestDeleteResourcesAction(t *testing.T) {
g := NewWithT(t)

ctx := context.Background()
ns := xid.New().String()

scheme := runtime.NewScheme()
utilruntime.Must(corev1.AddToScheme(scheme))
utilruntime.Must(appsv1.AddToScheme(scheme))

client := NewFakeClient(
scheme,
&appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: appsv1.SchemeGroupVersion.String(),
Kind: "Deployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: "my-deployment",
Namespace: ns,
Labels: map[string]string{
labels.K8SCommon.PartOf: "foo",
},
},
},
&appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: appsv1.SchemeGroupVersion.String(),
Kind: "Deployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: "my-deployment-2",
Namespace: ns,
Labels: map[string]string{
labels.K8SCommon.PartOf: "baz",
},
},
},
)

action := components.NewDeleteResourcesAction(
ctx,
components.WithDeleteResourcesTypes(&appsv1.Deployment{}),
components.WithDeleteResourcesLabel(labels.K8SCommon.PartOf, "foo"))

err := action.Execute(ctx, &components.ReconciliationRequest{
Client: client,
Instance: nil,
DSCI: &dsciv1.DSCInitialization{Spec: dsciv1.DSCInitializationSpec{ApplicationsNamespace: ns}},
DSC: &dscv1.DataScienceCluster{},
Platform: cluster.OpenDataHub,
})

g.Expect(err).ShouldNot(HaveOccurred())

deployments := appsv1.DeploymentList{}
err = client.List(ctx, &deployments)

g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(deployments.Items).Should(HaveLen(1))
g.Expect(deployments.Items[0]).To(
gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{
"ObjectMeta": gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{
"Name": Equal("my-deployment-2"),
}),
}),
)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/operator-framework/api v0.18.0
github.com/pkg/errors v0.9.1
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.68.0
github.com/rs/xid v1.6.0
github.com/spf13/afero v1.10.0
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y=
Expand Down

0 comments on commit e0ac99e

Please sign in to comment.