-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_test.go
150 lines (124 loc) · 3.1 KB
/
job_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
package fuq
import (
"bytes"
"encoding/json"
"testing"
)
func TestReadJobFile(t *testing.T) {
jfData := `
# Test data file... this is a comment
name FooBar
num_tasks 23
working_dir ~/sims/FooBar
log_dir ~/sims/FooBar/logs
command /bin/echo # another comment
`
r := bytes.NewReader([]byte(jfData))
jd, err := ReadJobFile(r)
if err != nil {
t.Fatalf("error reading job file: %v", err)
}
if jd.NumTasks != 23 {
t.Error("wrong number of tasks, expected 23, found: %d", jd.NumTasks)
}
strCmpTable := stringCompareTable{
{jd.Name, "FooBar", "name"},
{jd.WorkingDir, "~/sims/FooBar", "working directory"},
{jd.LoggingDir, "~/sims/FooBar/logs", "logging directory"},
{jd.Command, "/bin/echo", "command"},
}
strCmpTable.Check(t)
}
func checkParsedJobStatus(t *testing.T, s string, js JobStatus, expectOK bool) {
pjs, err := ParseJobStatus(s)
parseOK := (err == nil)
if parseOK != expectOK {
if expectOK {
t.Fatalf("error parsing '%s': %v", s, err)
} else {
t.Fatalf("expected error parsing '%s' but none found")
}
}
if !expectOK {
return
}
if pjs != js {
t.Errorf("parsed '%s' into JobStatus %v, but expect %v",
s, pjs, js)
}
}
func TestParseJobStatus(t *testing.T) {
checkParsedJobStatus(t, `waiting`, Waiting, true)
checkParsedJobStatus(t, `waitin`, Waiting, false)
checkParsedJobStatus(t, `running`, Running, true)
checkParsedJobStatus(t, `"running"`, Running, false)
checkParsedJobStatus(t, `finished`, Finished, true)
checkParsedJobStatus(t, `finish`, Finished, false)
checkParsedJobStatus(t, `paused`, Paused, true)
checkParsedJobStatus(t, `pause`, Paused, false)
checkParsedJobStatus(t, `cancelled`, Cancelled, true)
checkParsedJobStatus(t, `cancel`, Cancelled, false)
}
func TestJobStatusMarshalJSON(t *testing.T) {
inp := make(map[string]JobStatus)
inp["key1"] = Waiting
inp["key2"] = Running
inp["key3"] = Paused
inp["key4"] = Finished
inp["key5"] = Cancelled
js, err := json.Marshal(inp)
if err != nil {
t.Fatalf("error marshaling json: %v", err)
}
var out bytes.Buffer
if err := json.Indent(&out, js, "", " "); err != nil {
t.Fatalf("error indenting json: %v", err)
}
expected := `{
"key1": "waiting",
"key2": "running",
"key3": "paused",
"key4": "finished",
"key5": "cancelled"
}`
jsPP := out.String()
if jsPP != expected {
t.Errorf("expected json: '%v', but found '%v'", expected, jsPP)
}
}
func TestJobStatusUnmarshalJSON(t *testing.T) {
out := make(map[string]JobStatus)
inp := []byte(`{
"key1": "waiting",
"key2": "running",
"key3": "paused",
"key4": "finished",
"key5": "cancelled"
}`)
if err := json.Unmarshal(inp, &out); err != nil {
t.Fatalf("error unmarshaling json: %v", err)
}
table := []struct {
k string
v JobStatus
}{
{"key1", Waiting},
{"key2", Running},
{"key3", Paused},
{"key4", Finished},
{"key5", Cancelled},
}
for _, kvp := range table {
key, value := kvp.k, kvp.v
actual, found := out[kvp.k]
if !found {
t.Errorf("key '%s' not in map", key)
continue
}
if actual != value {
t.Errorf("key '%s' should have value %v but found %v",
key, value, actual)
continue
}
}
}