|
16 | 16 |
|
17 | 17 | package clilogging
|
18 | 18 |
|
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 |
31 | 31 | }
|
32 | 32 |
|
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, |
43 | 37 | }
|
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 |
55 | 47 | }
|
| 48 | + return cmd, mockCF |
56 | 49 | }
|
57 | 50 |
|
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 | + }) |
68 | 65 | }
|
69 | 66 | }
|
70 | 67 |
|
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) |
83 | 76 | }
|
84 | 77 |
|
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 |
88 | 79 | 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) |
98 | 88 | }
|
99 | 89 |
|
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 |
102 | 91 | 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) |
123 | 98 | }
|
0 commit comments