-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathout_test.go
146 lines (120 loc) · 3.46 KB
/
out_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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
cv "github.com/glycerine/goconvey/convey"
)
// job output test
//
func TestJobOutputIsWrittenToDisk(t *testing.T) {
cv.Convey("When we submit a simple echo job, the output should be found on disk, in a file tagged with the job id", t, func() {
var jobserv *JobServ
var err error
var jobservPid int
remote := false
// *** universal test cfg setup
skipbye := false
cfg := NewTestConfig(t)
defer cfg.ByeTestConfig(&skipbye)
// *** end universal test setup
//cfg := DefaultCfg()
cfg.DebugMode = true // reply to badsig packets
//cfg.SendTimeoutMsec = 30000
cfg.Odir = "testo"
// delete old contents of testo so we can run the test
// repeatedly, but don't auto-cleanup after the test. => human can inspect.
CleanupOutdir(cfg)
cv.So(DirExists(cfg.Odir), cv.ShouldEqual, false)
cfg.WaitUntilAddrAvailable(cfg.JservAddr())
if remote {
jobservPid, err = NewExternalJobServ(cfg)
if err != nil {
panic(err)
}
fmt.Printf("\n")
fmt.Printf("[pid %d] spawned a new external JobServ with pid %d\n", os.Getpid(), jobservPid)
} else {
jobserv, err = NewJobServ(cfg)
if err != nil {
panic(err)
}
}
var skip bool
defer CleanupServer(cfg, jobservPid, jobserv, remote, &skip)
// don't do this, since we are testing for output: defer CleanupOutdir(cfg)
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
j := NewJob()
//j.Cmd = "./bin/sleep.sh"
j.Cmd = "/bin/echo"
j.Args = []string{"hello world"}
j.Dir = pwd + "/new_sub_dir"
err = os.Mkdir(j.Dir, 0775)
if err != nil {
panic(err)
}
sub, err := NewSubmitter(cfg, false)
if err != nil {
panic(err)
}
reply, _, err := sub.SubmitJobGetReply(j)
if err != nil {
panic(err)
}
vv("[pid %d] submitter got reply %s with job.Aboutjid=%d full reply: %#v\n", os.Getpid(), reply.Msg, reply.Aboutjid, reply)
// WaitForJob returns only after the request to watch the job has been registered
waitchan, err := sub.WaitForJob(reply.Aboutjid)
if err != nil {
panic(err)
}
vv("past the WaitForJob")
worker, err := NewWorker(cfg, nil)
if err != nil {
panic(err)
}
// to test the wait-finish, get to it sooner while job is in background
go func() {
worker.DoOneJob()
worker.Destroy()
}()
// gotta wait for server to write
reply2 := <-waitchan
vv("reply2 from <-waitchan is '%#v'", reply2)
if reply2 != nil {
if reply2.Id == -1 {
fmt.Printf("\n There was an error while we were waiting for the job to finish.\n")
panic(reply2.Out)
}
fmt.Printf("\n [pid %d] after WaitForJob, submitter got reply2 %s with job.Aboutid=%d full reply: %#v\n", os.Getpid(), reply2.Msg, reply2.Aboutjid, reply2)
}
// now we should be safe to shutdown
CleanupServer(cfg, jobservPid, jobserv, remote, nil)
skip = true // tell the deferred CleanupServer they don't need to run now.
fn := fmt.Sprintf("%s/%s/out.%05d", j.Dir, cfg.Odir, reply.Aboutjid)
odir := fmt.Sprintf("%s/%s", j.Dir, cfg.Odir)
fmt.Printf("\nout_test is checking for file: %s\n", fn)
dire := DirExists(odir)
cv.So(dire, cv.ShouldEqual, true)
if dire {
filee := FileExists(fn)
cv.So(filee, cv.ShouldEqual, true)
if filee {
slurp, err := ioutil.ReadFile(fn)
if err != nil {
panic(err)
}
line := strings.Trim(string(slurp), " \n\t")
cv.So(line, cv.ShouldEqual, "hello world")
} else {
skipbye = true
}
} else {
skipbye = true
}
})
}