test_util.go 3.06 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package testutil

import (
	"flag"
	"fmt"
	mathRand "math/rand"
	"regexp"
	"strings"
	"time"

	"github.com/op/go-logging"
	"github.com/spf13/viper"
)

31
// TestRandomNumberGenerator a random number generator for testing
32
33
34
35
36
type TestRandomNumberGenerator struct {
	rand      *mathRand.Rand
	maxNumber int
}

37
// NewTestRandomNumberGenerator constructs a new `TestRandomNumberGenerator`
38
39
40
41
42
43
44
func NewTestRandomNumberGenerator(maxNumber int) *TestRandomNumberGenerator {
	return &TestRandomNumberGenerator{
		mathRand.New(mathRand.NewSource(time.Now().UnixNano())),
		maxNumber,
	}
}

45
// Next generates next random number
46
47
48
49
func (randNumGenerator *TestRandomNumberGenerator) Next() int {
	return randNumGenerator.rand.Intn(randNumGenerator.maxNumber)
}

50
// SetupTestConfig sets up configurations for tetsing
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
func SetupTestConfig() {
	viper.AddConfigPath(".")
	viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
	viper.AutomaticEnv()
	viper.SetDefault("peer.ledger.test.loadYAML", true)
	loadYAML := viper.GetBool("peer.ledger.test.loadYAML")
	if loadYAML {
		viper.SetConfigName("test")
		err := viper.ReadInConfig()
		if err != nil { // Handle errors reading the config file
			panic(fmt.Errorf("Fatal error config file: %s \n", err))
		}
	}
	var formatter = logging.MustStringFormatter(
		`%{color}%{time:15:04:05.000} [%{module}] %{shortfunc} [%{shortfile}] -> %{level:.4s} %{id:03x}%{color:reset} %{message}`,
	)
	logging.SetFormatter(formatter)
}

70
// SetupCoreYAMLConfig sets up configurations for testing
71
72
73
74
75
76
77
78
79
80
81
82
func SetupCoreYAMLConfig(coreYamlPath string) {
	viper.SetConfigName("core")
	viper.SetEnvPrefix("CORE")
	viper.AddConfigPath(coreYamlPath)
	viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
	viper.AutomaticEnv()
	err := viper.ReadInConfig()
	if err != nil { // Handle errors reading the config file
		panic(fmt.Errorf("Fatal error config file: %s \n", err))
	}
}

83
84
85
86
87
88
89
// ResetConfigToDefaultValues resets configurations optins back to defaults
func ResetConfigToDefaultValues() {
	//reset to defaults
	viper.Set("ledger.state.stateDatabase", "goleveldb")
	viper.Set("ledger.state.historyDatabase", false)
}

90
// SetLogLevel sets up log level
91
92
93
94
func SetLogLevel(level logging.Level, module string) {
	logging.SetLevel(level, module)
}

95
// ParseTestParams parses tests params
96
97
98
99
100
101
102
103
104
105
func ParseTestParams() []string {
	testParams := flag.String("testParams", "", "Test specific parameters")
	flag.Parse()
	regex, err := regexp.Compile(",(\\s+)?")
	if err != nil {
		panic(fmt.Errorf("err = %s\n", err))
	}
	paramsArray := regex.Split(*testParams, -1)
	return paramsArray
}