github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/ca/initializer_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 initializer_test 20 21 import ( 22 v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/ca/v1" 23 initializer "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/ca" 24 "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/ca/mocks" 25 . "github.com/onsi/ginkgo/v2" 26 . "github.com/onsi/gomega" 27 "github.com/pkg/errors" 28 ) 29 30 var _ = Describe("Initializing the CA before start up", func() { 31 var ( 32 init *initializer.Initializer 33 ca *mocks.IBPCA 34 ) 35 36 BeforeEach(func() { 37 ca = &mocks.IBPCA{} 38 init = &initializer.Initializer{} 39 }) 40 41 Context("create", func() { 42 It("returns an error if unable to override server config", func() { 43 msg := "failed to override" 44 ca.OverrideServerConfigReturns(errors.New(msg)) 45 _, err := init.Create(nil, &v1.ServerConfig{}, ca) 46 Expect(err).To(HaveOccurred()) 47 Expect(err.Error()).To(Equal(msg)) 48 }) 49 50 It("returns an error if unable to write config", func() { 51 msg := "failed to write config" 52 ca.ParseCryptoReturns(nil, errors.New(msg)) 53 _, err := init.Create(nil, &v1.ServerConfig{}, ca) 54 Expect(err).To(HaveOccurred()) 55 Expect(err.Error()).To(Equal(msg)) 56 }) 57 58 It("returns an error if unable to write config", func() { 59 msg := "failed to parse crypto" 60 ca.WriteConfigReturns(errors.New(msg)) 61 _, err := init.Create(nil, &v1.ServerConfig{}, ca) 62 Expect(err).To(HaveOccurred()) 63 Expect(err.Error()).To(Equal(msg)) 64 }) 65 66 It("returns an error if unable to init", func() { 67 msg := "failed to init" 68 ca.InitReturns(errors.New(msg)) 69 _, err := init.Create(nil, &v1.ServerConfig{}, ca) 70 Expect(err).To(HaveOccurred()) 71 Expect(err.Error()).To(Equal(msg)) 72 }) 73 74 It("returns an error if unable to parse ca block", func() { 75 msg := "failed to parse ca block" 76 ca.ParseCABlockReturns(nil, errors.New(msg)) 77 _, err := init.Create(nil, &v1.ServerConfig{}, ca) 78 Expect(err).To(HaveOccurred()) 79 Expect(err.Error()).To(Equal(msg)) 80 }) 81 82 It("returns an error if unable to remove home directory", func() { 83 msg := "failed to remove home directory" 84 ca.RemoveHomeDirReturns(errors.New(msg)) 85 _, err := init.Create(nil, &v1.ServerConfig{}, ca) 86 Expect(err).To(HaveOccurred()) 87 Expect(err.Error()).To(Equal(msg)) 88 }) 89 90 It("returns a response containing server config and map contains all crypto material", func() { 91 result, err := init.Create(nil, &v1.ServerConfig{}, ca) 92 Expect(err).NotTo(HaveOccurred()) 93 Expect(result).NotTo(Equal(nil)) 94 }) 95 }) 96 97 Context("update", func() { 98 It("returns an error if unable to override server config", func() { 99 msg := "failed to override" 100 ca.OverrideServerConfigReturns(errors.New(msg)) 101 _, err := init.Update(nil, &v1.ServerConfig{}, ca) 102 Expect(err).To(HaveOccurred()) 103 Expect(err.Error()).To(Equal(msg)) 104 }) 105 106 It("returns an error if unable to parse crypto", func() { 107 msg := "failed to parse crypto" 108 ca.ParseCryptoReturns(nil, errors.New(msg)) 109 _, err := init.Update(nil, &v1.ServerConfig{}, ca) 110 Expect(err).To(HaveOccurred()) 111 Expect(err.Error()).To(Equal(msg)) 112 }) 113 114 It("returns a response containing server config and map contains all crypto material", func() { 115 result, err := init.Update(nil, &v1.ServerConfig{}, ca) 116 Expect(err).NotTo(HaveOccurred()) 117 Expect(result).NotTo(Equal(nil)) 118 }) 119 }) 120 })