Skip to content

Commit f1437a8

Browse files
committed
[FAB-3473] Improve UT coverage for peer/clilogging
This CR reworks some of the peer/clilogging commands. With these changes in place and updates to the unit tests, the unit test coverage for the package improves from 25% to 66%. Further work is still needed to improve the coverage for this package and other peer CLI packages but this is at least a start. Change-Id: I77df0ca7af7565953ea43e287067d530a7a9dc6d Signed-off-by: Will Lahti <[email protected]>
1 parent 47778ba commit f1437a8

File tree

8 files changed

+180
-166
lines changed

8 files changed

+180
-166
lines changed

peer/clilogging/common.go

+21
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,31 @@ package clilogging
1919
import (
2020
"github.com/hyperledger/fabric/common/errors"
2121
"github.com/hyperledger/fabric/peer/common"
22+
pb "github.com/hyperledger/fabric/protos/peer"
2223

2324
"github.com/spf13/cobra"
2425
)
2526

27+
// LoggingCmdFactory holds the clients used by LoggingCmd
28+
type LoggingCmdFactory struct {
29+
AdminClient pb.AdminClient
30+
}
31+
32+
// InitCmdFactory init the LoggingCmdFactory with default admin client
33+
func InitCmdFactory() (*LoggingCmdFactory, error) {
34+
var err error
35+
var adminClient pb.AdminClient
36+
37+
adminClient, err = common.GetAdminClient()
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
return &LoggingCmdFactory{
43+
AdminClient: adminClient,
44+
}, nil
45+
}
46+
2647
func checkLoggingCmdParams(cmd *cobra.Command, args []string) error {
2748
var err error
2849
if cmd.Name() == "revertlevels" {

peer/clilogging/getlevel.go

+18-24
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,36 @@ limitations under the License.
1717
package clilogging
1818

1919
import (
20-
"github.com/hyperledger/fabric/peer/common"
2120
pb "github.com/hyperledger/fabric/protos/peer"
2221

2322
"github.com/spf13/cobra"
2423
"golang.org/x/net/context"
2524
)
2625

27-
func getLevelCmd() *cobra.Command {
28-
return loggingGetLevelCmd
29-
}
26+
func getLevelCmd(cf *LoggingCmdFactory) *cobra.Command {
27+
var loggingGetLevelCmd = &cobra.Command{
28+
Use: "getlevel <module>",
29+
Short: "Returns the logging level of the requested module logger.",
30+
Long: `Returns the logging level of the requested module logger`,
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
return getLevel(cf, cmd, args)
33+
},
34+
}
3035

31-
var loggingGetLevelCmd = &cobra.Command{
32-
Use: "getlevel <module>",
33-
Short: "Returns the logging level of the requested module logger.",
34-
Long: `Returns the logging level of the requested module logger`,
35-
Run: func(cmd *cobra.Command, args []string) {
36-
getLevel(cmd, args)
37-
},
36+
return loggingGetLevelCmd
3837
}
3938

40-
func getLevel(cmd *cobra.Command, args []string) (err error) {
39+
func getLevel(cf *LoggingCmdFactory, cmd *cobra.Command, args []string) (err error) {
4140
err = checkLoggingCmdParams(cmd, args)
42-
43-
if err != nil {
44-
logger.Warningf("Error: %s", err)
45-
} else {
46-
adminClient, err := common.GetAdminClient()
47-
if err != nil {
48-
logger.Warningf("%s", err)
49-
return err
41+
if err == nil {
42+
if cf == nil {
43+
cf, err = InitCmdFactory()
44+
if err != nil {
45+
return err
46+
}
5047
}
51-
52-
logResponse, err := adminClient.GetModuleLogLevel(context.Background(), &pb.LogLevelRequest{LogModule: args[0]})
53-
48+
logResponse, err := cf.AdminClient.GetModuleLogLevel(context.Background(), &pb.LogLevelRequest{LogModule: args[0]})
5449
if err != nil {
55-
logger.Warningf("Error retrieving log level")
5650
return err
5751
}
5852
logger.Infof("Current log level for peer module '%s': %s", logResponse.LogModule, logResponse.LogLevel)

peer/clilogging/logging.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ const loggingFuncName = "logging"
2929
var logger = flogging.MustGetLogger("cli/logging")
3030

3131
// Cmd returns the cobra command for Logging
32-
func Cmd() *cobra.Command {
33-
loggingCmd.AddCommand(getLevelCmd())
34-
loggingCmd.AddCommand(setLevelCmd())
35-
loggingCmd.AddCommand(revertLevelsCmd())
32+
func Cmd(cf *LoggingCmdFactory) *cobra.Command {
33+
loggingCmd.AddCommand(getLevelCmd(cf))
34+
loggingCmd.AddCommand(setLevelCmd(cf))
35+
loggingCmd.AddCommand(revertLevelsCmd(cf))
3636

3737
return loggingCmd
3838
}

peer/clilogging/logging_test.go

+64-89
Original file line numberDiff line numberDiff line change
@@ -16,108 +16,83 @@
1616

1717
package clilogging
1818

19-
import "testing"
20-
21-
// TestGetLevelEmptyParams tests the parameter checking for getlevel, which
22-
// should return an error when no parameters are provided
23-
func TestGetLevelEmptyParams(t *testing.T) {
24-
var args []string
25-
26-
err := checkLoggingCmdParams(getLevelCmd(), args)
27-
28-
if err == nil {
29-
t.FailNow()
30-
}
19+
import (
20+
"testing"
21+
22+
"github.com/hyperledger/fabric/peer/common"
23+
"github.com/spf13/cobra"
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
type testCase struct {
28+
name string
29+
args []string
30+
shouldErr bool
3131
}
3232

33-
// TestGetLevel tests the parameter checking for getlevel, which should
34-
// should return a nil error when one (or more) parameters are provided
35-
func TestGetLevel(t *testing.T) {
36-
args := make([]string, 1)
37-
args[0] = "peer"
38-
39-
err := checkLoggingCmdParams(getLevelCmd(), args)
40-
41-
if err != nil {
42-
t.Fatal(err)
33+
func initLoggingTest(command string) (*cobra.Command, *LoggingCmdFactory) {
34+
adminClient := common.GetMockAdminClient(nil)
35+
mockCF := &LoggingCmdFactory{
36+
AdminClient: adminClient,
4337
}
44-
}
45-
46-
// TestSetLevelEmptyParams tests the parameter checking for setlevel, which
47-
// should return an error when no parameters are provided
48-
func TestSetLevelEmptyParams(t *testing.T) {
49-
var args []string
50-
51-
err := checkLoggingCmdParams(setLevelCmd(), args)
52-
53-
if err == nil {
54-
t.FailNow()
38+
var cmd *cobra.Command
39+
if command == "getlevel" {
40+
cmd = getLevelCmd(mockCF)
41+
} else if command == "setlevel" {
42+
cmd = setLevelCmd(mockCF)
43+
} else if command == "revertlevels" {
44+
cmd = revertLevelsCmd(mockCF)
45+
} else {
46+
// should only happen when there's a typo in a test case below
5547
}
48+
return cmd, mockCF
5649
}
5750

58-
// TestSetLevelEmptyParams tests the parameter checking for setlevel, which
59-
// should return an error when only one parameter is provided
60-
func TestSetLevelOneParam(t *testing.T) {
61-
args := make([]string, 1)
62-
args[0] = "peer"
63-
64-
err := checkLoggingCmdParams(setLevelCmd(), args)
65-
66-
if err == nil {
67-
t.FailNow()
51+
func runTests(t *testing.T, command string, tc []testCase) {
52+
cmd, _ := initLoggingTest(command)
53+
assert := assert.New(t)
54+
for i := 0; i < len(tc); i++ {
55+
t.Run(tc[i].name, func(t *testing.T) {
56+
cmd.SetArgs(tc[i].args)
57+
err := cmd.Execute()
58+
if tc[i].shouldErr {
59+
assert.NotNil(err)
60+
}
61+
if !tc[i].shouldErr {
62+
assert.Nil(err)
63+
}
64+
})
6865
}
6966
}
7067

71-
// TestSetLevelEmptyParams tests the parameter checking for setlevel, which
72-
// should return an error when an invalid log level is provided
73-
func TestSetLevelInvalid(t *testing.T) {
74-
args := make([]string, 2)
75-
args[0] = "peer"
76-
args[1] = "invalidlevel"
77-
78-
err := checkLoggingCmdParams(setLevelCmd(), args)
79-
80-
if err == nil {
81-
t.FailNow()
82-
}
68+
// TestGetLevel tests getlevel with various parameters
69+
func TestGetLevel(t *testing.T) {
70+
var tc []testCase
71+
tc = append(tc,
72+
testCase{"NoParameters", []string{}, true},
73+
testCase{"Valid", []string{"peer"}, false},
74+
)
75+
runTests(t, "getlevel", tc)
8376
}
8477

85-
// TestSetLevelEmptyParams tests the parameter checking for setlevel, which
86-
// should return a nil error when two parameters, the second of which is a
87-
// valid log level, are provided
78+
// TestStLevel tests setlevel with various parameters
8879
func TestSetLevel(t *testing.T) {
89-
args := make([]string, 2)
90-
args[0] = "peer"
91-
args[1] = "debug"
92-
93-
err := checkLoggingCmdParams(setLevelCmd(), args)
94-
95-
if err != nil {
96-
t.Fatal(err)
97-
}
80+
var tc []testCase
81+
tc = append(tc,
82+
testCase{"NoParameters", []string{}, true},
83+
testCase{"OneParameter", []string{"peer"}, true},
84+
testCase{"Valid", []string{"peer", "warning"}, false},
85+
testCase{"InvalidLevel", []string{"peer", "invalidlevel"}, true},
86+
)
87+
runTests(t, "setlevel", tc)
9888
}
9989

100-
// TestRevertLevels tests the parameter checking for revertlevels, which
101-
// should return a nil error when zero parameters are provided
90+
// TestRevertLevels tests revertlevels with various parameters
10291
func TestRevertLevels(t *testing.T) {
103-
var args []string
104-
105-
err := checkLoggingCmdParams(revertLevelsCmd(), args)
106-
107-
if err != nil {
108-
t.Fatal(err)
109-
}
110-
}
111-
112-
// TestRevertLevels_extraParameter tests the parameter checking for setlevel, which
113-
// should return an error when any amount of parameters are provided
114-
func TestRevertLevels_extraParameter(t *testing.T) {
115-
args := make([]string, 1)
116-
args[0] = "extraparameter"
117-
118-
err := checkLoggingCmdParams(revertLevelsCmd(), args)
119-
120-
if err == nil {
121-
t.FailNow()
122-
}
92+
var tc []testCase
93+
tc = append(tc,
94+
testCase{"Valid", []string{}, false},
95+
testCase{"ExtraParameter", []string{"peer"}, true},
96+
)
97+
runTests(t, "revertlevels", tc)
12398
}

peer/clilogging/revertlevels.go

+17-24
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,33 @@ import (
2020
"golang.org/x/net/context"
2121

2222
"github.com/golang/protobuf/ptypes/empty"
23-
"github.com/hyperledger/fabric/peer/common"
2423

2524
"github.com/spf13/cobra"
2625
)
2726

28-
func revertLevelsCmd() *cobra.Command {
27+
func revertLevelsCmd(cf *LoggingCmdFactory) *cobra.Command {
28+
var loggingRevertLevelsCmd = &cobra.Command{
29+
Use: "revertlevels",
30+
Short: "Reverts the logging levels to the levels at the end of peer startup.",
31+
Long: `Reverts the logging levels to the levels at the end of peer startup`,
32+
RunE: func(cmd *cobra.Command, args []string) error {
33+
return revertLevels(cf, cmd, args)
34+
},
35+
}
2936
return loggingRevertLevelsCmd
3037
}
3138

32-
var loggingRevertLevelsCmd = &cobra.Command{
33-
Use: "revertlevels",
34-
Short: "Reverts the logging levels to the levels at the end of peer startup.",
35-
Long: `Reverts the logging levels to the levels at the end of peer startup`,
36-
Run: func(cmd *cobra.Command, args []string) {
37-
revertLevels(cmd, args)
38-
},
39-
}
40-
41-
func revertLevels(cmd *cobra.Command, args []string) (err error) {
39+
func revertLevels(cf *LoggingCmdFactory, cmd *cobra.Command, args []string) (err error) {
4240
err = checkLoggingCmdParams(cmd, args)
43-
44-
if err != nil {
45-
logger.Warningf("Error: %s", err)
46-
} else {
47-
adminClient, err := common.GetAdminClient()
48-
if err != nil {
49-
logger.Warningf("%s", err)
50-
return err
41+
if err == nil {
42+
if cf == nil {
43+
cf, err = InitCmdFactory()
44+
if err != nil {
45+
return err
46+
}
5147
}
52-
53-
_, err = adminClient.RevertLogLevels(context.Background(), &empty.Empty{})
54-
48+
_, err = cf.AdminClient.RevertLogLevels(context.Background(), &empty.Empty{})
5549
if err != nil {
56-
logger.Warningf("%s", err)
5750
return err
5851
}
5952
logger.Info("Log levels reverted to the levels at the end of peer startup.")

peer/clilogging/setlevel.go

+17-24
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,34 @@ package clilogging
1919
import (
2020
"golang.org/x/net/context"
2121

22-
"github.com/hyperledger/fabric/peer/common"
2322
pb "github.com/hyperledger/fabric/protos/peer"
2423

2524
"github.com/spf13/cobra"
2625
)
2726

28-
func setLevelCmd() *cobra.Command {
27+
func setLevelCmd(cf *LoggingCmdFactory) *cobra.Command {
28+
var loggingSetLevelCmd = &cobra.Command{
29+
Use: "setlevel <module> <log level>",
30+
Short: "Sets the logging level of the requested module logger.",
31+
Long: `Sets the logging level of the requested module logger`,
32+
RunE: func(cmd *cobra.Command, args []string) error {
33+
return setLevel(cf, cmd, args)
34+
},
35+
}
2936
return loggingSetLevelCmd
3037
}
3138

32-
var loggingSetLevelCmd = &cobra.Command{
33-
Use: "setlevel <module> <log level>",
34-
Short: "Sets the logging level of the requested module logger.",
35-
Long: `Sets the logging level of the requested module logger`,
36-
Run: func(cmd *cobra.Command, args []string) {
37-
setLevel(cmd, args)
38-
},
39-
}
40-
41-
func setLevel(cmd *cobra.Command, args []string) (err error) {
39+
func setLevel(cf *LoggingCmdFactory, cmd *cobra.Command, args []string) (err error) {
4240
err = checkLoggingCmdParams(cmd, args)
43-
44-
if err != nil {
45-
logger.Warningf("Error: %s", err)
46-
} else {
47-
adminClient, err := common.GetAdminClient()
48-
if err != nil {
49-
logger.Warningf("%s", err)
50-
return err
41+
if err == nil {
42+
if cf == nil {
43+
cf, err = InitCmdFactory()
44+
if err != nil {
45+
return err
46+
}
5147
}
52-
53-
logResponse, err := adminClient.SetModuleLogLevel(context.Background(), &pb.LogLevelRequest{LogModule: args[0], LogLevel: args[1]})
54-
48+
logResponse, err := cf.AdminClient.SetModuleLogLevel(context.Background(), &pb.LogLevelRequest{LogModule: args[0], LogLevel: args[1]})
5549
if err != nil {
56-
logger.Warningf("%s", err)
5750
return err
5851
}
5952
logger.Infof("Log level set for peer module '%s': %s", logResponse.LogModule, logResponse.LogLevel)

0 commit comments

Comments
 (0)