From f62a2c015715741fe44f3e6ec26cc8aa8e1463a7 Mon Sep 17 00:00:00 2001 From: Gari Singh Date: Mon, 11 Nov 2019 09:16:31 -0500 Subject: [PATCH] FAB-16437 Remove common/mocks/policies Change-Id: Id08ba203c662f43132980ede390d22fd473def33 Signed-off-by: Gari Singh --- common/mocks/policies/policies.go | 62 ------ common/mocks/policies/policies_test.go | 31 --- core/chaincode/chaincode_suite_test.go | 11 + core/chaincode/chaincode_support_test.go | 2 +- core/chaincode/exectransaction_test.go | 78 ++++--- .../executetransaction_pvtdata_test.go | 3 + core/chaincode/mock/collection_store.go | 2 - core/chaincode/mock/policy.go | 192 +++++++++++++++++ core/chaincode/mock/policy_manager.go | 196 ++++++++++++++++++ core/mocks/txvalidator/support.go | 6 - core/peer/mock_helpers.go | 8 +- core/scc/qscc/query_test.go | 2 +- internal/peer/gossip/mcs_test.go | 6 + internal/peer/gossip/mocks/mocks.go | 5 +- internal/peer/gossip/mocks/policy_manager.go | 196 ++++++++++++++++++ 15 files changed, 657 insertions(+), 143 deletions(-) delete mode 100644 common/mocks/policies/policies.go delete mode 100644 common/mocks/policies/policies_test.go create mode 100644 core/chaincode/mock/policy.go create mode 100644 core/chaincode/mock/policy_manager.go create mode 100644 internal/peer/gossip/mocks/policy_manager.go diff --git a/common/mocks/policies/policies.go b/common/mocks/policies/policies.go deleted file mode 100644 index 17939e50e00..00000000000 --- a/common/mocks/policies/policies.go +++ /dev/null @@ -1,62 +0,0 @@ -/* -Copyright IBM Corp. All Rights Reserved. - -SPDX-License-Identifier: Apache-2.0 -*/ - -package policies - -import ( - "github.com/hyperledger/fabric/common/policies" - "github.com/hyperledger/fabric/msp" - "github.com/hyperledger/fabric/protoutil" -) - -// Policy is a mock implementation of the policies.Policy interface -type Policy struct { - // Err is the error returned by Evaluate - Err error -} - -// EvaluateSignedData returns the Err set in Policy -func (p *Policy) EvaluateSignedData(signatureSet []*protoutil.SignedData) error { - return p.Err -} - -// EvaluateIdentities returns nil -func (p *Policy) EvaluateIdentities(ids []msp.Identity) error { - return p.Err -} - -// Manager is a mock implementation of the policies.Manager interface -type Manager struct { - // Policy is returned as the output to GetPolicy if a Policy - // for id is not in PolicyMap - Policy *Policy - - // PolicyMap is returned is used to look up Policies in - PolicyMap map[string]policies.Policy - - // SubManagers is used for the return value of Manager - SubManagersMap map[string]*Manager -} - -// Manager returns the Manager from SubManagers for the last component of the path -func (m *Manager) Manager(path []string) (policies.Manager, bool) { - if len(path) == 0 { - return m, true - } - manager, ok := m.SubManagersMap[path[len(path)-1]] - return manager, ok -} - -// GetPolicy returns the value of Manager.Policy and whether it was nil or not -func (m *Manager) GetPolicy(id string) (policies.Policy, bool) { - if m.PolicyMap != nil { - policy, ok := m.PolicyMap[id] - if ok { - return policy, true - } - } - return m.Policy, m.Policy != nil -} diff --git a/common/mocks/policies/policies_test.go b/common/mocks/policies/policies_test.go deleted file mode 100644 index c01af1414e1..00000000000 --- a/common/mocks/policies/policies_test.go +++ /dev/null @@ -1,31 +0,0 @@ -/* -Copyright IBM Corp. 2016 All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package policies - -import ( - "testing" - - "github.com/hyperledger/fabric/common/policies" -) - -func TestPolicyManagerInterface(t *testing.T) { - _ = policies.Manager(&Manager{}) -} - -func TestPolicyInterface(t *testing.T) { - _ = policies.Policy(&Policy{}) -} diff --git a/core/chaincode/chaincode_suite_test.go b/core/chaincode/chaincode_suite_test.go index 650bc753add..e5f87d8dc22 100644 --- a/core/chaincode/chaincode_suite_test.go +++ b/core/chaincode/chaincode_suite_test.go @@ -11,6 +11,7 @@ import ( "github.com/hyperledger/fabric/common/channelconfig" commonledger "github.com/hyperledger/fabric/common/ledger" + "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/core/chaincode" "github.com/hyperledger/fabric/core/common/privdata" "github.com/hyperledger/fabric/core/container/ccintf" @@ -135,3 +136,13 @@ type applicationCapabilities interface { type applicationConfig interface { channelconfig.Application } + +//go:generate counterfeiter -o mock/policy_manager.go -fake-name PolicyManager . policyManager +type policyManager interface { + policies.Manager +} + +//go:generate counterfeiter -o mock/policy.go -fake-name Policy . policy +type policy interface { + policies.Policy +} diff --git a/core/chaincode/chaincode_support_test.go b/core/chaincode/chaincode_support_test.go index c1018b4c3b9..7d9f3a50bcc 100644 --- a/core/chaincode/chaincode_support_test.go +++ b/core/chaincode/chaincode_support_test.go @@ -270,7 +270,7 @@ func initMockPeer(channelIDs ...string) (*peer.Peer, *ChaincodeSupport, func(), globalBlockNum = make(map[string]uint64, len(channelIDs)) for _, id := range channelIDs { - if err := peer.CreateMockChannel(peerInstance, id); err != nil { + if err := peer.CreateMockChannel(peerInstance, id, &mock.PolicyManager{}); err != nil { cleanup() return nil, nil, func() {}, err } diff --git a/core/chaincode/exectransaction_test.go b/core/chaincode/exectransaction_test.go index 537bab182a7..a1e79da028d 100644 --- a/core/chaincode/exectransaction_test.go +++ b/core/chaincode/exectransaction_test.go @@ -36,7 +36,7 @@ import ( "github.com/hyperledger/fabric/common/crypto/tlsgen" "github.com/hyperledger/fabric/common/flogging" "github.com/hyperledger/fabric/common/metrics/disabled" - mockpolicies "github.com/hyperledger/fabric/common/mocks/policies" + "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/common/util" "github.com/hyperledger/fabric/core/aclmgmt" @@ -57,7 +57,7 @@ import ( cut "github.com/hyperledger/fabric/core/ledger/util" "github.com/hyperledger/fabric/core/peer" "github.com/hyperledger/fabric/core/policy" - "github.com/hyperledger/fabric/core/policy/mocks" + policymocks "github.com/hyperledger/fabric/core/policy/mocks" "github.com/hyperledger/fabric/core/scc" "github.com/hyperledger/fabric/core/scc/lscc" "github.com/hyperledger/fabric/internal/peer/packaging" @@ -211,17 +211,6 @@ func initPeer(channelIDs ...string) (*cm.Lifecycle, net.Listener, *ChaincodeSupp scc.DeploySysCC(lsccImpl, chaincodeSupport) - for _, id := range channelIDs { - if err = peer.CreateMockChannel(peerInstance, id); err != nil { - closeListenerAndSleep(lis) - return nil, nil, nil, nil, err - } - // any channel other than the default testchannelid does not have a MSP set up -> create one - if id != "testchannelid" { - mspmgmt.XXXSetMSPManager(id, mspmgmt.GetManagerForChain("testchannelid")) - } - } - go grpcServer.Serve(lis) cleanup := func() { @@ -622,9 +611,9 @@ const ( // Test the execution of a chaincode that invokes another chaincode. func TestChaincodeInvokeChaincode(t *testing.T) { - channel := "testchannelid" - channel2 := channel + "2" - ml, lis, chaincodeSupport, cleanup, err := initPeer(channel, channel2) + channel1 := "testchannelid" + channel2 := channel1 + "2" + ml, lis, chaincodeSupport, cleanup, err := initPeer(channel1, channel2) if err != nil { t.Fail() t.Logf("Error creating peer: %s", err) @@ -632,6 +621,23 @@ func TestChaincodeInvokeChaincode(t *testing.T) { defer cleanup() defer closeListenerAndSleep(lis) + mockPolicy := &mock.Policy{} + mockPolicy.EvaluateSignedDataReturns(nil) + + polMgrChannel1 := &mock.PolicyManager{} + polMgrChannel1.GetPolicyReturns(mockPolicy, true) + err = peer.CreateMockChannel(chaincodeSupport.Peer, channel1, polMgrChannel1) + if err != nil { + t.Fatalf("Failed to create mock channel: %s", err) + } + + polMgrChannel2 := &mock.PolicyManager{} + polMgrChannel2.GetPolicyReturns(mockPolicy, true) + err = peer.CreateMockChannel(chaincodeSupport.Peer, channel2, polMgrChannel2) + if err != nil { + t.Fatalf("Failed to create mock channel: %s", err) + } + peerInstance := chaincodeSupport.Peer var nextBlockNumber1 uint64 = 1 @@ -661,7 +667,7 @@ func TestChaincodeInvokeChaincode(t *testing.T) { pb.ChaincodeSpec_GOLANG, chaincodeExample02GolangPath, util.ToChaincodeArgs("init", "a", strconv.Itoa(initialA), "b", strconv.Itoa(initialB)), - channel, + channel1, nextBlockNumber1, chaincodeSupport, ) @@ -682,7 +688,7 @@ func TestChaincodeInvokeChaincode(t *testing.T) { chaincode2Type, chaincode2Path, util.ToChaincodeArgs("init"), - channel, + channel1, nextBlockNumber1, chaincodeSupport, ) @@ -703,27 +709,21 @@ func TestChaincodeInvokeChaincode(t *testing.T) { Args: util.ToChaincodeArgs(ccContext1.Name, "invoke", "a", "b", "10", ""), }, } - _, _, _, err = invoke(channel, chaincode2InvokeSpec, nextBlockNumber1, []byte("Alice"), chaincodeSupport) + _, _, _, err = invoke(channel1, chaincode2InvokeSpec, nextBlockNumber1, []byte("Alice"), chaincodeSupport) require.NoErrorf(t, err, "error invoking %s: %s", chaincode2Name, err) nextBlockNumber1++ // Check the state in the ledger - err = checkFinalState(peerInstance, channel, ccContext1, initialA-10, initialB+10) + err = checkFinalState(peerInstance, channel1, ccContext1, initialA-10, initialB+10) require.NoErrorf(t, err, "incorrect final state after transaction for %s: %s", chaincode1Name, err) // Change the policies of the two channels in such a way: // 1. Alice has reader access to both the channels. // 2. Bob has access only to chainID2. // Therefore the chaincode invocation should fail. - pm := peerInstance.GetPolicyManager(channel) - pm.(*mockpolicies.Manager).PolicyMap = map[string]policies.Policy{ - policies.ChannelApplicationWriters: &CreatorPolicy{Creators: [][]byte{[]byte("Alice")}}, - } - pm = peerInstance.GetPolicyManager(channel2) - pm.(*mockpolicies.Manager).PolicyMap = map[string]policies.Policy{ - policies.ChannelApplicationWriters: &CreatorPolicy{Creators: [][]byte{[]byte("Alice"), []byte("Bob")}}, - } + polMgrChannel1.GetPolicyReturns(&CreatorPolicy{Creators: [][]byte{[]byte("Alice")}}, true) + polMgrChannel2.GetPolicyReturns(&CreatorPolicy{Creators: [][]byte{[]byte("Alice"), []byte("Bob")}}, true) // deploy chaincode2 on channel2 _, ccContext3, err := deployChaincode( @@ -748,18 +748,18 @@ func TestChaincodeInvokeChaincode(t *testing.T) { Version: chaincode2Version, }, Input: &pb.ChaincodeInput{ - Args: util.ToChaincodeArgs(ccContext1.Name, "invoke", "a", "b", "10", channel), + Args: util.ToChaincodeArgs(ccContext1.Name, "invoke", "a", "b", "10", channel1), }, } // as Bob, invoke chaincode2 on channel2 so that it invokes chaincode1 on channel _, _, _, err = invoke(channel2, chaincode2InvokeSpec, nextBlockNumber2, []byte("Bob"), chaincodeSupport) - require.Errorf(t, err, "as Bob, invoking <%s/%s> via <%s/%s> should fail, but it succeeded.", ccContext1.Name, channel, chaincode2Name, channel2) + require.Errorf(t, err, "as Bob, invoking <%s/%s> via <%s/%s> should fail, but it succeeded.", ccContext1.Name, channel1, chaincode2Name, channel2) assert.True(t, strings.Contains(err.Error(), "[Creator not recognized [Bob]]")) // as Alice, invoke chaincode2 on channel2 so that it invokes chaincode1 on channel _, _, _, err = invoke(channel2, chaincode2InvokeSpec, nextBlockNumber2, []byte("Alice"), chaincodeSupport) - require.NoError(t, err, "as Alice, invoking <%s/%s> via <%s/%s> should should of succeeded, but it failed: %s", ccContext1.Name, channel, chaincode2Name, channel2, err) + require.NoError(t, err, "as Alice, invoking <%s/%s> via <%s/%s> should should of succeeded, but it failed: %s", ccContext1.Name, channel1, chaincode2Name, channel2, err) nextBlockNumber2++ } @@ -779,6 +779,14 @@ func TestChaincodeInvokeChaincodeErrorCase(t *testing.T) { } defer cleanup() + polMgr := &mock.PolicyManager{} + mockPolicy := &mock.Policy{} + mockPolicy.EvaluateIdentitiesReturns(nil) + mockPolicy.EvaluateSignedDataReturns(nil) + polMgr.GetPolicyReturns(mockPolicy, true) + + peer.CreateMockChannel(chaincodeSupport.Peer, channelID, polMgr) + ml.ChaincodeEndorsementInfoStub = func(_, name string, _ ledger.SimpleQueryExecutor) (*lifecycle.ChaincodeEndorsementInfo, error) { switch name { case "lscc": @@ -874,6 +882,8 @@ func TestChaincodeInit(t *testing.T) { defer cleanup() + peer.CreateMockChannel(chaincodeSupport.Peer, channelID, nil) + url := "github.com/hyperledger/fabric/core/chaincode/testdata/src/chaincodes/init_private_data" cID := &pb.ChaincodeID{Name: "init_pvtdata", Path: url, Version: "0"} @@ -928,6 +938,8 @@ func TestQueries(t *testing.T) { defer cleanup() + peer.CreateMockChannel(chaincodeSupport.Peer, channelID, nil) + url := "github.com/hyperledger/fabric/core/chaincode/testdata/src/chaincodes/map" cID := &pb.ChaincodeID{Name: "tmap", Path: url, Version: "0"} @@ -1271,10 +1283,10 @@ func (c *CreatorPolicy) EvaluateIdentities(identities []msp.Identity) error { func newPolicyChecker(peerInstance *peer.Peer) policy.PolicyChecker { return policy.NewPolicyChecker( policies.PolicyManagerGetterFunc(peerInstance.GetPolicyManager), - &mocks.MockIdentityDeserializer{ + &policymocks.MockIdentityDeserializer{ Identity: []byte("Admin"), Msg: []byte("msg1"), }, - &mocks.MockMSPPrincipalGetter{Principal: []byte("Admin")}, + &policymocks.MockMSPPrincipalGetter{Principal: []byte("Admin")}, ) } diff --git a/core/chaincode/executetransaction_pvtdata_test.go b/core/chaincode/executetransaction_pvtdata_test.go index 0a4d5a9f4d7..1356936abcf 100644 --- a/core/chaincode/executetransaction_pvtdata_test.go +++ b/core/chaincode/executetransaction_pvtdata_test.go @@ -15,6 +15,7 @@ import ( "github.com/hyperledger/fabric-protos-go/common" pb "github.com/hyperledger/fabric-protos-go/peer" "github.com/hyperledger/fabric/common/util" + "github.com/hyperledger/fabric/core/peer" "github.com/spf13/viper" "github.com/stretchr/testify/assert" ) @@ -35,6 +36,8 @@ func TestQueriesPrivateData(t *testing.T) { defer cleanup() + peer.CreateMockChannel(chaincodeSupport.Peer, channelID, nil) + url := "github.com/hyperledger/fabric/core/chaincode/testdata/src/chaincodes/map" cID := &pb.ChaincodeID{Name: "tmap", Path: url, Version: "0"} diff --git a/core/chaincode/mock/collection_store.go b/core/chaincode/mock/collection_store.go index 1f6fe93445a..4118abaf80b 100644 --- a/core/chaincode/mock/collection_store.go +++ b/core/chaincode/mock/collection_store.go @@ -593,5 +593,3 @@ func (fake *CollectionStore) recordInvocation(key string, args []interface{}) { } fake.invocations[key] = append(fake.invocations[key], args) } - -var _ privdata.CollectionStore = new(CollectionStore) diff --git a/core/chaincode/mock/policy.go b/core/chaincode/mock/policy.go new file mode 100644 index 00000000000..08749bda11a --- /dev/null +++ b/core/chaincode/mock/policy.go @@ -0,0 +1,192 @@ +// Code generated by counterfeiter. DO NOT EDIT. +package mock + +import ( + "sync" + + "github.com/hyperledger/fabric/msp" + "github.com/hyperledger/fabric/protoutil" +) + +type Policy struct { + EvaluateIdentitiesStub func([]msp.Identity) error + evaluateIdentitiesMutex sync.RWMutex + evaluateIdentitiesArgsForCall []struct { + arg1 []msp.Identity + } + evaluateIdentitiesReturns struct { + result1 error + } + evaluateIdentitiesReturnsOnCall map[int]struct { + result1 error + } + EvaluateSignedDataStub func([]*protoutil.SignedData) error + evaluateSignedDataMutex sync.RWMutex + evaluateSignedDataArgsForCall []struct { + arg1 []*protoutil.SignedData + } + evaluateSignedDataReturns struct { + result1 error + } + evaluateSignedDataReturnsOnCall map[int]struct { + result1 error + } + invocations map[string][][]interface{} + invocationsMutex sync.RWMutex +} + +func (fake *Policy) EvaluateIdentities(arg1 []msp.Identity) error { + var arg1Copy []msp.Identity + if arg1 != nil { + arg1Copy = make([]msp.Identity, len(arg1)) + copy(arg1Copy, arg1) + } + fake.evaluateIdentitiesMutex.Lock() + ret, specificReturn := fake.evaluateIdentitiesReturnsOnCall[len(fake.evaluateIdentitiesArgsForCall)] + fake.evaluateIdentitiesArgsForCall = append(fake.evaluateIdentitiesArgsForCall, struct { + arg1 []msp.Identity + }{arg1Copy}) + fake.recordInvocation("EvaluateIdentities", []interface{}{arg1Copy}) + fake.evaluateIdentitiesMutex.Unlock() + if fake.EvaluateIdentitiesStub != nil { + return fake.EvaluateIdentitiesStub(arg1) + } + if specificReturn { + return ret.result1 + } + fakeReturns := fake.evaluateIdentitiesReturns + return fakeReturns.result1 +} + +func (fake *Policy) EvaluateIdentitiesCallCount() int { + fake.evaluateIdentitiesMutex.RLock() + defer fake.evaluateIdentitiesMutex.RUnlock() + return len(fake.evaluateIdentitiesArgsForCall) +} + +func (fake *Policy) EvaluateIdentitiesCalls(stub func([]msp.Identity) error) { + fake.evaluateIdentitiesMutex.Lock() + defer fake.evaluateIdentitiesMutex.Unlock() + fake.EvaluateIdentitiesStub = stub +} + +func (fake *Policy) EvaluateIdentitiesArgsForCall(i int) []msp.Identity { + fake.evaluateIdentitiesMutex.RLock() + defer fake.evaluateIdentitiesMutex.RUnlock() + argsForCall := fake.evaluateIdentitiesArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *Policy) EvaluateIdentitiesReturns(result1 error) { + fake.evaluateIdentitiesMutex.Lock() + defer fake.evaluateIdentitiesMutex.Unlock() + fake.EvaluateIdentitiesStub = nil + fake.evaluateIdentitiesReturns = struct { + result1 error + }{result1} +} + +func (fake *Policy) EvaluateIdentitiesReturnsOnCall(i int, result1 error) { + fake.evaluateIdentitiesMutex.Lock() + defer fake.evaluateIdentitiesMutex.Unlock() + fake.EvaluateIdentitiesStub = nil + if fake.evaluateIdentitiesReturnsOnCall == nil { + fake.evaluateIdentitiesReturnsOnCall = make(map[int]struct { + result1 error + }) + } + fake.evaluateIdentitiesReturnsOnCall[i] = struct { + result1 error + }{result1} +} + +func (fake *Policy) EvaluateSignedData(arg1 []*protoutil.SignedData) error { + var arg1Copy []*protoutil.SignedData + if arg1 != nil { + arg1Copy = make([]*protoutil.SignedData, len(arg1)) + copy(arg1Copy, arg1) + } + fake.evaluateSignedDataMutex.Lock() + ret, specificReturn := fake.evaluateSignedDataReturnsOnCall[len(fake.evaluateSignedDataArgsForCall)] + fake.evaluateSignedDataArgsForCall = append(fake.evaluateSignedDataArgsForCall, struct { + arg1 []*protoutil.SignedData + }{arg1Copy}) + fake.recordInvocation("EvaluateSignedData", []interface{}{arg1Copy}) + fake.evaluateSignedDataMutex.Unlock() + if fake.EvaluateSignedDataStub != nil { + return fake.EvaluateSignedDataStub(arg1) + } + if specificReturn { + return ret.result1 + } + fakeReturns := fake.evaluateSignedDataReturns + return fakeReturns.result1 +} + +func (fake *Policy) EvaluateSignedDataCallCount() int { + fake.evaluateSignedDataMutex.RLock() + defer fake.evaluateSignedDataMutex.RUnlock() + return len(fake.evaluateSignedDataArgsForCall) +} + +func (fake *Policy) EvaluateSignedDataCalls(stub func([]*protoutil.SignedData) error) { + fake.evaluateSignedDataMutex.Lock() + defer fake.evaluateSignedDataMutex.Unlock() + fake.EvaluateSignedDataStub = stub +} + +func (fake *Policy) EvaluateSignedDataArgsForCall(i int) []*protoutil.SignedData { + fake.evaluateSignedDataMutex.RLock() + defer fake.evaluateSignedDataMutex.RUnlock() + argsForCall := fake.evaluateSignedDataArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *Policy) EvaluateSignedDataReturns(result1 error) { + fake.evaluateSignedDataMutex.Lock() + defer fake.evaluateSignedDataMutex.Unlock() + fake.EvaluateSignedDataStub = nil + fake.evaluateSignedDataReturns = struct { + result1 error + }{result1} +} + +func (fake *Policy) EvaluateSignedDataReturnsOnCall(i int, result1 error) { + fake.evaluateSignedDataMutex.Lock() + defer fake.evaluateSignedDataMutex.Unlock() + fake.EvaluateSignedDataStub = nil + if fake.evaluateSignedDataReturnsOnCall == nil { + fake.evaluateSignedDataReturnsOnCall = make(map[int]struct { + result1 error + }) + } + fake.evaluateSignedDataReturnsOnCall[i] = struct { + result1 error + }{result1} +} + +func (fake *Policy) Invocations() map[string][][]interface{} { + fake.invocationsMutex.RLock() + defer fake.invocationsMutex.RUnlock() + fake.evaluateIdentitiesMutex.RLock() + defer fake.evaluateIdentitiesMutex.RUnlock() + fake.evaluateSignedDataMutex.RLock() + defer fake.evaluateSignedDataMutex.RUnlock() + copiedInvocations := map[string][][]interface{}{} + for key, value := range fake.invocations { + copiedInvocations[key] = value + } + return copiedInvocations +} + +func (fake *Policy) recordInvocation(key string, args []interface{}) { + fake.invocationsMutex.Lock() + defer fake.invocationsMutex.Unlock() + if fake.invocations == nil { + fake.invocations = map[string][][]interface{}{} + } + if fake.invocations[key] == nil { + fake.invocations[key] = [][]interface{}{} + } + fake.invocations[key] = append(fake.invocations[key], args) +} diff --git a/core/chaincode/mock/policy_manager.go b/core/chaincode/mock/policy_manager.go new file mode 100644 index 00000000000..6245db27d2e --- /dev/null +++ b/core/chaincode/mock/policy_manager.go @@ -0,0 +1,196 @@ +// Code generated by counterfeiter. DO NOT EDIT. +package mock + +import ( + "sync" + + "github.com/hyperledger/fabric/common/policies" +) + +type PolicyManager struct { + GetPolicyStub func(string) (policies.Policy, bool) + getPolicyMutex sync.RWMutex + getPolicyArgsForCall []struct { + arg1 string + } + getPolicyReturns struct { + result1 policies.Policy + result2 bool + } + getPolicyReturnsOnCall map[int]struct { + result1 policies.Policy + result2 bool + } + ManagerStub func([]string) (policies.Manager, bool) + managerMutex sync.RWMutex + managerArgsForCall []struct { + arg1 []string + } + managerReturns struct { + result1 policies.Manager + result2 bool + } + managerReturnsOnCall map[int]struct { + result1 policies.Manager + result2 bool + } + invocations map[string][][]interface{} + invocationsMutex sync.RWMutex +} + +func (fake *PolicyManager) GetPolicy(arg1 string) (policies.Policy, bool) { + fake.getPolicyMutex.Lock() + ret, specificReturn := fake.getPolicyReturnsOnCall[len(fake.getPolicyArgsForCall)] + fake.getPolicyArgsForCall = append(fake.getPolicyArgsForCall, struct { + arg1 string + }{arg1}) + fake.recordInvocation("GetPolicy", []interface{}{arg1}) + fake.getPolicyMutex.Unlock() + if fake.GetPolicyStub != nil { + return fake.GetPolicyStub(arg1) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.getPolicyReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *PolicyManager) GetPolicyCallCount() int { + fake.getPolicyMutex.RLock() + defer fake.getPolicyMutex.RUnlock() + return len(fake.getPolicyArgsForCall) +} + +func (fake *PolicyManager) GetPolicyCalls(stub func(string) (policies.Policy, bool)) { + fake.getPolicyMutex.Lock() + defer fake.getPolicyMutex.Unlock() + fake.GetPolicyStub = stub +} + +func (fake *PolicyManager) GetPolicyArgsForCall(i int) string { + fake.getPolicyMutex.RLock() + defer fake.getPolicyMutex.RUnlock() + argsForCall := fake.getPolicyArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *PolicyManager) GetPolicyReturns(result1 policies.Policy, result2 bool) { + fake.getPolicyMutex.Lock() + defer fake.getPolicyMutex.Unlock() + fake.GetPolicyStub = nil + fake.getPolicyReturns = struct { + result1 policies.Policy + result2 bool + }{result1, result2} +} + +func (fake *PolicyManager) GetPolicyReturnsOnCall(i int, result1 policies.Policy, result2 bool) { + fake.getPolicyMutex.Lock() + defer fake.getPolicyMutex.Unlock() + fake.GetPolicyStub = nil + if fake.getPolicyReturnsOnCall == nil { + fake.getPolicyReturnsOnCall = make(map[int]struct { + result1 policies.Policy + result2 bool + }) + } + fake.getPolicyReturnsOnCall[i] = struct { + result1 policies.Policy + result2 bool + }{result1, result2} +} + +func (fake *PolicyManager) Manager(arg1 []string) (policies.Manager, bool) { + var arg1Copy []string + if arg1 != nil { + arg1Copy = make([]string, len(arg1)) + copy(arg1Copy, arg1) + } + fake.managerMutex.Lock() + ret, specificReturn := fake.managerReturnsOnCall[len(fake.managerArgsForCall)] + fake.managerArgsForCall = append(fake.managerArgsForCall, struct { + arg1 []string + }{arg1Copy}) + fake.recordInvocation("Manager", []interface{}{arg1Copy}) + fake.managerMutex.Unlock() + if fake.ManagerStub != nil { + return fake.ManagerStub(arg1) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.managerReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *PolicyManager) ManagerCallCount() int { + fake.managerMutex.RLock() + defer fake.managerMutex.RUnlock() + return len(fake.managerArgsForCall) +} + +func (fake *PolicyManager) ManagerCalls(stub func([]string) (policies.Manager, bool)) { + fake.managerMutex.Lock() + defer fake.managerMutex.Unlock() + fake.ManagerStub = stub +} + +func (fake *PolicyManager) ManagerArgsForCall(i int) []string { + fake.managerMutex.RLock() + defer fake.managerMutex.RUnlock() + argsForCall := fake.managerArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *PolicyManager) ManagerReturns(result1 policies.Manager, result2 bool) { + fake.managerMutex.Lock() + defer fake.managerMutex.Unlock() + fake.ManagerStub = nil + fake.managerReturns = struct { + result1 policies.Manager + result2 bool + }{result1, result2} +} + +func (fake *PolicyManager) ManagerReturnsOnCall(i int, result1 policies.Manager, result2 bool) { + fake.managerMutex.Lock() + defer fake.managerMutex.Unlock() + fake.ManagerStub = nil + if fake.managerReturnsOnCall == nil { + fake.managerReturnsOnCall = make(map[int]struct { + result1 policies.Manager + result2 bool + }) + } + fake.managerReturnsOnCall[i] = struct { + result1 policies.Manager + result2 bool + }{result1, result2} +} + +func (fake *PolicyManager) Invocations() map[string][][]interface{} { + fake.invocationsMutex.RLock() + defer fake.invocationsMutex.RUnlock() + fake.getPolicyMutex.RLock() + defer fake.getPolicyMutex.RUnlock() + fake.managerMutex.RLock() + defer fake.managerMutex.RUnlock() + copiedInvocations := map[string][][]interface{}{} + for key, value := range fake.invocations { + copiedInvocations[key] = value + } + return copiedInvocations +} + +func (fake *PolicyManager) recordInvocation(key string, args []interface{}) { + fake.invocationsMutex.Lock() + defer fake.invocationsMutex.Unlock() + if fake.invocations == nil { + fake.invocations = map[string][][]interface{}{} + } + if fake.invocations[key] == nil { + fake.invocations[key] = [][]interface{}{} + } + fake.invocations[key] = append(fake.invocations[key], args) +} diff --git a/core/mocks/txvalidator/support.go b/core/mocks/txvalidator/support.go index a7f5e2f133c..7b1d1b7e2a2 100644 --- a/core/mocks/txvalidator/support.go +++ b/core/mocks/txvalidator/support.go @@ -21,8 +21,6 @@ import ( "github.com/hyperledger/fabric-protos-go/common" "github.com/hyperledger/fabric/common/channelconfig" - mockpolicies "github.com/hyperledger/fabric/common/mocks/policies" - "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/core/ledger" "github.com/hyperledger/fabric/msp" ) @@ -63,10 +61,6 @@ func (ms *Support) Apply(configtx *common.ConfigEnvelope) error { return ms.ApplyVal } -func (ms *Support) PolicyManager() policies.Manager { - return &mockpolicies.Manager{} -} - func (ms *Support) GetMSPIDs() []string { return []string{"SampleOrg"} } diff --git a/core/peer/mock_helpers.go b/core/peer/mock_helpers.go index cef62947683..dcb3134e200 100644 --- a/core/peer/mock_helpers.go +++ b/core/peer/mock_helpers.go @@ -11,11 +11,11 @@ import ( configtxtest "github.com/hyperledger/fabric/common/configtx/test" mockchannelconfig "github.com/hyperledger/fabric/common/mocks/config" mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx" - mockpolicies "github.com/hyperledger/fabric/common/mocks/policies" + "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/core/ledger" ) -func CreateMockChannel(p *Peer, cid string) error { +func CreateMockChannel(p *Peer, cid string, policyMgr policies.Manager) error { var ledger ledger.PeerLedger var err error @@ -41,9 +41,7 @@ func CreateMockChannel(p *Peer, cid string) error { p.channels[cid] = &Channel{ ledger: ledger, resources: &mockchannelconfig.Resources{ - PolicyManagerVal: &mockpolicies.Manager{ - Policy: &mockpolicies.Policy{}, - }, + PolicyManagerVal: policyMgr, ConfigtxValidatorVal: &mockconfigtx.Validator{}, ApplicationConfigVal: &mockchannelconfig.MockApplication{CapabilitiesRv: &mockchannelconfig.MockApplicationCapabilities{}}, }, diff --git a/core/scc/qscc/query_test.go b/core/scc/qscc/query_test.go index 4a5da31314f..133c19ed4de 100644 --- a/core/scc/qscc/query_test.go +++ b/core/scc/qscc/query_test.go @@ -58,7 +58,7 @@ func setupTestLedger(chainid string, path string) (*shimtest.MockStub, *peer.Pee LedgerMgr: ledgerMgr, CryptoProvider: cryptoProvider, } - peer.CreateMockChannel(peerInstance, chainid) + peer.CreateMockChannel(peerInstance, chainid, nil) lq := &LedgerQuerier{ aclProvider: mockAclProvider, diff --git a/internal/peer/gossip/mcs_test.go b/internal/peer/gossip/mcs_test.go index c26a81807c6..e59a388aa31 100644 --- a/internal/peer/gossip/mcs_test.go +++ b/internal/peer/gossip/mcs_test.go @@ -31,6 +31,12 @@ import ( "github.com/stretchr/testify/mock" ) +//go:generate counterfeiter -o mocks/policy_manager.go -fake-name PolicyManager . policyManager + +type policyManager interface { + policies.Manager +} + //go:generate counterfeiter -o mocks/signer_serializer.go --fake-name SignerSerializer . signerSerializer type signerSerializer interface { diff --git a/internal/peer/gossip/mocks/mocks.go b/internal/peer/gossip/mocks/mocks.go index 9fd70a2cec5..6b4641c7b42 100644 --- a/internal/peer/gossip/mocks/mocks.go +++ b/internal/peer/gossip/mocks/mocks.go @@ -13,7 +13,6 @@ import ( "time" mspproto "github.com/hyperledger/fabric-protos-go/msp" - mockpolicies "github.com/hyperledger/fabric/common/mocks/policies" "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/msp" "github.com/hyperledger/fabric/protoutil" @@ -23,7 +22,9 @@ import ( type ChannelPolicyManagerGetter struct{} func (c *ChannelPolicyManagerGetter) Manager(channelID string) policies.Manager { - return &mockpolicies.Manager{Policy: &mockpolicies.Policy{Err: nil}} + policyMgr := &PolicyManager{} + policyMgr.GetPolicyReturns(nil, true) + return policyMgr } type ChannelPolicyManagerGetterWithManager struct { diff --git a/internal/peer/gossip/mocks/policy_manager.go b/internal/peer/gossip/mocks/policy_manager.go new file mode 100644 index 00000000000..b6aeca42592 --- /dev/null +++ b/internal/peer/gossip/mocks/policy_manager.go @@ -0,0 +1,196 @@ +// Code generated by counterfeiter. DO NOT EDIT. +package mocks + +import ( + "sync" + + "github.com/hyperledger/fabric/common/policies" +) + +type PolicyManager struct { + GetPolicyStub func(string) (policies.Policy, bool) + getPolicyMutex sync.RWMutex + getPolicyArgsForCall []struct { + arg1 string + } + getPolicyReturns struct { + result1 policies.Policy + result2 bool + } + getPolicyReturnsOnCall map[int]struct { + result1 policies.Policy + result2 bool + } + ManagerStub func([]string) (policies.Manager, bool) + managerMutex sync.RWMutex + managerArgsForCall []struct { + arg1 []string + } + managerReturns struct { + result1 policies.Manager + result2 bool + } + managerReturnsOnCall map[int]struct { + result1 policies.Manager + result2 bool + } + invocations map[string][][]interface{} + invocationsMutex sync.RWMutex +} + +func (fake *PolicyManager) GetPolicy(arg1 string) (policies.Policy, bool) { + fake.getPolicyMutex.Lock() + ret, specificReturn := fake.getPolicyReturnsOnCall[len(fake.getPolicyArgsForCall)] + fake.getPolicyArgsForCall = append(fake.getPolicyArgsForCall, struct { + arg1 string + }{arg1}) + fake.recordInvocation("GetPolicy", []interface{}{arg1}) + fake.getPolicyMutex.Unlock() + if fake.GetPolicyStub != nil { + return fake.GetPolicyStub(arg1) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.getPolicyReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *PolicyManager) GetPolicyCallCount() int { + fake.getPolicyMutex.RLock() + defer fake.getPolicyMutex.RUnlock() + return len(fake.getPolicyArgsForCall) +} + +func (fake *PolicyManager) GetPolicyCalls(stub func(string) (policies.Policy, bool)) { + fake.getPolicyMutex.Lock() + defer fake.getPolicyMutex.Unlock() + fake.GetPolicyStub = stub +} + +func (fake *PolicyManager) GetPolicyArgsForCall(i int) string { + fake.getPolicyMutex.RLock() + defer fake.getPolicyMutex.RUnlock() + argsForCall := fake.getPolicyArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *PolicyManager) GetPolicyReturns(result1 policies.Policy, result2 bool) { + fake.getPolicyMutex.Lock() + defer fake.getPolicyMutex.Unlock() + fake.GetPolicyStub = nil + fake.getPolicyReturns = struct { + result1 policies.Policy + result2 bool + }{result1, result2} +} + +func (fake *PolicyManager) GetPolicyReturnsOnCall(i int, result1 policies.Policy, result2 bool) { + fake.getPolicyMutex.Lock() + defer fake.getPolicyMutex.Unlock() + fake.GetPolicyStub = nil + if fake.getPolicyReturnsOnCall == nil { + fake.getPolicyReturnsOnCall = make(map[int]struct { + result1 policies.Policy + result2 bool + }) + } + fake.getPolicyReturnsOnCall[i] = struct { + result1 policies.Policy + result2 bool + }{result1, result2} +} + +func (fake *PolicyManager) Manager(arg1 []string) (policies.Manager, bool) { + var arg1Copy []string + if arg1 != nil { + arg1Copy = make([]string, len(arg1)) + copy(arg1Copy, arg1) + } + fake.managerMutex.Lock() + ret, specificReturn := fake.managerReturnsOnCall[len(fake.managerArgsForCall)] + fake.managerArgsForCall = append(fake.managerArgsForCall, struct { + arg1 []string + }{arg1Copy}) + fake.recordInvocation("Manager", []interface{}{arg1Copy}) + fake.managerMutex.Unlock() + if fake.ManagerStub != nil { + return fake.ManagerStub(arg1) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.managerReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *PolicyManager) ManagerCallCount() int { + fake.managerMutex.RLock() + defer fake.managerMutex.RUnlock() + return len(fake.managerArgsForCall) +} + +func (fake *PolicyManager) ManagerCalls(stub func([]string) (policies.Manager, bool)) { + fake.managerMutex.Lock() + defer fake.managerMutex.Unlock() + fake.ManagerStub = stub +} + +func (fake *PolicyManager) ManagerArgsForCall(i int) []string { + fake.managerMutex.RLock() + defer fake.managerMutex.RUnlock() + argsForCall := fake.managerArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *PolicyManager) ManagerReturns(result1 policies.Manager, result2 bool) { + fake.managerMutex.Lock() + defer fake.managerMutex.Unlock() + fake.ManagerStub = nil + fake.managerReturns = struct { + result1 policies.Manager + result2 bool + }{result1, result2} +} + +func (fake *PolicyManager) ManagerReturnsOnCall(i int, result1 policies.Manager, result2 bool) { + fake.managerMutex.Lock() + defer fake.managerMutex.Unlock() + fake.ManagerStub = nil + if fake.managerReturnsOnCall == nil { + fake.managerReturnsOnCall = make(map[int]struct { + result1 policies.Manager + result2 bool + }) + } + fake.managerReturnsOnCall[i] = struct { + result1 policies.Manager + result2 bool + }{result1, result2} +} + +func (fake *PolicyManager) Invocations() map[string][][]interface{} { + fake.invocationsMutex.RLock() + defer fake.invocationsMutex.RUnlock() + fake.getPolicyMutex.RLock() + defer fake.getPolicyMutex.RUnlock() + fake.managerMutex.RLock() + defer fake.managerMutex.RUnlock() + copiedInvocations := map[string][][]interface{}{} + for key, value := range fake.invocations { + copiedInvocations[key] = value + } + return copiedInvocations +} + +func (fake *PolicyManager) recordInvocation(key string, args []interface{}) { + fake.invocationsMutex.Lock() + defer fake.invocationsMutex.Unlock() + if fake.invocations == nil { + fake.invocations = map[string][][]interface{}{} + } + if fake.invocations[key] == nil { + fake.invocations[key] = [][]interface{}{} + } + fake.invocations[key] = append(fake.invocations[key], args) +}