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