github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/k8s/ca/override/override_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 override_test
    20  
    21  import (
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  
    25  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    26  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources"
    27  	"github.com/IBM-Blockchain/fabric-operator/pkg/offering/k8s/ca/override"
    28  	"github.com/IBM-Blockchain/fabric-operator/pkg/util"
    29  	networkingv1 "k8s.io/api/networking/v1"
    30  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  )
    32  
    33  var _ = Describe("K8S CA Overrides", func() {
    34  	var (
    35  		overrider *override.Override
    36  		instance  *current.IBPCA
    37  	)
    38  
    39  	BeforeEach(func() {
    40  		overrider = &override.Override{}
    41  	})
    42  
    43  	Context("Ingress", func() {
    44  		var (
    45  			ingress *networkingv1.Ingress
    46  		)
    47  
    48  		BeforeEach(func() {
    49  			var err error
    50  
    51  			ingress, err = util.GetIngressFromFile("../../../../../definitions/ca/ingress.yaml")
    52  			Expect(err).NotTo(HaveOccurred())
    53  
    54  			instance = &current.IBPCA{
    55  				ObjectMeta: metav1.ObjectMeta{
    56  					Name:      "ingress1",
    57  					Namespace: "namespace1",
    58  				},
    59  				Spec: current.IBPCASpec{
    60  					Domain: "domain1",
    61  				},
    62  			}
    63  		})
    64  
    65  		When("creating ingress", func() {
    66  			It("sets appropriate values", func() {
    67  				err := overrider.Ingress(instance, ingress, resources.Create)
    68  				Expect(err).NotTo(HaveOccurred())
    69  				VerifyIngressCommonOverrides(instance, ingress)
    70  			})
    71  		})
    72  
    73  		When("creating ingress with custom ingress class", func() {
    74  			It("sets appropriate values", func() {
    75  				instance.Spec.Ingress = current.Ingress{
    76  					Class: "custom",
    77  				}
    78  				err := overrider.Ingress(instance, ingress, resources.Create)
    79  				Expect(err).NotTo(HaveOccurred())
    80  				VerifyIngressCommonOverrides(instance, ingress)
    81  			})
    82  		})
    83  
    84  		When("updating ingress", func() {
    85  			It("sets appropriate values", func() {
    86  				err := overrider.Ingress(instance, ingress, resources.Update)
    87  				Expect(err).NotTo(HaveOccurred())
    88  				VerifyIngressCommonOverrides(instance, ingress)
    89  			})
    90  		})
    91  
    92  		When("updating ingress with custom ingress class", func() {
    93  			It("sets appropriate values", func() {
    94  				instance.Spec.Ingress = current.Ingress{
    95  					Class: "custom",
    96  				}
    97  				err := overrider.Ingress(instance, ingress, resources.Update)
    98  				Expect(err).NotTo(HaveOccurred())
    99  				VerifyIngressCommonOverrides(instance, ingress)
   100  			})
   101  		})
   102  	})
   103  })
   104  
   105  func VerifyIngressCommonOverrides(instance *current.IBPCA, ingress *networkingv1.Ingress) {
   106  	By("setting annotation for custom ingress class", func() {
   107  		if instance.Spec.Ingress.Class != "" {
   108  			Expect(ingress.ObjectMeta.Annotations["kubernetes.io/ingress.class"]).To(Equal(instance.Spec.Ingress.Class))
   109  		} else {
   110  			Expect(ingress.ObjectMeta.Annotations["kubernetes.io/ingress.class"]).To(Equal("nginx"))
   111  		}
   112  	})
   113  
   114  	By("setting api host in rules host", func() {
   115  		Expect(ingress.Spec.Rules[0].Host).To(Equal(instance.Namespace + "-" + instance.Name + "-ca" + "." + instance.Spec.Domain))
   116  	})
   117  
   118  	By("setting api tls host", func() {
   119  		Expect(ingress.Spec.TLS[0].Hosts).To(Equal([]string{instance.Namespace + "-" + instance.Name + "-ca" + "." + instance.Spec.Domain}))
   120  	})
   121  
   122  	By("setting backend service name", func() {
   123  		Expect(ingress.Spec.Rules[0].HTTP.Paths[0].Backend.Service.Name).To(Equal(instance.Name))
   124  	})
   125  
   126  	By("setting backend service port", func() {
   127  		Expect(ingress.Spec.Rules[0].HTTP.Paths[0].Backend.Service.Port.Name).To(Equal("http"))
   128  	})
   129  
   130  	By("setting operations host in rules host", func() {
   131  		Expect(ingress.Spec.Rules[1].Host).To(Equal(instance.Namespace + "-" + instance.Name + "-operations" + "." + instance.Spec.Domain))
   132  	})
   133  
   134  	By("setting operations tls host", func() {
   135  		Expect(ingress.Spec.TLS[1].Hosts).To(Equal([]string{instance.Namespace + "-" + instance.Name + "-operations" + "." + instance.Spec.Domain}))
   136  	})
   137  
   138  	By("setting backend service name", func() {
   139  		Expect(ingress.Spec.Rules[1].HTTP.Paths[0].Backend.Service.Name).To(Equal(instance.Name))
   140  	})
   141  
   142  	By("setting backend service port", func() {
   143  		Expect(ingress.Spec.Rules[1].HTTP.Paths[0].Backend.Service.Port.Name).To(Equal("operations"))
   144  	})
   145  }