github.com/IBM-Blockchain/fabric-operator@v1.0.4/integration/restartmgr/restartmgr_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 restartmgr_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  	baseorderer "github.com/IBM-Blockchain/fabric-operator/pkg/offering/base/orderer"
    40  	"github.com/IBM-Blockchain/fabric-operator/pkg/util"
    41  
    42  	"k8s.io/client-go/kubernetes"
    43  )
    44  
    45  func TestRestart(t *testing.T) {
    46  	RegisterFailHandler(Fail)
    47  	RunSpecs(t, "RestartMgr Suite")
    48  }
    49  
    50  const (
    51  	ccTarFile = "gocc.tar.gz"
    52  
    53  	FabricBinaryVersion   = "2.2.3"
    54  	FabricCABinaryVersion = "1.5.1"
    55  
    56  	peerAdminUsername = "peer-admin"
    57  	peerUsername      = "peer"
    58  	ordererUsername   = "orderer"
    59  
    60  	IBPCAS      = "ibpcas"
    61  	IBPPEERS    = "ibppeers"
    62  	IBPORDERERS = "ibporderers"
    63  )
    64  
    65  var (
    66  	wd          string // Working directory of test
    67  	namespace   string
    68  	domain      string
    69  	kclient     *kubernetes.Clientset
    70  	ibpCRClient *ibpclient.IBPClient
    71  	colorIndex  uint
    72  	testFailed  bool
    73  	caHost      string
    74  	tlsBytes    []byte
    75  
    76  	org1ca   *helper.CA
    77  	org1peer *helper.Peer
    78  	orderer  *helper.Orderer
    79  )
    80  
    81  var _ = BeforeSuite(func() {
    82  	SetDefaultEventuallyTimeout(600 * time.Second)
    83  	SetDefaultEventuallyPollingInterval(time.Second)
    84  
    85  	var err error
    86  
    87  	domain = os.Getenv("DOMAIN")
    88  	if domain == "" {
    89  		domain = integration.TestAutomation1IngressDomain
    90  	}
    91  
    92  	wd, err = os.Getwd()
    93  	Expect(err).NotTo(HaveOccurred())
    94  	fmt.Fprintf(GinkgoWriter, "Working directory: %s\n", wd)
    95  
    96  	cleanupFiles()
    97  
    98  	cfg := &integration.Config{
    99  		OperatorServiceAccount: "../../config/rbac/service_account.yaml",
   100  		OperatorRole:           "../../config/rbac/role.yaml",
   101  		OperatorRoleBinding:    "../../config/rbac/role_binding.yaml",
   102  		OperatorDeployment:     "../../testdata/deploy/operator.yaml",
   103  		OrdererSecret:          "../../testdata/deploy/orderer/secret.yaml",
   104  		PeerSecret:             "../../testdata/deploy/peer/secret.yaml",
   105  		ConsoleTLSSecret:       "../../testdata/deploy/console/tlssecret.yaml",
   106  	}
   107  
   108  	namespace, kclient, ibpCRClient, err = integration.Setup(GinkgoWriter, cfg, "restartmgr", "")
   109  	Expect(err).NotTo(HaveOccurred())
   110  
   111  	downloadBinaries()
   112  
   113  	CreateNetwork()
   114  })
   115  
   116  var _ = AfterSuite(func() {
   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  	By("starting Orderer pod", func() {
   222  		orderer = GetOrderer(profile.TLS.Cert, caHost)
   223  		err = helper.CreateOrderer(ibpCRClient, orderer.CR)
   224  		Expect(err).NotTo(HaveOccurred())
   225  	})
   226  
   227  	Eventually(org1peer.PodIsRunning).Should((Equal(true)))
   228  	Eventually(orderer.Nodes[0].PodIsRunning).Should((Equal(true)))
   229  }
   230  
   231  func downloadBinaries() {
   232  	os.Setenv("FABRIC_VERSION", FabricBinaryVersion)
   233  	os.Setenv("FABRIC_CA_VERSION", FabricCABinaryVersion)
   234  	sess, err := helper.StartSession(
   235  		helper.GetCommand(helper.AbsPath(wd, "../../scripts/download_binaries.sh")),
   236  		"Download Binaries",
   237  	)
   238  	Expect(err).NotTo(HaveOccurred())
   239  	Eventually(sess).Should(gexec.Exit(0))
   240  }
   241  
   242  func cleanupFiles() {
   243  	os.RemoveAll(filepath.Join(wd, Org1CA().Name))
   244  	os.RemoveAll(filepath.Join(wd, Org1Peer("", "", "").Name))
   245  	os.RemoveAll(filepath.Join(wd, GetOrderer("", "").Nodes[0].Name))
   246  	os.RemoveAll(filepath.Join(wd, ccTarFile))
   247  }
   248  
   249  func Org1CA() *helper.CA {
   250  	cr := helper.Org1CACR(namespace, domain)
   251  
   252  	return &helper.CA{
   253  		Domain:     domain,
   254  		Name:       cr.Name,
   255  		Namespace:  namespace,
   256  		WorkingDir: wd,
   257  		CR:         cr,
   258  		CRClient:   ibpCRClient,
   259  		KClient:    kclient,
   260  		NativeResourcePoller: integration.NativeResourcePoller{
   261  			Name:      cr.Name,
   262  			Namespace: namespace,
   263  			Client:    kclient,
   264  		},
   265  	}
   266  }
   267  
   268  func Org1Peer(tlsCert, caHost, adminCert string) *helper.Peer {
   269  	cr, err := helper.Org1PeerCR(namespace, domain, peerUsername, tlsCert, caHost, adminCert)
   270  	Expect(err).NotTo(HaveOccurred())
   271  
   272  	return &helper.Peer{
   273  		Domain:     domain,
   274  		Name:       cr.Name,
   275  		Namespace:  namespace,
   276  		WorkingDir: wd,
   277  		CR:         cr,
   278  		CRClient:   ibpCRClient,
   279  		KClient:    kclient,
   280  		NativeResourcePoller: integration.NativeResourcePoller{
   281  			Name:      cr.Name,
   282  			Namespace: namespace,
   283  			Client:    kclient,
   284  		},
   285  	}
   286  }
   287  
   288  func GetOrderer(tlsCert, caHost string) *helper.Orderer {
   289  	cr, err := helper.OrdererCR(namespace, domain, ordererUsername, tlsCert, caHost)
   290  	Expect(err).NotTo(HaveOccurred())
   291  
   292  	nodes := []helper.Orderer{
   293  		helper.Orderer{
   294  			Name:      cr.Name + "node1",
   295  			Namespace: namespace,
   296  			CR:        cr.DeepCopy(),
   297  			NodeName:  fmt.Sprintf("%s%s%d", cr.Name, baseorderer.NODE, 1),
   298  			NativeResourcePoller: integration.NativeResourcePoller{
   299  				Name:      cr.Name + "node1",
   300  				Namespace: namespace,
   301  				Client:    kclient,
   302  			},
   303  		},
   304  	}
   305  
   306  	nodes[0].CR.ObjectMeta.Name = cr.Name + "node1"
   307  
   308  	return &helper.Orderer{
   309  		Name:      cr.Name,
   310  		Namespace: namespace,
   311  		CR:        cr,
   312  		NodeName:  fmt.Sprintf("%s-%s%d", cr.Name, baseorderer.NODE, 1),
   313  		NativeResourcePoller: integration.NativeResourcePoller{
   314  			Name:      cr.Name,
   315  			Namespace: namespace,
   316  			Client:    kclient,
   317  		},
   318  		Nodes:    nodes,
   319  		CRClient: ibpCRClient,
   320  	}
   321  }