github.com/kaituanwang/hyperledger@v2.0.1+incompatible/orderer/common/localconfig/config_test.go (about) 1 // Copyright IBM Corp. All Rights Reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package localconfig 5 6 import ( 7 "fmt" 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "testing" 12 "time" 13 14 "github.com/hyperledger/fabric/core/config/configtest" 15 "github.com/mitchellh/mapstructure" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestLoadGoodConfig(t *testing.T) { 20 cleanup := configtest.SetDevFabricConfigPath(t) 21 defer cleanup() 22 cfg, err := Load() 23 assert.NotNil(t, cfg, "Could not load config") 24 assert.Nil(t, err, "Load good config returned unexpected error") 25 } 26 27 func TestMissingConfigValueOverridden(t *testing.T) { 28 t.Run("when the value is missing and not overridden", func(t *testing.T) { 29 cleanup := configtest.SetDevFabricConfigPath(t) 30 defer cleanup() 31 cfg, err := Load() 32 assert.NotNil(t, cfg, "Could not load config") 33 assert.NoError(t, err, "Load good config returned unexpected error") 34 assert.Nil(t, cfg.Kafka.TLS.ClientRootCAs) 35 }) 36 37 t.Run("when the value is missing and is overridden", func(t *testing.T) { 38 os.Setenv("ORDERER_KAFKA_TLS_CLIENTROOTCAS", "msp/tlscacerts/tlsroot.pem") 39 cleanup := configtest.SetDevFabricConfigPath(t) 40 defer cleanup() 41 cfg, err := Load() 42 assert.NotNil(t, cfg, "Could not load config") 43 assert.NoError(t, err, "Load good config returned unexpected error") 44 assert.NotNil(t, cfg.Kafka.TLS.ClientRootCAs) 45 }) 46 } 47 48 func TestLoadMissingConfigFile(t *testing.T) { 49 envVar1 := "FABRIC_CFG_PATH" 50 envVal1 := "invalid fabric cfg path" 51 os.Setenv(envVar1, envVal1) 52 defer os.Unsetenv(envVar1) 53 54 cfg, err := Load() 55 assert.Nil(t, cfg, "Loaded missing config file") 56 assert.NotNil(t, err, "Loaded missing config file without error") 57 } 58 59 func TestLoadMalformedConfigFile(t *testing.T) { 60 name, err := ioutil.TempDir("", "hyperledger_fabric") 61 assert.Nil(t, err, "Error creating temp dir: %s", err) 62 defer func() { 63 err = os.RemoveAll(name) 64 assert.Nil(t, os.RemoveAll(name), "Error removing temp dir: %s", err) 65 }() 66 67 { 68 // Create a malformed orderer.yaml file in temp dir 69 f, err := os.OpenFile(filepath.Join(name, "orderer.yaml"), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) 70 assert.Nil(t, err, "Error creating file: %s", err) 71 f.WriteString("General: 42") 72 assert.NoError(t, f.Close(), "Error closing file") 73 } 74 75 envVar1 := "FABRIC_CFG_PATH" 76 envVal1 := name 77 os.Setenv(envVar1, envVal1) 78 defer os.Unsetenv(envVar1) 79 80 cfg, err := Load() 81 assert.Nil(t, cfg, "Loaded missing config file") 82 assert.NotNil(t, err, "Loaded missing config file without error") 83 } 84 85 // TestEnvInnerVar verifies that with the Unmarshal function that 86 // the environmental overrides still work on internal vars. This was 87 // a bug in the original viper implementation that is worked around in 88 // the Load codepath for now 89 func TestEnvInnerVar(t *testing.T) { 90 envVar1 := "ORDERER_GENERAL_LISTENPORT" 91 envVal1 := uint16(80) 92 envVar2 := "ORDERER_KAFKA_RETRY_SHORTINTERVAL" 93 envVal2 := "42s" 94 os.Setenv(envVar1, fmt.Sprintf("%d", envVal1)) 95 os.Setenv(envVar2, envVal2) 96 defer os.Unsetenv(envVar1) 97 defer os.Unsetenv(envVar2) 98 cleanup := configtest.SetDevFabricConfigPath(t) 99 defer cleanup() 100 config, _ := Load() 101 102 assert.NotNil(t, config, "Could not load config") 103 assert.Equal(t, config.General.ListenPort, envVal1, "Environmental override of inner config test 1 did not work") 104 105 v2, _ := time.ParseDuration(envVal2) 106 assert.Equal(t, config.Kafka.Retry.ShortInterval, v2, "Environmental override of inner config test 2 did not work") 107 } 108 109 func TestKafkaTLSConfig(t *testing.T) { 110 testCases := []struct { 111 name string 112 tls TLS 113 shouldPanic bool 114 }{ 115 {"Disabled", TLS{Enabled: false}, false}, 116 {"EnabledNoPrivateKey", TLS{Enabled: true, Certificate: "public.key"}, true}, 117 {"EnabledNoPublicKey", TLS{Enabled: true, PrivateKey: "private.key"}, true}, 118 {"EnabledNoTrustedRoots", TLS{Enabled: true, PrivateKey: "private.key", Certificate: "public.key"}, true}, 119 } 120 for _, tc := range testCases { 121 t.Run(tc.name, func(t *testing.T) { 122 uconf := &TopLevel{Kafka: Kafka{TLS: tc.tls}} 123 if tc.shouldPanic { 124 assert.Panics(t, func() { uconf.completeInitialization("/dummy/path") }, "Should panic") 125 } else { 126 assert.NotPanics(t, func() { uconf.completeInitialization("/dummy/path") }, "Should not panic") 127 } 128 }) 129 } 130 } 131 132 func TestKafkaSASLPlain(t *testing.T) { 133 testCases := []struct { 134 name string 135 sasl SASLPlain 136 shouldPanic bool 137 }{ 138 {"Disabled", SASLPlain{Enabled: false}, false}, 139 {"EnabledUserPassword", SASLPlain{Enabled: true, User: "user", Password: "pwd"}, false}, 140 {"EnabledNoUserPassword", SASLPlain{Enabled: true}, true}, 141 {"EnabledNoUser", SASLPlain{Enabled: true, Password: "pwd"}, true}, 142 {"EnabledNoPassword", SASLPlain{Enabled: true, User: "user"}, true}, 143 } 144 for _, tc := range testCases { 145 t.Run(tc.name, func(t *testing.T) { 146 uconf := &TopLevel{Kafka: Kafka{SASLPlain: tc.sasl}} 147 if tc.shouldPanic { 148 assert.Panics(t, func() { uconf.completeInitialization("/dummy/path") }, "Should panic") 149 } else { 150 assert.NotPanics(t, func() { uconf.completeInitialization("/dummy/path") }, "Should not panic") 151 } 152 }) 153 } 154 } 155 156 func TestClusterDefaults(t *testing.T) { 157 cleanup := configtest.SetDevFabricConfigPath(t) 158 defer cleanup() 159 cfg, err := Load() 160 161 assert.NoError(t, err) 162 assert.Equal(t, cfg.General.Cluster.ReplicationMaxRetries, Defaults.General.Cluster.ReplicationMaxRetries) 163 } 164 165 func TestConsensusConfig(t *testing.T) { 166 name, err := ioutil.TempDir("", "hyperledger_fabric") 167 assert.Nil(t, err, "Error creating temp dir: %s", err) 168 defer func() { 169 err = os.RemoveAll(name) 170 assert.Nil(t, os.RemoveAll(name), "Error removing temp dir: %s", err) 171 }() 172 173 content := `--- 174 Consensus: 175 Foo: bar 176 Hello: 177 World: 42 178 ` 179 180 f, err := os.OpenFile(filepath.Join(name, "orderer.yaml"), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) 181 assert.Nil(t, err, "Error creating file: %s", err) 182 f.WriteString(content) 183 assert.NoError(t, f.Close(), "Error closing file") 184 185 envVar1 := "FABRIC_CFG_PATH" 186 envVal1 := name 187 os.Setenv(envVar1, envVal1) 188 defer os.Unsetenv(envVar1) 189 190 conf, err := Load() 191 assert.NoError(t, err, "Load good config returned unexpected error") 192 assert.NotNil(t, conf, "Could not load config") 193 194 consensus := conf.Consensus 195 assert.IsType(t, map[string]interface{}{}, consensus, "Expected Consensus to be of type map[string]interface{}") 196 197 foo := &struct { 198 Foo string 199 Hello struct { 200 World int 201 } 202 }{} 203 err = mapstructure.Decode(consensus, foo) 204 assert.NoError(t, err, "Failed to decode Consensus to struct") 205 assert.Equal(t, foo.Foo, "bar") 206 assert.Equal(t, foo.Hello.World, 42) 207 } 208 209 func TestConnectionTimeout(t *testing.T) { 210 t.Run("without connection timeout overridden", func(t *testing.T) { 211 cleanup := configtest.SetDevFabricConfigPath(t) 212 defer cleanup() 213 cfg, err := Load() 214 assert.NotNil(t, cfg, "Could not load config") 215 assert.NoError(t, err, "Load good config returned unexpected error") 216 assert.Equal(t, cfg.General.ConnectionTimeout, 0*time.Second) 217 }) 218 219 t.Run("with connection timeout overridden", func(t *testing.T) { 220 os.Setenv("ORDERER_GENERAL_CONNECTIONTIMEOUT", "10s") 221 defer os.Unsetenv("ORDERER_GENERAL_CONNECTIONTIMEOUT") 222 cleanup := configtest.SetDevFabricConfigPath(t) 223 defer cleanup() 224 cfg, err := Load() 225 assert.NotNil(t, cfg, "Could not load config") 226 assert.NoError(t, err, "Load good config returned unexpected error") 227 assert.Equal(t, cfg.General.ConnectionTimeout, 10*time.Second) 228 }) 229 }