This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
runner_test.go
226 lines (210 loc) · 6.47 KB
/
runner_test.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package hrp
import (
"math"
"os"
"os/exec"
"testing"
"time"
"github.com/rs/zerolog/log"
)
func buildHashicorpPlugin() {
log.Info().Msg("[init] build hashicorp go plugin")
cmd := exec.Command("go", "build",
"-o", "examples/debugtalk.bin",
"examples/plugin/hashicorp.go", "examples/plugin/debugtalk.go")
if err := cmd.Run(); err != nil {
panic(err)
}
}
func removeHashicorpPlugin() {
log.Info().Msg("[teardown] remove hashicorp plugin")
os.Remove("examples/debugtalk.bin")
}
func TestHttpRunnerWithGoPlugin(t *testing.T) {
buildHashicorpPlugin()
defer removeHashicorpPlugin()
assertRunTestCases(t)
}
func TestHttpRunnerWithPythonPlugin(t *testing.T) {
assertRunTestCases(t)
}
func assertRunTestCases(t *testing.T) {
testcase1 := &TestCase{
Config: NewConfig("TestCase1").
SetBaseURL("http://httpbin.org"),
TestSteps: []IStep{
NewStep("headers").
GET("/headers").
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("headers.\"Content-Type\"", "application/json", "check http response Content-Type"),
NewStep("user-agent").
GET("/user-agent").
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("headers.\"Content-Type\"", "application/json", "check http response Content-Type"),
NewStep("TestCase3").CallRefCase(&TestCase{Config: NewConfig("TestCase3").SetBaseURL("http://httpbin.org"), TestSteps: []IStep{
NewStep("ip").
GET("/ip").
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("headers.\"Content-Type\"", "application/json", "check http response Content-Type"),
}}),
NewStep("TestCase4").CallRefCase(&demoRefAPIYAMLPath),
NewStep("TestCase5").CallRefCase(&demoTestCaseJSONPath),
},
}
testcase2 := &TestCase{
Config: NewConfig("TestCase2").SetWeight(3),
}
testcase3 := &TestCase{
Config: NewConfig("TestCase1").
SetBaseURL("https://postman-echo.com"),
TestSteps: []IStep{
NewStep("TestCase5").CallRefAPI(&demoAPIYAMLPath),
},
}
testcase4 := &demoRefTestCaseJSONPath
r := NewRunner(t)
r.SetPluginLogOn()
err := r.Run(testcase1, testcase2, testcase3, testcase4)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
}
func TestInitRendezvous(t *testing.T) {
rendezvousBonudaryTestcase := &TestCase{
Config: NewConfig("run request with functions").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{
"n": 5,
"a": 12.3,
"b": 3.45,
}),
TestSteps: []IStep{
NewStep("test negative number").
Rendezvous("test negative number").
WithUserNumber(-1),
NewStep("test overflow number").
Rendezvous("test overflow number").
WithUserNumber(1000000),
NewStep("test negative percent").
Rendezvous("test very low percent").
WithUserPercent(-0.5),
NewStep("test very low percent").
Rendezvous("test very low percent").
WithUserPercent(0.00001),
NewStep("test overflow percent").
Rendezvous("test overflow percent").
WithUserPercent(1.5),
NewStep("test conflict params").
Rendezvous("test conflict params").
WithUserNumber(1).
WithUserPercent(0.123),
NewStep("test negative timeout").
Rendezvous("test negative timeout").
WithTimeout(-1000),
},
}
type rendezvousParam struct {
number int64
percent float32
timeout int64
}
expectedRendezvousParams := []rendezvousParam{
{number: 100, percent: 1, timeout: 5000},
{number: 100, percent: 1, timeout: 5000},
{number: 100, percent: 1, timeout: 5000},
{number: 0, percent: 0.00001, timeout: 5000},
{number: 100, percent: 1, timeout: 5000},
{number: 100, percent: 1, timeout: 5000},
{number: 100, percent: 1, timeout: 5000},
}
rendezvousList := initRendezvous(rendezvousBonudaryTestcase, 100)
for i, r := range rendezvousList {
if r.Number != expectedRendezvousParams[i].number {
t.Fatalf("run rendezvous %v error: expected number: %v, real number: %v", r.Name, expectedRendezvousParams[i].number, r.Number)
}
if math.Abs(float64(r.Percent-expectedRendezvousParams[i].percent)) > 0.001 {
t.Fatalf("run rendezvous %v error: expected percent: %v, real percent: %v", r.Name, expectedRendezvousParams[i].percent, r.Percent)
}
if r.Timeout != expectedRendezvousParams[i].timeout {
t.Fatalf("run rendezvous %v error: expected timeout: %v, real timeout: %v", r.Name, expectedRendezvousParams[i].timeout, r.Timeout)
}
}
}
func TestThinkTime(t *testing.T) {
buildHashicorpPlugin()
defer removeHashicorpPlugin()
testcases := []*TestCase{
{
Config: NewConfig("TestCase1"),
TestSteps: []IStep{
NewStep("thinkTime").SetThinkTime(2),
},
},
{
Config: NewConfig("TestCase2").
SetThinkTime(thinkTimeIgnore, nil, 0),
TestSteps: []IStep{
NewStep("thinkTime").SetThinkTime(0.5),
},
},
{
Config: NewConfig("TestCase3").
SetThinkTime(thinkTimeRandomPercentage, nil, 0),
TestSteps: []IStep{
NewStep("thinkTime").SetThinkTime(1),
},
},
{
Config: NewConfig("TestCase4").
SetThinkTime(thinkTimeRandomPercentage, map[string]interface{}{"min_percentage": 2, "max_percentage": 3}, 2.5),
TestSteps: []IStep{
NewStep("thinkTime").SetThinkTime(1),
},
},
{
Config: NewConfig("TestCase5"),
TestSteps: []IStep{
NewStep("thinkTime").CallRefCase(&demoThinkTimeJsonPath), // think time: 3s, random pct: {"min_percentage":1, "max_percentage":1.5}, limit: 4s
},
},
}
expectedMinValue := []float64{2, 0, 0.5, 2, 3}
expectedMaxValue := []float64{2.5, 0.5, 2, 3, 10}
for idx, testcase := range testcases {
r := NewRunner(t)
startTime := time.Now()
err := r.Run(testcase)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
duration := time.Since(startTime)
minValue := time.Duration(expectedMinValue[idx]*1000) * time.Millisecond
maxValue := time.Duration(expectedMaxValue[idx]*1000) * time.Millisecond
if duration < minValue || duration > maxValue {
t.Fatalf("failed to test think time, expect value: [%v, %v], actual value: %v", minValue, maxValue, duration)
}
}
}
func TestGenHTMLReport(t *testing.T) {
summary := newOutSummary()
caseSummary1 := newSummary()
caseSummary2 := newSummary()
stepResult1 := &stepData{}
stepResult2 := &stepData{
Name: "Test",
StepType: stepTypeRequest,
Success: false,
ContentSize: 0,
Attachment: "err",
}
caseSummary1.Records = []*stepData{stepResult1, stepResult2, nil}
summary.appendCaseSummary(caseSummary1)
summary.appendCaseSummary(caseSummary2)
err := summary.genHTMLReport()
if err != nil {
t.Error(err)
}
}