github.com/Hnampk/fabric@v2.1.1+incompatible/cmd/common/config_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 "fmt" 11 "math/rand" 12 "os" 13 "path/filepath" 14 "testing" 15 "time" 16 17 "github.com/hyperledger/fabric/cmd/common/comm" 18 "github.com/hyperledger/fabric/cmd/common/signer" 19 "github.com/stretchr/testify/assert" 20 ) 21 22 func TestConfig(t *testing.T) { 23 rand.Seed(time.Now().UnixNano()) 24 configFilePath := filepath.Join(os.TempDir(), fmt.Sprintf("config-%d.yaml", rand.Int())) 25 fmt.Println(configFilePath) 26 t.Run("save and load a config", func(t *testing.T) { 27 c := Config{ 28 TLSConfig: comm.Config{ 29 CertPath: "foo", 30 KeyPath: "foo", 31 PeerCACertPath: "foo", 32 Timeout: time.Second * 3, 33 }, 34 SignerConfig: signer.Config{ 35 KeyPath: "foo", 36 IdentityPath: "foo", 37 MSPID: "foo", 38 }, 39 } 40 41 err := c.ToFile(configFilePath) 42 defer os.RemoveAll(configFilePath) 43 assert.NoError(t, err) 44 45 c2, err := ConfigFromFile(configFilePath) 46 assert.NoError(t, err) 47 assert.Equal(t, c, c2) 48 }) 49 50 t.Run("bad config isn't saved", func(t *testing.T) { 51 c := Config{} 52 err := c.ToFile(configFilePath) 53 assert.Contains(t, err.Error(), "config isn't valid") 54 }) 55 56 t.Run("bad config isn't loaded", func(t *testing.T) { 57 _, err := ConfigFromFile(filepath.Join("testdata", "not_a_yaml.yaml")) 58 assert.Contains(t, err.Error(), "error unmarshaling YAML file") 59 }) 60 61 t.Run("file that doesn't exist isn't loaded", func(t *testing.T) { 62 _, err := ConfigFromFile(filepath.Join("testdata", "not_a_file.yaml")) 63 assert.Contains(t, err.Error(), "no such file or directory") 64 }) 65 }