Skip to content

Commit f0c5495

Browse files
FAB-4067 increase UT in peer/common
Not much more can be eked out, needs refactor to mock properly Change-Id: I34f98ee9b6db08484c1c52b5427dac878995ecce Signed-off-by: Christopher Ferris <[email protected]>
1 parent f662859 commit f0c5495

File tree

2 files changed

+228
-9
lines changed

2 files changed

+228
-9
lines changed

peer/common/common.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func InitConfig(cmdRoot string) error {
4747

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

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

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

7070
return nil
@@ -96,14 +96,15 @@ func GetAdminClient() (pb.AdminClient, error) {
9696
// core.yaml
9797
func SetLogLevelFromViper(module string) error {
9898
var err error
99-
if module != "" {
100-
logLevelFromViper := viper.GetString("logging." + module)
101-
err = CheckLogLevel(logLevelFromViper)
102-
if err != nil {
103-
return err
104-
}
105-
_, err = flogging.SetModuleLevel(module, logLevelFromViper)
99+
if module == "" {
100+
return fmt.Errorf("log level not set, no module name provided")
106101
}
102+
logLevelFromViper := viper.GetString("logging." + module)
103+
err = CheckLogLevel(logLevelFromViper)
104+
if err != nil {
105+
return err
106+
}
107+
_, err = flogging.SetModuleLevel(module, logLevelFromViper)
107108
return err
108109
}
109110

peer/common/common_test.go

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

0 commit comments

Comments
 (0)