github.com/kaituanwang/hyperledger@v2.0.1+incompatible/internal/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 "path/filepath" 13 "strings" 14 "testing" 15 16 "github.com/hyperledger/fabric/common/flogging" 17 "github.com/hyperledger/fabric/common/util" 18 "github.com/hyperledger/fabric/core/config/configtest" 19 "github.com/hyperledger/fabric/internal/peer/common" 20 "github.com/hyperledger/fabric/msp" 21 "github.com/spf13/cobra" 22 "github.com/spf13/viper" 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestInitConfig(t *testing.T) { 27 cleanup := configtest.SetDevFabricConfigPath(t) 28 defer cleanup() 29 30 type args struct { 31 cmdRoot string 32 } 33 tests := []struct { 34 name string 35 args args 36 wantErr bool 37 }{ 38 { 39 name: "Empty command root", 40 args: args{cmdRoot: ""}, 41 wantErr: true, 42 }, 43 { 44 name: "Bad command root", 45 args: args{cmdRoot: "cre"}, 46 wantErr: true, 47 }, 48 { 49 name: "Good command root", 50 args: args{cmdRoot: "core"}, 51 wantErr: false, 52 }, 53 } 54 for _, tt := range tests { 55 t.Run(tt.name, func(t *testing.T) { 56 if err := common.InitConfig(tt.args.cmdRoot); (err != nil) != tt.wantErr { 57 t.Errorf("InitConfig() error = %v, wantErr %v", err, tt.wantErr) 58 } 59 }) 60 } 61 } 62 63 func TestInitCryptoMissingDir(t *testing.T) { 64 dir := os.TempDir() + "/" + util.GenerateUUID() 65 err := common.InitCrypto(dir, "SampleOrg", msp.ProviderTypeToString(msp.FABRIC)) 66 assert.Error(t, err, "Should be able to initialize crypto with non-existing directory") 67 assert.Contains(t, err.Error(), fmt.Sprintf("folder \"%s\" does not exist", dir)) 68 } 69 70 func TestInitCrypto(t *testing.T) { 71 mspConfigPath := configtest.GetDevMspDir() 72 localMspId := "SampleOrg" 73 err := common.InitCrypto(mspConfigPath, localMspId, msp.ProviderTypeToString(msp.FABRIC)) 74 assert.NoError(t, err, "Unexpected error [%s] calling InitCrypto()", err) 75 err = common.InitCrypto("/etc/foobaz", localMspId, msp.ProviderTypeToString(msp.FABRIC)) 76 assert.Error(t, err, fmt.Sprintf("Expected error [%s] calling InitCrypto()", err)) 77 localMspId = "" 78 err = common.InitCrypto(mspConfigPath, localMspId, msp.ProviderTypeToString(msp.FABRIC)) 79 assert.Error(t, err, fmt.Sprintf("Expected error [%s] calling InitCrypto()", err)) 80 } 81 82 func TestSetBCCSPKeystorePath(t *testing.T) { 83 cfgKey := "peer.BCCSP.SW.FileKeyStore.KeyStore" 84 cfgPath := "./testdata" 85 absPath, _ := filepath.Abs(cfgPath) 86 keystorePath := "/msp/keystore" 87 88 os.Setenv("FABRIC_CFG_PATH", cfgPath) 89 viper.Reset() 90 _ = common.InitConfig("notset") 91 common.SetBCCSPKeystorePath() 92 t.Log(viper.GetString(cfgKey)) 93 assert.Equal(t, "", viper.GetString(cfgKey)) 94 95 viper.Reset() 96 _ = common.InitConfig("absolute") 97 common.SetBCCSPKeystorePath() 98 t.Log(viper.GetString(cfgKey)) 99 assert.Equal(t, keystorePath, viper.GetString(cfgKey)) 100 101 viper.Reset() 102 _ = common.InitConfig("relative") 103 common.SetBCCSPKeystorePath() 104 t.Log(viper.GetString(cfgKey)) 105 assert.Equal(t, filepath.Join(absPath, keystorePath), 106 viper.GetString(cfgKey)) 107 108 viper.Reset() 109 os.Unsetenv("FABRIC_CFG_PATH") 110 } 111 112 func TestCheckLogLevel(t *testing.T) { 113 type args struct { 114 level string 115 } 116 tests := []struct { 117 name string 118 args args 119 wantErr bool 120 }{ 121 { 122 name: "Empty level", 123 args: args{level: ""}, 124 wantErr: true, 125 }, 126 { 127 name: "Valid level", 128 args: args{level: "warning"}, 129 wantErr: false, 130 }, 131 { 132 name: "Invalid level", 133 args: args{level: "foobaz"}, 134 wantErr: true, 135 }, 136 { 137 name: "Valid level", 138 args: args{level: "error"}, 139 wantErr: false, 140 }, 141 { 142 name: "Valid level", 143 args: args{level: "info"}, 144 wantErr: false, 145 }, 146 } 147 for _, tt := range tests { 148 t.Run(tt.name, func(t *testing.T) { 149 if err := common.CheckLogLevel(tt.args.level); (err != nil) != tt.wantErr { 150 t.Errorf("CheckLogLevel() args = %v error = %v, wantErr %v", tt.args, err, tt.wantErr) 151 } 152 }) 153 } 154 } 155 156 func TestGetDefaultSigner(t *testing.T) { 157 tests := []struct { 158 name string 159 want msp.SigningIdentity 160 wantErr bool 161 }{ 162 { 163 name: "Should return DefaultSigningIdentity", 164 want: nil, 165 wantErr: false, 166 }, 167 } 168 for _, tt := range tests { 169 t.Run(tt.name, func(t *testing.T) { 170 _, err := common.GetDefaultSigner() 171 if (err != nil) != tt.wantErr { 172 t.Errorf("GetDefaultSigner() error = %v, wantErr %v", err, tt.wantErr) 173 return 174 } 175 }) 176 } 177 } 178 179 func TestInitCmd(t *testing.T) { 180 cleanup := configtest.SetDevFabricConfigPath(t) 181 defer cleanup() 182 defer viper.Reset() 183 184 // test that InitCmd doesn't remove existing loggers from the logger levels map 185 flogging.MustGetLogger("test") 186 flogging.ActivateSpec("test=error") 187 assert.Equal(t, "error", flogging.LoggerLevel("test")) 188 flogging.MustGetLogger("chaincode") 189 assert.Equal(t, flogging.DefaultLevel(), flogging.LoggerLevel("chaincode")) 190 flogging.MustGetLogger("test.test2") 191 flogging.ActivateSpec("test.test2=warn") 192 assert.Equal(t, "warn", flogging.LoggerLevel("test.test2")) 193 194 origEnvValue := os.Getenv("FABRIC_LOGGING_SPEC") 195 os.Setenv("FABRIC_LOGGING_SPEC", "chaincode=debug:test.test2=fatal:abc=error") 196 common.InitCmd(&cobra.Command{}, nil) 197 assert.Equal(t, "debug", flogging.LoggerLevel("chaincode")) 198 assert.Equal(t, "info", flogging.LoggerLevel("test")) 199 assert.Equal(t, "fatal", flogging.LoggerLevel("test.test2")) 200 assert.Equal(t, "error", flogging.LoggerLevel("abc")) 201 os.Setenv("FABRIC_LOGGING_SPEC", origEnvValue) 202 } 203 204 func TestInitCmdWithoutInitCrypto(t *testing.T) { 205 cleanup := configtest.SetDevFabricConfigPath(t) 206 defer cleanup() 207 defer viper.Reset() 208 209 peerCmd := &cobra.Command{ 210 Use: "peer", 211 } 212 lifecycleCmd := &cobra.Command{ 213 Use: "lifecycle", 214 } 215 chaincodeCmd := &cobra.Command{ 216 Use: "chaincode", 217 } 218 packageCmd := &cobra.Command{ 219 Use: "package", 220 } 221 // peer lifecycle chaincode package 222 chaincodeCmd.AddCommand(packageCmd) 223 lifecycleCmd.AddCommand(chaincodeCmd) 224 peerCmd.AddCommand(lifecycleCmd) 225 226 // MSPCONFIGPATH is default value 227 common.InitCmd(packageCmd, nil) 228 229 // set MSPCONFIGPATH to be a missing dir, the function InitCrypto will fail 230 // confirm that 'peer lifecycle chaincode package' mandates does not require MSPCONFIG information 231 viper.SetEnvPrefix("core") 232 viper.AutomaticEnv() 233 replacer := strings.NewReplacer(".", "_") 234 viper.SetEnvKeyReplacer(replacer) 235 dir := os.TempDir() + "/" + util.GenerateUUID() 236 os.Setenv("CORE_PEER_MSPCONFIGPATH", dir) 237 238 common.InitCmd(packageCmd, nil) 239 }