github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/migrator/peer/fabric/migrator_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 fabric_test 20 21 import ( 22 "errors" 23 24 . "github.com/onsi/ginkgo/v2" 25 . "github.com/onsi/gomega" 26 27 current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1" 28 config "github.com/IBM-Blockchain/fabric-operator/operatorconfig" 29 "github.com/IBM-Blockchain/fabric-operator/pkg/migrator/peer/fabric" 30 "github.com/IBM-Blockchain/fabric-operator/pkg/migrator/peer/fabric/mocks" 31 ) 32 33 var _ = Describe("Peer migrator", func() { 34 var ( 35 migrator *mocks.Migrator 36 instance *current.IBPPeer 37 ) 38 const FABRIC_V2 = "2.2.5-1" 39 40 BeforeEach(func() { 41 migrator = &mocks.Migrator{} 42 migrator.MigrationNeededReturns(true) 43 44 instance = ¤t.IBPPeer{} 45 }) 46 47 Context("migrate to version", func() { 48 Context("V2", func() { 49 It("returns error on failure", func() { 50 migrator.UpgradeDBsReturns(errors.New("failed to reset peer")) 51 err := fabric.V2Migrate(instance, migrator, FABRIC_V2, config.DBMigrationTimeouts{}) 52 Expect(err).To(HaveOccurred()) 53 Expect(err).Should(MatchError(ContainSubstring("failed to reset peer"))) 54 }) 55 56 It("migrates", func() { 57 err := fabric.V2Migrate(instance, migrator, FABRIC_V2, config.DBMigrationTimeouts{}) 58 Expect(err).NotTo(HaveOccurred()) 59 }) 60 }) 61 }) 62 63 Context("V2 migration", func() { 64 It("returns immediately when migration not needed", func() { 65 migrator.MigrationNeededReturns(false) 66 err := fabric.V2Migrate(instance, migrator, FABRIC_V2, config.DBMigrationTimeouts{}) 67 Expect(err).NotTo(HaveOccurred()) 68 Expect(migrator.UpdateConfigCallCount()).To(Equal(0)) 69 Expect(migrator.UpgradeDBsCallCount()).To(Equal(0)) 70 }) 71 72 It("returns an error if unable to update config", func() { 73 migrator.UpdateConfigReturns(errors.New("failed to update config")) 74 err := fabric.V2Migrate(instance, migrator, FABRIC_V2, config.DBMigrationTimeouts{}) 75 Expect(err).To(HaveOccurred()) 76 Expect(err).Should(MatchError(ContainSubstring("failed to update config"))) 77 }) 78 79 It("returns an error if unable to reset peer", func() { 80 migrator.UpgradeDBsReturns(errors.New("failed to reset peer")) 81 err := fabric.V2Migrate(instance, migrator, FABRIC_V2, config.DBMigrationTimeouts{}) 82 Expect(err).To(HaveOccurred()) 83 Expect(err).Should(MatchError(ContainSubstring("failed to reset peer"))) 84 }) 85 }) 86 })