github.com/IBM-Blockchain/fabric-operator@v1.0.4/integration/actions/ca/ca_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 ca_test
    20  
    21  import (
    22  	"fmt"
    23  	"os"
    24  	"path/filepath"
    25  	"strings"
    26  	"testing"
    27  	"time"
    28  
    29  	. "github.com/onsi/ginkgo/v2"
    30  	. "github.com/onsi/gomega"
    31  	"github.com/onsi/gomega/gexec"
    32  
    33  	"github.com/IBM-Blockchain/fabric-operator/integration"
    34  	"github.com/IBM-Blockchain/fabric-operator/integration/helper"
    35  	ibpclient "github.com/IBM-Blockchain/fabric-operator/pkg/client"
    36  	"github.com/IBM-Blockchain/fabric-operator/pkg/util"
    37  
    38  	"k8s.io/client-go/kubernetes"
    39  )
    40  
    41  func TestCa(t *testing.T) {
    42  	RegisterFailHandler(Fail)
    43  	RunSpecs(t, "Ca Suite")
    44  }
    45  
    46  const (
    47  	ccTarFile = "gocc.tar.gz"
    48  
    49  	FabricBinaryVersion   = "2.2.3"
    50  	FabricCABinaryVersion = "1.5.1"
    51  
    52  	IBPCAS = "ibpcas"
    53  
    54  	pathToRoot = "../../../"
    55  )
    56  
    57  var (
    58  	wd          string // Working directory of test
    59  	namespace   string
    60  	domain      string
    61  	kclient     *kubernetes.Clientset
    62  	ibpCRClient *ibpclient.IBPClient
    63  	colorIndex  uint
    64  	testFailed  bool
    65  	caHost      string
    66  	tlsBytes    []byte
    67  
    68  	org1ca *helper.CA
    69  )
    70  
    71  var _ = BeforeSuite(func() {
    72  	SetDefaultEventuallyTimeout(420 * time.Second)
    73  	SetDefaultEventuallyPollingInterval(time.Second)
    74  
    75  	var err error
    76  	domain = os.Getenv("DOMAIN")
    77  	if domain == "" {
    78  		domain = integration.TestAutomation1IngressDomain
    79  	}
    80  
    81  	wd, err = os.Getwd()
    82  	Expect(err).NotTo(HaveOccurred())
    83  	fmt.Fprintf(GinkgoWriter, "Working directory: %s\n", wd)
    84  
    85  	cleanupFiles()
    86  
    87  	cfg := &integration.Config{
    88  		OperatorServiceAccount: "../../../config/rbac/service_account.yaml",
    89  		OperatorRole:           "../../../config/rbac/role.yaml",
    90  		OperatorRoleBinding:    "../../../config/rbac/role_binding.yaml",
    91  		OperatorDeployment:     "../../../testdata/deploy/operator.yaml",
    92  		OrdererSecret:          "../../../testdata/deploy/orderer/secret.yaml",
    93  		PeerSecret:             "../../../testdata/deploy/peer/secret.yaml",
    94  		ConsoleTLSSecret:       "../../../testdata/deploy/console/tlssecret.yaml",
    95  	}
    96  
    97  	namespace, kclient, ibpCRClient, err = integration.Setup(GinkgoWriter, cfg, "ca-actions", pathToRoot)
    98  	Expect(err).NotTo(HaveOccurred())
    99  
   100  	downloadBinaries()
   101  
   102  	CreateNetwork()
   103  })
   104  
   105  var _ = AfterSuite(func() {
   106  
   107  	if strings.ToLower(os.Getenv("SAVE_TEST")) == "true" {
   108  		return
   109  	}
   110  
   111  	integration.Cleanup(GinkgoWriter, kclient, namespace)
   112  	cleanupFiles()
   113  })
   114  
   115  func CreateNetwork() {
   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  	tlsBytes, 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, tlsBytes)
   134  		}).Should(Equal(true))
   135  	})
   136  
   137  	org1ca.TLSToFile(tlsBytes)
   138  }
   139  
   140  func downloadBinaries() {
   141  	os.Setenv("FABRIC_VERSION", FabricBinaryVersion)
   142  	os.Setenv("FABRIC_CA_VERSION", FabricCABinaryVersion)
   143  	path := pathToRoot + "scripts/download_binaries.sh"
   144  	sess, err := helper.StartSession(
   145  		helper.GetCommand(helper.AbsPath(wd, path)),
   146  		"Download Binaries",
   147  	)
   148  	Expect(err).NotTo(HaveOccurred())
   149  	Eventually(sess).Should(gexec.Exit(0))
   150  }
   151  
   152  func cleanupFiles() {
   153  	os.RemoveAll(filepath.Join(wd, Org1CA().Name))
   154  	os.RemoveAll(filepath.Join(wd, ccTarFile))
   155  }
   156  
   157  func Org1CA() *helper.CA {
   158  	cr := helper.Org1CACR(namespace, domain)
   159  
   160  	return &helper.CA{
   161  		Domain:     domain,
   162  		Name:       cr.Name,
   163  		Namespace:  namespace,
   164  		WorkingDir: wd,
   165  		CR:         cr,
   166  		CRClient:   ibpCRClient,
   167  		KClient:    kclient,
   168  		NativeResourcePoller: integration.NativeResourcePoller{
   169  			Name:      cr.Name,
   170  			Namespace: namespace,
   171  			Client:    kclient,
   172  		},
   173  	}
   174  }