github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/chaincode/config_test.go (about) 1 /* 2 Copyright hechain. 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/hechain20/hechain/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 viper.Set("chaincode.system.somecc", true) 40 41 config := chaincode.GlobalConfig() 42 Expect(config.TLSEnabled).To(BeTrue()) 43 Expect(config.Keepalive).To(Equal(50 * time.Second)) 44 Expect(config.ExecuteTimeout).To(Equal(20 * time.Hour)) 45 Expect(config.InstallTimeout).To(Equal(30 * time.Minute)) 46 Expect(config.StartupTimeout).To(Equal(30 * time.Hour)) 47 Expect(config.LogFormat).To(Equal("test-chaincode-logging-format")) 48 Expect(config.LogLevel).To(Equal("warn")) 49 Expect(config.ShimLogLevel).To(Equal("warn")) 50 Expect(config.SCCAllowlist).To(Equal(map[string]bool{"somecc": true})) 51 }) 52 53 Context("when an invalid keepalive is configured", func() { 54 BeforeEach(func() { 55 viper.Set("chaincode.keepalive", "abc") 56 }) 57 58 It("falls back to no keepalive", func() { 59 config := chaincode.GlobalConfig() 60 Expect(config.Keepalive).To(Equal(time.Duration(0))) 61 }) 62 }) 63 64 Context("when the execute timeout is less than the minimum", func() { 65 BeforeEach(func() { 66 viper.Set("chaincode.executetimeout", "15") 67 }) 68 69 It("falls back to the minimum start timeout", func() { 70 config := chaincode.GlobalConfig() 71 Expect(config.ExecuteTimeout).To(Equal(30 * time.Second)) 72 }) 73 }) 74 75 Context("when the startup timeout is less than the minimum", func() { 76 BeforeEach(func() { 77 viper.Set("chaincode.startuptimeout", "15") 78 }) 79 80 It("falls back to the minimum start timeout", func() { 81 config := chaincode.GlobalConfig() 82 Expect(config.StartupTimeout).To(Equal(5 * time.Second)) 83 }) 84 }) 85 86 Context("when an invalid log level is configured", func() { 87 BeforeEach(func() { 88 viper.Set("chaincode.logging.level", "foo") 89 viper.Set("chaincode.logging.shim", "bar") 90 }) 91 92 It("falls back to info", func() { 93 config := chaincode.GlobalConfig() 94 Expect(config.LogLevel).To(Equal("info")) 95 Expect(config.ShimLogLevel).To(Equal("info")) 96 }) 97 }) 98 }) 99 100 Describe("IsDevMode", func() { 101 It("returns true when iff the mode equals 'dev'", func() { 102 viper.Set("chaincode.mode", chaincode.DevModeUserRunsChaincode) 103 Expect(chaincode.IsDevMode()).To(BeTrue()) 104 viper.Set("chaincode.mode", "dev") 105 Expect(chaincode.IsDevMode()).To(BeTrue()) 106 107 viper.Set("chaincode.mode", "empty") 108 Expect(chaincode.IsDevMode()).To(BeFalse()) 109 viper.Set("chaincode.mode", "nonsense") 110 Expect(chaincode.IsDevMode()).To(BeFalse()) 111 }) 112 }) 113 }) 114 115 func capture() (restore func()) { 116 viper.SetEnvPrefix("CORE") 117 viper.AutomaticEnv() 118 config := map[string]string{ 119 "peer.tls.enabled": viper.GetString("peer.tls.enabled"), 120 "chaincode.keepalive": viper.GetString("chaincode.keepalive"), 121 "chaincode.executetimeout": viper.GetString("chaincode.executetimeout"), 122 "chaincode.startuptimeout": viper.GetString("chaincode.startuptimeout"), 123 "chaincode.logging.format": viper.GetString("chaincode.logging.format"), 124 "chaincode.logging.level": viper.GetString("chaincode.logging.level"), 125 "chaincode.logging.shim": viper.GetString("chaincode.logging.shim"), 126 } 127 128 return func() { 129 for k, val := range config { 130 viper.Set(k, val) 131 } 132 } 133 }