github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/orderer/common/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 "io/ioutil" 22 "os" 23 "path/filepath" 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestGoodConfig(t *testing.T) { 31 assert.NotNil(t, Load(), "Could not load config") 32 } 33 34 func TestMissingConfigFile(t *testing.T) { 35 envVar1 := "FABRIC_CFG_PATH" 36 envVal1 := "invalid fabric cfg path" 37 os.Setenv(envVar1, envVal1) 38 defer os.Unsetenv(envVar1) 39 40 assert.Panics(t, func() { Load() }, "Should panic") 41 } 42 43 func TestMalformedConfigFile(t *testing.T) { 44 name, err := ioutil.TempDir("", "hyperledger_fabric") 45 assert.Nil(t, err, "Error creating temp dir: %s", err) 46 defer func() { 47 err = os.RemoveAll(name) 48 assert.Nil(t, os.RemoveAll(name), "Error removing temp dir: %s", err) 49 }() 50 51 { 52 // Create a malformed orderer.yaml file in temp dir 53 f, err := os.OpenFile(filepath.Join(name, "orderer.yaml"), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) 54 assert.Nil(t, err, "Error creating file: %s", err) 55 f.WriteString("General: 42") 56 assert.NoError(t, f.Close(), "Error closing file") 57 } 58 59 envVar1 := "FABRIC_CFG_PATH" 60 envVal1 := name 61 os.Setenv(envVar1, envVal1) 62 defer os.Unsetenv(envVar1) 63 64 assert.Panics(t, func() { Load() }, "Should panic") 65 } 66 67 // TestEnvInnerVar verifies that with the Unmarshal function that 68 // the environmental overrides still work on internal vars. This was 69 // a bug in the original viper implementation that is worked around in 70 // the Load codepath for now 71 func TestEnvInnerVar(t *testing.T) { 72 envVar1 := "ORDERER_GENERAL_LISTENPORT" 73 envVal1 := uint16(80) 74 envVar2 := "ORDERER_KAFKA_RETRY_SHORTINTERVAL" 75 envVal2 := "42s" 76 os.Setenv(envVar1, fmt.Sprintf("%d", envVal1)) 77 os.Setenv(envVar2, envVal2) 78 defer os.Unsetenv(envVar1) 79 defer os.Unsetenv(envVar2) 80 config := Load() 81 82 assert.NotNil(t, config, "Could not load config") 83 assert.Equal(t, config.General.ListenPort, envVal1, "Environmental override of inner config test 1 did not work") 84 85 v2, _ := time.ParseDuration(envVal2) 86 assert.Equal(t, config.Kafka.Retry.ShortInterval, v2, "Environmental override of inner config test 2 did not work") 87 } 88 89 const DummyPath = "/dummy/path" 90 91 func TestKafkaTLSConfig(t *testing.T) { 92 testCases := []struct { 93 name string 94 tls TLS 95 shouldPanic bool 96 }{ 97 {"Disabled", TLS{Enabled: false}, false}, 98 {"EnabledNoPrivateKey", TLS{Enabled: true, Certificate: "public.key"}, true}, 99 {"EnabledNoPublicKey", TLS{Enabled: true, PrivateKey: "private.key"}, true}, 100 {"EnabledNoTrustedRoots", TLS{Enabled: true, PrivateKey: "private.key", Certificate: "public.key"}, true}, 101 } 102 for _, tc := range testCases { 103 t.Run(tc.name, func(t *testing.T) { 104 uconf := &TopLevel{Kafka: Kafka{TLS: tc.tls}} 105 if tc.shouldPanic { 106 assert.Panics(t, func() { uconf.completeInitialization(DummyPath) }, "should panic") 107 } else { 108 assert.NotPanics(t, func() { uconf.completeInitialization(DummyPath) }, "should not panic") 109 } 110 }) 111 } 112 } 113 114 func TestProfileConfig(t *testing.T) { 115 uconf := &TopLevel{General: General{Profile: Profile{Enabled: true}}} 116 uconf.completeInitialization(DummyPath) 117 assert.Equal(t, defaults.General.Profile.Address, uconf.General.Profile.Address, "Expected profile address to be filled with default value") 118 }