@@ -8,8 +8,10 @@ import (
88 "github.com/stretchr/testify/mock"
99 "github.com/stretchr/testify/suite"
1010 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
11+ kerrors "k8s.io/apimachinery/pkg/api/errors"
1112 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1213 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
14+ "k8s.io/apimachinery/pkg/runtime/schema"
1315 "k8s.io/apimachinery/pkg/types"
1416 "sigs.k8s.io/controller-runtime/pkg/client"
1517
@@ -20,6 +22,7 @@ import (
2022
2123 "github.com/platform-mesh/platform-mesh-operator/api/v1alpha1"
2224 "github.com/platform-mesh/platform-mesh-operator/internal/config"
25+ ctrl "sigs.k8s.io/controller-runtime"
2326)
2427
2528type DeployTestSuite struct {
@@ -154,3 +157,37 @@ func (s *DeployTestSuite) Test_applyReleaseWithValues() {
154157 s .Assert ().NoError (err , "ApplyReleaseWithValues should not return an error" )
155158
156159}
160+
161+ func (s * DeployTestSuite ) Test_Finalize_DeletesFluxObjects () {
162+ ctx := context .TODO ()
163+
164+ // mock deletions
165+ s .clientMock .EXPECT ().Delete (mock .Anything , mock .MatchedBy (func (obj client.Object ) bool {
166+ u := obj .(* unstructured.Unstructured )
167+ gvk := u .GroupVersionKind ()
168+ return gvk .Group == "delivery.ocm.software" && gvk .Version == "v1alpha1" && gvk .Kind == "Resource" && u .GetName () == "platform-mesh-operator-components" && u .GetNamespace () == "default"
169+ })).Return (nil ).Once ()
170+
171+ s .clientMock .EXPECT ().Delete (mock .Anything , mock .MatchedBy (func (obj client.Object ) bool {
172+ u := obj .(* unstructured.Unstructured )
173+ gvk := u .GroupVersionKind ()
174+ return gvk .Group == "helm.toolkit.fluxcd.io" && gvk .Version == "v2" && gvk .Kind == "HelmRelease" && u .GetName () == "platform-mesh-operator-components" && u .GetNamespace () == "default"
175+ })).Return (nil ).Once ()
176+
177+ // asserts
178+ res , opErr := s .testObj .Finalize (ctx , & v1alpha1.PlatformMesh {})
179+ s .Require ().Nil (opErr )
180+ s .Require ().Equal (ctrl.Result {}, res )
181+ }
182+
183+ func (s * DeployTestSuite ) Test_Finalize_AlreadyDeleted () {
184+ ctx := context .TODO ()
185+
186+ // mock deletions with NotFound errors
187+ s .clientMock .EXPECT ().Delete (mock .Anything , mock .Anything ).Return (kerrors .NewNotFound (schema.GroupResource {Group : "delivery.ocm.software" , Resource : "Resource" }, "platform-mesh-operator-components" )).Once ()
188+ s .clientMock .EXPECT ().Delete (mock .Anything , mock .Anything ).Return (kerrors .NewNotFound (schema.GroupResource {Group : "helm.toolkit.fluxcd.io" , Resource : "HelmRelease" }, "platform-mesh-operator-components" )).Once ()
189+
190+ res , opErr := s .testObj .Finalize (ctx , & v1alpha1.PlatformMesh {})
191+ s .Require ().Nil (opErr )
192+ s .Require ().Equal (ctrl.Result {}, res )
193+ }
0 commit comments