github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/base/orderer/orderer_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 baseorderer_test
    20  
    21  import (
    22  	"context"
    23  
    24  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    25  	cmocks "github.com/IBM-Blockchain/fabric-operator/controllers/mocks"
    26  	config "github.com/IBM-Blockchain/fabric-operator/operatorconfig"
    27  	ordererinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/orderer"
    28  	managermocks "github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources/mocks"
    29  	baseorderer "github.com/IBM-Blockchain/fabric-operator/pkg/offering/base/orderer"
    30  	"github.com/IBM-Blockchain/fabric-operator/pkg/offering/base/orderer/mocks"
    31  	orderermocks "github.com/IBM-Blockchain/fabric-operator/pkg/offering/base/orderer/mocks"
    32  	. "github.com/onsi/ginkgo/v2"
    33  	. "github.com/onsi/gomega"
    34  	corev1 "k8s.io/api/core/v1"
    35  	"k8s.io/apimachinery/pkg/runtime"
    36  	"k8s.io/apimachinery/pkg/types"
    37  	"k8s.io/apimachinery/pkg/util/intstr"
    38  	"sigs.k8s.io/controller-runtime/pkg/client"
    39  )
    40  
    41  var _ = Describe("Base Orderer", func() {
    42  	var (
    43  		orderer        *baseorderer.Orderer
    44  		instance       *current.IBPOrderer
    45  		mockKubeClient *cmocks.Client
    46  		nodeManager    *orderermocks.NodeManager
    47  
    48  		ordererNodeMgr *managermocks.ResourceManager
    49  		update         *mocks.Update
    50  	)
    51  
    52  	BeforeEach(func() {
    53  		mockKubeClient = &cmocks.Client{}
    54  		update = &mocks.Update{}
    55  		instance = &current.IBPOrderer{
    56  			Spec: current.IBPOrdererSpec{
    57  				ClusterSize: 1,
    58  				License: current.License{
    59  					Accept: true,
    60  				},
    61  				OrdererType:       "etcdraft",
    62  				SystemChannelName: "testchainid",
    63  				OrgName:           "orderermsp",
    64  				MSPID:             "orderermsp",
    65  				ExternalAddress:   "ibporderer:7050",
    66  				ImagePullSecrets:  []string{"regcred"},
    67  			},
    68  		}
    69  		instance.Kind = "IBPOrderer"
    70  
    71  		mockKubeClient.GetStub = func(ctx context.Context, types types.NamespacedName, obj client.Object) error {
    72  			switch obj.(type) {
    73  			case *current.IBPOrderer:
    74  				o := obj.(*current.IBPOrderer)
    75  				o.Kind = "IBPOrderer"
    76  				instance = o
    77  			case *corev1.Service:
    78  				o := obj.(*corev1.Service)
    79  				o.Spec.Type = corev1.ServiceTypeNodePort
    80  				o.Spec.Ports = append(o.Spec.Ports, corev1.ServicePort{
    81  					Name: "orderer-api",
    82  					TargetPort: intstr.IntOrString{
    83  						IntVal: 7051,
    84  					},
    85  					NodePort: int32(7051),
    86  				})
    87  			}
    88  			return nil
    89  		}
    90  
    91  		ordererNodeMgr = &managermocks.ResourceManager{}
    92  
    93  		nodeManager = &orderermocks.NodeManager{}
    94  		orderer = &baseorderer.Orderer{
    95  			Client: mockKubeClient,
    96  			Scheme: &runtime.Scheme{},
    97  			Config: &config.Config{
    98  				OrdererInitConfig: &ordererinit.Config{
    99  					ConfigTxFile: "../../../../defaultconfig/orderer/configtx.yaml",
   100  					OUFile:       "../../../../defaultconfig/orderer/ouconfig.yaml",
   101  				},
   102  			},
   103  
   104  			NodeManager:        nodeManager,
   105  			OrdererNodeManager: ordererNodeMgr,
   106  		}
   107  	})
   108  
   109  	Context("Reconciles", func() {
   110  		PIt("reconciles IBPOrderer", func() {
   111  			_, err := orderer.Reconcile(instance, update)
   112  			Expect(err).NotTo(HaveOccurred())
   113  		})
   114  	})
   115  
   116  	Context("check csr hosts", func() {
   117  		It("adds csr hosts if not present", func() {
   118  			instance = &current.IBPOrderer{
   119  				Spec: current.IBPOrdererSpec{
   120  					Secret: &current.SecretSpec{
   121  						Enrollment: &current.EnrollmentSpec{},
   122  					},
   123  				},
   124  			}
   125  			hosts := []string{"test.com", "127.0.0.1"}
   126  			orderer.CheckCSRHosts(instance, hosts)
   127  			Expect(instance.Spec.Secret.Enrollment.TLS).NotTo(BeNil())
   128  			Expect(instance.Spec.Secret.Enrollment.TLS.CSR).NotTo(BeNil())
   129  			Expect(instance.Spec.Secret.Enrollment.TLS.CSR.Hosts).To(Equal(hosts))
   130  		})
   131  
   132  		It("appends csr hosts if passed", func() {
   133  			hostsCustom := []string{"custom.domain.com"}
   134  			hosts := []string{"test.com", "127.0.0.1"}
   135  			instance = &current.IBPOrderer{
   136  				Spec: current.IBPOrdererSpec{
   137  					Secret: &current.SecretSpec{
   138  						Enrollment: &current.EnrollmentSpec{
   139  							TLS: &current.Enrollment{
   140  								CSR: &current.CSR{
   141  									Hosts: hostsCustom,
   142  								},
   143  							},
   144  						},
   145  					},
   146  				},
   147  			}
   148  			orderer.CheckCSRHosts(instance, hosts)
   149  			Expect(instance.Spec.Secret.Enrollment.TLS).NotTo(BeNil())
   150  			Expect(instance.Spec.Secret.Enrollment.TLS.CSR).NotTo(BeNil())
   151  			Expect(instance.Spec.Secret.Enrollment.TLS.CSR.Hosts).To(ContainElement(hostsCustom[0]))
   152  			Expect(instance.Spec.Secret.Enrollment.TLS.CSR.Hosts).To(ContainElement(hosts[0]))
   153  			Expect(instance.Spec.Secret.Enrollment.TLS.CSR.Hosts).To(ContainElement(hosts[1]))
   154  		})
   155  	})
   156  
   157  	Context("images overrides", func() {
   158  		var images *current.OrdererImages
   159  
   160  		Context("using registry url", func() {
   161  			BeforeEach(func() {
   162  				images = &current.OrdererImages{
   163  					OrdererInitImage: "ordererinitimage",
   164  					OrdererInitTag:   "2.0.0",
   165  					OrdererImage:     "ordererimage",
   166  					OrdererTag:       "2.0.0",
   167  					GRPCWebImage:     "grpcimage",
   168  					GRPCWebTag:       "2.0.0",
   169  				}
   170  			})
   171  
   172  			It("overrides images based with registry url and does not append more value on each call", func() {
   173  				images.Override(images, "ghcr.io/ibm-blockchain/", "amd64")
   174  				Expect(images.OrdererInitImage).To(Equal("ghcr.io/ibm-blockchain/ordererinitimage"))
   175  				Expect(images.OrdererInitTag).To(Equal("2.0.0"))
   176  				Expect(images.OrdererImage).To(Equal("ghcr.io/ibm-blockchain/ordererimage"))
   177  				Expect(images.OrdererTag).To(Equal("2.0.0"))
   178  				Expect(images.GRPCWebImage).To(Equal("ghcr.io/ibm-blockchain/grpcimage"))
   179  				Expect(images.GRPCWebTag).To(Equal("2.0.0"))
   180  			})
   181  
   182  			It("overrides images based with registry url and does not append more value on each call", func() {
   183  				images.Override(images, "ghcr.io/ibm-blockchain/images/", "s390")
   184  				Expect(images.OrdererInitImage).To(Equal("ghcr.io/ibm-blockchain/images/ordererinitimage"))
   185  				Expect(images.OrdererInitTag).To(Equal("2.0.0"))
   186  				Expect(images.OrdererImage).To(Equal("ghcr.io/ibm-blockchain/images/ordererimage"))
   187  				Expect(images.OrdererTag).To(Equal("2.0.0"))
   188  				Expect(images.GRPCWebImage).To(Equal("ghcr.io/ibm-blockchain/images/grpcimage"))
   189  				Expect(images.GRPCWebTag).To(Equal("2.0.0"))
   190  			})
   191  		})
   192  
   193  		Context("using fully qualified path", func() {
   194  			BeforeEach(func() {
   195  				images = &current.OrdererImages{
   196  					OrdererInitImage: "ghcr.io/ibm-blockchain/ordererinitimage",
   197  					OrdererInitTag:   "2.0.0",
   198  					OrdererImage:     "ghcr.io/ibm-blockchain/ordererimage",
   199  					OrdererTag:       "2.0.0",
   200  					GRPCWebImage:     "ghcr.io/ibm-blockchain/grpcimage",
   201  					GRPCWebTag:       "2.0.0",
   202  				}
   203  			})
   204  
   205  			It("keeps images and adds arch to tag", func() {
   206  				images.Override(images, "", "amd64")
   207  				Expect(images.OrdererInitImage).To(Equal("ghcr.io/ibm-blockchain/ordererinitimage"))
   208  				Expect(images.OrdererInitTag).To(Equal("2.0.0"))
   209  				Expect(images.OrdererImage).To(Equal("ghcr.io/ibm-blockchain/ordererimage"))
   210  				Expect(images.OrdererTag).To(Equal("2.0.0"))
   211  				Expect(images.GRPCWebImage).To(Equal("ghcr.io/ibm-blockchain/grpcimage"))
   212  				Expect(images.GRPCWebTag).To(Equal("2.0.0"))
   213  			})
   214  		})
   215  	})
   216  })