gitee.com/zhaochuninhefei/fabric-ca-gm@v0.0.2/cmd/fabric-ca-client/command/getcainfo_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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 command
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"gitee.com/zhaochuninhefei/fabric-ca-gm/cmd/fabric-ca-client/command/mocks"
    24  	"gitee.com/zhaochuninhefei/fabric-ca-gm/lib"
    25  	"github.com/pkg/errors"
    26  	"github.com/spf13/viper"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestNewGetCACertCmd(t *testing.T) {
    31  	cmd := new(mocks.Command)
    32  	cmd.On("GetViper").Return(viper.New())
    33  	getcacertCmd := newGetCAInfoCmd(cmd)
    34  	assert.NotNil(t, getcacertCmd)
    35  }
    36  
    37  func TestGetCommand(t *testing.T) {
    38  	cmd := new(mocks.Command)
    39  	cmd.On("GetViper").Return(viper.New())
    40  	getcacertCmd := newGetCAInfoCmd(cmd)
    41  	cobraCmd := getcacertCmd.getCommand()
    42  	assert.NotNil(t, cobraCmd)
    43  	assert.Equal(t, cobraCmd.Name(), "getcainfo")
    44  	assert.Equal(t, cobraCmd.Short, GetCAInfoCmdShortDesc)
    45  	assert.Equal(t, cobraCmd.Use, GetCAInfoCmdUsage)
    46  
    47  	f1 := reflect.ValueOf(cobraCmd.PreRunE)
    48  	f2 := reflect.ValueOf(getcacertCmd.preRunGetCACert)
    49  	assert.Equal(t, f1.Pointer(), f2.Pointer(), "PreRunE function variable of the getcacert cobra command must be preRunGetCACert function of the getCACert struct")
    50  
    51  	f1 = reflect.ValueOf(cobraCmd.RunE)
    52  	f2 = reflect.ValueOf(getcacertCmd.runGetCACert)
    53  	assert.Equal(t, f1.Pointer(), f2.Pointer(), "RunE function variable of the getcacert cobra command must be runGetCACert function of the getCACert struct")
    54  }
    55  
    56  func TestBadConfigPreRunGetCACert(t *testing.T) {
    57  	cmd := new(mocks.Command)
    58  	cmd.On("GetViper").Return(viper.New())
    59  	cmd.On("ConfigInit").Return(errors.New("Failed to initialize config"))
    60  	getcacertCmd := newGetCAInfoCmd(cmd)
    61  	cobraCmd := getcacertCmd.getCommand()
    62  	err := getcacertCmd.preRunGetCACert(cobraCmd, []string{})
    63  	assert.Error(t, err)
    64  	assert.Equal(t, err.Error(), "Failed to initialize config")
    65  }
    66  
    67  func TestGoodConfigPreRunGetCACert(t *testing.T) {
    68  	cmd := new(mocks.Command)
    69  	cmd.On("GetViper").Return(viper.New())
    70  	cmd.On("ConfigInit").Return(nil)
    71  	cmd.On("GetClientCfg").Return(&lib.ClientConfig{})
    72  	getcacertCmd := newGetCAInfoCmd(cmd)
    73  	cobraCmd := getcacertCmd.getCommand()
    74  	err := getcacertCmd.preRunGetCACert(cobraCmd, []string{})
    75  	assert.NoError(t, err)
    76  }