github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/orderer/localconfig/config_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package config 18 19 import ( 20 "fmt" 21 "os" 22 "testing" 23 "time" 24 25 "github.com/hyperledger/fabric/common/viperutil" 26 cf "github.com/hyperledger/fabric/core/config" 27 28 "github.com/spf13/viper" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func TestGoodConfig(t *testing.T) { 33 config := Load() 34 if config == nil { 35 t.Fatalf("Could not load config") 36 } 37 t.Logf("%+v", config) 38 } 39 40 func TestBadConfig(t *testing.T) { 41 config := viper.New() 42 config.SetConfigName("orderer") 43 cf.AddDevConfigPath(config) 44 45 err := config.ReadInConfig() 46 if err != nil { 47 t.Fatalf("Error reading %s plugin config: %s", Prefix, err) 48 } 49 50 var uconf struct{} 51 52 err = viperutil.EnhancedExactUnmarshal(config, &uconf) 53 if err == nil { 54 t.Fatalf("Should have failed to unmarshal") 55 } 56 } 57 58 // TestEnvInnerVar verifies that with the Unmarshal function that 59 // the environmental overrides still work on internal vars. This was 60 // a bug in the original viper implementation that is worked around in 61 // the Load codepath for now 62 func TestEnvInnerVar(t *testing.T) { 63 envVar1 := "ORDERER_GENERAL_LISTENPORT" 64 envVal1 := uint16(80) 65 envVar2 := "ORDERER_KAFKA_RETRY_PERIOD" 66 envVal2 := "42s" 67 os.Setenv(envVar1, fmt.Sprintf("%d", envVal1)) 68 os.Setenv(envVar2, envVal2) 69 defer os.Unsetenv(envVar1) 70 defer os.Unsetenv(envVar2) 71 config := Load() 72 73 if config == nil { 74 t.Fatalf("Could not load config") 75 } 76 77 if config.General.ListenPort != envVal1 { 78 t.Fatalf("Environmental override of inner config test 1 did not work") 79 } 80 v2, _ := time.ParseDuration(envVal2) 81 if config.Kafka.Retry.Period != v2 { 82 t.Fatalf("Environmental override of inner config test 2 did not work") 83 } 84 } 85 86 const DummyPath = "/dummy/path" 87 88 func TestKafkaTLSConfig(t *testing.T) { 89 testCases := []struct { 90 name string 91 tls TLS 92 shouldPanic bool 93 }{ 94 {"Disabled", TLS{Enabled: false}, false}, 95 {"EnabledNoPrivateKey", TLS{Enabled: true, Certificate: "public.key"}, true}, 96 {"EnabledNoPublicKey", TLS{Enabled: true, PrivateKey: "private.key"}, true}, 97 {"EnabledNoTrustedRoots", TLS{Enabled: true, PrivateKey: "private.key", Certificate: "public.key"}, true}, 98 } 99 for _, tc := range testCases { 100 t.Run(tc.name, func(t *testing.T) { 101 uconf := &TopLevel{Kafka: Kafka{TLS: tc.tls}} 102 if tc.shouldPanic { 103 assert.Panics(t, func() { uconf.completeInitialization(DummyPath) }, "should panic") 104 } else { 105 assert.NotPanics(t, func() { uconf.completeInitialization(DummyPath) }, "should not panic") 106 } 107 }) 108 } 109 }