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