-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner_test.go
262 lines (240 loc) · 6.27 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package starbox_test
import (
"context"
"errors"
"reflect"
"strings"
"testing"
"time"
"github.com/1set/starbox"
"github.com/1set/starlet"
"github.com/psanford/memfs"
"go.starlark.net/starlark"
)
func TestRunnerConfig_Empty(t *testing.T) {
cfg := starbox.NewRunConfig()
t.Logf("config: %v", cfg)
if _, e := cfg.Execute(); e == nil || e.Error() != "no starbox instance" {
t.Errorf("got unexpected error: %v", e)
return
}
res, err := cfg.Starbox(starbox.New("aloha")).Execute()
if err == nil || err.Error() != "starlet: run: no script to execute" {
t.Errorf("got unexpected error: %v", err)
return
}
if len(res) > 0 {
t.Errorf("expect empty, got %v", res)
return
}
}
func TestRunnerConfig_Full(t *testing.T) {
box := starbox.New("aloha")
box.SetModuleSet(starbox.SafeModuleSet)
cfg := starbox.NewRunConfig().
FileName("mine.star").
Script("print('Hello, {}!'.format(word)); x = word.upper(); print(__modules__)").
Context(context.TODO()).
KeyValue("word", "World").
Timeout(5*time.Second).
Inspect(false).
InspectCond(func(_ starlet.StringAnyMap, e error) bool { return e != nil }).
KeyValue("word", "Star")
cfg2 := cfg.Starbox(box)
t.Logf("config1: %v", cfg)
t.Logf("config2: %v", cfg2)
_, e1 := cfg.Execute()
if e1 == nil {
t.Error("expect error, got nil")
return
}
t.Logf("error1: %v", e1)
res, e2 := cfg2.Execute()
if e2 != nil {
t.Errorf("expect nil, got %v", e2)
return
}
if res["x"].(string) != "STAR" {
t.Errorf("expect x=STAR, got %v", res["x"])
return
}
if em := []string{"atom", "base64", "csv", "go_idiomatic", "hashlib", "json", "math", "random", "re", "string", "struct", "time"}; !reflect.DeepEqual(em, box.GetModuleNames()) {
t.Errorf("expect %v, got %v", em, box.GetModuleNames())
return
}
t.Logf("result: %v", res)
t.Logf("box: %v", box)
}
func TestRunnerConfig_Reuse(t *testing.T) {
cfg := starbox.New("aloha").
CreateRunConfig().
FileName("mine.star").
Script("print('Hello, {}!'.format(word)); x = word.upper()").
Timeout(-1*time.Nanosecond).
Inspect(false).
KeyValue("word", "World")
res, err := cfg.Execute()
if err != nil {
t.Errorf("expect nil, got %v", err)
return
}
if res["x"].(string) != "WORLD" {
t.Errorf("expect x=WORLD, got %v", res["x"])
return
}
// reuse the same config
box2 := starbox.New("hello")
res2, err2 := cfg.Starbox(box2).Execute()
if err2 != nil {
t.Errorf("expect nil, got %v", err2)
return
}
if res2["x"].(string) != "WORLD" {
t.Errorf("expect x=WORLD, got %v", res2["x"])
return
}
// reuse the box
res3, err3 := cfg.Starbox(box2).Execute()
if err3 != nil {
t.Errorf("expect nil, got %v", err3)
return
}
if res3["x"].(string) != "WORLD" {
t.Errorf("expect x=WORLD, got %v", res3["x"])
return
}
t.Logf("box2: %v", box2)
}
func TestRunnerConfig_KeyValues(t *testing.T) {
cfg := starbox.New("aloha").CreateRunConfig().
KeyValueMap(starlet.StringAnyMap{"a": 1}).
KeyValue("a", 10).
KeyValue("b", 20).
KeyValueMap(starlet.StringAnyMap{"a": 100, "c": 50}).
KeyValue("c", 1000).
KeyValueMap(starlet.StringAnyMap{"d": 10000}).
Script("r = a + b + c + d")
res, err := cfg.Execute()
if err != nil {
t.Errorf("expect nil, got %v", err)
return
}
if res["r"].(int64) != 11120 {
t.Errorf("expect r=11120, got %v", res["r"])
return
}
}
func TestRunnerConfig_Clone(t *testing.T) {
cfg := starbox.New("aloha").CreateRunConfig().
KeyValue("a", 10).
KeyValue("b", 20).
KeyValue("c", 30).
Script("r = a + b + c")
cfg2 := cfg.Clone()
cfg2.KeyValue("a", 100).KeyValue("b", 200).KeyValue("c", 300)
// compare pointers
if cfg == cfg2 {
t.Error("expect different pointers, got same")
return
}
}
func TestRunnerConfig_RunWithName(t *testing.T) {
var sb strings.Builder
b := starbox.New("test")
b.SetPrintFunc(func(thread *starlark.Thread, msg string) {
sb.WriteString(msg)
})
_, err := b.CreateRunConfig().FileName("one.star").Script(`print('Aloha!'`).Execute()
if err == nil {
t.Error("expect error, got nil")
return
}
if err.Error() != "starlark: exec: one.star:1:15: got end of file, want ')'" {
t.Errorf("expect syntax error, got %v", err)
return
}
}
func TestRunnerConfig_RunByName(t *testing.T) {
// create a virtual filesystem
mn := `exact.star`
s1 := hereDoc(`
a = 10
b = 20
print('Aloha', a+b)
`)
fs := memfs.New()
fs.WriteFile(mn, []byte(s1), 0644)
// create a new Starbox instance
var sb strings.Builder
b := starbox.New("test")
b.SetFS(fs)
b.SetPrintFunc(func(thread *starlark.Thread, msg string) {
sb.WriteString(msg)
})
// run the missing script
_, err := b.CreateRunConfig().FileName("missing.star").Execute()
if err == nil {
t.Error("expect error, got nil")
return
}
// run the exact script
out, err := b.CreateRunConfig().FileName(mn).Execute()
if err != nil {
t.Errorf("expect nil, got %v", err)
return
}
if out["a"].(int64) != int64(10) || out["b"].(int64) != int64(20) {
t.Errorf("expect a=10, b=20, got %v", out)
return
}
if es := "Aloha 30"; sb.String() != es {
t.Errorf("expect %q, got %v", es, sb.String())
return
}
}
func TestRunnerConfig_RunTimeout(t *testing.T) {
b := starbox.New("test")
b.SetModuleSet(starbox.SafeModuleSet)
_, err := b.CreateRunConfig().Script(`sleep(1)`).Timeout(50 * time.Millisecond).Execute()
if err == nil {
t.Error("expect error, got nil")
return
}
if errors.Is(err, context.DeadlineExceeded) {
t.Logf("expect timeout error: %v", err)
} else {
t.Errorf("unexpected context error, got %v", err)
}
}
func TestRunnerConfig_RunContext(t *testing.T) {
b := starbox.New("test")
b.SetModuleSet(starbox.SafeModuleSet)
ctx, cancel := context.WithCancel(context.TODO())
go func() {
time.Sleep(50 * time.Millisecond)
cancel()
}()
_, err := b.CreateRunConfig().Script(`sleep(1)`).Context(ctx).Execute()
if err == nil {
t.Error("expect error, got nil")
return
}
if errors.Is(err, context.Canceled) {
t.Logf("expect cancel error: %v", err)
} else {
t.Errorf("unexpected context error, got %v", err)
}
}
func TestRunnerConfig_Inspect(t *testing.T) {
b := starbox.New("test")
b.SetModuleSet(starbox.SafeModuleSet)
res, err := b.CreateRunConfig().Script(`a = 100; print('Hello, World!')`).Inspect(true).Execute()
if err != nil {
t.Errorf("expect nil, got %v", err)
return
}
if res == nil {
t.Error("expect not nil, got nil")
return
}
}