github.com/IBM-Blockchain/fabric-operator@v1.0.4/integration/actions/orderer/orderer_suite_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 orderer_test
    20  
    21  import (
    22  	"fmt"
    23  	"net/url"
    24  	"os"
    25  	"path/filepath"
    26  	"strings"
    27  	"testing"
    28  	"time"
    29  
    30  	. "github.com/onsi/ginkgo/v2"
    31  	. "github.com/onsi/gomega"
    32  	"github.com/onsi/gomega/gexec"
    33  
    34  	"github.com/IBM-Blockchain/fabric-operator/integration"
    35  	"github.com/IBM-Blockchain/fabric-operator/integration/helper"
    36  	ibpclient "github.com/IBM-Blockchain/fabric-operator/pkg/client"
    37  	baseorderer "github.com/IBM-Blockchain/fabric-operator/pkg/offering/base/orderer"
    38  	"github.com/IBM-Blockchain/fabric-operator/pkg/util"
    39  
    40  	"k8s.io/client-go/kubernetes"
    41  )
    42  
    43  func TestOrderer(t *testing.T) {
    44  	RegisterFailHandler(Fail)
    45  	RunSpecs(t, "Orderer Suite")
    46  }
    47  
    48  const (
    49  	ccTarFile = "gocc.tar.gz"
    50  
    51  	FabricBinaryVersion   = "2.2.3"
    52  	FabricCABinaryVersion = "1.5.1"
    53  
    54  	ordererUsername = "orderer"
    55  
    56  	IBPCAS      = "ibpcas"
    57  	IBPORDERERS = "ibporderers"
    58  
    59  	pathToRoot = "../../../"
    60  )
    61  
    62  var (
    63  	wd          string // Working directory of test
    64  	namespace   string
    65  	domain      string
    66  	kclient     *kubernetes.Clientset
    67  	ibpCRClient *ibpclient.IBPClient
    68  	colorIndex  uint
    69  	testFailed  bool
    70  	caHost      string
    71  	tlsBytes    []byte
    72  
    73  	org1ca  *helper.CA
    74  	orderer *helper.Orderer
    75  )
    76  
    77  var _ = BeforeSuite(func() {
    78  	SetDefaultEventuallyTimeout(480 * time.Second)
    79  	SetDefaultEventuallyPollingInterval(time.Second)
    80  
    81  	var err error
    82  	domain = os.Getenv("DOMAIN")
    83  	if domain == "" {
    84  		domain = integration.TestAutomation1IngressDomain
    85  	}
    86  
    87  	wd, err = os.Getwd()
    88  	Expect(err).NotTo(HaveOccurred())
    89  	fmt.Fprintf(GinkgoWriter, "Working directory: %s\n", wd)
    90  
    91  	cleanupFiles()
    92  
    93  	cfg := &integration.Config{
    94  		OperatorServiceAccount: "../../../config/rbac/service_account.yaml",
    95  		OperatorRole:           "../../../config/rbac/role.yaml",
    96  		OperatorRoleBinding:    "../../../config/rbac/role_binding.yaml",
    97  		OperatorDeployment:     "../../../testdata/deploy/operator.yaml",
    98  		OrdererSecret:          "../../../testdata/deploy/orderer/secret.yaml",
    99  		PeerSecret:             "../../../testdata/deploy/peer/secret.yaml",
   100  		ConsoleTLSSecret:       "../../../testdata/deploy/console/tlssecret.yaml",
   101  	}
   102  
   103  	namespace, kclient, ibpCRClient, err = integration.Setup(GinkgoWriter, cfg, "orderer-actions", pathToRoot)
   104  	Expect(err).NotTo(HaveOccurred())
   105  
   106  	downloadBinaries()
   107  
   108  	CreateNetwork()
   109  })
   110  
   111  var _ = AfterSuite(func() {
   112  	if strings.ToLower(os.Getenv("SAVE_TEST")) == "true" {
   113  		return
   114  	}
   115  
   116  	integration.Cleanup(GinkgoWriter, kclient, namespace)
   117  	cleanupFiles()
   118  })
   119  
   120  func CreateNetwork() {
   121  	By("starting CA pod", func() {
   122  		org1ca = Org1CA()
   123  		helper.CreateCA(ibpCRClient, org1ca.CR)
   124  
   125  		Eventually(org1ca.PodIsRunning).Should((Equal(true)))
   126  	})
   127  
   128  	profile, err := org1ca.ConnectionProfile()
   129  	Expect(err).NotTo(HaveOccurred())
   130  
   131  	tlsBytes, err = util.Base64ToBytes(profile.TLS.Cert)
   132  	Expect(err).NotTo(HaveOccurred())
   133  
   134  	By("performing CA health check", func() {
   135  		Eventually(func() bool {
   136  			url := fmt.Sprintf("https://%s/cainfo", org1ca.Address())
   137  			fmt.Fprintf(GinkgoWriter, "Waiting for CA health check to pass for '%s' at url: %s\n", org1ca.Name, url)
   138  			return org1ca.HealthCheck(url, tlsBytes)
   139  		}).Should(Equal(true))
   140  	})
   141  
   142  	org1ca.TLSToFile(tlsBytes)
   143  
   144  	caURL, err := url.Parse(profile.Endpoints.API)
   145  	Expect(err).NotTo(HaveOccurred())
   146  	caHost = strings.Split(caURL.Host, ":")[0]
   147  
   148  	By("enrolling ca admin", func() {
   149  		os.Setenv("FABRIC_CA_CLIENT_HOME", filepath.Join(wd, org1ca.Name, "org1ca-admin"))
   150  		sess, err := helper.StartSession(org1ca.Enroll("admin", "adminpw"), "Enroll CA Admin")
   151  		Expect(err).NotTo(HaveOccurred())
   152  		Eventually(sess).Should(gexec.Exit(0))
   153  	})
   154  
   155  	By("registering orderer identity", func() {
   156  		os.Setenv("FABRIC_CA_CLIENT_HOME", filepath.Join(wd, org1ca.Name, "org1ca-admin"))
   157  		sess, err := helper.StartSession(org1ca.Register(ordererUsername, "ordererpw", "orderer"), "Register Orderer Identity")
   158  		Expect(err).NotTo(HaveOccurred())
   159  		Eventually(sess).Should(gexec.Exit(0))
   160  
   161  		os.Setenv("FABRIC_CA_CLIENT_HOME", filepath.Join(wd, org1ca.Name, "org1ca-admin"))
   162  		sess, err = helper.StartSession(org1ca.Register("orderer2", "ordererpw2", "orderer"), "Register Orderer Identity")
   163  		Expect(err).NotTo(HaveOccurred())
   164  		Eventually(sess).Should(gexec.Exit(0))
   165  	})
   166  
   167  	By("starting Orderer pod", func() {
   168  		orderer = GetOrderer(profile.TLS.Cert, caHost)
   169  		err = helper.CreateOrderer(ibpCRClient, orderer.CR)
   170  		Expect(err).NotTo(HaveOccurred())
   171  	})
   172  
   173  	Eventually(orderer.Nodes[0].PodIsRunning).Should((Equal(true)))
   174  }
   175  
   176  func downloadBinaries() {
   177  	os.Setenv("FABRIC_VERSION", FabricBinaryVersion)
   178  	os.Setenv("FABRIC_CA_VERSION", FabricCABinaryVersion)
   179  	sess, err := helper.StartSession(
   180  		helper.GetCommand(helper.AbsPath(wd, pathToRoot+"scripts/download_binaries.sh")),
   181  		"Download Binaries",
   182  	)
   183  	Expect(err).NotTo(HaveOccurred())
   184  	Eventually(sess).Should(gexec.Exit(0))
   185  }
   186  
   187  func cleanupFiles() {
   188  	os.RemoveAll(filepath.Join(wd, Org1CA().Name))
   189  	os.RemoveAll(filepath.Join(wd, GetOrderer("", "").Nodes[0].Name))
   190  	os.RemoveAll(filepath.Join(wd, ccTarFile))
   191  }
   192  
   193  func Org1CA() *helper.CA {
   194  	cr := helper.Org1CACR(namespace, domain)
   195  
   196  	return &helper.CA{
   197  		Domain:     domain,
   198  		Name:       cr.Name,
   199  		Namespace:  namespace,
   200  		WorkingDir: wd,
   201  		CR:         cr,
   202  		CRClient:   ibpCRClient,
   203  		KClient:    kclient,
   204  		NativeResourcePoller: integration.NativeResourcePoller{
   205  			Name:      cr.Name,
   206  			Namespace: namespace,
   207  			Client:    kclient,
   208  		},
   209  	}
   210  }
   211  
   212  func GetOrderer(tlsCert, caHost string) *helper.Orderer {
   213  	cr, err := helper.OrdererCR(namespace, domain, ordererUsername, tlsCert, caHost)
   214  	Expect(err).NotTo(HaveOccurred())
   215  
   216  	nodes := []helper.Orderer{
   217  		helper.Orderer{
   218  			Name:      cr.Name + "node1",
   219  			Namespace: namespace,
   220  			CR:        cr.DeepCopy(),
   221  			NodeName:  fmt.Sprintf("%s%s%d", cr.Name, baseorderer.NODE, 1),
   222  			NativeResourcePoller: integration.NativeResourcePoller{
   223  				Name:      cr.Name + "node1",
   224  				Namespace: namespace,
   225  				Client:    kclient,
   226  			},
   227  		},
   228  		helper.Orderer{
   229  			Name:      cr.Name + "node2",
   230  			Namespace: namespace,
   231  			CR:        cr.DeepCopy(),
   232  			NodeName:  fmt.Sprintf("%s%s%d", cr.Name, baseorderer.NODE, 2),
   233  			NativeResourcePoller: integration.NativeResourcePoller{
   234  				Name:      cr.Name + "node2",
   235  				Namespace: namespace,
   236  				Client:    kclient,
   237  			},
   238  		},
   239  		helper.Orderer{
   240  			Name:      cr.Name + "node3",
   241  			Namespace: namespace,
   242  			CR:        cr.DeepCopy(),
   243  			NodeName:  fmt.Sprintf("%s%s%d", cr.Name, baseorderer.NODE, 3),
   244  			NativeResourcePoller: integration.NativeResourcePoller{
   245  				Name:      cr.Name + "node3",
   246  				Namespace: namespace,
   247  				Client:    kclient,
   248  			},
   249  		},
   250  	}
   251  
   252  	nodes[0].CR.ObjectMeta.Name = cr.Name + "node1"
   253  	nodes[1].CR.ObjectMeta.Name = cr.Name + "node2"
   254  	nodes[2].CR.ObjectMeta.Name = cr.Name + "node3"
   255  
   256  	return &helper.Orderer{
   257  		Name:      cr.Name,
   258  		Namespace: namespace,
   259  		CR:        cr,
   260  		NodeName:  fmt.Sprintf("%s-%s%d", cr.Name, baseorderer.NODE, 1),
   261  		NativeResourcePoller: integration.NativeResourcePoller{
   262  			Name:      cr.Name,
   263  			Namespace: namespace,
   264  			Client:    kclient,
   265  		},
   266  		Nodes:    nodes,
   267  		CRClient: ibpCRClient,
   268  	}
   269  }