github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/common/enroller/enroller_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 enroller_test
    20  
    21  import (
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  	"github.com/pkg/errors"
    25  
    26  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    27  	"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/common/config"
    28  	"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/common/enroller"
    29  	"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/common/enroller/mocks"
    30  )
    31  
    32  var _ = Describe("Enroller", func() {
    33  	var (
    34  		mockCryptoEnroller *mocks.CryptoEnroller
    35  		testEnroller       *enroller.Enroller
    36  	)
    37  
    38  	BeforeEach(func() {
    39  		mockCryptoEnroller = &mocks.CryptoEnroller{}
    40  		testEnroller = &enroller.Enroller{
    41  			Enroller: mockCryptoEnroller,
    42  		}
    43  	})
    44  
    45  	Context("get crypto", func() {
    46  		BeforeEach(func() {
    47  			mockCryptoEnroller.GetEnrollmentRequestReturns(&current.Enrollment{})
    48  			mockCryptoEnroller.EnrollReturns(&config.Response{}, nil)
    49  		})
    50  
    51  		It("returns response", func() {
    52  			resp, err := testEnroller.GetCrypto()
    53  			Expect(err).NotTo(HaveOccurred())
    54  			Expect(resp).NotTo(BeNil())
    55  		})
    56  
    57  		It("returns error if enroll fails", func() {
    58  			mockCryptoEnroller.EnrollReturns(nil, errors.New("enroll failed"))
    59  
    60  			resp, err := testEnroller.GetCrypto()
    61  			Expect(err).To(HaveOccurred())
    62  			Expect(err).To(MatchError(ContainSubstring("enroll failed")))
    63  			Expect(resp).To(BeNil())
    64  		})
    65  	})
    66  
    67  	Context("ping CA", func() {
    68  		It("returns true if ca reachable", func() {
    69  			err := testEnroller.PingCA()
    70  			Expect(err).To(BeNil())
    71  		})
    72  
    73  		It("returns true if ca reachable", func() {
    74  			mockCryptoEnroller.PingCAReturns(errors.New("ping failed"))
    75  
    76  			err := testEnroller.PingCA()
    77  			Expect(err).To(HaveOccurred())
    78  			Expect(err).To(MatchError(ContainSubstring("ping failed")))
    79  		})
    80  	})
    81  
    82  	Context("validate", func() {
    83  		var req *current.Enrollment
    84  
    85  		BeforeEach(func() {
    86  			req = &current.Enrollment{
    87  				CAHost:       "host",
    88  				CAPort:       "1234",
    89  				EnrollID:     "id",
    90  				EnrollSecret: "secret",
    91  				CATLS: &current.CATLS{
    92  					CACert: "cacert",
    93  				},
    94  			}
    95  			mockCryptoEnroller.GetEnrollmentRequestReturns(req)
    96  		})
    97  
    98  		It("successfull validation returns no error", func() {
    99  			err := testEnroller.Validate()
   100  			Expect(err).NotTo(HaveOccurred())
   101  		})
   102  
   103  		It("returns error if missing CA host", func() {
   104  			req.CAHost = ""
   105  
   106  			err := testEnroller.Validate()
   107  			Expect(err).To(MatchError("unable to enroll, CA host not specified"))
   108  		})
   109  
   110  		It("returns error if missing CA port", func() {
   111  			req.CAPort = ""
   112  
   113  			err := testEnroller.Validate()
   114  			Expect(err).To(MatchError("unable to enroll, CA port not specified"))
   115  		})
   116  
   117  		It("returns error if missing enrollment ID", func() {
   118  			req.EnrollID = ""
   119  
   120  			err := testEnroller.Validate()
   121  			Expect(err).To(MatchError("unable to enroll, enrollment ID not specified"))
   122  		})
   123  
   124  		It("returns error if missing enrollment secret", func() {
   125  			req.EnrollSecret = ""
   126  
   127  			err := testEnroller.Validate()
   128  			Expect(err).To(MatchError("unable to enroll, enrollment secret not specified"))
   129  		})
   130  
   131  		It("returns error if missing CA TLS cert", func() {
   132  			req.CATLS.CACert = ""
   133  
   134  			err := testEnroller.Validate()
   135  			Expect(err).To(MatchError("unable to enroll, CA TLS certificate not specified"))
   136  		})
   137  	})
   138  })