github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/peer/coreconfigmap_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 initializer_test
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  	corev1 "k8s.io/api/core/v1"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/apimachinery/pkg/types"
    30  
    31  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    32  	initializer "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer"
    33  	v2 "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer/config/v2"
    34  	"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer/mocks"
    35  	k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
    36  )
    37  
    38  var _ = Describe("core config map", func() {
    39  	var (
    40  		coreCM   *initializer.CoreConfigMap
    41  		instance *current.IBPPeer
    42  		client   *mocks.Client
    43  	)
    44  
    45  	BeforeEach(func() {
    46  		client = &mocks.Client{}
    47  		coreCM = &initializer.CoreConfigMap{
    48  			Config: &initializer.Config{
    49  				CorePeerFile:   "../../../defaultconfig/peer/core.yaml",
    50  				CorePeerV2File: "../../../defaultconfig/peer/v2/core.yaml",
    51  				OUFile:         "../../../defaultconfig/peer/ouconfig.yaml",
    52  				InterOUFile:    "../../../defaultconfig/peer/ouconfig-inter.yaml",
    53  			},
    54  			Client:    client,
    55  			GetLabels: func(o metav1.Object) map[string]string { return map[string]string{} },
    56  		}
    57  
    58  		instance = &current.IBPPeer{}
    59  
    60  		client.GetStub = func(ctx context.Context, types types.NamespacedName, obj k8sclient.Object) error {
    61  			switch obj.(type) {
    62  			case *corev1.ConfigMap:
    63  				if types.Name == fmt.Sprintf("%s-config", instance.Name) {
    64  					cm := obj.(*corev1.ConfigMap)
    65  					cm.BinaryData = map[string][]byte{}
    66  				}
    67  			}
    68  			return nil
    69  		}
    70  	})
    71  
    72  	Context("get core config", func() {
    73  		It("returns config map containing peer's core config", func() {
    74  			cm, err := coreCM.GetCoreConfig(instance)
    75  			Expect(err).NotTo(HaveOccurred())
    76  			Expect(cm).NotTo(BeNil())
    77  		})
    78  	})
    79  
    80  	Context("create or update config map", func() {
    81  		BeforeEach(func() {
    82  			client.GetStub = func(ctx context.Context, types types.NamespacedName, obj k8sclient.Object) error {
    83  				switch obj.(type) {
    84  				case *corev1.ConfigMap:
    85  					if types.Name == fmt.Sprintf("%s-config", instance.Name) {
    86  						cm := obj.(*corev1.ConfigMap)
    87  						cm.BinaryData = map[string][]byte{}
    88  					}
    89  				}
    90  				return nil
    91  			}
    92  
    93  		})
    94  
    95  		It("adds default configs", func() {
    96  			err := coreCM.CreateOrUpdate(instance, &v2.Core{})
    97  			Expect(err).NotTo(HaveOccurred())
    98  
    99  			By("adding node OU config section", func() {
   100  				_, obj, _ := client.CreateOrUpdateArgsForCall(0)
   101  
   102  				cm := obj.(*corev1.ConfigMap)
   103  				Expect(cm.BinaryData["config.yaml"]).To(ContainSubstring("Enable: true"))
   104  			})
   105  		})
   106  	})
   107  
   108  	Context("add node ou to config map", func() {
   109  		When("nodeoudisabled is set to false", func() {
   110  			BeforeEach(func() {
   111  				f := false
   112  				instance.Spec.DisableNodeOU = &f
   113  			})
   114  
   115  			It("adds nodeou configs as enabled", func() {
   116  				err := coreCM.AddNodeOU(instance)
   117  				Expect(err).NotTo(HaveOccurred())
   118  
   119  				_, obj, _ := client.CreateOrUpdateArgsForCall(0)
   120  
   121  				cm := obj.(*corev1.ConfigMap)
   122  				Expect(cm.BinaryData["config.yaml"]).To(ContainSubstring("Enable: true"))
   123  			})
   124  		})
   125  
   126  		When("nodeoudisabled is set to true", func() {
   127  			BeforeEach(func() {
   128  				t := true
   129  				instance.Spec.DisableNodeOU = &t
   130  			})
   131  
   132  			It("adds nodeou configs as disabled", func() {
   133  				err := coreCM.AddNodeOU(instance)
   134  				Expect(err).NotTo(HaveOccurred())
   135  
   136  				_, obj, _ := client.CreateOrUpdateArgsForCall(0)
   137  
   138  				cm := obj.(*corev1.ConfigMap)
   139  				Expect(cm.BinaryData["config.yaml"]).To(ContainSubstring("Enable: false"))
   140  			})
   141  		})
   142  	})
   143  })