github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/base/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  	"encoding/json"
    23  
    24  	. "github.com/onsi/ginkgo/v2"
    25  	. "github.com/onsi/gomega"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  
    28  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    29  	v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/ca/v1"
    30  	"github.com/IBM-Blockchain/fabric-operator/pkg/offering/k8s/ca/override"
    31  )
    32  
    33  var _ = Describe("Base 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("Affnity", func() {
    44  		BeforeEach(func() {
    45  			instance = &current.IBPCA{
    46  				Spec: current.IBPCASpec{
    47  					Arch:   []string{"test-arch"},
    48  					Zone:   "dal",
    49  					Region: "us-south",
    50  				},
    51  			}
    52  			instance.Name = "ca1"
    53  		})
    54  
    55  		It("returns an proper affinity when arch is passed", func() {
    56  			instance.Spec.Arch = []string{"test-arch"}
    57  			a := overrider.GetAffinity(instance)
    58  
    59  			By("setting node affinity", func() {
    60  				Expect(a.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions[0].Values).To(Equal([]string{"test-arch"}))
    61  				Expect(a.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions[1].Values).To(Equal([]string{"dal"}))
    62  				Expect(a.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions[2].Values).To(Equal([]string{"us-south"}))
    63  			})
    64  
    65  			By("setting pod anti affinity", func() {
    66  				Expect(a.PodAntiAffinity).NotTo(BeNil())
    67  				Expect(a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchExpressions[0].Values).To(Equal([]string{"ca1"}))
    68  			})
    69  		})
    70  
    71  		It("returns a proper affinity when no arch is passed", func() {
    72  			instance.Spec.Arch = []string{}
    73  			a := overrider.GetAffinity(instance)
    74  			Expect(a.NodeAffinity).NotTo(BeNil())
    75  
    76  			By("setting node affinity", func() {
    77  				Expect(a.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions[0].Values).To(Equal([]string{"dal"}))
    78  				Expect(a.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions[1].Values).To(Equal([]string{"us-south"}))
    79  			})
    80  
    81  			By("setting pod anti affinity", func() {
    82  				Expect(a.PodAntiAffinity).NotTo(BeNil())
    83  				Expect(len(a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution)).To(Equal(2))
    84  				Expect(a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchExpressions[0].Values).To(Equal([]string{"ca1"}))
    85  			})
    86  		})
    87  
    88  		It("returns a proper affinity for postgres CA", func() {
    89  			caOverrides := &v1.ServerConfig{
    90  				CAConfig: v1.CAConfig{
    91  					DB: &v1.CAConfigDB{
    92  						Type: "postgres",
    93  					},
    94  				},
    95  			}
    96  			bytes, err := json.Marshal(caOverrides)
    97  			Expect(err).NotTo(HaveOccurred())
    98  			rawMessage := json.RawMessage(bytes)
    99  			instance.Spec.ConfigOverride = &current.ConfigOverride{
   100  				CA: &runtime.RawExtension{Raw: rawMessage},
   101  			}
   102  
   103  			a := overrider.GetAffinity(instance)
   104  
   105  			By("not setting zone or region in node affinity", func() {
   106  				Expect(a.NodeAffinity).NotTo(BeNil())
   107  				Expect(len(a.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions)).To(Equal(1))
   108  				Expect(a.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions[0].Values).To(Equal([]string{"test-arch"}))
   109  			})
   110  
   111  			By("setting pod anti affinity with hostname topology key", func() {
   112  				Expect(a.PodAntiAffinity).NotTo(BeNil())
   113  				Expect(len(a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution)).To(Equal(3))
   114  				Expect(a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchExpressions[0].Values).To(Equal([]string{"ca1"}))
   115  				Expect(a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[2].PodAffinityTerm.TopologyKey).To(Equal("kubernetes.io/hostname"))
   116  			})
   117  		})
   118  	})
   119  })