github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/base/peer/override/pvc_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  	corev1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/api/resource"
    26  
    27  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    28  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources"
    29  	"github.com/IBM-Blockchain/fabric-operator/pkg/offering/base/peer/override"
    30  	"github.com/IBM-Blockchain/fabric-operator/pkg/util"
    31  )
    32  
    33  var _ = Describe("Base Peer PVC Overrides", func() {
    34  	var (
    35  		overrider *override.Override
    36  		instance  *current.IBPPeer
    37  		pvc       *corev1.PersistentVolumeClaim
    38  	)
    39  
    40  	BeforeEach(func() {
    41  		var err error
    42  
    43  		pvc, err = util.GetPVCFromFile("../../../../../definitions/peer/pvc.yaml")
    44  		Expect(err).NotTo(HaveOccurred())
    45  
    46  		overrider = &override.Override{}
    47  		instance = &current.IBPPeer{
    48  			Spec: current.IBPPeerSpec{
    49  				Zone:   "zone1",
    50  				Region: "region1",
    51  				Storage: &current.PeerStorages{
    52  					Peer: &current.StorageSpec{
    53  						Size:  "100m",
    54  						Class: "manual",
    55  					},
    56  				},
    57  			},
    58  		}
    59  	})
    60  
    61  	Context("create", func() {
    62  		It("overrides values based on spec", func() {
    63  			err := overrider.PVC(instance, pvc, resources.Create)
    64  			Expect(err).NotTo(HaveOccurred())
    65  
    66  			By("setting storage class", func() {
    67  				Expect(pvc.Spec.StorageClassName).To(Equal(&instance.Spec.Storage.Peer.Class))
    68  			})
    69  
    70  			By("setting requested storage size", func() {
    71  				expectedRequests, err := resource.ParseQuantity(instance.Spec.Storage.Peer.Size)
    72  				Expect(err).NotTo(HaveOccurred())
    73  				Expect(pvc.Spec.Resources.Requests).To(Equal(corev1.ResourceList{corev1.ResourceStorage: expectedRequests}))
    74  			})
    75  
    76  			By("setting zone labels", func() {
    77  				Expect(pvc.ObjectMeta.Labels["zone"]).To(Equal(instance.Spec.Zone))
    78  			})
    79  
    80  			By("setting region labels", func() {
    81  				Expect(pvc.ObjectMeta.Labels["region"]).To(Equal(instance.Spec.Region))
    82  			})
    83  		})
    84  
    85  		It("sets class to manual if spec used local", func() {
    86  			instance.Spec.Storage.Peer.Class = "manual"
    87  			err := overrider.PVC(instance, pvc, resources.Create)
    88  			Expect(err).NotTo(HaveOccurred())
    89  			Expect(*pvc.Spec.StorageClassName).To(Equal("manual"))
    90  		})
    91  
    92  		It("returns an error if invalid value for size is used", func() {
    93  			instance.Spec.Storage.Peer.Size = "10x"
    94  			err := overrider.PVC(instance, pvc, resources.Create)
    95  			Expect(err).To(HaveOccurred())
    96  			Expect(err.Error()).To(ContainSubstring("quantities must match the regular expression"))
    97  		})
    98  	})
    99  })