github.com/IBM-Blockchain/fabric-operator@v1.0.4/integration/e2ev2/peer_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 e2ev2_test
    20  
    21  import (
    22  	"context"
    23  	"encoding/json"
    24  	"time"
    25  
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  	"k8s.io/utils/pointer"
    29  
    30  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    31  	"github.com/IBM-Blockchain/fabric-operator/pkg/apis/common"
    32  	v2 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/peer/v2"
    33  	"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/common/config"
    34  	v2peerconfig "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer/config/v2"
    35  
    36  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    37  	"k8s.io/apimachinery/pkg/runtime"
    38  	"k8s.io/apimachinery/pkg/types"
    39  )
    40  
    41  const (
    42  	IBPPEERS = "ibppeers"
    43  )
    44  
    45  var _ = Describe("peer", func() {
    46  	BeforeEach(func() {
    47  		Eventually(org1peer.PodIsRunning).Should((Equal(true)))
    48  
    49  		ClearOperatorConfig()
    50  	})
    51  
    52  	AfterEach(func() {
    53  		// Set flag if a test falls
    54  		if CurrentGinkgoTestDescription().Failed {
    55  			testFailed = true
    56  		}
    57  	})
    58  
    59  	Context("config overrides", func() {
    60  		var (
    61  			bytes []byte
    62  		)
    63  
    64  		BeforeEach(func() {
    65  			// Make sure the config is in expected state
    66  			cm, err := kclient.CoreV1().ConfigMaps(namespace).Get(context.TODO(), org1peer.Name+"-config", metav1.GetOptions{})
    67  			Expect(err).NotTo(HaveOccurred())
    68  
    69  			coreBytes := cm.BinaryData["core.yaml"]
    70  			peerConfig, err := v2peerconfig.ReadCoreFromBytes(coreBytes)
    71  			Expect(err).NotTo(HaveOccurred())
    72  			Expect(peerConfig.Peer.ID).To(Equal("testPeerID"))
    73  
    74  			// Update the config overrides
    75  			result := ibpCRClient.Get().Namespace(namespace).Resource(IBPPEERS).Name(org1peer.Name).Do(context.TODO())
    76  			Expect(result.Error()).NotTo(HaveOccurred())
    77  
    78  			peer := &current.IBPPeer{}
    79  			result.Into(peer)
    80  
    81  			configOverride := &v2peerconfig.Core{
    82  				Core: v2.Core{
    83  					Peer: v2.Peer{
    84  						Keepalive: v2.KeepAlive{
    85  							MinInterval: common.MustParseDuration("20h"),
    86  						},
    87  					},
    88  				},
    89  			}
    90  
    91  			configBytes, err := json.Marshal(configOverride)
    92  			Expect(err).NotTo(HaveOccurred())
    93  			peer.Spec.ConfigOverride = &runtime.RawExtension{Raw: configBytes}
    94  
    95  			bytes, err = json.Marshal(peer)
    96  			Expect(err).NotTo(HaveOccurred())
    97  		})
    98  
    99  		It("updates config based on overrides", func() {
   100  			result := ibpCRClient.Patch(types.MergePatchType).Namespace(namespace).Resource(IBPPEERS).Name(org1peer.Name).Body(bytes).Do(context.TODO())
   101  			Expect(result.Error()).NotTo(HaveOccurred())
   102  
   103  			var peerConfig *v2peerconfig.Core
   104  			Eventually(func() bool {
   105  				cm, err := kclient.CoreV1().ConfigMaps(namespace).Get(context.TODO(), org1peer.Name+"-config", metav1.GetOptions{})
   106  				if err != nil {
   107  					return false
   108  				}
   109  
   110  				coreBytes := cm.BinaryData["core.yaml"]
   111  				peerConfig, err = v2peerconfig.ReadCoreFromBytes(coreBytes)
   112  				if err != nil {
   113  					return false
   114  				}
   115  
   116  				if peerConfig.Peer.Keepalive.MinInterval.Duration == common.MustParseDuration("20h").Duration {
   117  					return true
   118  				}
   119  
   120  				return false
   121  			}).Should(Equal(true))
   122  
   123  			Expect(peerConfig.Peer.ID).To(Equal("testPeerID"))
   124  			Expect(peerConfig.Peer.Keepalive.MinInterval.Duration).To(Equal(common.MustParseDuration("20h").Duration))
   125  		})
   126  	})
   127  
   128  	Context("node ou updated", func() {
   129  		var (
   130  			podName string
   131  			bytes   []byte
   132  		)
   133  
   134  		BeforeEach(func() {
   135  			// Pods seem to run slower and restart slower when running test in Travis.
   136  			SetDefaultEventuallyTimeout(540 * time.Second)
   137  
   138  			Eventually(func() int { return len(org1peer.GetRunningPods()) }).Should(Equal(1))
   139  			podName = org1peer.GetRunningPods()[0].Name
   140  
   141  			// Make sure config is in expected state
   142  			cm, err := kclient.CoreV1().ConfigMaps(namespace).Get(context.TODO(), org1peer.Name+"-config", metav1.GetOptions{})
   143  			Expect(err).NotTo(HaveOccurred())
   144  
   145  			configBytes := cm.BinaryData["config.yaml"]
   146  			cfg, err := config.NodeOUConfigFromBytes(configBytes)
   147  			Expect(err).NotTo(HaveOccurred())
   148  			Expect(cfg.NodeOUs.Enable).To(Equal(true))
   149  
   150  			// Update the config overrides
   151  			result := ibpCRClient.Get().Namespace(namespace).Resource(IBPPEERS).Name(org1peer.Name).Do(context.TODO())
   152  			Expect(result.Error()).NotTo(HaveOccurred())
   153  
   154  			peer := &current.IBPPeer{}
   155  			result.Into(peer)
   156  
   157  			// Disable node ou
   158  			peer.Spec.DisableNodeOU = pointer.Bool(true)
   159  			bytes, err = json.Marshal(peer)
   160  			Expect(err).NotTo(HaveOccurred())
   161  		})
   162  
   163  		It("disables nodeOU", func() {
   164  			result := ibpCRClient.Patch(types.MergePatchType).Namespace(namespace).Resource(IBPPEERS).Name(org1peer.Name).Body(bytes).Do(context.TODO())
   165  			Expect(result.Error()).NotTo(HaveOccurred())
   166  
   167  			peer := &current.IBPPeer{}
   168  			result.Into(peer)
   169  			Expect(peer.Spec.NodeOUDisabled()).To(Equal(true))
   170  
   171  			By("updating config map", func() {
   172  				Eventually(func() bool {
   173  					cm, err := kclient.CoreV1().ConfigMaps(namespace).Get(context.TODO(), org1peer.Name+"-config", metav1.GetOptions{})
   174  					if err != nil {
   175  						return false
   176  					}
   177  
   178  					configBytes := cm.BinaryData["config.yaml"]
   179  					nodeOUConfig, err := config.NodeOUConfigFromBytes(configBytes)
   180  					if err != nil {
   181  						return false
   182  					}
   183  
   184  					return nodeOUConfig.NodeOUs.Enable
   185  				}).Should(Equal(false))
   186  			})
   187  
   188  			By("restarting peer pods", func() {
   189  				Eventually(func() bool {
   190  					pods := org1peer.GetRunningPods()
   191  					if len(pods) == 0 {
   192  						return false
   193  					}
   194  
   195  					newPodName := pods[0].Name
   196  					if newPodName != podName {
   197  						return true
   198  					}
   199  
   200  					return false
   201  				}).Should(Equal(true))
   202  			})
   203  		})
   204  	})
   205  })