github.com/redhat-appstudio/release-service@v0.0.0-20240507045911-a8558ef3422a/controllers/releaseplanadmission/controller_test.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package releaseplanadmission 18 19 import ( 20 "reflect" 21 22 . "github.com/onsi/ginkgo/v2" 23 . "github.com/onsi/gomega" 24 25 "k8s.io/apimachinery/pkg/types" 26 "k8s.io/client-go/kubernetes/scheme" 27 ctrl "sigs.k8s.io/controller-runtime" 28 "sigs.k8s.io/controller-runtime/pkg/metrics/server" 29 "sigs.k8s.io/controller-runtime/pkg/reconcile" 30 ) 31 32 var _ = Describe("ReleasePlanAdmission Controller", Ordered, func() { 33 // For the Reconcile function test we don't want to make a successful call as it will call every single operation 34 // defined there. We don't have any control over the operations being executed, and we want to keep a clean env for 35 // the adapter tests. 36 When("Reconcile is called", func() { 37 It("should succeed even if the releasePlanAdmission is not found", func() { 38 controller := &Controller{ 39 client: k8sClient, 40 log: ctrl.Log, 41 } 42 43 req := ctrl.Request{ 44 NamespacedName: types.NamespacedName{ 45 Name: "non-existent", 46 Namespace: "default", 47 }, 48 } 49 result, err := controller.Reconcile(ctx, req) 50 Expect(reflect.TypeOf(result)).To(Equal(reflect.TypeOf(reconcile.Result{}))) 51 Expect(err).To(BeNil()) 52 }) 53 }) 54 55 When("Register is called", func() { 56 It("should setup the controller successfully", func() { 57 controller := &Controller{ 58 client: k8sClient, 59 log: ctrl.Log, 60 } 61 62 mgr, _ := ctrl.NewManager(cfg, ctrl.Options{ 63 Scheme: scheme.Scheme, 64 Metrics: server.Options{ 65 BindAddress: "0", // disables metrics 66 }, 67 LeaderElection: false, 68 }) 69 Expect(controller.Register(mgr, &ctrl.Log, nil)).To(Succeed()) 70 }) 71 }) 72 73 })