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