Skip to content

Commit

Permalink
FAB-4067 increase UT in peer/common
Browse files Browse the repository at this point in the history
Not much more can be eked out, needs refactor to mock properly

Change-Id: I34f98ee9b6db08484c1c52b5427dac878995ecce
Signed-off-by: Christopher Ferris <chrisfer@us.ibm.com>
  • Loading branch information
christo4ferris committed May 25, 2017
1 parent f662859 commit f0c5495
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 9 deletions.
19 changes: 10 additions & 9 deletions peer/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func InitConfig(cmdRoot string) error {

err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
return fmt.Errorf("Fatal error when reading %s config file: %s\n", cmdRoot, err)
return fmt.Errorf("Error when reading %s config file: %s", cmdRoot, err)
}

return nil
Expand All @@ -64,7 +64,7 @@ func InitCrypto(mspMgrConfigDir string, localMSPID string) error {

err = mspmgmt.LoadLocalMsp(mspMgrConfigDir, bccspConfig, localMSPID)
if err != nil {
return fmt.Errorf("Fatal error when setting up MSP from directory %s: err %s\n", mspMgrConfigDir, err)
return fmt.Errorf("Error when setting up MSP from directory %s: err %s", mspMgrConfigDir, err)
}

return nil
Expand Down Expand Up @@ -96,14 +96,15 @@ func GetAdminClient() (pb.AdminClient, error) {
// core.yaml
func SetLogLevelFromViper(module string) error {
var err error
if module != "" {
logLevelFromViper := viper.GetString("logging." + module)
err = CheckLogLevel(logLevelFromViper)
if err != nil {
return err
}
_, err = flogging.SetModuleLevel(module, logLevelFromViper)
if module == "" {
return fmt.Errorf("log level not set, no module name provided")
}
logLevelFromViper := viper.GetString("logging." + module)
err = CheckLogLevel(logLevelFromViper)
if err != nil {
return err
}
_, err = flogging.SetModuleLevel(module, logLevelFromViper)
return err
}

Expand Down
218 changes: 218 additions & 0 deletions peer/common/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package common_test

import (
"testing"

"github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/peer/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/stretchr/testify/assert"
)

func TestInitConfig(t *testing.T) {
type args struct {
cmdRoot string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Empty command root",
args: args{cmdRoot: ""},
wantErr: true,
},
{
name: "Bad command root",
args: args{cmdRoot: "cre"},
wantErr: true,
},
{
name: "Good command root",
args: args{cmdRoot: "core"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := common.InitConfig(tt.args.cmdRoot); (err != nil) != tt.wantErr {
t.Errorf("InitConfig() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestInitCrypto(t *testing.T) {

mspConfigPath, err := config.GetDevMspDir()
localMspId := "DEFAULT"
err = common.InitCrypto(mspConfigPath, localMspId)
assert.NoError(t, err, "Unexpected error [%s] calling InitCrypto()", err)
err = common.InitCrypto("/etc/foobaz", localMspId)
assert.Error(t, err, "Expected error [%s] calling InitCrypto()", err)
localMspId = ""
err = common.InitCrypto(mspConfigPath, localMspId)
assert.Error(t, err, "Expected error [%s] calling InitCrypto()", err)
}

func TestGetEndorserClient(t *testing.T) {
tests := []struct {
name string
want pb.EndorserClient
wantErr bool
}{
{
name: "Should not return EndorserClient, there is no peer running",
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := common.GetEndorserClient()
if (err != nil) != tt.wantErr {
t.Errorf("GetEndorserClient() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}

func TestSetLogLevelFromViper(t *testing.T) {
type args struct {
module string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Empty module name",
args: args{module: ""},
wantErr: true,
},
{
name: "Invalid module name",
args: args{module: "policy"},
wantErr: true,
},
{
name: "Valid module name",
args: args{module: "cauthdsl"},
wantErr: false,
},
{
name: "Valid module name",
args: args{module: "peer"},
wantErr: false,
},
{
name: "Valid module name",
args: args{module: "gossip"},
wantErr: false,
},
{
name: "Valid module name",
args: args{module: "grpc"},
wantErr: false,
},
{
name: "Valid module name",
args: args{module: "msp"},
wantErr: false,
},
{
name: "Valid module name",
args: args{module: "ledger"},
wantErr: false,
},
{
name: "Valid module name",
args: args{module: "policies"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := common.SetLogLevelFromViper(tt.args.module); (err != nil) != tt.wantErr {
t.Errorf("SetLogLevelFromViper() args = %v error = %v, wantErr %v", tt.args, err, tt.wantErr)
}
})
}
}

func TestCheckLogLevel(t *testing.T) {
type args struct {
level string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Empty module name",
args: args{level: ""},
wantErr: true,
},
{
name: "Valie module name",
args: args{level: "warning"},
wantErr: false,
},
{
name: "Valie module name",
args: args{level: "foobaz"},
wantErr: true,
},
{
name: "Valie module name",
args: args{level: "error"},
wantErr: false,
},
{
name: "Valie module name",
args: args{level: "info"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := common.CheckLogLevel(tt.args.level); (err != nil) != tt.wantErr {
t.Errorf("CheckLogLevel() args = %v error = %v, wantErr %v", tt.args, err, tt.wantErr)
}
})
}
}

func TestGetDefaultSigner(t *testing.T) {
tests := []struct {
name string
want msp.SigningIdentity
wantErr bool
}{
{
name: "Should return DefaultSigningIdentity",
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := common.GetDefaultSigner()
if (err != nil) != tt.wantErr {
t.Errorf("GetDefaultSigner() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}

0 comments on commit f0c5495

Please sign in to comment.