github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/core/ledger/testutil/test_util.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 testutil 18 19 import ( 20 "flag" 21 "fmt" 22 mathRand "math/rand" 23 "regexp" 24 "strings" 25 "time" 26 27 "github.com/hyperledger/fabric/core/config" 28 "github.com/op/go-logging" 29 "github.com/spf13/viper" 30 ) 31 32 // TestRandomNumberGenerator a random number generator for testing 33 type TestRandomNumberGenerator struct { 34 rand *mathRand.Rand 35 maxNumber int 36 } 37 38 // NewTestRandomNumberGenerator constructs a new `TestRandomNumberGenerator` 39 func NewTestRandomNumberGenerator(maxNumber int) *TestRandomNumberGenerator { 40 return &TestRandomNumberGenerator{ 41 mathRand.New(mathRand.NewSource(time.Now().UnixNano())), 42 maxNumber, 43 } 44 } 45 46 // Next generates next random number 47 func (randNumGenerator *TestRandomNumberGenerator) Next() int { 48 return randNumGenerator.rand.Intn(randNumGenerator.maxNumber) 49 } 50 51 // SetupTestConfig sets up configurations for tetsing 52 func SetupTestConfig() { 53 viper.AddConfigPath(".") 54 viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) 55 viper.AutomaticEnv() 56 viper.SetDefault("peer.ledger.test.loadYAML", true) 57 loadYAML := viper.GetBool("peer.ledger.test.loadYAML") 58 if loadYAML { 59 viper.SetConfigName("test") 60 err := viper.ReadInConfig() 61 if err != nil { // Handle errors reading the config file 62 panic(fmt.Errorf("Fatal error config file: %s \n", err)) 63 } 64 } 65 var formatter = logging.MustStringFormatter( 66 `%{color}%{time:15:04:05.000} [%{module}] %{shortfunc} [%{shortfile}] -> %{level:.4s} %{id:03x}%{color:reset} %{message}`, 67 ) 68 logging.SetFormatter(formatter) 69 } 70 71 // SetupCoreYAMLConfig sets up configurations for testing 72 func SetupCoreYAMLConfig() { 73 viper.SetConfigName("core") 74 viper.SetEnvPrefix("CORE") 75 viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) 76 viper.AutomaticEnv() 77 78 err := config.AddDevConfigPath(nil) 79 if err != nil { 80 panic(fmt.Errorf("Fatal error adding dev dir: %s \n", err)) 81 } 82 83 err = viper.ReadInConfig() 84 if err != nil { // Handle errors reading the config file 85 panic(fmt.Errorf("Fatal error config file: %s \n", err)) 86 } 87 } 88 89 // ResetConfigToDefaultValues resets configurations optins back to defaults 90 func ResetConfigToDefaultValues() { 91 //reset to defaults 92 viper.Set("ledger.state.stateDatabase", "goleveldb") 93 viper.Set("ledger.history.enableHistoryDatabase", false) 94 } 95 96 // SetLogLevel sets up log level 97 func SetLogLevel(level logging.Level, module string) { 98 logging.SetLevel(level, module) 99 } 100 101 // ParseTestParams parses tests params 102 func ParseTestParams() []string { 103 testParams := flag.String("testParams", "", "Test specific parameters") 104 flag.Parse() 105 regex, err := regexp.Compile(",(\\s+)?") 106 if err != nil { 107 panic(fmt.Errorf("err = %s\n", err)) 108 } 109 paramsArray := regex.Split(*testParams, -1) 110 return paramsArray 111 }