github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/core/peer/peer_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package peer 18 19 import ( 20 "fmt" 21 "net" 22 "os" 23 "testing" 24 25 configtxtest "github.com/hyperledger/fabric/common/configtx/test" 26 "github.com/hyperledger/fabric/common/localmsp" 27 ccp "github.com/hyperledger/fabric/core/common/ccprovider" 28 "github.com/hyperledger/fabric/core/deliverservice" 29 "github.com/hyperledger/fabric/core/deliverservice/blocksprovider" 30 "github.com/hyperledger/fabric/core/mocks/ccprovider" 31 "github.com/hyperledger/fabric/gossip/api" 32 "github.com/hyperledger/fabric/gossip/service" 33 "github.com/hyperledger/fabric/msp/mgmt" 34 "github.com/hyperledger/fabric/msp/mgmt/testtools" 35 "github.com/hyperledger/fabric/peer/gossip/mcs" 36 "github.com/spf13/viper" 37 "github.com/stretchr/testify/assert" 38 "google.golang.org/grpc" 39 ) 40 41 type mockDeliveryClient struct { 42 } 43 44 // StartDeliverForChannel dynamically starts delivery of new blocks from ordering service 45 // to channel peers. 46 func (ds *mockDeliveryClient) StartDeliverForChannel(chainID string, ledgerInfo blocksprovider.LedgerInfo) error { 47 return nil 48 } 49 50 // StopDeliverForChannel dynamically stops delivery of new blocks from ordering service 51 // to channel peers. 52 func (ds *mockDeliveryClient) StopDeliverForChannel(chainID string) error { 53 return nil 54 } 55 56 // Stop terminates delivery service and closes the connection 57 func (*mockDeliveryClient) Stop() { 58 59 } 60 61 type mockDeliveryClientFactory struct { 62 } 63 64 func (*mockDeliveryClientFactory) Service(g service.GossipService, endpoints []string, mcs api.MessageCryptoService) (deliverclient.DeliverService, error) { 65 return &mockDeliveryClient{}, nil 66 } 67 68 func TestInitialize(t *testing.T) { 69 viper.Set("peer.fileSystemPath", "/var/hyperledger/test/") 70 71 // we mock this because we can't import the chaincode package lest we create an import cycle 72 ccp.RegisterChaincodeProviderFactory(&ccprovider.MockCcProviderFactory{}) 73 74 Initialize(nil) 75 } 76 77 func TestCreateChainFromBlock(t *testing.T) { 78 viper.Set("peer.fileSystemPath", "/var/hyperledger/test/") 79 defer os.RemoveAll("/var/hyperledger/test/") 80 testChainID := "mytestchainid" 81 block, err := configtxtest.MakeGenesisBlock(testChainID) 82 if err != nil { 83 fmt.Printf("Failed to create a config block, err %s\n", err) 84 t.FailNow() 85 } 86 87 // Initialize gossip service 88 grpcServer := grpc.NewServer() 89 socket, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 13611)) 90 assert.NoError(t, err) 91 go grpcServer.Serve(socket) 92 defer grpcServer.Stop() 93 94 msptesttools.LoadMSPSetupForTesting() 95 96 identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize() 97 messageCryptoService := mcs.New(&mcs.MockChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager()) 98 service.InitGossipServiceCustomDeliveryFactory(identity, "localhost:13611", grpcServer, &mockDeliveryClientFactory{}, messageCryptoService) 99 100 err = CreateChainFromBlock(block) 101 if err != nil { 102 t.Fatalf("failed to create chain %s", err) 103 } 104 105 // Correct ledger 106 ledger := GetLedger(testChainID) 107 if ledger == nil { 108 t.Fatalf("failed to get correct ledger") 109 } 110 111 // Bad ledger 112 ledger = GetLedger("BogusChain") 113 if ledger != nil { 114 t.Fatalf("got a bogus ledger") 115 } 116 117 // Correct block 118 block = GetCurrConfigBlock(testChainID) 119 if block == nil { 120 t.Fatalf("failed to get correct block") 121 } 122 123 // Bad block 124 block = GetCurrConfigBlock("BogusBlock") 125 if block != nil { 126 t.Fatalf("got a bogus block") 127 } 128 129 // Chaos monkey test 130 Initialize(nil) 131 132 SetCurrConfigBlock(block, testChainID) 133 134 channels := GetChannelsInfo() 135 if len(channels) != 1 { 136 t.Fatalf("incorrect number of channels") 137 } 138 } 139 140 func TestNewPeerClientConnection(t *testing.T) { 141 if _, err := NewPeerClientConnection(); err != nil { 142 t.Log(err) 143 } 144 } 145 146 func TestGetLocalIP(t *testing.T) { 147 ip := GetLocalIP() 148 t.Log(ip) 149 }