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