forked from 33cn/chain33
/
config.go
176 lines (129 loc) · 3.98 KB
/
config.go
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package testflow
import (
"fmt"
"reflect"
"sync"
"time"
"github.com/33cn/chain33/cmd/autotest/types"
"github.com/33cn/chain33/common/log/log15"
"github.com/BurntSushi/toml"
)
var (
fileLog = log15.New()
stdLog = log15.New()
)
//contract type
/*
bty,
token,
trade,
*/
type TestCaseFile struct {
Dapp string `toml:"dapp"`
Filename string `toml:"filename"`
}
type TestCaseConfig struct {
CliCommand string `toml:"cliCmd"`
CheckTimeout int `toml:"checkTimeout"`
TestCaseFileArr []TestCaseFile `toml:"TestCaseFile"`
}
type autoTestResult struct {
dapp string
totalCase int
failCase int
failCaseID []string
}
var (
configFile string
resultChan = make(chan *autoTestResult, 1)
testResultArr = make([]*autoTestResult, 0)
autoTestConfig = &TestCaseConfig{}
checkSleepTime = 1 //second, s
)
type TestRunner interface {
RunTest(tomlFile string, wg *sync.WaitGroup)
}
func InitFlowConfig(conf string, log string) {
fileLog.SetHandler(log15.Must.FileHandler(log, types.AutoTestLogFormat()))
configFile = conf
}
func StartAutoTest() bool {
stdLog.Info("[================================BeginAutoTest===============================]")
fileLog.Info("[================================BeginAutoTest===============================]")
var wg sync.WaitGroup
if _, err := toml.DecodeFile(configFile, &autoTestConfig); err != nil {
stdLog.Error("TomlDecodeAutoTestConfig", "Filename", configFile, "Err", err.Error())
return false
}
if len(autoTestConfig.CliCommand) == 0 {
stdLog.Error("NullChain33Cli")
return false
}
//init types
types.Init(autoTestConfig.CliCommand, autoTestConfig.CheckTimeout/checkSleepTime)
for _, caseFile := range autoTestConfig.TestCaseFileArr {
wg.Add(1)
go newTestFlow(caseFile.Dapp, caseFile.Filename, &wg)
}
//collect test results
go func() {
for {
res, more := <-resultChan
if more {
testResultArr = append(testResultArr, res)
} else {
return
}
}
}()
wg.Wait()
close(resultChan)
time.Sleep(1 * time.Second)
//log with test result
bSuccess := true
fmt.Println("==================================AutoTestResultSummary======================================")
fileLog.Info("====================================AutoTestResultSummary========================================")
for _, res := range testResultArr {
if res.failCase > 0 {
bSuccess = false
stdLog.Error("TestFailed", "dapp", res.dapp, "TotalCase", res.totalCase, "TotalFail", res.failCase, "FailID", res.failCaseID)
fileLog.Error("TestFailed", "dapp", res.dapp, "TotalCase", res.totalCase, "TotalFail", res.failCase, "FailID", res.failCaseID)
} else {
stdLog.Info("TestSuccess", "dapp", res.dapp, "TotalCase", res.totalCase, "TotalFail", res.failCase)
fileLog.Info("TestSuccess", "dapp", res.dapp, "TotalCase", res.totalCase, "TotalFail", res.failCase)
}
}
return bSuccess
}
func newTestFlow(dapp string, filename string, wg *sync.WaitGroup) {
defer wg.Done()
configType := types.GetAutoTestConfig(dapp)
if configType == nil {
stdLog.Error("GetAutoTestConfigType", "DappName", dapp, "Error", "NotSupportAutoTestType")
return
}
if configType.Kind() == reflect.Ptr {
configType = configType.Elem()
}
caseConf := reflect.New(configType)
if _, err := toml.DecodeFile(filename, caseConf.Interface()); err != nil {
stdLog.Error("TomlDecodeTestCaseFile", "Dapp", dapp, "Filename", filename, "Error", err.Error())
return
}
//get config fields
fields := caseConf.Elem().NumField()
caseArrList := make([]interface{}, fields)
for i := 0; i < fields; i++ {
caseArrList[i] = caseConf.Elem().Field(i).Interface()
}
tester := NewTestOperator(stdLog, fileLog, dapp)
go tester.AddCaseArray(caseArrList...)
go tester.HandleDependency()
go tester.RunSendFlow()
go tester.RunCheckFlow()
testRes := tester.WaitTest()
resultChan <- testRes
}