github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/peer/chaincode/install_test.go (about) 1 /* 2 Copyright IBM Corp. 2016-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 chaincode 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "os" 23 "testing" 24 25 "github.com/hyperledger/fabric/peer/common" 26 pb "github.com/hyperledger/fabric/protos/peer" 27 28 "github.com/spf13/cobra" 29 "github.com/spf13/viper" 30 ) 31 32 func initInstallTest(fsPath string, t *testing.T) (*cobra.Command, *ChaincodeCmdFactory) { 33 viper.Set("peer.fileSystemPath", fsPath) 34 finitInstallTest(fsPath) 35 36 //if mkdir fails everthing will fail... but it should not 37 if err := os.Mkdir(fsPath, 0755); err != nil { 38 t.Fatalf("could not create install env") 39 } 40 41 InitMSP() 42 43 signer, err := common.GetDefaultSigner() 44 if err != nil { 45 t.Fatalf("Get default signer error: %v", err) 46 } 47 48 mockCF := &ChaincodeCmdFactory{ 49 Signer: signer, 50 } 51 52 cmd := installCmd(mockCF) 53 AddFlags(cmd) 54 55 return cmd, mockCF 56 } 57 58 func finitInstallTest(fsPath string) { 59 os.RemoveAll(fsPath) 60 } 61 62 // TestBadVersion tests generation of install command 63 func TestBadVersion(t *testing.T) { 64 fsPath := "/tmp/installtest" 65 66 cmd, _ := initInstallTest(fsPath, t) 67 defer finitInstallTest(fsPath) 68 69 args := []string{"-n", "example02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"} 70 cmd.SetArgs(args) 71 72 if err := cmd.Execute(); err == nil { 73 t.Fatal("Expected error executing install command for version not specified") 74 } 75 } 76 77 // TestNonExistentCC non existent chaincode should fail as expected 78 func TestNonExistentCC(t *testing.T) { 79 fsPath := "/tmp/installtest" 80 81 cmd, _ := initInstallTest(fsPath, t) 82 defer finitInstallTest(fsPath) 83 84 args := []string{"-n", "badexample02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/bad_example02", "-v", "testversion"} 85 cmd.SetArgs(args) 86 87 if err := cmd.Execute(); err == nil { 88 t.Fatal("Expected error executing install command for bad chaincode") 89 } 90 91 if _, err := os.Stat(fsPath + "/chaincodes/badexample02.testversion"); err == nil { 92 t.Fatal("chaincode example02.testversion should not exist") 93 } 94 } 95 96 // TestInstallFromPackage installs using package 97 func TestInstallFromPackage(t *testing.T) { 98 pdir := newTempDir() 99 defer os.RemoveAll(pdir) 100 101 ccpackfile := pdir + "/ccpack.file" 102 err := createSignedCDSPackage([]string{"-n", "somecc", "-p", "some/go/package", "-v", "0", ccpackfile}, false) 103 if err != nil { 104 t.Fatalf("could not create package :%v", err) 105 } 106 107 fsPath := "/tmp/installtest" 108 109 cmd, mockCF := initInstallTest(fsPath, t) 110 defer finitInstallTest(fsPath) 111 112 mockResponse := &pb.ProposalResponse{ 113 Response: &pb.Response{Status: 200}, 114 Endorsement: &pb.Endorsement{}, 115 } 116 117 mockEndorserClient := common.GetMockEndorserClient(mockResponse, nil) 118 119 mockCF.EndorserClient = mockEndorserClient 120 121 args := []string{ccpackfile} 122 cmd.SetArgs(args) 123 124 if err := cmd.Execute(); err != nil { 125 t.Fatal("error executing install command from package") 126 } 127 } 128 129 // TestInstallFromBadPackage tests bad package failure 130 func TestInstallFromBadPackage(t *testing.T) { 131 pdir := newTempDir() 132 defer os.RemoveAll(pdir) 133 134 ccpackfile := pdir + "/ccpack.file" 135 err := ioutil.WriteFile(ccpackfile, []byte("really bad CC package"), 0700) 136 if err != nil { 137 t.Fatalf("could not create package :%v", err) 138 } 139 140 fsPath := "/tmp/installtest" 141 142 cmd, mockCF := initInstallTest(fsPath, t) 143 defer finitInstallTest(fsPath) 144 145 //this should not reach the endorser which will respond with success 146 mockResponse := &pb.ProposalResponse{ 147 Response: &pb.Response{Status: 200}, 148 Endorsement: &pb.Endorsement{}, 149 } 150 151 mockEndorserClient := common.GetMockEndorserClient(mockResponse, nil) 152 153 mockCF.EndorserClient = mockEndorserClient 154 155 args := []string{ccpackfile} 156 cmd.SetArgs(args) 157 158 if err := cmd.Execute(); err == nil { 159 t.Fatal("expected error installing bad package") 160 } 161 } 162 163 func installEx02() error { 164 signer, err := common.GetDefaultSigner() 165 if err != nil { 166 return fmt.Errorf("Get default signer error: %v", err) 167 } 168 169 mockResponse := &pb.ProposalResponse{ 170 Response: &pb.Response{Status: 200}, 171 Endorsement: &pb.Endorsement{}, 172 } 173 174 mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil) 175 176 mockCF := &ChaincodeCmdFactory{ 177 EndorserClient: mockEndorerClient, 178 Signer: signer, 179 } 180 181 cmd := installCmd(mockCF) 182 AddFlags(cmd) 183 184 args := []string{"-n", "example02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "-v", "anotherversion"} 185 cmd.SetArgs(args) 186 187 if err := cmd.Execute(); err != nil { 188 return fmt.Errorf("Run chaincode upgrade cmd error:%v", err) 189 } 190 191 return nil 192 } 193 194 func TestInstall(t *testing.T) { 195 InitMSP() 196 if err := installEx02(); err != nil { 197 t.Fatalf("Install failed with error: %v", err) 198 } 199 }