github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/core/config/config_test.go (about) 1 /* 2 * Copyright IBM Corp All Rights Reserved 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 */ 7 8 package config 9 10 import ( 11 "io/ioutil" 12 "os" 13 "testing" 14 "time" 15 16 "github.com/spf13/viper" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 func TestConfig_dirExists(t *testing.T) { 21 tmpF := os.TempDir() 22 exists := dirExists(tmpF) 23 assert.True(t, exists, 24 "%s directory exists but dirExists returned false", tmpF) 25 26 tmpF = "/blah-" + time.Now().Format(time.RFC3339Nano) 27 exists = dirExists(tmpF) 28 assert.False(t, exists, 29 "%s directory does not exist but dirExists returned true", 30 tmpF) 31 } 32 33 func TestConfig_AddDevConfigPath(t *testing.T) { 34 // Case 1: use viper instance to call AddDevConfigPath 35 v := viper.New() 36 err := AddDevConfigPath(v) 37 assert.NoError(t, err, "Error while adding dev config path to viper") 38 39 // Case 2: default viper instance to call AddDevConfigPath 40 err = AddDevConfigPath(nil) 41 assert.NoError(t, err, "Error while adding dev config path to default viper") 42 43 // Error case: GOPATH is empty 44 gopath := os.Getenv("GOPATH") 45 os.Setenv("GOPATH", "") 46 defer os.Setenv("GOPATH", gopath) 47 err = AddDevConfigPath(v) 48 assert.Error(t, err, "GOPATH is empty, expected error from AddDevConfigPath") 49 } 50 51 func TestConfig_InitViper(t *testing.T) { 52 // Case 1: use viper instance to call InitViper 53 v := viper.New() 54 err := InitViper(v, "") 55 assert.NoError(t, err, "Error returned by InitViper") 56 57 // Case 2: default viper instance to call InitViper 58 err = InitViper(nil, "") 59 assert.NoError(t, err, "Error returned by InitViper") 60 } 61 62 func TestConfig_GetPath(t *testing.T) { 63 // Case 1: non existent viper property 64 path := GetPath("foo") 65 assert.Equal(t, "", path, "GetPath should have returned empty string for path 'foo'") 66 67 // Case 2: viper property that has absolute path 68 viper.Set("testpath", "/test/config.yml") 69 path = GetPath("testpath") 70 assert.Equal(t, "/test/config.yml", path) 71 } 72 73 func TestConfig_TranslatePathInPlace(t *testing.T) { 74 // Case 1: relative path 75 p := "foo" 76 TranslatePathInPlace(OfficialPath, &p) 77 assert.NotEqual(t, "foo", p, "TranslatePathInPlace failed to translate path %s", p) 78 79 // Case 2: absolute path 80 p = "/foo" 81 TranslatePathInPlace(OfficialPath, &p) 82 assert.Equal(t, "/foo", p, "TranslatePathInPlace failed to translate path %s", p) 83 } 84 85 func TestConfig_GetDevMspDir(t *testing.T) { 86 // Success case 87 _, err := GetDevMspDir() 88 assert.NoError(t, err) 89 90 // Error case: GOPATH is empty 91 gopath := os.Getenv("GOPATH") 92 os.Setenv("GOPATH", "") 93 defer os.Setenv("GOPATH", gopath) 94 _, err = GetDevMspDir() 95 assert.Error(t, err, "GOPATH is empty, expected error from GetDevMspDir") 96 97 // Error case: GOPATH is set to temp dir 98 dir, err1 := ioutil.TempDir("/tmp", "devmspdir") 99 assert.NoError(t, err1) 100 defer os.RemoveAll(dir) 101 os.Setenv("GOPATH", dir) 102 _, err = GetDevMspDir() 103 assert.Error(t, err, "GOPATH is set to temp dir, expected error from GetDevMspDir") 104 } 105 106 func TestConfig_GetDevConfigDir(t *testing.T) { 107 gopath := os.Getenv("GOPATH") 108 os.Setenv("GOPATH", "") 109 defer os.Setenv("GOPATH", gopath) 110 _, err := GetDevConfigDir() 111 assert.Error(t, err, "GOPATH is empty, expected error from GetDevConfigDir") 112 }