-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage_test.go
104 lines (99 loc) · 2.72 KB
/
message_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
package main
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
func TestTubeRequired(t *testing.T) {
_, e := NewMessage("executable", []string{"arg"}, "", "", "", "", "", 0, 0)
if e == nil {
t.Errorf("Should have missing tube error")
}
if e.Error() != "Missing required param -tube" {
t.Errorf("[%s] isn't [%s]", e.Error(), "Missing required param -tube")
}
// Test same thing from json
jsonstr := `[{"cmd": "echo", "args": ["arg1", "arg2"], "out": "out.txt", "pri": 15, "delay": 120}]`
_, e = MessagesFromJSON([]byte(jsonstr))
if e == nil {
t.Errorf("Should have missing tube error")
}
if e.Error() != "Missing required param -tube" {
t.Errorf("[%s] isn't [%s]", e.Error(), "Missing required param -tube")
}
}
func TestValidJSONUnmarshall(t *testing.T) {
jsonstr := `[{"tube": "testtube", "cmd": "echo", "args": ["arg1", "arg2"], "stdout": "out.txt", "stderr": "err.txt", "pri": 15, "delay": 120}]`
messages, e := MessagesFromJSON([]byte(jsonstr))
if e != nil {
t.Fatal(e)
}
if len(messages) != 1 {
t.Errorf("[%d] isn't [%d]", len(messages), 1)
}
msg := messages[0]
if msg.Tube != "testtube" {
t.Errorf("[%s] isn't [%s]", msg.Tube, "testtube")
}
if msg.Executable != "echo" {
t.Errorf("[%s] isn't [%s]", msg.Executable, "echo")
}
if msg.Arguments[0] != "arg1" {
t.Errorf("[%s] isn't [%s]", msg.Arguments[0], "arg1")
}
if msg.Arguments[1] != "arg2" {
t.Errorf("[%s] isn't [%s]", msg.Arguments[1], "arg2")
}
if msg.Stdout != "out.txt" {
t.Errorf("[%s] isn't [%s]", msg.Stdout, "out.txt")
}
if msg.Stderr != "err.txt" {
t.Errorf("[%s] isn't [%s]", msg.Stderr, "err.txt")
}
if msg.Priority != 15 {
t.Errorf("[%d] isn't [%d]", msg.Priority, 15)
}
if msg.Delay != 120 {
t.Errorf("[%d] isn't [%d]", msg.Delay, 120)
}
}
func TestDefaultWorkdir(t *testing.T) {
msg, e := NewMessage("executable", []string{"arg"}, "", "", "", "", "testtube", 0, 0)
if e != nil {
t.Fatal(e)
}
if msg.Workdir != "/tmp" {
t.Errorf("[%s] isn't [%s]", msg.Workdir, "/tmp")
}
}
func TestReadOut(t *testing.T) {
logfile := "glowtestreadout.log"
logdata := "log data whatevs"
e := ioutil.WriteFile(logfile, []byte(logdata), os.ModePerm)
if e != nil {
t.Fatal(e)
}
defer func(t *testing.T, logfile string) {
e := os.Remove(logfile)
if e != nil {
t.Fatal(e)
}
}(t, logfile)
msg, e := NewMessage("executable", []string{"arg"}, "email", "workdir", logfile, logfile, "testtube", 0, 0)
hostname, e := os.Hostname()
if e != nil {
t.Fatal(e)
}
expectedOutput := fmt.Sprintf(`hostname: %s
stdout: %s
stderr: %s
STDOUT:
log data whatevs
STDERR:
log data whatevs`, hostname, logfile, logfile)
actualOutput := msg.readOut()
if expectedOutput != actualOutput {
t.Errorf("[%s] isn't [%s]", expectedOutput, actualOutput)
}
}