github.com/IBM-Blockchain/fabric-operator@v1.0.4/integration/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  	"context"
    23  	"encoding/base64"
    24  	"fmt"
    25  	"os"
    26  	"path/filepath"
    27  	"strings"
    28  	"testing"
    29  	"time"
    30  
    31  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    32  	"github.com/IBM-Blockchain/fabric-operator/integration"
    33  	"github.com/IBM-Blockchain/fabric-operator/integration/helper"
    34  	ibpclient "github.com/IBM-Blockchain/fabric-operator/pkg/client"
    35  	. "github.com/onsi/ginkgo/v2"
    36  	. "github.com/onsi/gomega"
    37  	"github.com/onsi/gomega/gexec"
    38  	corev1 "k8s.io/api/core/v1"
    39  	k8serrors "k8s.io/apimachinery/pkg/api/errors"
    40  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    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  	FabricBinaryVersion   = "2.2.3"
    51  	FabricCABinaryVersion = "1.5.1"
    52  	peerAdminUsername     = "peer-admin"
    53  	peerUsername          = "peer"
    54  )
    55  
    56  var (
    57  	namespaceSuffix        = "peer"
    58  	operatorDeploymentFile = "../../testdata/deploy/operator.yaml"
    59  
    60  	namespace   string
    61  	kclient     *kubernetes.Clientset
    62  	ibpCRClient *ibpclient.IBPClient
    63  	testFailed  bool
    64  	wd          string
    65  )
    66  
    67  var _ = BeforeSuite(func() {
    68  	SetDefaultEventuallyTimeout(240 * time.Second)
    69  	SetDefaultEventuallyPollingInterval(time.Second)
    70  
    71  	var err error
    72  
    73  	wd, err = os.Getwd()
    74  	Expect(err).NotTo(HaveOccurred())
    75  	fmt.Fprintf(GinkgoWriter, "Working directory: %s\n", wd)
    76  
    77  	cfg := &integration.Config{
    78  		OperatorDeployment:     operatorDeploymentFile,
    79  		OperatorServiceAccount: "../../config/rbac/service_account.yaml",
    80  		OperatorRole:           "../../config/rbac/role.yaml",
    81  		OperatorRoleBinding:    "../../config/rbac/role_binding.yaml",
    82  		OrdererSecret:          "../../testdata/deploy/orderer/secret.yaml",
    83  		PeerSecret:             "../../testdata/deploy/peer/secret.yaml",
    84  		ConsoleTLSSecret:       "../../testdata/deploy/console/tlssecret.yaml",
    85  	}
    86  
    87  	namespace, kclient, ibpCRClient, err = integration.Setup(GinkgoWriter, cfg, namespaceSuffix, "")
    88  	Expect(err).NotTo(HaveOccurred())
    89  
    90  	downloadBinaries()
    91  })
    92  
    93  var _ = AfterSuite(func() {
    94  
    95  	if strings.ToLower(os.Getenv("SAVE_TEST")) == "true" {
    96  		return
    97  	}
    98  
    99  	err := integration.Cleanup(GinkgoWriter, kclient, namespace)
   100  	Expect(err).NotTo(HaveOccurred())
   101  })
   102  
   103  func CreatePeer(peer *Peer) {
   104  	result := ibpCRClient.Post().Namespace(namespace).Resource("ibppeers").Body(peer.CR).Do(context.TODO())
   105  	err := result.Error()
   106  	if !k8serrors.IsAlreadyExists(err) {
   107  		Expect(err).NotTo(HaveOccurred())
   108  	}
   109  }
   110  
   111  type Peer struct {
   112  	Name string
   113  	CR   *current.IBPPeer
   114  	integration.NativeResourcePoller
   115  }
   116  
   117  func (peer *Peer) pollForCRStatus() current.IBPCRStatusType {
   118  	crStatus := &current.IBPPeer{}
   119  
   120  	result := ibpCRClient.Get().Namespace(namespace).Resource("ibppeers").Name(peer.Name).Do(context.TODO())
   121  	result.Into(crStatus)
   122  
   123  	return crStatus.Status.Type
   124  }
   125  
   126  func (peer *Peer) ingressExists() bool {
   127  	opts := metav1.ListOptions{
   128  		LabelSelector: fmt.Sprintf("app=%s", peer.Name),
   129  	}
   130  	ingressList, err := kclient.NetworkingV1().Ingresses(namespace).List(context.TODO(), opts)
   131  	if err != nil {
   132  		return false
   133  	}
   134  	for _, ingress := range ingressList.Items {
   135  		if strings.HasPrefix(ingress.Name, peer.Name) {
   136  			return true
   137  		}
   138  	}
   139  
   140  	return false
   141  }
   142  
   143  func (peer *Peer) getPVCStorageFromSpec(name string) string {
   144  	pvc, err := kclient.CoreV1().PersistentVolumeClaims(namespace).Get(context.TODO(), name, metav1.GetOptions{})
   145  	if err != nil {
   146  		return ""
   147  	}
   148  
   149  	storage := pvc.Spec.Resources.Requests[corev1.ResourceStorage]
   150  
   151  	return storage.String()
   152  }
   153  
   154  func (peer *Peer) checkAdminCertUpdate() string {
   155  	secretName := fmt.Sprintf("%s-%s-%s", "ecert", peer.Name, "admincerts")
   156  	sec, err := kclient.CoreV1().Secrets(namespace).Get(context.TODO(), secretName, metav1.GetOptions{})
   157  	Expect(err).NotTo(HaveOccurred())
   158  
   159  	certBytes := sec.Data["admincert-0.pem"]
   160  	str := base64.StdEncoding.EncodeToString(certBytes)
   161  	return str
   162  }
   163  
   164  func downloadBinaries() {
   165  	os.Setenv("FABRIC_VERSION", FabricBinaryVersion)
   166  	os.Setenv("FABRIC_CA_VERSION", FabricCABinaryVersion)
   167  	sess, err := helper.StartSession(helper.GetCommand(filepath.Join(wd, "../../scripts/download_binaries.sh")), "Download Binaries")
   168  	Expect(err).NotTo(HaveOccurred())
   169  	Eventually(sess).Should(gexec.Exit(0))
   170  }