github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/peer/channel/join_test.go (about) 1 /* 2 Copyright Digital Asset Holdings, LLC 2017 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 channel 18 19 import ( 20 "io/ioutil" 21 "os" 22 "testing" 23 24 "github.com/hyperledger/fabric/peer/common" 25 pb "github.com/hyperledger/fabric/protos/peer" 26 ) 27 28 func TestJoin(t *testing.T) { 29 InitMSP() 30 31 mockblockfile := "/tmp/mockjointest.block" 32 ioutil.WriteFile(mockblockfile, []byte(""), 0644) 33 defer os.Remove(mockblockfile) 34 signer, err := common.GetDefaultSigner() 35 if err != nil { 36 t.Fatalf("Get default signer error: %v", err) 37 } 38 39 mockResponse := &pb.ProposalResponse{ 40 Response: &pb.Response{Status: 200}, 41 Endorsement: &pb.Endorsement{}, 42 } 43 44 mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil) 45 46 mockCF := &ChannelCmdFactory{ 47 EndorserClient: mockEndorerClient, 48 BroadcastFactory: mockBroadcastClientFactory, 49 Signer: signer, 50 } 51 52 cmd := joinCmd(mockCF) 53 54 AddFlags(cmd) 55 56 args := []string{"-b", mockblockfile} 57 cmd.SetArgs(args) 58 59 if err := cmd.Execute(); err != nil { 60 t.Fail() 61 t.Error("expected join command to succeed") 62 } 63 } 64 65 func TestJoinNonExistentBlock(t *testing.T) { 66 InitMSP() 67 68 signer, err := common.GetDefaultSigner() 69 if err != nil { 70 t.Fatalf("Get default signer error: %v", err) 71 } 72 73 mockResponse := &pb.ProposalResponse{ 74 Response: &pb.Response{Status: 200}, 75 Endorsement: &pb.Endorsement{}, 76 } 77 78 mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil) 79 80 mockCF := &ChannelCmdFactory{ 81 EndorserClient: mockEndorerClient, 82 BroadcastFactory: mockBroadcastClientFactory, 83 Signer: signer, 84 } 85 86 cmd := joinCmd(mockCF) 87 88 AddFlags(cmd) 89 90 args := []string{"-b", "mockchain.block"} 91 cmd.SetArgs(args) 92 93 if err := cmd.Execute(); err == nil { 94 t.Fail() 95 t.Error("expected join command to fail") 96 } else if err, _ = err.(GBFileNotFoundErr); err == nil { 97 t.Fail() 98 t.Error("expected file not found error") 99 } 100 } 101 102 func TestBadProposalResponse(t *testing.T) { 103 InitMSP() 104 105 mockblockfile := "/tmp/mockjointest.block" 106 ioutil.WriteFile(mockblockfile, []byte(""), 0644) 107 defer os.Remove(mockblockfile) 108 signer, err := common.GetDefaultSigner() 109 if err != nil { 110 t.Fatalf("Get default signer error: %v", err) 111 } 112 113 mockResponse := &pb.ProposalResponse{ 114 Response: &pb.Response{Status: 500}, 115 Endorsement: &pb.Endorsement{}, 116 } 117 118 mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil) 119 120 mockCF := &ChannelCmdFactory{ 121 EndorserClient: mockEndorerClient, 122 BroadcastFactory: mockBroadcastClientFactory, 123 Signer: signer, 124 } 125 126 cmd := joinCmd(mockCF) 127 128 AddFlags(cmd) 129 130 args := []string{"-b", mockblockfile} 131 cmd.SetArgs(args) 132 133 if err := cmd.Execute(); err == nil { 134 t.Fail() 135 t.Error("expected join command to fail") 136 } else if err, _ = err.(ProposalFailedErr); err == nil { 137 t.Fail() 138 t.Error("expected proposal failure error") 139 } 140 }