github.com/ewagmig/fabric@v2.1.1+incompatible/core/chaincode/lifecycle/lifecycle_suite_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 "strings" 11 "testing" 12 13 "github.com/hyperledger/fabric-chaincode-go/shim" 14 "github.com/hyperledger/fabric/common/channelconfig" 15 commonledger "github.com/hyperledger/fabric/common/ledger" 16 "github.com/hyperledger/fabric/common/policies" 17 "github.com/hyperledger/fabric/common/util" 18 "github.com/hyperledger/fabric/core/aclmgmt" 19 "github.com/hyperledger/fabric/core/chaincode/lifecycle" 20 validation "github.com/hyperledger/fabric/core/handlers/validation/api/state" 21 "github.com/hyperledger/fabric/core/ledger" 22 "github.com/hyperledger/fabric/msp" 23 24 . "github.com/onsi/ginkgo" 25 . "github.com/onsi/gomega" 26 ) 27 28 //go:generate counterfeiter -o mock/channel_policy_reference_provider.go --fake-name ChannelPolicyReferenceProvider . ChannelPolicyReferenceProvider 29 30 //go:generate counterfeiter -o mock/convertible_policy.go --fake-name ConvertiblePolicy . convertiblePolicy 31 type convertiblePolicy interface { 32 policies.Policy 33 policies.Converter 34 } 35 36 //go:generate counterfeiter -o mock/inconvertible_policy.go --fake-name InconvertiblePolicy . inconvertiblePolicy 37 type inconvertiblePolicy interface { 38 policies.Policy 39 } 40 41 //go:generate counterfeiter -o mock/aclprovider.go --fake-name ACLProvider . aclProvider 42 type aclProvider interface { 43 aclmgmt.ACLProvider 44 } 45 46 //go:generate counterfeiter -o mock/chaincode_stub.go --fake-name ChaincodeStub . chaincodeStub 47 type chaincodeStub interface { 48 shim.ChaincodeStubInterface 49 } 50 51 //go:generate counterfeiter -o mock/state_iterator.go --fake-name StateIterator . stateIterator 52 type stateIterator interface { 53 shim.StateQueryIteratorInterface 54 } 55 56 //go:generate counterfeiter -o mock/chaincode_store.go --fake-name ChaincodeStore . chaincodeStore 57 type chaincodeStore interface { 58 lifecycle.ChaincodeStore 59 } 60 61 //go:generate counterfeiter -o mock/package_parser.go --fake-name PackageParser . packageParser 62 type packageParser interface { 63 lifecycle.PackageParser 64 } 65 66 //go:generate counterfeiter -o mock/scc_functions.go --fake-name SCCFunctions . sccFunctions 67 type sccFunctions interface { 68 lifecycle.SCCFunctions 69 } 70 71 //go:generate counterfeiter -o mock/rw_state.go --fake-name ReadWritableState . readWritableState 72 type readWritableState interface { 73 lifecycle.ReadWritableState 74 lifecycle.OpaqueState 75 lifecycle.RangeableState 76 } 77 78 //go:generate counterfeiter -o mock/query_executor.go --fake-name SimpleQueryExecutor . simpleQueryExecutor 79 type simpleQueryExecutor interface { 80 ledger.SimpleQueryExecutor 81 } 82 83 //go:generate counterfeiter -o mock/results_iterator.go --fake-name ResultsIterator . resultsIterator 84 type resultsIterator interface { 85 commonledger.ResultsIterator 86 } 87 88 //go:generate counterfeiter -o mock/channel_config.go --fake-name ChannelConfig . channelConfig 89 type channelConfig interface { 90 channelconfig.Resources 91 } 92 93 //go:generate counterfeiter -o mock/application_config.go --fake-name ApplicationConfig . applicationConfig 94 type applicationConfig interface { 95 channelconfig.Application 96 } 97 98 //go:generate counterfeiter -o mock/application_org_config.go --fake-name ApplicationOrgConfig . applicationOrgConfig 99 type applicationOrgConfig interface { 100 channelconfig.ApplicationOrg 101 } 102 103 //go:generate counterfeiter -o mock/policy_manager.go --fake-name PolicyManager . policyManager 104 type policyManager interface { 105 policies.Manager 106 } 107 108 //go:generate counterfeiter -o mock/application_capabilities.go --fake-name ApplicationCapabilities . applicationCapabilities 109 type applicationCapabilities interface { 110 channelconfig.ApplicationCapabilities 111 } 112 113 //go:generate counterfeiter -o mock/validation_state.go --fake-name ValidationState . validationState 114 type validationState interface { 115 validation.State 116 } 117 118 //go:generate counterfeiter -o mock/metadata_update_listener.go --fake-name MetadataUpdateListener . metadataUpdateListener 119 type metadataUpdateListener interface { 120 lifecycle.MetadataUpdateListener 121 } 122 123 //go:generate counterfeiter -o mock/chaincode_info_provider.go --fake-name ChaincodeInfoProvider . chaincodeInfoProvider 124 type chaincodeInfoProvider interface { 125 lifecycle.ChaincodeInfoProvider 126 } 127 128 //go:generate counterfeiter -o mock/legacy_metadata_provider.go --fake-name LegacyMetadataProvider . legacyMetadataProvider 129 type legacyMetadataProvider interface { 130 lifecycle.LegacyMetadataProvider 131 } 132 133 //go:generate counterfeiter -o mock/metadata_handler.go --fake-name MetadataHandler . metadataHandler 134 type metadataHandler interface { 135 lifecycle.MetadataHandler 136 } 137 138 //go:generate counterfeiter -o mock/msp_manager.go --fake-name MSPManager . mspManager 139 type mspManager interface { 140 msp.MSPManager 141 } 142 143 //go:generate counterfeiter -o mock/msp.go --fake-name MSP . msp1 144 type msp1 interface { 145 msp.MSP 146 } 147 148 func TestLifecycle(t *testing.T) { 149 RegisterFailHandler(Fail) 150 RunSpecs(t, "Lifecycle Suite") 151 } 152 153 // MapLedgerShim is a ledger 'implementation' based on a map, useful for pre-populating 154 // a ledger with desired serialized data. 155 type MapLedgerShim map[string][]byte 156 157 func (m MapLedgerShim) PutState(key string, value []byte) error { 158 m[key] = value 159 return nil 160 } 161 162 func (m MapLedgerShim) DelState(key string) error { 163 delete(m, key) 164 return nil 165 } 166 167 func (m MapLedgerShim) GetState(key string) (value []byte, err error) { 168 return m[key], nil 169 } 170 171 func (m MapLedgerShim) GetStateHash(key string) (value []byte, err error) { 172 if val, ok := m[key]; ok { 173 return util.ComputeSHA256(val), nil 174 } 175 return nil, nil 176 } 177 178 func (m MapLedgerShim) GetStateRange(prefix string) (map[string][]byte, error) { 179 result := map[string][]byte{} 180 for key, value := range m { 181 if strings.HasPrefix(key, prefix) { 182 result[key] = value 183 } 184 185 } 186 return result, nil 187 }