github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/manager/resources/ingress/manager_test.go (about)

     1  /*
     2   * Copyright contributors to the Hyperledger Fabric Operator project
     3   *
     4   * SPDX-License-Identifier: Apache-2.0
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at:
     9   *
    10   * 	  http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package ingress_test
    20  
    21  import (
    22  	"context"
    23  
    24  	"github.com/IBM-Blockchain/fabric-operator/controllers/mocks"
    25  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources"
    26  	ingress "github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources/ingress"
    27  	. "github.com/onsi/ginkgo/v2"
    28  	. "github.com/onsi/gomega"
    29  	"github.com/pkg/errors"
    30  	networkingv1 "k8s.io/api/networking/v1"
    31  	k8serror "k8s.io/apimachinery/pkg/api/errors"
    32  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    33  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    34  	"k8s.io/apimachinery/pkg/types"
    35  	"sigs.k8s.io/controller-runtime/pkg/client"
    36  )
    37  
    38  var _ = Describe("Ingress manager", func() {
    39  	var (
    40  		mockKubeClient *mocks.Client
    41  		manager        *ingress.Manager
    42  		instance       metav1.Object
    43  	)
    44  
    45  	BeforeEach(func() {
    46  		mockKubeClient = &mocks.Client{}
    47  
    48  		instance = &metav1.ObjectMeta{}
    49  
    50  		manager = &ingress.Manager{
    51  			IngressFile: "../../../../definitions/ca/ingress.yaml",
    52  			Client:      mockKubeClient,
    53  			OverrideFunc: func(object v1.Object, ingress *networkingv1.Ingress, action resources.Action) error {
    54  				return nil
    55  			},
    56  			LabelsFunc: func(v1.Object) map[string]string {
    57  				return map[string]string{}
    58  			},
    59  		}
    60  
    61  		instance = &metav1.ObjectMeta{}
    62  	})
    63  
    64  	Context("reconciles the ingress instance", func() {
    65  		It("does not try to create ingress if the get request returns an error other than 'not found'", func() {
    66  			errMsg := "connection refused"
    67  			mockKubeClient.GetReturns(errors.New(errMsg))
    68  			err := manager.Reconcile(instance, false)
    69  			Expect(err).To(HaveOccurred())
    70  			Expect(err.Error()).To(Equal(errMsg))
    71  		})
    72  
    73  		When("ingress does not exist", func() {
    74  			BeforeEach(func() {
    75  				notFoundErr := &k8serror.StatusError{
    76  					ErrStatus: metav1.Status{
    77  						Reason: metav1.StatusReasonNotFound,
    78  					},
    79  				}
    80  				mockKubeClient.GetReturns(notFoundErr)
    81  			})
    82  
    83  			It("returns an error if fails to load default config", func() {
    84  				manager.IngressFile = "bad.yaml"
    85  				err := manager.Reconcile(instance, false)
    86  				Expect(err).To(HaveOccurred())
    87  				Expect(err.Error()).To(ContainSubstring("no such file or directory"))
    88  			})
    89  
    90  			It("returns an error if override ingress value fails", func() {
    91  				manager.OverrideFunc = func(v1.Object, *networkingv1.Ingress, resources.Action) error {
    92  					return errors.New("creation override failed")
    93  				}
    94  				err := manager.Reconcile(instance, false)
    95  				Expect(err).To(HaveOccurred())
    96  				Expect(err.Error()).Should(ContainSubstring("creation override failed"))
    97  			})
    98  
    99  			It("returns an error if the creation of the Ingress fails", func() {
   100  				errMsg := "unable to create ingress"
   101  				mockKubeClient.CreateReturns(errors.New(errMsg))
   102  				err := manager.Reconcile(instance, false)
   103  				Expect(err).To(HaveOccurred())
   104  				Expect(err.Error()).To(Equal(errMsg))
   105  			})
   106  
   107  			It("does not return an error on a successfull ingress creation", func() {
   108  				ing := networkingv1.Ingress{
   109  					ObjectMeta: metav1.ObjectMeta{
   110  						Name:      instance.GetName(),
   111  						Namespace: instance.GetNamespace(),
   112  						Annotations: map[string]string{
   113  							"test": "test value",
   114  						},
   115  					},
   116  				}
   117  
   118  				count := 0
   119  				mockKubeClient.GetStub = func(ctx context.Context, types types.NamespacedName, obj client.Object) error {
   120  
   121  					switch obj.(type) {
   122  					case *networkingv1.Ingress:
   123  						if count == 0 {
   124  							// Send not found the first time to go to creation path
   125  							notFoundErr := &k8serror.StatusError{
   126  								ErrStatus: metav1.Status{
   127  									Reason: metav1.StatusReasonNotFound,
   128  								},
   129  							}
   130  							count++
   131  							return notFoundErr
   132  						}
   133  
   134  						i := obj.(*networkingv1.Ingress)
   135  						i.ObjectMeta = ing.ObjectMeta
   136  					}
   137  
   138  					return nil
   139  				}
   140  
   141  				err := manager.Reconcile(instance, false)
   142  				Expect(err).NotTo(HaveOccurred())
   143  			})
   144  		})
   145  	})
   146  })