github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/peer/clilogging/logging_test.go (about)

     1  /*
     2   Copyright Digital Asset Holdings, LLC 2016 All Rights Reserved.
     3  
     4   Licensed under the Apache License, Version 2.0 (the "License");
     5   you may not use this file except in compliance with the License.
     6   You may obtain a copy of the License at
     7  
     8        http://www.apache.org/licenses/LICENSE-2.0
     9  
    10   Unless required by applicable law or agreed to in writing, software
    11   distributed under the License is distributed on an "AS IS" BASIS,
    12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   See the License for the specific language governing permissions and
    14   limitations under the License.
    15  */
    16  
    17  package clilogging
    18  
    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  }
    32  
    33  func initLoggingTest(command string) (*cobra.Command, *LoggingCmdFactory) {
    34  	adminClient := common.GetMockAdminClient(nil)
    35  	mockCF := &LoggingCmdFactory{
    36  		AdminClient: adminClient,
    37  	}
    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
    47  	}
    48  	return cmd, mockCF
    49  }
    50  
    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  		})
    65  	}
    66  }
    67  
    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)
    76  }
    77  
    78  // TestStLevel tests setlevel with various parameters
    79  func TestSetLevel(t *testing.T) {
    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)
    88  }
    89  
    90  // TestRevertLevels tests revertlevels with various parameters
    91  func TestRevertLevels(t *testing.T) {
    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)
    98  }