github.com/defanghe/fabric@v2.1.1+incompatible/internal/peer/lifecycle/chaincode/install_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package chaincode_test 8 9 import ( 10 pb "github.com/hyperledger/fabric-protos-go/peer" 11 "github.com/hyperledger/fabric/bccsp/sw" 12 "github.com/hyperledger/fabric/internal/peer/lifecycle/chaincode" 13 "github.com/hyperledger/fabric/internal/peer/lifecycle/chaincode/mock" 14 "github.com/pkg/errors" 15 "github.com/spf13/cobra" 16 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 ) 20 21 var _ = Describe("Install", func() { 22 Describe("Installer", func() { 23 var ( 24 mockProposalResponse *pb.ProposalResponse 25 mockEndorserClient *mock.EndorserClient 26 mockReader *mock.Reader 27 mockSigner *mock.Signer 28 input *chaincode.InstallInput 29 installer *chaincode.Installer 30 ) 31 32 BeforeEach(func() { 33 mockEndorserClient = &mock.EndorserClient{} 34 mockProposalResponse = &pb.ProposalResponse{ 35 Response: &pb.Response{ 36 Status: 200, 37 }, 38 } 39 mockEndorserClient.ProcessProposalReturns(mockProposalResponse, nil) 40 41 input = &chaincode.InstallInput{ 42 PackageFile: "pkgFile", 43 } 44 45 mockReader = &mock.Reader{} 46 mockSigner = &mock.Signer{} 47 48 installer = &chaincode.Installer{ 49 Input: input, 50 EndorserClient: mockEndorserClient, 51 Reader: mockReader, 52 Signer: mockSigner, 53 } 54 }) 55 56 It("installs chaincodes", func() { 57 err := installer.Install() 58 Expect(err).NotTo(HaveOccurred()) 59 }) 60 61 Context("when the chaincode install package is not provided", func() { 62 BeforeEach(func() { 63 installer.Input.PackageFile = "" 64 }) 65 66 It("returns an error", func() { 67 err := installer.Install() 68 Expect(err).To(MatchError("chaincode install package must be provided")) 69 }) 70 }) 71 72 Context("when the package file cannot be read", func() { 73 BeforeEach(func() { 74 mockReader.ReadFileReturns(nil, errors.New("coffee")) 75 }) 76 77 It("returns an error", func() { 78 err := installer.Install() 79 Expect(err).To(MatchError("failed to read chaincode package at 'pkgFile': coffee")) 80 }) 81 }) 82 83 Context("when the signer cannot be serialized", func() { 84 BeforeEach(func() { 85 mockSigner.SerializeReturns(nil, errors.New("cafe")) 86 }) 87 88 It("returns an error", func() { 89 err := installer.Install() 90 Expect(err).To(MatchError("failed to serialize signer: cafe")) 91 }) 92 }) 93 94 Context("when the signer fails to sign the proposal", func() { 95 BeforeEach(func() { 96 mockSigner.SignReturns(nil, errors.New("tea")) 97 }) 98 99 It("returns an error", func() { 100 err := installer.Install() 101 Expect(err).To(MatchError("failed to create signed proposal for chaincode install: tea")) 102 }) 103 }) 104 105 Context("when the endorser fails to endorse the proposal", func() { 106 BeforeEach(func() { 107 mockEndorserClient.ProcessProposalReturns(nil, errors.New("latte")) 108 }) 109 110 It("returns an error", func() { 111 err := installer.Install() 112 Expect(err).To(MatchError("failed to endorse chaincode install: latte")) 113 }) 114 }) 115 116 Context("when the endorser returns a nil proposal response", func() { 117 BeforeEach(func() { 118 mockProposalResponse = nil 119 mockEndorserClient.ProcessProposalReturns(mockProposalResponse, nil) 120 }) 121 122 It("returns an error", func() { 123 err := installer.Install() 124 Expect(err).To(MatchError("chaincode install failed: received nil proposal response")) 125 }) 126 }) 127 128 Context("when the endorser returns a proposal response with a nil response", func() { 129 BeforeEach(func() { 130 mockProposalResponse.Response = nil 131 mockEndorserClient.ProcessProposalReturns(mockProposalResponse, nil) 132 }) 133 134 It("returns an error", func() { 135 err := installer.Install() 136 Expect(err).To(MatchError("chaincode install failed: received proposal response with nil response")) 137 }) 138 }) 139 140 Context("when the endorser returns a non-success status", func() { 141 BeforeEach(func() { 142 mockProposalResponse.Response = &pb.Response{ 143 Status: 500, 144 Message: "capuccino", 145 } 146 mockEndorserClient.ProcessProposalReturns(mockProposalResponse, nil) 147 }) 148 149 It("returns an error", func() { 150 err := installer.Install() 151 Expect(err).To(MatchError("chaincode install failed with status: 500 - capuccino")) 152 }) 153 }) 154 155 Context("when the payload contains bytes that aren't an InstallChaincodeResult", func() { 156 BeforeEach(func() { 157 mockProposalResponse.Response = &pb.Response{ 158 Payload: []byte("badpayloadbadpayload"), 159 Status: 200, 160 } 161 mockEndorserClient.ProcessProposalReturns(mockProposalResponse, nil) 162 }) 163 164 It("returns an error", func() { 165 err := installer.Install() 166 Expect(err).To(MatchError(ContainSubstring("failed to unmarshal proposal response's response payload"))) 167 }) 168 }) 169 }) 170 171 Describe("InstallCmd", func() { 172 var ( 173 installCmd *cobra.Command 174 ) 175 176 BeforeEach(func() { 177 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 178 Expect(err).To(BeNil()) 179 installCmd = chaincode.InstallCmd(nil, cryptoProvider) 180 installCmd.SetArgs([]string{ 181 "testpkg", 182 "--peerAddresses=test1", 183 "--tlsRootCertFiles=tls1", 184 }) 185 }) 186 187 AfterEach(func() { 188 chaincode.ResetFlags() 189 }) 190 191 It("sets up the installer and attempts to install the chaincode", func() { 192 err := installCmd.Execute() 193 Expect(err).To(MatchError(ContainSubstring("failed to retrieve endorser client for install"))) 194 }) 195 196 Context("when more than one peer address is provided", func() { 197 BeforeEach(func() { 198 installCmd.SetArgs([]string{ 199 "--peerAddresses=test3", 200 "--peerAddresses=test4", 201 }) 202 }) 203 204 It("returns an error", func() { 205 err := installCmd.Execute() 206 Expect(err).To(MatchError(ContainSubstring("failed to validate peer connection parameters"))) 207 }) 208 }) 209 }) 210 })