-
Notifications
You must be signed in to change notification settings - Fork 0
/
venom.go
137 lines (118 loc) · 2.94 KB
/
venom.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
package venom
import (
"fmt"
"io"
"os"
)
var (
//Version is set with -ldflags "-X github.com/ovh/venom/venom.Version=$(VERSION)"
Version = "snapshot"
)
func New() *Venom {
v := &Venom{
LogLevel: "info",
LogOutput: os.Stdout,
PrintFunc: fmt.Printf,
executors: map[string]Executor{},
contexts: map[string]TestCaseContext{},
variables: map[string]string{},
EnableProfiling: false,
IgnoreVariables: []string{},
OutputFormat: "xml",
}
return v
}
type Venom struct {
LogLevel string
LogOutput io.Writer
PrintFunc func(format string, a ...interface{}) (n int, err error)
executors map[string]Executor
contexts map[string]TestCaseContext
testsuites []TestSuite
variables map[string]string
IgnoreVariables []string
Parallel int
EnableProfiling bool
OutputFormat string
OutputDir string
StopOnFailure bool
}
func (v *Venom) AddVariables(variables map[string]string) {
for k, variable := range variables {
v.variables[k] = variable
}
}
// RegisterExecutor register Test Executors
func (v *Venom) RegisterExecutor(name string, e Executor) {
v.executors[name] = e
}
// WrapExecutor initializes a test by name
// no type -> exec is default
func (v *Venom) WrapExecutor(t map[string]interface{}, tcc TestCaseContext) (*ExecutorWrap, error) {
var name string
var retry, delay, timeout int
if itype, ok := t["type"]; ok {
name = fmt.Sprintf("%s", itype)
}
if name == "" && tcc.GetName() != "default" {
name = tcc.GetName()
} else if name == "" {
name = "exec"
}
retry, errRetry := getAttrInt(t, "retry")
if errRetry != nil {
return nil, errRetry
}
delay, errDelay := getAttrInt(t, "delay")
if errDelay != nil {
return nil, errDelay
}
timeout, errTimeout := getAttrInt(t, "timeout")
if errTimeout != nil {
return nil, errTimeout
}
if e, ok := v.executors[name]; ok {
ew := &ExecutorWrap{
executor: e,
retry: retry,
delay: delay,
timeout: timeout,
}
return ew, nil
}
return nil, fmt.Errorf("[%s] type '%s' is not implemented", tcc.GetName(), name)
}
// RegisterTestCaseContext new register TestCaseContext
func (v *Venom) RegisterTestCaseContext(name string, tcc TestCaseContext) {
v.contexts[name] = tcc
}
// ContextWrap initializes a context for a testcase
// no type -> parent context
func (v *Venom) ContextWrap(tc *TestCase) (TestCaseContext, error) {
if tc.Context == nil {
return v.contexts["default"], nil
}
var typeName string
if itype, ok := tc.Context["type"]; ok {
typeName = fmt.Sprintf("%s", itype)
}
if typeName == "" {
return v.contexts["default"], nil
}
v.contexts[typeName].SetTestCase(*tc)
return v.contexts[typeName], nil
}
func getAttrInt(t map[string]interface{}, name string) (int, error) {
var out int
if i, ok := t[name]; ok {
var ok bool
out, ok = i.(int)
if !ok {
return -1, fmt.Errorf("attribute %s '%s' is not an integer", name, i)
}
}
if out < 0 {
out = 0
}
return out, nil
}