github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/peer/common/common_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package common_test 8 9 import ( 10 "fmt" 11 "os" 12 "testing" 13 14 "github.com/hyperledger/fabric/common/util" 15 "github.com/hyperledger/fabric/core/config" 16 "github.com/hyperledger/fabric/msp" 17 "github.com/hyperledger/fabric/peer/common" 18 pb "github.com/hyperledger/fabric/protos/peer" 19 "github.com/stretchr/testify/assert" 20 ) 21 22 func TestInitConfig(t *testing.T) { 23 type args struct { 24 cmdRoot string 25 } 26 tests := []struct { 27 name string 28 args args 29 wantErr bool 30 }{ 31 { 32 name: "Empty command root", 33 args: args{cmdRoot: ""}, 34 wantErr: true, 35 }, 36 { 37 name: "Bad command root", 38 args: args{cmdRoot: "cre"}, 39 wantErr: true, 40 }, 41 { 42 name: "Good command root", 43 args: args{cmdRoot: "core"}, 44 wantErr: false, 45 }, 46 } 47 for _, tt := range tests { 48 t.Run(tt.name, func(t *testing.T) { 49 if err := common.InitConfig(tt.args.cmdRoot); (err != nil) != tt.wantErr { 50 t.Errorf("InitConfig() error = %v, wantErr %v", err, tt.wantErr) 51 } 52 }) 53 } 54 } 55 56 func TestINitCryptoMissingDir(t *testing.T) { 57 dir := os.TempDir() + "/" + util.GenerateUUID() 58 err := common.InitCrypto(dir, "DEFAULT") 59 assert.Error(t, err, "Should be able to initialize crypto with non-existing directory") 60 assert.Contains(t, err.Error(), fmt.Sprintf("missing %s folder", dir)) 61 } 62 63 func TestInitCrypto(t *testing.T) { 64 65 mspConfigPath, err := config.GetDevMspDir() 66 localMspId := "DEFAULT" 67 err = common.InitCrypto(mspConfigPath, localMspId) 68 assert.NoError(t, err, "Unexpected error [%s] calling InitCrypto()", err) 69 err = common.InitCrypto("/etc/foobaz", localMspId) 70 assert.Error(t, err, "Expected error [%s] calling InitCrypto()", err) 71 localMspId = "" 72 err = common.InitCrypto(mspConfigPath, localMspId) 73 assert.Error(t, err, "Expected error [%s] calling InitCrypto()", err) 74 } 75 76 func TestGetEndorserClient(t *testing.T) { 77 tests := []struct { 78 name string 79 want pb.EndorserClient 80 wantErr bool 81 }{ 82 { 83 name: "Should not return EndorserClient, there is no peer running", 84 want: nil, 85 wantErr: true, 86 }, 87 } 88 for _, tt := range tests { 89 t.Run(tt.name, func(t *testing.T) { 90 _, err := common.GetEndorserClient() 91 if (err != nil) != tt.wantErr { 92 t.Errorf("GetEndorserClient() error = %v, wantErr %v", err, tt.wantErr) 93 return 94 } 95 }) 96 } 97 } 98 99 func TestSetLogLevelFromViper(t *testing.T) { 100 type args struct { 101 module string 102 } 103 tests := []struct { 104 name string 105 args args 106 wantErr bool 107 }{ 108 { 109 name: "Empty module name", 110 args: args{module: ""}, 111 wantErr: true, 112 }, 113 { 114 name: "Invalid module name", 115 args: args{module: "policy"}, 116 wantErr: true, 117 }, 118 { 119 name: "Valid module name", 120 args: args{module: "cauthdsl"}, 121 wantErr: false, 122 }, 123 { 124 name: "Valid module name", 125 args: args{module: "peer"}, 126 wantErr: false, 127 }, 128 { 129 name: "Valid module name", 130 args: args{module: "gossip"}, 131 wantErr: false, 132 }, 133 { 134 name: "Valid module name", 135 args: args{module: "grpc"}, 136 wantErr: false, 137 }, 138 { 139 name: "Valid module name", 140 args: args{module: "msp"}, 141 wantErr: false, 142 }, 143 { 144 name: "Valid module name", 145 args: args{module: "ledger"}, 146 wantErr: false, 147 }, 148 { 149 name: "Valid module name", 150 args: args{module: "policies"}, 151 wantErr: false, 152 }, 153 } 154 for _, tt := range tests { 155 t.Run(tt.name, func(t *testing.T) { 156 if err := common.SetLogLevelFromViper(tt.args.module); (err != nil) != tt.wantErr { 157 t.Errorf("SetLogLevelFromViper() args = %v error = %v, wantErr %v", tt.args, err, tt.wantErr) 158 } 159 }) 160 } 161 } 162 163 func TestCheckLogLevel(t *testing.T) { 164 type args struct { 165 level string 166 } 167 tests := []struct { 168 name string 169 args args 170 wantErr bool 171 }{ 172 { 173 name: "Empty module name", 174 args: args{level: ""}, 175 wantErr: true, 176 }, 177 { 178 name: "Valie module name", 179 args: args{level: "warning"}, 180 wantErr: false, 181 }, 182 { 183 name: "Valie module name", 184 args: args{level: "foobaz"}, 185 wantErr: true, 186 }, 187 { 188 name: "Valie module name", 189 args: args{level: "error"}, 190 wantErr: false, 191 }, 192 { 193 name: "Valie module name", 194 args: args{level: "info"}, 195 wantErr: false, 196 }, 197 } 198 for _, tt := range tests { 199 t.Run(tt.name, func(t *testing.T) { 200 if err := common.CheckLogLevel(tt.args.level); (err != nil) != tt.wantErr { 201 t.Errorf("CheckLogLevel() args = %v error = %v, wantErr %v", tt.args, err, tt.wantErr) 202 } 203 }) 204 } 205 } 206 207 func TestGetDefaultSigner(t *testing.T) { 208 tests := []struct { 209 name string 210 want msp.SigningIdentity 211 wantErr bool 212 }{ 213 { 214 name: "Should return DefaultSigningIdentity", 215 want: nil, 216 wantErr: false, 217 }, 218 } 219 for _, tt := range tests { 220 t.Run(tt.name, func(t *testing.T) { 221 _, err := common.GetDefaultSigner() 222 if (err != nil) != tt.wantErr { 223 t.Errorf("GetDefaultSigner() error = %v, wantErr %v", err, tt.wantErr) 224 return 225 } 226 }) 227 } 228 }