github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/k8s/console/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  	networkingv1 "k8s.io/api/networking/v1"
    25  
    26  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    27  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources"
    28  	"github.com/IBM-Blockchain/fabric-operator/pkg/offering/k8s/console/override"
    29  	"github.com/IBM-Blockchain/fabric-operator/pkg/util"
    30  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  )
    32  
    33  var _ = Describe("K8S Console Overrides", func() {
    34  	var (
    35  		overrider *override.Override
    36  		instance  *current.IBPConsole
    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/console/ingress.yaml")
    52  			Expect(err).NotTo(HaveOccurred())
    53  
    54  			instance = &current.IBPConsole{
    55  				ObjectMeta: metav1.ObjectMeta{
    56  					Name:      "ingress1",
    57  					Namespace: "namespace1",
    58  				},
    59  				Spec: current.IBPConsoleSpec{
    60  					NetworkInfo: &current.NetworkInfo{
    61  						Domain: "domain1",
    62  					},
    63  				},
    64  			}
    65  		})
    66  
    67  		When("creating ingress", func() {
    68  			It("sets appropriate values", func() {
    69  				err := overrider.Ingress(instance, ingress, resources.Create)
    70  				Expect(err).NotTo(HaveOccurred())
    71  				VerifyIngressCommonOverrides(instance, ingress)
    72  			})
    73  		})
    74  
    75  		When("creating ingress with custom class", func() {
    76  			It("sets appropriate values", func() {
    77  				instance.Spec.Ingress = current.Ingress{
    78  					Class: "custom",
    79  				}
    80  				err := overrider.Ingress(instance, ingress, resources.Create)
    81  				Expect(err).NotTo(HaveOccurred())
    82  				VerifyIngressCommonOverrides(instance, ingress)
    83  			})
    84  		})
    85  
    86  		When("updating ingress", func() {
    87  			It("sets appropriate values", func() {
    88  				err := overrider.Ingress(instance, ingress, resources.Update)
    89  				Expect(err).NotTo(HaveOccurred())
    90  				VerifyIngressCommonOverrides(instance, ingress)
    91  			})
    92  		})
    93  
    94  		When("updating ingress with custom class", func() {
    95  			It("sets appropriate values", func() {
    96  				instance.Spec.Ingress = current.Ingress{
    97  					Class: "custom",
    98  				}
    99  				err := overrider.Ingress(instance, ingress, resources.Update)
   100  				Expect(err).NotTo(HaveOccurred())
   101  				VerifyIngressCommonOverrides(instance, ingress)
   102  			})
   103  		})
   104  	})
   105  })
   106  
   107  func VerifyIngressCommonOverrides(instance *current.IBPConsole, ingress *networkingv1.Ingress) {
   108  	By("setting annotation for custom ingress class", func() {
   109  		if instance.Spec.Ingress.Class != "" {
   110  			Expect(ingress.ObjectMeta.Annotations["kubernetes.io/ingress.class"]).To(Equal(instance.Spec.Ingress.Class))
   111  		} else {
   112  			Expect(ingress.ObjectMeta.Annotations["kubernetes.io/ingress.class"]).To(Equal("nginx"))
   113  		}
   114  	})
   115  
   116  	By("setting api host in rules host", func() {
   117  		Expect(ingress.Spec.Rules[0].Host).To(Equal(instance.Namespace + "-" + instance.Name + "-console" + "." + instance.Spec.NetworkInfo.Domain))
   118  	})
   119  
   120  	By("setting api tls host", func() {
   121  		Expect(ingress.Spec.TLS[0].Hosts).To(Equal([]string{instance.Namespace + "-" + instance.Name + "-console" + "." + instance.Spec.NetworkInfo.Domain}))
   122  	})
   123  
   124  	By("setting backend service name", func() {
   125  		Expect(ingress.Spec.Rules[0].HTTP.Paths[0].Backend.Service.Name).To(Equal(instance.Name))
   126  	})
   127  
   128  	By("setting backend service port", func() {
   129  		Expect(ingress.Spec.Rules[0].HTTP.Paths[0].Backend.Service.Port.Name).To(Equal("optools"))
   130  	})
   131  }