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