github.com/IBM-Blockchain/fabric-operator@v1.0.4/integration/actions/peer/peer_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 peer_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 TestPeer(t *testing.T) {
    45  	RegisterFailHandler(Fail)
    46  	RunSpecs(t, "Peer 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  
    58  	IBPCAS   = "ibpcas"
    59  	IBPPEERS = "ibppeers"
    60  
    61  	pathToRoot = "../../../"
    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  )
    78  
    79  var _ = BeforeSuite(func() {
    80  	SetDefaultEventuallyTimeout(420 * time.Second)
    81  	SetDefaultEventuallyPollingInterval(time.Second)
    82  
    83  	var err error
    84  
    85  	domain = os.Getenv("DOMAIN")
    86  	if domain == "" {
    87  		domain = integration.TestAutomation1IngressDomain
    88  	}
    89  
    90  	wd, err = os.Getwd()
    91  	Expect(err).NotTo(HaveOccurred())
    92  	fmt.Fprintf(GinkgoWriter, "Working directory: %s\n", wd)
    93  
    94  	cleanupFiles()
    95  
    96  	cfg := &integration.Config{
    97  		OperatorServiceAccount: "../../../config/rbac/service_account.yaml",
    98  		OperatorRole:           "../../../config/rbac/role.yaml",
    99  		OperatorRoleBinding:    "../../../config/rbac/role_binding.yaml",
   100  		OperatorDeployment:     "../../../testdata/deploy/operator.yaml",
   101  		OrdererSecret:          "../../../testdata/deploy/orderer/secret.yaml",
   102  		PeerSecret:             "../../../testdata/deploy/peer/secret.yaml",
   103  		ConsoleTLSSecret:       "../../../testdata/deploy/console/tlssecret.yaml",
   104  	}
   105  
   106  	namespace, kclient, ibpCRClient, err = integration.Setup(GinkgoWriter, cfg, "peer-actions", pathToRoot)
   107  	Expect(err).NotTo(HaveOccurred())
   108  
   109  	downloadBinaries()
   110  
   111  	CreateNetwork()
   112  })
   113  
   114  var _ = AfterSuite(func() {
   115  
   116  	if strings.ToLower(os.Getenv("SAVE_TEST")) == "true" {
   117  		return
   118  	}
   119  
   120  	integration.Cleanup(GinkgoWriter, kclient, namespace)
   121  
   122  	cleanupFiles()
   123  })
   124  
   125  func CreateNetwork() {
   126  	By("starting CA pod", func() {
   127  		org1ca = Org1CA()
   128  		helper.CreateCA(ibpCRClient, org1ca.CR)
   129  
   130  		Eventually(org1ca.PodIsRunning).Should((Equal(true)))
   131  	})
   132  
   133  	profile, err := org1ca.ConnectionProfile()
   134  	Expect(err).NotTo(HaveOccurred())
   135  
   136  	tlsBytes, err = util.Base64ToBytes(profile.TLS.Cert)
   137  	Expect(err).NotTo(HaveOccurred())
   138  
   139  	By("performing CA health check", func() {
   140  		Eventually(func() bool {
   141  			url := fmt.Sprintf("https://%s/cainfo", org1ca.Address())
   142  			fmt.Fprintf(GinkgoWriter, "Waiting for CA health check to pass for '%s' at url: %s\n", org1ca.Name, url)
   143  			return org1ca.HealthCheck(url, tlsBytes)
   144  		}).Should(Equal(true))
   145  	})
   146  
   147  	org1ca.TLSToFile(tlsBytes)
   148  
   149  	caURL, err := url.Parse(profile.Endpoints.API)
   150  	Expect(err).NotTo(HaveOccurred())
   151  	caHost = strings.Split(caURL.Host, ":")[0]
   152  
   153  	By("enrolling ca admin", func() {
   154  		os.Setenv("FABRIC_CA_CLIENT_HOME", filepath.Join(wd, org1ca.Name, "org1ca-admin"))
   155  		sess, err := helper.StartSession(org1ca.Enroll("admin", "adminpw"), "Enroll CA Admin")
   156  		Expect(err).NotTo(HaveOccurred())
   157  		Eventually(sess).Should(gexec.Exit(0))
   158  	})
   159  
   160  	By("registering peer identity", func() {
   161  		os.Setenv("FABRIC_CA_CLIENT_HOME", filepath.Join(wd, org1ca.Name, "org1ca-admin"))
   162  		sess, err := helper.StartSession(org1ca.Register(peerUsername, "peerpw", "peer"), "Register User")
   163  		Expect(err).NotTo(HaveOccurred())
   164  		Eventually(sess).Should(gexec.Exit(0))
   165  
   166  		os.Setenv("FABRIC_CA_CLIENT_HOME", filepath.Join(wd, org1ca.Name, "org1ca-admin"))
   167  		sess, err = helper.StartSession(org1ca.Register("peer2", "peerpw2", "peer"), "Register User")
   168  		Expect(err).NotTo(HaveOccurred())
   169  		Eventually(sess).Should(gexec.Exit(0))
   170  	})
   171  
   172  	By("registering and enrolling peer admin", func() {
   173  		os.Setenv("FABRIC_CA_CLIENT_HOME", filepath.Join(wd, org1ca.Name, "org1ca-admin"))
   174  		sess, err := helper.StartSession(org1ca.Register(peerAdminUsername, "peer-adminpw", "admin"), "Register Peer Admin")
   175  		Expect(err).NotTo(HaveOccurred())
   176  		Eventually(sess).Should(gexec.Exit(0))
   177  
   178  		os.Setenv("FABRIC_CA_CLIENT_HOME", filepath.Join(wd, "org1peer", peerAdminUsername))
   179  		sess, err = helper.StartSession(org1ca.Enroll(peerAdminUsername, "peer-adminpw"), "Enroll Peer Admin")
   180  		Expect(err).NotTo(HaveOccurred())
   181  		Eventually(sess).Should(gexec.Exit(0))
   182  
   183  		os.Setenv("FABRIC_CA_CLIENT_HOME", filepath.Join(wd, "org1peer", peerAdminUsername+"2"))
   184  		sess, err = helper.StartSession(org1ca.Enroll(peerAdminUsername, "peer-adminpw"), "Enroll Second Peer Admin")
   185  		Expect(err).NotTo(HaveOccurred())
   186  		Eventually(sess).Should(gexec.Exit(0))
   187  	})
   188  
   189  	adminCertBytes, err := ioutil.ReadFile(
   190  		filepath.Join(
   191  			wd,
   192  			"org1peer",
   193  			peerAdminUsername,
   194  			"msp",
   195  			"signcerts",
   196  			"cert.pem",
   197  		),
   198  	)
   199  	Expect(err).NotTo(HaveOccurred())
   200  	adminCertB64 := base64.StdEncoding.EncodeToString(adminCertBytes)
   201  
   202  	By("starting Peer pod", func() {
   203  		org1peer = Org1Peer(profile.TLS.Cert, caHost, adminCertB64)
   204  		err = helper.CreatePeer(ibpCRClient, org1peer.CR)
   205  		Expect(err).NotTo(HaveOccurred())
   206  	})
   207  
   208  	Eventually(org1peer.PodIsRunning).Should((Equal(true)))
   209  }
   210  
   211  func downloadBinaries() {
   212  	os.Setenv("FABRIC_VERSION", FabricBinaryVersion)
   213  	os.Setenv("FABRIC_CA_VERSION", FabricCABinaryVersion)
   214  	sess, err := helper.StartSession(
   215  		helper.GetCommand(helper.AbsPath(wd, pathToRoot+"scripts/download_binaries.sh")),
   216  		"Download Binaries",
   217  	)
   218  	Expect(err).NotTo(HaveOccurred())
   219  	Eventually(sess).Should(gexec.Exit(0))
   220  }
   221  
   222  func cleanupFiles() {
   223  	os.RemoveAll(filepath.Join(wd, Org1CA().Name))
   224  	os.RemoveAll(filepath.Join(wd, Org1Peer("", "", "").Name))
   225  	os.RemoveAll(filepath.Join(wd, ccTarFile))
   226  }
   227  
   228  func Org1CA() *helper.CA {
   229  	cr := helper.Org1CACR(namespace, domain)
   230  
   231  	return &helper.CA{
   232  		Domain:     domain,
   233  		Name:       cr.Name,
   234  		Namespace:  namespace,
   235  		WorkingDir: wd,
   236  		CR:         cr,
   237  		CRClient:   ibpCRClient,
   238  		KClient:    kclient,
   239  		NativeResourcePoller: integration.NativeResourcePoller{
   240  			Name:      cr.Name,
   241  			Namespace: namespace,
   242  			Client:    kclient,
   243  		},
   244  	}
   245  }
   246  
   247  func Org1Peer(tlsCert, caHost, adminCert string) *helper.Peer {
   248  	cr, err := helper.Org1PeerCR(namespace, domain, peerUsername, tlsCert, caHost, adminCert)
   249  	Expect(err).NotTo(HaveOccurred())
   250  
   251  	return &helper.Peer{
   252  		Domain:     domain,
   253  		Name:       cr.Name,
   254  		Namespace:  namespace,
   255  		WorkingDir: wd,
   256  		CR:         cr,
   257  		CRClient:   ibpCRClient,
   258  		KClient:    kclient,
   259  		NativeResourcePoller: integration.NativeResourcePoller{
   260  			Name:      cr.Name,
   261  			Namespace: namespace,
   262  			Client:    kclient,
   263  		},
   264  	}
   265  }