github.com/redhat-appstudio/release-service@v0.0.0-20240507143925-083712697924/controllers/releaseplan/controller_test.go (about)

     1  /*
     2  Copyright 2022.
     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 releaseplan
    18  
    19  import (
    20  	"reflect"
    21  	"sigs.k8s.io/controller-runtime/pkg/metrics/server"
    22  
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  
    26  	"k8s.io/apimachinery/pkg/types"
    27  	"k8s.io/client-go/kubernetes/scheme"
    28  	ctrl "sigs.k8s.io/controller-runtime"
    29  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    30  )
    31  
    32  var _ = Describe("ReleasePlan Controller", Ordered, func() {
    33  
    34  	// For the Reconcile function test we don't want to make a successful call as it will call every single operation
    35  	// defined there. We don't have any control over the operations being executed, and we want to keep a clean env for
    36  	// the adapter tests.
    37  	When("Reconcile is called", func() {
    38  		It("should succeed even if the releasePlan is not found", func() {
    39  			controller := &Controller{
    40  				client: k8sClient,
    41  				log:    ctrl.Log,
    42  			}
    43  
    44  			req := ctrl.Request{
    45  				NamespacedName: types.NamespacedName{
    46  					Name:      "non-existent",
    47  					Namespace: "default",
    48  				},
    49  			}
    50  			result, err := controller.Reconcile(ctx, req)
    51  			Expect(reflect.TypeOf(result)).To(Equal(reflect.TypeOf(reconcile.Result{})))
    52  			Expect(err).To(BeNil())
    53  		})
    54  	})
    55  
    56  	When("Register is called", func() {
    57  		It("should setup the controller successfully", func() {
    58  			controller := &Controller{
    59  				client: k8sClient,
    60  				log:    ctrl.Log,
    61  			}
    62  
    63  			mgr, _ := ctrl.NewManager(cfg, ctrl.Options{
    64  				Scheme: scheme.Scheme,
    65  				Metrics: server.Options{
    66  					BindAddress: "0", // disables metrics
    67  				},
    68  				LeaderElection: false,
    69  			})
    70  			Expect(controller.Register(mgr, &ctrl.Log, nil)).To(Succeed())
    71  		})
    72  	})
    73  
    74  })