github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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 "os" 22 "testing" 23 24 "github.com/hyperledger/fabric/peer/common" 25 pb "github.com/hyperledger/fabric/protos/peer" 26 27 "github.com/spf13/cobra" 28 "github.com/spf13/viper" 29 ) 30 31 func initInstallTest(fsPath string, t *testing.T) *cobra.Command { 32 viper.Set("peer.fileSystemPath", fsPath) 33 finitInstallTest(fsPath) 34 35 //if mkdir fails everthing will fail... but it should not 36 if err := os.Mkdir(fsPath, 0755); err != nil { 37 t.Fatalf("could not create install env") 38 } 39 40 InitMSP() 41 42 signer, err := common.GetDefaultSigner() 43 if err != nil { 44 t.Fatalf("Get default signer error: %v", err) 45 } 46 47 mockCF := &ChaincodeCmdFactory{ 48 Signer: signer, 49 } 50 51 cmd := installCmd(mockCF) 52 AddFlags(cmd) 53 54 return cmd 55 } 56 57 func finitInstallTest(fsPath string) { 58 os.RemoveAll(fsPath) 59 } 60 61 // TestBadVersion tests generation of install command 62 func TestBadVersion(t *testing.T) { 63 fsPath := "/tmp/installtest" 64 65 cmd := initInstallTest(fsPath, t) 66 defer finitInstallTest(fsPath) 67 68 args := []string{"-n", "example02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"} 69 cmd.SetArgs(args) 70 71 if err := cmd.Execute(); err == nil { 72 t.Fatal("Expected error executing install command for version not specified") 73 } 74 } 75 76 // TestNonExistentCC non existent chaincode should fail as expected 77 func TestNonExistentCC(t *testing.T) { 78 fsPath := "/tmp/installtest" 79 80 cmd := initInstallTest(fsPath, t) 81 defer finitInstallTest(fsPath) 82 83 args := []string{"-n", "badexample02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/bad_example02", "-v", "testversion"} 84 cmd.SetArgs(args) 85 86 if err := cmd.Execute(); err == nil { 87 t.Fatal("Expected error executing install command for bad chaincode") 88 } 89 90 if _, err := os.Stat(fsPath + "/chaincodes/badexample02.testversion"); err == nil { 91 t.Fatal("chaincode example02.testversion should not exist") 92 } 93 } 94 95 func installEx02() error { 96 97 signer, err := common.GetDefaultSigner() 98 if err != nil { 99 return fmt.Errorf("Get default signer error: %v", err) 100 } 101 102 mockResponse := &pb.ProposalResponse{ 103 Response: &pb.Response{Status: 200}, 104 Endorsement: &pb.Endorsement{}, 105 } 106 107 mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil) 108 109 mockCF := &ChaincodeCmdFactory{ 110 EndorserClient: mockEndorerClient, 111 Signer: signer, 112 } 113 114 cmd := installCmd(mockCF) 115 AddFlags(cmd) 116 117 args := []string{"-n", "example02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "-v", "anotherversion"} 118 cmd.SetArgs(args) 119 120 if err := cmd.Execute(); err != nil { 121 return fmt.Errorf("Run chaincode upgrade cmd error:%v", err) 122 } 123 124 return nil 125 } 126 127 func TestInstall(t *testing.T) { 128 InitMSP() 129 if err := installEx02(); err != nil { 130 t.Fatalf("Install failed with error: %v", err) 131 } 132 }