github.com/hyperledger-labs/bdls@v2.1.1+incompatible/core/chaincode/lifecycle/integration_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package lifecycle_test 8 9 import ( 10 "github.com/hyperledger/fabric-chaincode-go/shim" 11 "github.com/hyperledger/fabric-protos-go/ledger/queryresult" 12 pb "github.com/hyperledger/fabric-protos-go/peer" 13 lb "github.com/hyperledger/fabric-protos-go/peer/lifecycle" 14 "github.com/hyperledger/fabric/common/channelconfig" 15 "github.com/hyperledger/fabric/common/util" 16 "github.com/hyperledger/fabric/core/chaincode/lifecycle" 17 "github.com/hyperledger/fabric/core/chaincode/lifecycle/mock" 18 "github.com/hyperledger/fabric/core/dispatcher" 19 "github.com/hyperledger/fabric/protoutil" 20 21 . "github.com/onsi/ginkgo" 22 . "github.com/onsi/gomega" 23 24 "github.com/golang/protobuf/proto" 25 ) 26 27 var _ = Describe("Integration", func() { 28 var ( 29 resources *lifecycle.Resources 30 ef *lifecycle.ExternalFunctions 31 scc *lifecycle.SCC 32 33 fakeChannelConfigSource *mock.ChannelConfigSource 34 fakeChannelConfig *mock.ChannelConfig 35 fakeApplicationConfig *mock.ApplicationConfig 36 fakeCapabilities *mock.ApplicationCapabilities 37 fakeOrgConfig *mock.ApplicationOrgConfig 38 fakeStub *mock.ChaincodeStub 39 fakeACLProvider *mock.ACLProvider 40 fakeMSPManager *mock.MSPManager 41 fakeQueryExecutorProvider *mock.QueryExecutorProvider 42 fakeQueryExecutor *mock.SimpleQueryExecutor 43 fakeDeployedCCInfoProvider *mock.LegacyDeployedCCInfoProvider 44 45 fakeOrgKVStore map[string][]byte 46 fakePublicKVStore map[string][]byte 47 ) 48 49 BeforeEach(func() { 50 resources = &lifecycle.Resources{ 51 Serializer: &lifecycle.Serializer{}, 52 } 53 54 ef = &lifecycle.ExternalFunctions{ 55 Resources: resources, 56 } 57 58 fakeChannelConfigSource = &mock.ChannelConfigSource{} 59 fakeChannelConfig = &mock.ChannelConfig{} 60 fakeChannelConfigSource.GetStableChannelConfigReturns(fakeChannelConfig) 61 fakeApplicationConfig = &mock.ApplicationConfig{} 62 fakeChannelConfig.ApplicationConfigReturns(fakeApplicationConfig, true) 63 fakeCapabilities = &mock.ApplicationCapabilities{} 64 fakeCapabilities.LifecycleV20Returns(true) 65 fakeApplicationConfig.CapabilitiesReturns(fakeCapabilities) 66 fakeACLProvider = &mock.ACLProvider{} 67 68 fakeOrgConfig = &mock.ApplicationOrgConfig{} 69 fakeOrgConfig.MSPIDReturns("fake-mspid") 70 71 fakeMSPManager = &mock.MSPManager{} 72 fakeChannelConfig.MSPManagerReturns(fakeMSPManager) 73 fakeQueryExecutorProvider = &mock.QueryExecutorProvider{} 74 fakeQueryExecutor = &mock.SimpleQueryExecutor{} 75 fakeQueryExecutorProvider.TxQueryExecutorReturns(fakeQueryExecutor) 76 fakeDeployedCCInfoProvider = &mock.LegacyDeployedCCInfoProvider{} 77 78 fakeApplicationConfig.OrganizationsReturns(map[string]channelconfig.ApplicationOrg{ 79 "fakeOrg": fakeOrgConfig, 80 }) 81 82 scc = &lifecycle.SCC{ 83 Dispatcher: &dispatcher.Dispatcher{ 84 Protobuf: &dispatcher.ProtobufImpl{}, 85 }, 86 Functions: ef, 87 OrgMSPID: "fake-mspid", 88 ChannelConfigSource: fakeChannelConfigSource, 89 ACLProvider: fakeACLProvider, 90 QueryExecutorProvider: fakeQueryExecutorProvider, 91 DeployedCCInfoProvider: fakeDeployedCCInfoProvider, 92 } 93 94 fakePublicKVStore = map[string][]byte{} 95 fakeOrgKVStore = map[string][]byte{} 96 97 fakeStub = &mock.ChaincodeStub{} 98 99 fakeStub.GetChannelIDReturns("test-channel") 100 101 fakeStub.GetStateStub = func(key string) ([]byte, error) { 102 return fakePublicKVStore[key], nil 103 } 104 fakeStub.PutStateStub = func(key string, value []byte) error { 105 fakePublicKVStore[key] = value 106 return nil 107 } 108 fakeStub.GetStateByRangeStub = func(begin, end string) (shim.StateQueryIteratorInterface, error) { 109 fakeIterator := &mock.StateIterator{} 110 i := 0 111 for key, value := range fakePublicKVStore { 112 if key >= begin && key < end { 113 fakeIterator.HasNextReturnsOnCall(i, true) 114 fakeIterator.NextReturnsOnCall(i, &queryresult.KV{ 115 Key: key, 116 Value: value, 117 }, nil) 118 i++ 119 } 120 } 121 return fakeIterator, nil 122 } 123 124 fakeStub.PutPrivateDataStub = func(collection, key string, value []byte) error { 125 fakeOrgKVStore[key] = value 126 return nil 127 } 128 129 fakeStub.GetPrivateDataStub = func(collection, key string) ([]byte, error) { 130 return fakeOrgKVStore[key], nil 131 } 132 133 fakeStub.GetPrivateDataHashStub = func(collection, key string) ([]byte, error) { 134 return util.ComputeSHA256(fakeOrgKVStore[key]), nil 135 } 136 }) 137 138 Describe("Instantiation", func() { 139 It("defines the chaincode for the org, defines it for the channel, queries all namespaces, and queries the chaincode", func() { 140 // Define for the org 141 fakeStub.GetArgsReturns([][]byte{ 142 []byte("ApproveChaincodeDefinitionForMyOrg"), 143 protoutil.MarshalOrPanic(&lb.ApproveChaincodeDefinitionForMyOrgArgs{ 144 Name: "cc-name", 145 Version: "1.0", 146 Sequence: 1, 147 EndorsementPlugin: "builtin", 148 ValidationPlugin: "builtin", 149 ValidationParameter: []byte("validation-parameter"), 150 Source: &lb.ChaincodeSource{ 151 Type: &lb.ChaincodeSource_LocalPackage{ 152 LocalPackage: &lb.ChaincodeSource_Local{ 153 PackageId: "hash-value", 154 }, 155 }, 156 }, 157 }), 158 }) 159 response := scc.Invoke(fakeStub) 160 Expect(response.Status).To(Equal(int32(200))) 161 162 // Define for the channel 163 fakeStub.GetArgsReturns([][]byte{ 164 []byte("CommitChaincodeDefinition"), 165 protoutil.MarshalOrPanic(&lb.CommitChaincodeDefinitionArgs{ 166 Name: "cc-name", 167 Version: "1.0", 168 Sequence: 1, 169 EndorsementPlugin: "builtin", 170 ValidationPlugin: "builtin", 171 ValidationParameter: []byte("validation-parameter"), 172 }), 173 }) 174 response = scc.Invoke(fakeStub) 175 Expect(response.Message).To(Equal("")) 176 Expect(response.Status).To(Equal(int32(200))) 177 178 // Get channel definitions 179 fakeStub.GetArgsReturns([][]byte{ 180 []byte("QueryChaincodeDefinitions"), 181 protoutil.MarshalOrPanic(&lb.QueryChaincodeDefinitionsArgs{}), 182 }) 183 response = scc.Invoke(fakeStub) 184 Expect(response.Status).To(Equal(int32(200))) 185 definitionsResult := &lb.QueryChaincodeDefinitionsResult{} 186 err := proto.Unmarshal(response.Payload, definitionsResult) 187 Expect(err).NotTo(HaveOccurred()) 188 Expect(len(definitionsResult.ChaincodeDefinitions)).To(Equal(1)) 189 Expect(definitionsResult.ChaincodeDefinitions[0].Name).To(Equal("cc-name")) 190 191 // Get chaincode definition details 192 fakeStub.GetArgsReturns([][]byte{ 193 []byte("QueryChaincodeDefinition"), 194 protoutil.MarshalOrPanic(&lb.QueryChaincodeDefinitionArgs{ 195 Name: "cc-name", 196 }), 197 }) 198 response = scc.Invoke(fakeStub) 199 Expect(response.Status).To(Equal(int32(200))) 200 chaincodeResult := &lb.QueryChaincodeDefinitionResult{} 201 err = proto.Unmarshal(response.Payload, chaincodeResult) 202 Expect(err).NotTo(HaveOccurred()) 203 Expect(proto.Equal(chaincodeResult, &lb.QueryChaincodeDefinitionResult{ 204 Sequence: 1, 205 Version: "1.0", 206 EndorsementPlugin: "builtin", 207 ValidationPlugin: "builtin", 208 ValidationParameter: []byte("validation-parameter"), 209 Collections: &pb.CollectionConfigPackage{}, 210 Approvals: map[string]bool{ 211 "fake-mspid": true, 212 }, 213 })).To(BeTrue()) 214 }) 215 }) 216 217 })