github.com/IBM-Blockchain/fabric-operator@v1.0.4/integration/e2ev2/console_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  	"context"
    23  	"encoding/json"
    24  	"math/rand"
    25  	"time"
    26  
    27  	. "github.com/onsi/ginkgo/v2"
    28  	. "github.com/onsi/gomega"
    29  
    30  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    31  	"github.com/IBM-Blockchain/fabric-operator/integration"
    32  	k8serrors "k8s.io/apimachinery/pkg/api/errors"
    33  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    34  )
    35  
    36  var (
    37  	console *Console
    38  )
    39  
    40  var _ = Describe("console", func() {
    41  	BeforeEach(func() {
    42  		Eventually(console.PodIsRunning).Should((Equal(true)))
    43  	})
    44  
    45  	AfterEach(func() {
    46  		// Set flag if a test falls
    47  		if CurrentGinkgoTestDescription().Failed {
    48  			testFailed = true
    49  		}
    50  	})
    51  
    52  	Context("trigger actions", func() {
    53  		var (
    54  			podName    string
    55  			ibpconsole *current.IBPConsole
    56  		)
    57  
    58  		BeforeEach(func() {
    59  			Eventually(func() int {
    60  				return len(console.GetPods())
    61  			}).Should(Equal(1))
    62  
    63  			podName = console.GetPods()[0].Name
    64  
    65  			result := ibpCRClient.Get().Namespace(namespace).Resource("ibpconsoles").Name(console.Name).Do(context.TODO())
    66  			Expect(result.Error()).NotTo(HaveOccurred())
    67  
    68  			ibpconsole = &current.IBPConsole{}
    69  			result.Into(ibpconsole)
    70  		})
    71  
    72  		When("spec has restart flag set to true", func() {
    73  			BeforeEach(func() {
    74  				ibpconsole.Spec.Action.Restart = true
    75  			})
    76  
    77  			It("performs restart action", func() {
    78  				bytes, err := json.Marshal(ibpconsole)
    79  				Expect(err).NotTo(HaveOccurred())
    80  
    81  				result := ibpCRClient.Put().Namespace(namespace).Resource("ibpconsoles").Name(console.Name).Body(bytes).Do(context.TODO())
    82  				Expect(result.Error()).NotTo(HaveOccurred())
    83  
    84  				Eventually(console.PodIsRunning).Should((Equal(true)))
    85  
    86  				By("restarting console pod", func() {
    87  					Eventually(func() bool {
    88  						pods := console.GetPods()
    89  						if len(pods) == 0 {
    90  							return false
    91  						}
    92  
    93  						newPodName := pods[0].Name
    94  						if newPodName != podName {
    95  							return true
    96  						}
    97  
    98  						return false
    99  					}).Should(Equal(true))
   100  				})
   101  
   102  				By("setting restart flag back to false after restart", func() {
   103  					Eventually(func() bool {
   104  						result := ibpCRClient.Get().Namespace(namespace).Resource("ibpconsoles").Name(console.Name).Do(context.TODO())
   105  						console := &current.IBPConsole{}
   106  						result.Into(console)
   107  
   108  						return console.Spec.Action.Restart
   109  					}).Should(Equal(false))
   110  				})
   111  			})
   112  		})
   113  	})
   114  
   115  })
   116  
   117  func CreateConsole(console *Console) {
   118  	result := ibpCRClient.Post().Namespace(namespace).Resource("ibpconsoles").Body(console.CR).Do(context.TODO())
   119  	err := result.Error()
   120  	if !k8serrors.IsAlreadyExists(err) {
   121  		Expect(result.Error()).NotTo(HaveOccurred())
   122  	}
   123  }
   124  
   125  func GetConsole() *Console {
   126  	consolePort := randNum(30000, 32768)
   127  	proxyPort := randNum(30000, 32768)
   128  
   129  	useTagsFlag := true
   130  
   131  	cr := &current.IBPConsole{
   132  		TypeMeta: metav1.TypeMeta{
   133  			Kind: "IBPConsole",
   134  		},
   135  		ObjectMeta: metav1.ObjectMeta{
   136  			Name:      "ibpconsole",
   137  			Namespace: namespace,
   138  		},
   139  		Spec: current.IBPConsoleSpec{
   140  			License: current.License{
   141  				Accept: true,
   142  			},
   143  			ConnectionString:   "http://localhost:5984",
   144  			ServiceAccountName: "ibpconsole1",
   145  			NetworkInfo: &current.NetworkInfo{
   146  				Domain:      "test-domain",
   147  				ConsolePort: consolePort,
   148  				ProxyPort:   proxyPort,
   149  			},
   150  			Email:            "admin@ibm.com",
   151  			Password:         "cGFzc3dvcmQ=",
   152  			Zone:             "select",
   153  			Region:           "select",
   154  			ImagePullSecrets: []string{"regcred"},
   155  			Images: &current.ConsoleImages{
   156  				ConfigtxlatorImage: integration.ConfigtxlatorImage,
   157  				ConfigtxlatorTag:   integration.ConfigtxlatorTag,
   158  				ConsoleImage:       integration.ConsoleImage,
   159  				ConsoleTag:         integration.ConsoleTag,
   160  				ConsoleInitImage:   integration.InitImage,
   161  				ConsoleInitTag:     integration.InitTag,
   162  				CouchDBImage:       integration.CouchdbImage,
   163  				CouchDBTag:         integration.CouchdbTag,
   164  				DeployerImage:      integration.DeployerImage,
   165  				DeployerTag:        integration.DeployerTag,
   166  			},
   167  			Versions: &current.Versions{
   168  				CA: map[string]current.VersionCA{
   169  					integration.FabricCAVersion: current.VersionCA{
   170  						Default: true,
   171  						Version: integration.FabricCAVersion,
   172  						Image: current.CAImages{
   173  							CAInitImage: integration.InitImage,
   174  							CAInitTag:   integration.InitTag,
   175  							CAImage:     integration.CaImage,
   176  							CATag:       integration.CaTag,
   177  						},
   178  					},
   179  				},
   180  				Peer: map[string]current.VersionPeer{
   181  					integration.FabricVersion: current.VersionPeer{
   182  						Default: true,
   183  						Version: integration.FabricVersion,
   184  						Image: current.PeerImages{
   185  							PeerInitImage: integration.InitImage,
   186  							PeerInitTag:   integration.InitTag,
   187  							PeerImage:     integration.PeerImage,
   188  							PeerTag:       integration.PeerTag,
   189  							GRPCWebImage:  integration.GrpcwebImage,
   190  							GRPCWebTag:    integration.GrpcwebTag,
   191  							CouchDBImage:  integration.CouchdbImage,
   192  							CouchDBTag:    integration.CouchdbTag,
   193  						},
   194  					},
   195  				},
   196  				Orderer: map[string]current.VersionOrderer{
   197  					integration.FabricVersion: current.VersionOrderer{
   198  						Default: true,
   199  						Version: integration.FabricVersion,
   200  						Image: current.OrdererImages{
   201  							OrdererInitImage: integration.InitImage,
   202  							OrdererInitTag:   integration.InitTag,
   203  							OrdererImage:     integration.OrdererImage,
   204  							OrdererTag:       integration.OrdererTag,
   205  							GRPCWebImage:     integration.GrpcwebImage,
   206  							GRPCWebTag:       integration.GrpcwebTag,
   207  						},
   208  					},
   209  				},
   210  			},
   211  			UseTags: &useTagsFlag,
   212  		},
   213  	}
   214  
   215  	return &Console{
   216  		Name: cr.Name,
   217  		CR:   cr,
   218  		NativeResourcePoller: integration.NativeResourcePoller{
   219  			Name:      cr.Name,
   220  			Namespace: namespace,
   221  			Client:    kclient,
   222  		},
   223  	}
   224  }
   225  
   226  type Console struct {
   227  	Name string
   228  	CR   *current.IBPConsole
   229  	integration.NativeResourcePoller
   230  }
   231  
   232  func randNum(min, max int) int32 {
   233  	rand.Seed(time.Now().UnixNano())
   234  	return int32(rand.Intn(max-min+1) + min)
   235  }