github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/action/action_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 action_test
    20  
    21  import (
    22  	"errors"
    23  
    24  	. "github.com/onsi/ginkgo/v2"
    25  	. "github.com/onsi/gomega"
    26  	appsv1 "k8s.io/api/apps/v1"
    27  
    28  	controllermocks "github.com/IBM-Blockchain/fabric-operator/controllers/mocks"
    29  	"github.com/IBM-Blockchain/fabric-operator/pkg/action"
    30  	"github.com/IBM-Blockchain/fabric-operator/pkg/action/mocks"
    31  	"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/common"
    32  )
    33  
    34  var _ = Describe("actions", func() {
    35  	var (
    36  		client *controllermocks.Client
    37  	)
    38  
    39  	BeforeEach(func() {
    40  		client = &controllermocks.Client{}
    41  	})
    42  
    43  	Context("restart", func() {
    44  
    45  		It("returns an error if failed to get deployment", func() {
    46  			client.GetReturns(errors.New("get error"))
    47  			err := action.Restart(client, "name", "namespace")
    48  			Expect(err).To(HaveOccurred())
    49  			Expect(err.Error()).To(ContainSubstring("get error"))
    50  		})
    51  
    52  		It("returns error if fails to patch deployment", func() {
    53  			client.PatchReturns(errors.New("patch error"))
    54  			err := action.Restart(client, "name", "namespace")
    55  			Expect(err).To(HaveOccurred())
    56  			Expect(err.Error()).To(ContainSubstring("patch error"))
    57  		})
    58  
    59  		It("restarts deployment by updating annotations", func() {
    60  			err := action.Restart(client, "name", "namespace")
    61  			Expect(err).NotTo(HaveOccurred())
    62  			_, dep, _, _ := client.PatchArgsForCall(0)
    63  			deployment := dep.(*appsv1.Deployment)
    64  			annotation := deployment.Spec.Template.ObjectMeta.Annotations["kubectl.kubernetes.io/restartedAt"]
    65  			Expect(annotation).NotTo(Equal(""))
    66  
    67  		})
    68  	})
    69  
    70  	Context("reenroll", func() {
    71  		var (
    72  			instance   *mocks.ReenrollInstance
    73  			reenroller *mocks.Reenroller
    74  		)
    75  
    76  		BeforeEach(func() {
    77  			reenroller = &mocks.Reenroller{}
    78  			instance = &mocks.ReenrollInstance{}
    79  		})
    80  
    81  		It("returns an error if pod deletion fails", func() {
    82  			reenroller.RenewCertReturns(errors.New("renew failed"))
    83  			err := action.Reenroll(reenroller, client, common.ECERT, instance, true)
    84  			Expect(err).To(HaveOccurred())
    85  			Expect(err).Should(MatchError(ContainSubstring("renew failed")))
    86  		})
    87  
    88  		It("reenrolls ecert successfully", func() {
    89  			err := action.Reenroll(reenroller, client, common.ECERT, instance, true)
    90  			Expect(err).NotTo(HaveOccurred())
    91  			Expect(reenroller.RenewCertCallCount()).To(Equal(1))
    92  		})
    93  
    94  		It("reenrolls TLS successfully", func() {
    95  			err := action.Reenroll(reenroller, client, common.TLS, instance, true)
    96  			Expect(err).NotTo(HaveOccurred())
    97  			Expect(reenroller.RenewCertCallCount()).To(Equal(1))
    98  		})
    99  	})
   100  })