-
Notifications
You must be signed in to change notification settings - Fork 9
/
commands.go
402 lines (339 loc) · 9.97 KB
/
commands.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package test161
import (
"bytes"
"errors"
"fmt"
"github.com/imdario/mergo"
yaml "gopkg.in/yaml.v2"
"io/ioutil"
"math/rand"
"strconv"
"strings"
"text/template"
"time"
)
// This file has everything for creating command instances from command templates.
// In test161, a command is either a command run from the kernel menu (sy1), or
// a single userspace program (i.e. /testbin/huge).
//
// Command Templates: Some commands, (e.g. argtest, factorial), have output that
// depends on the input. For this reason, output may be specified as a golang
// template, with various functions provided. Furthermore, random inputs can also be
// generated using a template, which can be overriden in assignment files if needed.
//
// Command Instances: The input/expected output for an instance of a command instance
// is created by executing the templates.
//
// Composite Commands: Some commands may execute other commands, i.e. triple*. For
// these cases, there is an "external" property of the output line, which when set to
// "true", specifies that the text property should be used to look up the output from
// another command.
//
// Functions and function map & functions for command template evaluation
func add(a int, b int) int {
return a + b
}
func atoi(s string) (int, error) {
return strconv.Atoi(s)
}
func randInt(min, max int) (int, error) {
if min >= max {
return 0, errors.New("max must be greater than min")
}
// between 0 and max-min
temp := rand.Intn(max - min)
// between min and mix
return min + temp, nil
}
const stringChars string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()"
func randString(min, max int) (string, error) {
// Get the length of the string
l, err := randInt(min, max)
if err != nil {
return "", err
}
// Make it
b := make([]byte, l)
for i := 0; i < l; i++ {
b[i] = stringChars[rand.Intn(len(stringChars))]
}
return string(b), nil
}
func factorial(n int) int {
if n == 0 {
return 1
} else {
return n * factorial(n-1)
}
}
// Get something of length n that we can range over. This is useful
// for creating random inputs.
func ranger(n int) []int {
res := make([]int, n)
for i := 0; i < n; i++ {
res[i] = i
}
return res
}
// Functions we provide to the command templates.
var funcMap template.FuncMap = template.FuncMap{
"add": add,
"atoi": atoi,
"factorial": factorial,
"randInt": randInt,
"randString": randString,
"ranger": ranger,
}
// Data that we provide for command templates.
type templateData struct {
Args []string
ArgLen int
}
// Command options for panics and timesout
const (
CMD_OPT_NO = "no"
CMD_OPT_MAYBE = "maybe"
CMD_OPT_YES = "yes"
)
// Template for commands instances. These get expanded depending on the command environment.
type CommandTemplate struct {
Name string `yaml:"name"`
Output []*TemplOutputLine `yaml:"output"`
Input []string `yaml:"input"`
Panic string `yaml:"panics"` // CMD_OPT
TimesOut string `yaml:"timesout"` // CMD_OPT
Timeout float32 `yaml:"timeout"` // Timeout in sec. A timeout of 0.0 uses the test default.
}
// An expected line of output, which may either be expanded or not.
type TemplOutputLine struct {
Text string `yaml:"text"`
Trusted string `yaml:"trusted"`
External string `yaml:"external"`
}
// Command instance expected output line. The difference here is that we store the name
// of the key that we need to verify the output.
type ExpectedOutputLine struct {
Text string
Trusted bool
KeyName string
}
func (ct *CommandTemplate) Clone() *CommandTemplate {
clone := *ct
clone.Output = make([]*TemplOutputLine, 0, len(ct.Output))
clone.Input = make([]string, 0, len(ct.Input))
for _, o := range ct.Output {
copy := *o
clone.Output = append(clone.Output, ©)
}
for _, s := range ct.Input {
clone.Input = append(clone.Input, s)
}
return &clone
}
// Expand the golang text template using the provided tempate data.
// We do this on a per-command instance basis, since output can change
// depending on input.
func expandLine(t string, templdata interface{}) ([]string, error) {
res := make([]string, 0)
bb := &bytes.Buffer{}
if tmpl, err := template.New("CommandInstance").Funcs(funcMap).Parse(t); err != nil {
return nil, err
} else if tmpl.Execute(bb, templdata); err != nil {
return nil, err
} else {
lines := strings.Split(bb.String(), "\n")
for _, l := range lines {
if strings.TrimSpace(l) != "" {
res = append(res, l)
}
}
}
return res, nil
}
// Expand template output lines into the actual expected output. This may be
// called recursively if the output line references another command. The
// 'processed' map takes care of checking for cycles so we don't get stuck.
func expandOutput(id string, tmpl *CommandTemplate, td *templateData,
processed map[string]bool, env *TestEnvironment) ([]*ExpectedOutputLine, error) {
var ok bool
// Check for cycles
if _, ok = processed[id]; ok {
return nil, errors.New("Cycle detected in command template output. ID: " + id)
}
processed[id] = true
// expected output
expected := make([]*ExpectedOutputLine, 0)
// Expand each expected output line, possibly referencing external commands
for _, origline := range tmpl.Output {
if origline.External == "true" {
copy := make(map[string]bool)
for key, _ := range processed {
copy[key] = true
}
if otherTmpl, ok := env.Commands[origline.Text]; ok {
if more, err := expandOutput(origline.Text, otherTmpl, td, copy, env); err != nil {
return nil, err
} else {
expected = append(expected, more...)
}
}
} else {
if lines, err := expandLine(origline.Text, td); err != nil {
return nil, err
} else {
for _, expandedline := range lines {
expectedline := &ExpectedOutputLine{
Text: expandedline,
}
if origline.Trusted == "true" {
expectedline.Trusted = true
expectedline.KeyName = id
} else {
expectedline.Trusted = false
expectedline.KeyName = ""
}
expected = append(expected, expectedline)
}
}
}
}
return expected, nil
}
func (c *Command) Id() string {
_, id, _ := (&c.Input).splitCommand()
return id
}
// Instantiate the command (input, expected output) using the command template.
// This needs to be must be done prior to executing the command.
func (c *Command) Instantiate(env *TestEnvironment) error {
pfx, id, args := (&c.Input).splitCommand()
tmpl, ok := env.Commands[id]
if !ok {
return fmt.Errorf("Command '%v' is not recognized by test161.\n", id)
}
// Individual tests can override the template in the commands files.
// This merges the overrides on top of a copy of the command template.
tmpl = tmpl.Clone()
if err := mergo.Merge(tmpl, &c.Config, mergo.WithOverride); err != nil {
return err
}
c.Panic = tmpl.Panic
c.TimesOut = tmpl.TimesOut
c.Timeout = tmpl.Timeout
// Input
// Check if we need to create some input. If args haven't already been
// specified, and there is an input template, use that to create input.
if len(args) == 0 && len(tmpl.Input) > 0 {
args = make([]string, 0)
for _, line := range tmpl.Input {
if temp, err := expandLine(line, "No data"); err != nil {
return err
} else {
args = append(args, temp...)
}
}
} else if len(args) > 0 {
// Expand the provided arguments as well
argLine := ""
for i, arg := range args {
if i > 0 {
argLine += " "
}
argLine += arg
}
if temp, err := expandLine(argLine, "No data"); err != nil {
return err
} else {
// Break
args = []string{}
for _, arg := range temp {
args = append(args, splitArgs(arg)...)
}
}
}
// Output
// template data for the output
td := &templateData{args, len(args)}
processed := make(map[string]bool)
if expected, err := expandOutput(id, tmpl, td, processed, env); err != nil {
return err
} else {
// Piece back together a command line for the command
commandLine := ""
if len(pfx) > 0 {
commandLine += pfx + " "
}
commandLine += id
for _, arg := range args {
commandLine += " " + arg
}
c.Input.Line = commandLine
c.ExpectedOutput = expected
return nil
}
}
// CommandTemplate Collection. We just use this for loading and move the
// references into a map in the global environment.
type CommandTemplates struct {
Templates []*CommandTemplate `yaml:"templates"`
}
func CommandTemplatesFromFile(file string) (*CommandTemplates, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("Error reading commands file %v: %v", file, err)
}
c, err := CommandTemplatesFromString(string(data))
if err != nil {
err = fmt.Errorf("Error loading commands file %v: %v", file, err)
}
return c, err
}
func CommandTemplatesFromString(text string) (*CommandTemplates, error) {
cmds := &CommandTemplates{}
err := yaml.Unmarshal([]byte(text), cmds)
if err != nil {
return nil, err
}
for _, t := range cmds.Templates {
t.fixDefaults()
}
return cmds, nil
}
func (t *CommandTemplate) fixDefaults() {
// Fix poor default value support in go-yaml/golang.
//
// If we find an empty output line, delete it - these are commands
// that do not expect output. If we find a command with no expected
// output, add the default expected output.
// Default for panic is to not allow it, i.e. must return to prompt
if t.Panic != CMD_OPT_MAYBE && t.Panic != CMD_OPT_YES {
t.Panic = CMD_OPT_NO
}
if t.TimesOut != CMD_OPT_MAYBE && t.TimesOut != CMD_OPT_YES {
t.TimesOut = CMD_OPT_NO
}
if len(t.Output) == 1 && strings.TrimSpace(t.Output[0].Text) == "" {
t.Output = nil
} else if len(t.Output) == 0 {
t.Output = []*TemplOutputLine{
&TemplOutputLine{
Trusted: "true",
External: "false",
Text: t.Name + ": SUCCESS",
},
}
} else {
for _, line := range t.Output {
if line.Trusted != "false" {
line.Trusted = "true"
}
if line.External != "true" {
line.External = "false"
}
}
}
}
// Seed random for the random input templates and sys161
func init() {
rand.Seed(time.Now().UnixNano())
}