|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package cceventmgmt |
| 8 | + |
| 9 | +import ( |
| 10 | + "os" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/golang/protobuf/proto" |
| 14 | + "github.com/hyperledger/fabric/common/flogging" |
| 15 | + "github.com/hyperledger/fabric/core/common/ccprovider" |
| 16 | + "github.com/hyperledger/fabric/protos/ledger/rwset/kvrwset" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | +) |
| 19 | + |
| 20 | +func TestMain(m *testing.M) { |
| 21 | + flogging.SetModuleLevel("eventmgmt", "debug") |
| 22 | + os.Exit(m.Run()) |
| 23 | +} |
| 24 | +func TestCCEventMgmt(t *testing.T) { |
| 25 | + cc1Def := &ChaincodeDefinition{Name: "cc1", Version: "v1", Hash: []byte("cc1")} |
| 26 | + cc1DBArtifactsTar := []byte("cc1DBArtifacts") |
| 27 | + |
| 28 | + cc2Def := &ChaincodeDefinition{Name: "cc2", Version: "v1", Hash: []byte("cc2")} |
| 29 | + cc2DBArtifactsTar := []byte("cc2DBArtifacts") |
| 30 | + |
| 31 | + cc3Def := &ChaincodeDefinition{Name: "cc3", Version: "v1", Hash: []byte("cc3")} |
| 32 | + cc3DBArtifactsTar := []byte("cc3DBArtifacts") |
| 33 | + |
| 34 | + // cc1 is deployed and installed. cc2 is deployed but not installed. cc3 is not deployed but installed |
| 35 | + mockProvider := newMockProvider() |
| 36 | + mockProvider.setChaincodeInstalled(cc1Def, cc1DBArtifactsTar) |
| 37 | + mockProvider.setChaincodeDeployed("channel1", cc1Def) |
| 38 | + mockProvider.setChaincodeDeployed("channel1", cc2Def) |
| 39 | + mockProvider.setChaincodeInstalled(cc3Def, cc3DBArtifactsTar) |
| 40 | + setEventMgrForTest(newMgr(mockProvider)) |
| 41 | + defer clearEventMgrForTest() |
| 42 | + |
| 43 | + handler1, handler2 := &mockHandler{}, &mockHandler{} |
| 44 | + eventMgr := GetMgr() |
| 45 | + assert.NotNil(t, eventMgr) |
| 46 | + eventMgr.Register("channel1", handler1) |
| 47 | + eventMgr.Register("channel2", handler2) |
| 48 | + |
| 49 | + cc2ExpectedEvent := &mockEvent{cc2Def, cc2DBArtifactsTar} |
| 50 | + cc3ExpectedEvent := &mockEvent{cc3Def, cc3DBArtifactsTar} |
| 51 | + |
| 52 | + // Deploy cc3 on chain1 - only handler1 should recieve event because cc3 is being deployed only on chain1 |
| 53 | + eventMgr.HandleChaincodeDeploy("channel1", []*ChaincodeDefinition{cc3Def}) |
| 54 | + assert.Contains(t, handler1.eventsRecieved, cc3ExpectedEvent) |
| 55 | + assert.NotContains(t, handler2.eventsRecieved, cc3ExpectedEvent) |
| 56 | + |
| 57 | + // Deploy cc3 on chain2 as well and this time handler2 should also recieve event |
| 58 | + eventMgr.HandleChaincodeDeploy("channel2", []*ChaincodeDefinition{cc3Def}) |
| 59 | + assert.Contains(t, handler2.eventsRecieved, cc3ExpectedEvent) |
| 60 | + |
| 61 | + // Install CC2 - only handler1 should receive event because cc2 is deployed only on chain1 and not on chain2 |
| 62 | + eventMgr.HandleChaincodeInstall(cc2Def, cc2DBArtifactsTar) |
| 63 | + assert.Contains(t, handler1.eventsRecieved, cc2ExpectedEvent) |
| 64 | + assert.NotContains(t, handler2.eventsRecieved, cc2ExpectedEvent) |
| 65 | +} |
| 66 | + |
| 67 | +func TestLSCCListener(t *testing.T) { |
| 68 | + channelName := "testChannel" |
| 69 | + cc1Def := &ChaincodeDefinition{Name: "testChaincode", Version: "v1", Hash: []byte("hash_testChaincode")} |
| 70 | + cc1DBArtifactsTar := []byte("cc1DBArtifacts") |
| 71 | + // cc1 is installed but not deployed |
| 72 | + mockProvider := newMockProvider() |
| 73 | + mockProvider.setChaincodeInstalled(cc1Def, cc1DBArtifactsTar) |
| 74 | + setEventMgrForTest(newMgr(mockProvider)) |
| 75 | + defer clearEventMgrForTest() |
| 76 | + handler1 := &mockHandler{} |
| 77 | + GetMgr().Register(channelName, handler1) |
| 78 | + lsccStateListener := &KVLedgerLSCCStateListener{} |
| 79 | + |
| 80 | + sampleChaincodeData := &ccprovider.ChaincodeData{Name: cc1Def.Name, Version: cc1Def.Version, Id: cc1Def.Hash} |
| 81 | + sampleChaincodeDataBytes, err := proto.Marshal(sampleChaincodeData) |
| 82 | + assert.NoError(t, err, "") |
| 83 | + lsccStateListener.HandleStateUpdates(channelName, []*kvrwset.KVWrite{ |
| 84 | + &kvrwset.KVWrite{Key: cc1Def.Name, Value: sampleChaincodeDataBytes}, |
| 85 | + }) |
| 86 | + assert.Contains(t, handler1.eventsRecieved, &mockEvent{cc1Def, cc1DBArtifactsTar}) |
| 87 | +} |
| 88 | + |
| 89 | +type mockProvider struct { |
| 90 | + chaincodesDeployed map[[3]string]bool |
| 91 | + chaincodesInstalled map[[2]string][]byte |
| 92 | +} |
| 93 | + |
| 94 | +type mockHandler struct { |
| 95 | + eventsRecieved []*mockEvent |
| 96 | +} |
| 97 | + |
| 98 | +type mockEvent struct { |
| 99 | + def *ChaincodeDefinition |
| 100 | + dbArtifactsTar []byte |
| 101 | +} |
| 102 | + |
| 103 | +func (l *mockHandler) HandleChaincodeDeploy(chaincodeDefinition *ChaincodeDefinition, dbArtifactsTar []byte) error { |
| 104 | + l.eventsRecieved = append(l.eventsRecieved, &mockEvent{def: chaincodeDefinition, dbArtifactsTar: dbArtifactsTar}) |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func newMockProvider() *mockProvider { |
| 109 | + return &mockProvider{ |
| 110 | + make(map[[3]string]bool), |
| 111 | + make(map[[2]string][]byte), |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func (p *mockProvider) setChaincodeDeployed(chainid string, chaincodeDefinition *ChaincodeDefinition) { |
| 116 | + p.chaincodesDeployed[[3]string{chainid, chaincodeDefinition.Name, chaincodeDefinition.Version}] = true |
| 117 | +} |
| 118 | + |
| 119 | +func (p *mockProvider) setChaincodeInstalled(chaincodeDefinition *ChaincodeDefinition, dbArtifactsTar []byte) { |
| 120 | + p.chaincodesInstalled[[2]string{chaincodeDefinition.Name, chaincodeDefinition.Version}] = dbArtifactsTar |
| 121 | +} |
| 122 | + |
| 123 | +func (p *mockProvider) setChaincodeDeployAndInstalled(chainid string, chaincodeDefinition *ChaincodeDefinition, dbArtifactsTar []byte) { |
| 124 | + p.setChaincodeDeployed(chainid, chaincodeDefinition) |
| 125 | + p.setChaincodeInstalled(chaincodeDefinition, dbArtifactsTar) |
| 126 | +} |
| 127 | + |
| 128 | +func (p *mockProvider) IsChaincodeDeployed(chainid string, chaincodeDefinition *ChaincodeDefinition) (bool, error) { |
| 129 | + return p.chaincodesDeployed[[3]string{chainid, chaincodeDefinition.Name, chaincodeDefinition.Version}], nil |
| 130 | +} |
| 131 | + |
| 132 | +func (p *mockProvider) RetrieveChaincodeArtifacts(chaincodeDefinition *ChaincodeDefinition) (installed bool, dbArtifactsTar []byte, err error) { |
| 133 | + dbArtifactsTar, ok := p.chaincodesInstalled[[2]string{chaincodeDefinition.Name, chaincodeDefinition.Version}] |
| 134 | + if !ok { |
| 135 | + return false, nil, nil |
| 136 | + } |
| 137 | + return true, dbArtifactsTar, nil |
| 138 | +} |
| 139 | + |
| 140 | +func setEventMgrForTest(eventMgr *Mgr) { |
| 141 | + mgr = eventMgr |
| 142 | +} |
| 143 | + |
| 144 | +func clearEventMgrForTest() { |
| 145 | + mgr = nil |
| 146 | +} |
0 commit comments