github.com/xiaqingdoc/fabric@v2.1.1+incompatible/cmd/common/cli_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package common
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"math/rand"
    13  	"os"
    14  	"path/filepath"
    15  	"testing"
    16  
    17  	"github.com/hyperledger/fabric/cmd/common/signer"
    18  	"github.com/pkg/errors"
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  func TestCLI(t *testing.T) {
    23  	var testCmdInvoked bool
    24  	var exited bool
    25  	// Overwrite exit
    26  	terminate = func(_ int) {
    27  		exited = true
    28  	}
    29  	// Overwrite stdout with writing to this buffer
    30  	testBuff := &bytes.Buffer{}
    31  	outWriter = testBuff
    32  
    33  	var returnValue error
    34  	cli := NewCLI("cli", "cli help")
    35  	testCommand := func(conf Config) error {
    36  		// If we exited, the command wasn't executed
    37  		if exited {
    38  			return nil
    39  		}
    40  		// Else, the command was executed - so ensure it was executed
    41  		// with the expected config
    42  		assert.Equal(t, Config{
    43  			SignerConfig: signer.Config{
    44  				MSPID:        "SampleOrg",
    45  				KeyPath:      "key.pem",
    46  				IdentityPath: "cert.pem",
    47  			},
    48  		}, conf)
    49  		testCmdInvoked = true
    50  		return returnValue
    51  	}
    52  	cli.Command("test", "test help", testCommand)
    53  
    54  	t.Run("Loading a non existent config", func(t *testing.T) {
    55  		defer testBuff.Reset()
    56  		// Overwrite user home directory with testdata
    57  		dir := filepath.Join("testdata", "non_existent_config")
    58  		cli.Run([]string{"test", "--configFile", filepath.Join(dir, "config.yaml")})
    59  		assert.Contains(t, testBuff.String(), fmt.Sprint("Failed loading config open ", dir))
    60  		assert.Contains(t, testBuff.String(), "config.yaml: no such file or directory")
    61  		assert.True(t, exited)
    62  	})
    63  
    64  	t.Run("Loading a valid config and the command succeeds", func(t *testing.T) {
    65  		defer testBuff.Reset()
    66  		testCmdInvoked = false
    67  		exited = false
    68  		// Overwrite user home directory with testdata
    69  		dir := filepath.Join("testdata", "valid_config")
    70  		// Ensure that a valid config results in running our command
    71  		cli.Run([]string{"test", "--configFile", filepath.Join(dir, "config.yaml")})
    72  		assert.True(t, testCmdInvoked)
    73  		assert.False(t, exited)
    74  	})
    75  
    76  	t.Run("Loading a valid config but the command fails", func(t *testing.T) {
    77  		returnValue = errors.New("something went wrong")
    78  		defer func() {
    79  			returnValue = nil
    80  		}()
    81  		defer testBuff.Reset()
    82  		testCmdInvoked = false
    83  		exited = false
    84  		// Overwrite user home directory with testdata
    85  		dir := filepath.Join("testdata", "valid_config")
    86  		// Ensure that a valid config results in running our command
    87  		cli.Run([]string{"test", "--configFile", filepath.Join(dir, "config.yaml")})
    88  		assert.True(t, testCmdInvoked)
    89  		assert.True(t, exited)
    90  		assert.Contains(t, testBuff.String(), "something went wrong")
    91  	})
    92  
    93  	t.Run("Saving a config", func(t *testing.T) {
    94  		defer testBuff.Reset()
    95  		testCmdInvoked = false
    96  		exited = false
    97  		dir := filepath.Join(os.TempDir(), fmt.Sprintf("config%d", rand.Int()))
    98  		os.Mkdir(dir, 0700)
    99  		defer os.RemoveAll(dir)
   100  
   101  		userCert := filepath.Join(dir, "cert.pem")
   102  		userKey := filepath.Join(dir, "key.pem")
   103  		userCertFlag := fmt.Sprintf("--userCert=%s", userCert)
   104  		userKeyFlag := fmt.Sprintf("--userKey=%s", userKey)
   105  		os.Create(userCert)
   106  		os.Create(userKey)
   107  
   108  		cli.Run([]string{saveConfigCommand, "--MSP=SampleOrg", userCertFlag, userKeyFlag})
   109  		assert.Contains(t, testBuff.String(), "--configFile must be used to specify the configuration file")
   110  		testBuff.Reset()
   111  		// Persist the config
   112  		cli.Run([]string{saveConfigCommand, "--MSP=SampleOrg", userCertFlag, userKeyFlag, "--configFile", filepath.Join(dir, "config.yaml")})
   113  
   114  		// Run a different command and ensure the config was successfully persisted
   115  		cli.Command("assert", "", func(conf Config) error {
   116  			assert.Equal(t, Config{
   117  				SignerConfig: signer.Config{
   118  					MSPID:        "SampleOrg",
   119  					KeyPath:      userKey,
   120  					IdentityPath: userCert,
   121  				},
   122  			}, conf)
   123  			return nil
   124  		})
   125  		cli.Run([]string{"assert", "--configFile", filepath.Join(dir, "config.yaml")})
   126  	})
   127  }