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