forked from rockintest/rocktest-go
/
call.go
127 lines (95 loc) · 2.39 KB
/
call.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
package scenario
import (
"fmt"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
)
func (module *Module) Call(params map[string]interface{}, scenario *Scenario) error {
paramsEx, err := scenario.ExpandMap(params)
if err != nil {
return err
}
val, err := scenario.GetString(paramsEx, "value", nil)
if err != nil {
return err
}
re, err := regexp.Compile(` *(.*)->(.*) *`)
if err != nil {
log.Errorf(err.Error())
return err
}
if re.Match([]byte(val)) {
// Call a function
res := re.FindAllStringSubmatch(val, -1)
mod := res[0][1]
fun := res[0][2]
if mod == "" {
log.Debugf("Call local function %s", fun)
steps, ok := scenario.Functions[fun]
if !ok {
return fmt.Errorf("function %s does not exist", fun)
} else {
inFunctionBefore := scenario.InFunction
scenario.InFunction = true
scenario.pushContext()
// The module name is actually the function name for this call
scenario.PutContext("module", fun)
scenario.AddVariables(paramsEx)
err := scenario.RunSteps(steps)
if err != nil {
return err
}
scenario.popContext()
scenario.InFunction = inFunctionBefore
}
} else {
log.Debugf("Call function %s in module %d", fun, mod)
calledScenario := NewScenario()
calledScenario.Caller = scenario
calledScenario.Root = scenario.Root
if !strings.HasSuffix(mod, ".yaml") {
mod = mod + ".yaml"
}
context, err := scenario.GetList(paramsEx, "context", nil)
if err == nil {
if context[0] == "all" {
calledScenario.CopyVariables(scenario)
} else {
for _, v := range context {
calledScenario.CopyVariable(fmt.Sprint(v), scenario)
}
}
}
calledScenario.AddVariables(paramsEx)
err = calledScenario.RunFunction(mod, fun)
if err != nil {
return err
}
}
} else {
// Call a scenario
calledScenario := NewScenario()
calledScenario.Caller = scenario
calledScenario.Root = scenario.Root
if !strings.HasSuffix(val, ".yaml") {
val = val + ".yaml"
}
context, err := scenario.GetList(paramsEx, "context", nil)
if err == nil {
if context[0] == "all" {
calledScenario.CopyVariables(scenario)
} else {
for _, v := range context {
calledScenario.CopyVariable(fmt.Sprint(v), scenario)
}
}
}
calledScenario.AddVariables(paramsEx)
err = calledScenario.RunFromRoot(val)
if err != nil {
return err
}
}
return nil
}