github.com/kaituanwang/hyperledger@v2.0.1+incompatible/core/chaincode/config_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package chaincode_test 8 9 import ( 10 "time" 11 12 "github.com/hyperledger/fabric/core/chaincode" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 "github.com/spf13/viper" 16 ) 17 18 var _ = Describe("Config", func() { 19 var restore func() 20 21 BeforeEach(func() { 22 restore = capture() 23 }) 24 25 AfterEach(func() { 26 restore() 27 }) 28 29 Describe("GlobalConfig", func() { 30 It("captures the configuration from viper", func() { 31 viper.Set("peer.tls.enabled", "true") 32 viper.Set("chaincode.keepalive", "50") 33 viper.Set("chaincode.executetimeout", "20h") 34 viper.Set("chaincode.installTimeout", "30m") 35 viper.Set("chaincode.startuptimeout", "30h") 36 viper.Set("chaincode.logging.format", "test-chaincode-logging-format") 37 viper.Set("chaincode.logging.level", "warning") 38 viper.Set("chaincode.logging.shim", "warning") 39 40 config := chaincode.GlobalConfig() 41 Expect(config.TLSEnabled).To(BeTrue()) 42 Expect(config.Keepalive).To(Equal(50 * time.Second)) 43 Expect(config.ExecuteTimeout).To(Equal(20 * time.Hour)) 44 Expect(config.InstallTimeout).To(Equal(30 * time.Minute)) 45 Expect(config.StartupTimeout).To(Equal(30 * time.Hour)) 46 Expect(config.LogFormat).To(Equal("test-chaincode-logging-format")) 47 Expect(config.LogLevel).To(Equal("warn")) 48 Expect(config.ShimLogLevel).To(Equal("warn")) 49 }) 50 51 Context("when an invalid keepalive is configured", func() { 52 BeforeEach(func() { 53 viper.Set("chaincode.keepalive", "abc") 54 }) 55 56 It("falls back to no keepalive", func() { 57 config := chaincode.GlobalConfig() 58 Expect(config.Keepalive).To(Equal(time.Duration(0))) 59 }) 60 }) 61 62 Context("when the execute timeout is less than the minimum", func() { 63 BeforeEach(func() { 64 viper.Set("chaincode.executetimeout", "15") 65 }) 66 67 It("falls back to the minimum start timeout", func() { 68 config := chaincode.GlobalConfig() 69 Expect(config.ExecuteTimeout).To(Equal(30 * time.Second)) 70 }) 71 }) 72 73 Context("when the startup timeout is less than the minimum", func() { 74 BeforeEach(func() { 75 viper.Set("chaincode.startuptimeout", "15") 76 }) 77 78 It("falls back to the minimum start timeout", func() { 79 config := chaincode.GlobalConfig() 80 Expect(config.StartupTimeout).To(Equal(5 * time.Second)) 81 }) 82 }) 83 84 Context("when an invalid log level is configured", func() { 85 BeforeEach(func() { 86 viper.Set("chaincode.logging.level", "foo") 87 viper.Set("chaincode.logging.shim", "bar") 88 }) 89 90 It("falls back to info", func() { 91 config := chaincode.GlobalConfig() 92 Expect(config.LogLevel).To(Equal("info")) 93 Expect(config.ShimLogLevel).To(Equal("info")) 94 }) 95 }) 96 }) 97 98 Describe("IsDevMode", func() { 99 It("returns true when iff the mode equals 'dev'", func() { 100 viper.Set("chaincode.mode", chaincode.DevModeUserRunsChaincode) 101 Expect(chaincode.IsDevMode()).To(BeTrue()) 102 viper.Set("chaincode.mode", "dev") 103 Expect(chaincode.IsDevMode()).To(BeTrue()) 104 105 viper.Set("chaincode.mode", "empty") 106 Expect(chaincode.IsDevMode()).To(BeFalse()) 107 viper.Set("chaincode.mode", "nonsense") 108 Expect(chaincode.IsDevMode()).To(BeFalse()) 109 }) 110 }) 111 }) 112 113 func capture() (restore func()) { 114 viper.SetEnvPrefix("CORE") 115 viper.AutomaticEnv() 116 config := map[string]string{ 117 "peer.tls.enabled": viper.GetString("peer.tls.enabled"), 118 "chaincode.keepalive": viper.GetString("chaincode.keepalive"), 119 "chaincode.executetimeout": viper.GetString("chaincode.executetimeout"), 120 "chaincode.startuptimeout": viper.GetString("chaincode.startuptimeout"), 121 "chaincode.logging.format": viper.GetString("chaincode.logging.format"), 122 "chaincode.logging.level": viper.GetString("chaincode.logging.level"), 123 "chaincode.logging.shim": viper.GetString("chaincode.logging.shim"), 124 } 125 126 return func() { 127 for k, val := range config { 128 viper.Set(k, val) 129 } 130 } 131 }