github.com/IBM-Blockchain/fabric-operator@v1.0.4/integration/migration/fabric/fabric_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 fabric_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  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    35  	"github.com/IBM-Blockchain/fabric-operator/integration"
    36  	"github.com/IBM-Blockchain/fabric-operator/integration/helper"
    37  	ibpclient "github.com/IBM-Blockchain/fabric-operator/pkg/client"
    38  	"github.com/IBM-Blockchain/fabric-operator/pkg/util"
    39  
    40  	corev1 "k8s.io/api/core/v1"
    41  	"k8s.io/apimachinery/pkg/api/resource"
    42  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    43  	"k8s.io/client-go/kubernetes"
    44  )
    45  
    46  func TestFabric(t *testing.T) {
    47  	RegisterFailHandler(Fail)
    48  	RunSpecs(t, "Fabric Suite")
    49  }
    50  
    51  const (
    52  	defaultConfigs        = "../../../defaultconfig"
    53  	defaultPeerDef        = "../../../definitions/peer"
    54  	defaultCADef          = "../../../definitions/ca"
    55  	defaultOrdererDef     = "../../../definitions/orderer"
    56  	defaultConsoleDef     = "../../../definitions/console"
    57  	FabricBinaryVersion   = "2.2.3"
    58  	FabricCABinaryVersion = "1.5.1"
    59  	domain                = "vcap.me"
    60  )
    61  
    62  var (
    63  	namespaceSuffix = "migration"
    64  
    65  	namespace   string
    66  	kclient     *kubernetes.Clientset
    67  	ibpCRClient *ibpclient.IBPClient
    68  	testFailed  bool
    69  	wd          string // Working directory of test
    70  )
    71  
    72  var (
    73  	err error
    74  
    75  	org1ca  *helper.CA
    76  	caHost  string
    77  	tlsCert []byte
    78  )
    79  
    80  var _ = BeforeSuite(func() {
    81  	SetDefaultEventuallyTimeout(300 * time.Second)
    82  	SetDefaultEventuallyPollingInterval(time.Second)
    83  
    84  	wd, err = os.Getwd()
    85  	Expect(err).NotTo(HaveOccurred())
    86  	fmt.Fprintf(GinkgoWriter, "Working directory: %s\n", wd)
    87  
    88  	cfg := &integration.Config{
    89  		OperatorServiceAccount: "../../../config/rbac/service_account.yaml",
    90  		OperatorRole:           "../../../config/rbac/role.yaml",
    91  		OperatorRoleBinding:    "../../../config/rbac/role_binding.yaml",
    92  		OperatorDeployment:     "../../../testdata/deploy/operator.yaml",
    93  		OrdererSecret:          "../../../testdata/deploy/orderer/secret.yaml",
    94  		PeerSecret:             "../../../testdata/deploy/peer/secret.yaml",
    95  		ConsoleTLSSecret:       "../../../testdata/deploy/console/tlssecret.yaml",
    96  	}
    97  
    98  	namespace, kclient, ibpCRClient, err = integration.Setup(GinkgoWriter, cfg, namespaceSuffix, "../../..")
    99  	Expect(err).NotTo(HaveOccurred())
   100  
   101  	downloadBinaries()
   102  	startCA()
   103  	registerAndEnrollIDs()
   104  })
   105  
   106  var _ = AfterSuite(func() {
   107  
   108  	if strings.ToLower(os.Getenv("SAVE_TEST")) == "true" {
   109  		return
   110  	}
   111  
   112  	integration.Cleanup(GinkgoWriter, kclient, namespace)
   113  })
   114  
   115  func startCA() {
   116  	By("starting CA pod", func() {
   117  		org1ca = Org1CA()
   118  		helper.CreateCA(ibpCRClient, org1ca.CR)
   119  
   120  		Eventually(org1ca.PodIsRunning).Should((Equal(true)))
   121  	})
   122  
   123  	profile, err := org1ca.ConnectionProfile()
   124  	Expect(err).NotTo(HaveOccurred())
   125  
   126  	tlsCert, err = util.Base64ToBytes(profile.TLS.Cert)
   127  	Expect(err).NotTo(HaveOccurred())
   128  
   129  	By("performing CA health check", func() {
   130  		Eventually(func() bool {
   131  			url := fmt.Sprintf("https://%s/cainfo", org1ca.Address())
   132  			fmt.Fprintf(GinkgoWriter, "Waiting for CA health check to pass for '%s' at url: %s\n", org1ca.Name, url)
   133  			return org1ca.HealthCheck(url, tlsCert)
   134  		}).Should(Equal(true))
   135  	})
   136  
   137  	org1ca.TLSToFile(tlsCert)
   138  
   139  	caURL, err := url.Parse(profile.Endpoints.API)
   140  	Expect(err).NotTo(HaveOccurred())
   141  	caHost = strings.Split(caURL.Host, ":")[0]
   142  }
   143  
   144  func registerAndEnrollIDs() {
   145  	By("enrolling ca admin", func() {
   146  		os.Setenv("FABRIC_CA_CLIENT_HOME", filepath.Join(wd, org1ca.Name, "org1ca-admin"))
   147  		sess, err := helper.StartSession(
   148  			org1ca.Enroll("admin", "adminpw"),
   149  			"Enroll CA Admin",
   150  		)
   151  		Expect(err).NotTo(HaveOccurred())
   152  		Eventually(sess).Should(gexec.Exit(0))
   153  	})
   154  
   155  	By("registering peer identity", func() {
   156  		os.Setenv("FABRIC_CA_CLIENT_HOME", filepath.Join(wd, org1ca.Name, "org1ca-admin"))
   157  		sess, err := helper.StartSession(
   158  			org1ca.Register(peerUsername, "peerpw", "peer"),
   159  			"Register User",
   160  		)
   161  		Expect(err).NotTo(HaveOccurred())
   162  		Eventually(sess).Should(gexec.Exit(0))
   163  	})
   164  
   165  	By("registering orderer identity", func() {
   166  		os.Setenv("FABRIC_CA_CLIENT_HOME", filepath.Join(wd, org1ca.Name, "org1ca-admin"))
   167  		sess, err := helper.StartSession(
   168  			org1ca.Register(ordererUsername, "ordererpw", "orderer"),
   169  			"Register User",
   170  		)
   171  		Expect(err).NotTo(HaveOccurred())
   172  		Eventually(sess).Should(gexec.Exit(0))
   173  	})
   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, "../../../scripts/download_binaries.sh")),
   181  		"Download Binaries",
   182  	)
   183  	Expect(err).NotTo(HaveOccurred())
   184  	Eventually(sess).Should(gexec.Exit(0))
   185  }
   186  
   187  func Org1CA() *helper.CA {
   188  	cr := &current.IBPCA{
   189  		ObjectMeta: metav1.ObjectMeta{
   190  			Name:      "org1ca",
   191  			Namespace: namespace,
   192  		},
   193  		Spec: current.IBPCASpec{
   194  			License: current.License{
   195  				Accept: true,
   196  			},
   197  			ImagePullSecrets: []string{"regcred"},
   198  			Images: &current.CAImages{
   199  				CAImage:     integration.CaImage,
   200  				CATag:       integration.CaTag,
   201  				CAInitImage: integration.InitImage,
   202  				CAInitTag:   integration.InitTag,
   203  			},
   204  			Resources: &current.CAResources{
   205  				CA: &corev1.ResourceRequirements{
   206  					Requests: corev1.ResourceList{
   207  						corev1.ResourceCPU:              resource.MustParse("50m"),
   208  						corev1.ResourceMemory:           resource.MustParse("100M"),
   209  						corev1.ResourceEphemeralStorage: resource.MustParse("100M"),
   210  					},
   211  					Limits: corev1.ResourceList{
   212  						corev1.ResourceCPU:              resource.MustParse("50m"),
   213  						corev1.ResourceMemory:           resource.MustParse("100M"),
   214  						corev1.ResourceEphemeralStorage: resource.MustParse("1G"),
   215  					},
   216  				},
   217  			},
   218  			Zone:          "select",
   219  			Region:        "select",
   220  			Domain:        domain,
   221  			FabricVersion: integration.FabricCAVersion,
   222  		},
   223  	}
   224  
   225  	return &helper.CA{
   226  		Domain:     domain,
   227  		Name:       cr.Name,
   228  		Namespace:  namespace,
   229  		WorkingDir: wd,
   230  		CR:         cr,
   231  		CRClient:   ibpCRClient,
   232  		KClient:    kclient,
   233  		NativeResourcePoller: integration.NativeResourcePoller{
   234  			Name:      cr.Name,
   235  			Namespace: namespace,
   236  			Client:    kclient,
   237  		},
   238  	}
   239  }