github.com/lzy4123/fabric@v2.1.1+incompatible/internal/peer/chaincode/flags_test.go (about) 1 /* 2 Copyright IBM Corp. 2016-2017 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package chaincode 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric/bccsp/sw" 13 "github.com/hyperledger/fabric/internal/peer/common" 14 "github.com/spf13/cobra" 15 "github.com/spf13/viper" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestOrdererFlags(t *testing.T) { 20 var ( 21 ca = "root.crt" 22 key = "client.key" 23 cert = "client.crt" 24 endpoint = "orderer.example.com:7050" 25 sn = "override.example.com" 26 ) 27 28 testCmd := &cobra.Command{ 29 Use: "test", 30 Run: func(cmd *cobra.Command, args []string) { 31 t.Logf("rootcert: %s", viper.GetString("orderer.tls.rootcert.file")) 32 assert.Equal(t, ca, viper.GetString("orderer.tls.rootcert.file")) 33 assert.Equal(t, key, viper.GetString("orderer.tls.clientKey.file")) 34 assert.Equal(t, cert, viper.GetString("orderer.tls.clientCert.file")) 35 assert.Equal(t, endpoint, viper.GetString("orderer.address")) 36 assert.Equal(t, sn, viper.GetString("orderer.tls.serverhostoverride")) 37 assert.Equal(t, true, viper.GetBool("orderer.tls.enabled")) 38 assert.Equal(t, true, viper.GetBool("orderer.tls.clientAuthRequired")) 39 }, 40 PersistentPreRun: func(cmd *cobra.Command, args []string) { 41 common.SetOrdererEnv(cmd, args) 42 }, 43 } 44 45 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 46 assert.NoError(t, err) 47 runCmd := Cmd(nil, cryptoProvider) 48 49 runCmd.AddCommand(testCmd) 50 51 runCmd.SetArgs([]string{"test", "--cafile", ca, "--keyfile", key, 52 "--certfile", cert, "--orderer", endpoint, "--tls", "--clientauth", 53 "--ordererTLSHostnameOverride", sn}) 54 err = runCmd.Execute() 55 assert.NoError(t, err) 56 57 // check env one more time 58 t.Logf("address: %s", viper.GetString("orderer.address")) 59 assert.Equal(t, ca, viper.GetString("orderer.tls.rootcert.file")) 60 assert.Equal(t, key, viper.GetString("orderer.tls.clientKey.file")) 61 assert.Equal(t, cert, viper.GetString("orderer.tls.clientCert.file")) 62 assert.Equal(t, endpoint, viper.GetString("orderer.address")) 63 assert.Equal(t, sn, viper.GetString("orderer.tls.serverhostoverride")) 64 assert.Equal(t, true, viper.GetBool("orderer.tls.enabled")) 65 assert.Equal(t, true, viper.GetBool("orderer.tls.clientAuthRequired")) 66 }