github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/manager/resources/serviceaccount/manager_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 serviceaccount_test
    20  
    21  import (
    22  	"github.com/IBM-Blockchain/fabric-operator/controllers/mocks"
    23  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources"
    24  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources/serviceaccount"
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  	"github.com/pkg/errors"
    28  	corev1 "k8s.io/api/core/v1"
    29  	k8serror "k8s.io/apimachinery/pkg/api/errors"
    30  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    32  )
    33  
    34  var _ = Describe("Service Account manager", func() {
    35  	var (
    36  		mockKubeClient *mocks.Client
    37  		manager        *serviceaccount.Manager
    38  		instance       metav1.Object
    39  	)
    40  
    41  	BeforeEach(func() {
    42  		mockKubeClient = &mocks.Client{}
    43  		manager = &serviceaccount.Manager{
    44  			ServiceAccountFile: "../../../../definitions/ca/serviceaccount.yaml",
    45  			Client:             mockKubeClient,
    46  			OverrideFunc: func(v1.Object, *corev1.ServiceAccount, resources.Action) error {
    47  				return nil
    48  			},
    49  			LabelsFunc: func(v1.Object) map[string]string {
    50  				return map[string]string{}
    51  			},
    52  		}
    53  
    54  		instance = &metav1.ObjectMeta{}
    55  	})
    56  
    57  	Context("reconciles the  instance", func() {
    58  		It("does not try to create service account if the get request returns an error other than 'not found'", func() {
    59  			errMsg := "connection refused"
    60  			mockKubeClient.GetReturns(errors.New(errMsg))
    61  			err := manager.Reconcile(instance, false)
    62  			Expect(err).To(HaveOccurred())
    63  			Expect(err.Error()).To(Equal(errMsg))
    64  		})
    65  
    66  		When("service account does not exist", func() {
    67  			BeforeEach(func() {
    68  				notFoundErr := &k8serror.StatusError{
    69  					ErrStatus: metav1.Status{
    70  						Reason: metav1.StatusReasonNotFound,
    71  					},
    72  				}
    73  				mockKubeClient.GetReturns(notFoundErr)
    74  			})
    75  
    76  			It("returns an error if fails to load default config", func() {
    77  				manager.ServiceAccountFile = "bad.yaml"
    78  				err := manager.Reconcile(instance, false)
    79  				Expect(err).To(HaveOccurred())
    80  				Expect(err.Error()).To(ContainSubstring("no such file or directory"))
    81  			})
    82  
    83  			It("returns an error if override service account value fails", func() {
    84  				manager.OverrideFunc = func(v1.Object, *corev1.ServiceAccount, resources.Action) error {
    85  					return errors.New("creation override failed")
    86  				}
    87  				err := manager.Reconcile(instance, false)
    88  				Expect(err).To(HaveOccurred())
    89  				Expect(err.Error()).Should(ContainSubstring("creation override failed"))
    90  			})
    91  
    92  			It("returns an error if the creation of the service account fails", func() {
    93  				errMsg := "unable to create service account"
    94  				mockKubeClient.CreateReturns(errors.New(errMsg))
    95  				err := manager.Reconcile(instance, false)
    96  				Expect(err).To(HaveOccurred())
    97  				Expect(err.Error()).To(Equal(errMsg))
    98  			})
    99  
   100  			It("does not return an error on a successfull role creation", func() {
   101  				err := manager.Reconcile(instance, false)
   102  				Expect(err).NotTo(HaveOccurred())
   103  			})
   104  		})
   105  	})
   106  })