-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclean_test.go
257 lines (213 loc) · 5.74 KB
/
clean_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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"time"
)
// test-creation utils
/* how to setup a test:
// *** universal test cfg setup
skipbye := false
cfg := NewTestConfig()
defer cfg.ByeTestConfig(&skipbye)
// *** end universal test setup
remote := true // or false
defer CleanupServer(cfg, jobservPid, jobserv, remote, &skipbye)
defer CleanupOutdir(cfg)
// during tests, if you want to preserve output directories that would
// normally be mopped up by the deferred functions, set skipbye = true
*/
// get the origdir where the tests are being run from once,
// so that we are not likely to get flaked out on by tests
// running and the cwd changing and then we get two test dirs nested
// (which has happened, and crashed those test runs).
// So we use a TestMain() func.
var origTestMainDir string
func TestMain(m *testing.M) {
// Setup code
var err error
origTestMainDir, err = os.Getwd()
if err != nil {
panic(err)
}
// Run the tests
exitCode := m.Run()
// Teardown code
//fmt.Println("Teardown code after running tests")
// Exit with the appropriate code
os.Exit(exitCode)
}
// make a new fake-home-temp-directory for testing
// and cd into it. Save GOQ_HOME for later restoration.
func NewTestConfig(t *testing.T) *Config {
cfg := NewConfig()
cfg.origdir, cfg.tempdir = MakeAndMoveToTempDir() // cd to tempdir
// link back to bin
err := os.Symlink(cfg.origdir+"/bin", cfg.tempdir+"/bin")
if err != nil {
// update: Arg. Windows needs admin privs to create symlinks.
if strings.Contains(err.Error(), "required privilege is not held") {
// just copy instead
err = os.MkdirAll(fixSlash(cfg.tempdir+"/bin"), 0775)
panicOn(err)
err = CopyDir(cfg.origdir+"/bin", cfg.tempdir+"/bin")
panicOn(err)
}
panicOn(err)
}
cfg.orighome = os.Getenv("GOQ_HOME")
os.Setenv("GOQ_HOME", cfg.tempdir)
if t != nil {
os.Setenv("GOQ_TESTNAME", t.Name())
}
cfg.Home = cfg.tempdir
cfg.JservPort = 2776
cfg.JservIP = GetExternalIP()
cfg.DebugMode = true
cfg.Odir = "o"
cfg.SendTimeoutMsec = 1000
cfg.RecvTimeoutMsec = 1000
cfg.Heartbeat = 5
// match outer env for UseQUIC
cfg.UseQUIC = GetEnvBool("GOQ_USE_QUIC", false)
GenNewCreds(cfg)
addr := cfg.JservAddr()
vv("waiting until addr '%v' is avail", addr)
cfg.WaitUntilAddrAvailable(addr)
// not needed. GOQ_HOME should suffice. InjectConfigIntoEnv(cfg)
return cfg
}
// restore GOQ_HOME and previous working directory
// allow to skip if test goes awry, even if it was deferred.
func (cfg *Config) ByeTestConfig(skip *bool) {
if skip != nil && !(*skip) {
TempDirCleanup(cfg.origdir, cfg.tempdir)
os.Setenv("GOQ_HOME", cfg.orighome)
}
VPrintf("\n ByeTestConfig done.\n")
}
func CleanupOutdir(cfg *Config) {
if DirExists(cfg.Odir) {
c := exec.Command("/bin/rm", "-rf", cfg.Odir)
c.CombinedOutput()
}
VPrintf("\n CleanupOutdir '%s' done.\n", cfg.Odir)
}
// *important* cleanup, and wait for cleanup to finish, so the next test can run.
// skip lets us say we've already done this
func CleanupServer(cfg *Config, jobservPid int, jobserv *JobServ, remote bool, skip *bool) {
if skip == nil || !*skip {
if remote {
SendShutdown(cfg)
WaitForShutdownWithTimeout(jobservPid, cfg)
} else {
// this wait is really important!!! even locally! Otherwise the next test gets hosed
// because the clients will connect to the old server which then dies.
jobserv.Ctrl <- die
<-jobserv.Done
}
}
VPrintf("\n CleanupServer done.\n")
}
func MakeAndMoveToTempDir() (origdir string, tmpdir string) {
// make new temp dir that will have no ".goqclusterid files in it
var err error
origdir = origTestMainDir
tmpdir, err = ioutil.TempDir(origdir, "tempgoqtestdir")
if err != nil {
panic(err)
}
err = os.Chdir(tmpdir)
if err != nil {
panic(err)
}
return origdir, tmpdir
}
func TempDirCleanup(origdir string, tmpdir string) {
// cleanup
os.Chdir(origdir)
err := os.RemoveAll(tmpdir)
for i := 0; err != nil && i < 100; i++ {
// probably somebody still writing. just try again; up
// to 100 times (1 second).
// reference: https://github.com/golang/go/issues/20841
time.Sleep(10 * time.Millisecond)
err = os.RemoveAll(tmpdir)
}
VPrintf("\n TempDirCleanup of '%s' done.\n", tmpdir)
}
func HelperNewJobServ(cfg *Config, remote bool) (jobserv *JobServ, jobservPid int) {
var err error
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)
}
}
return
}
func HelperNewWorker(cfg *Config) *Worker {
worker, err := NewWorker(cfg, nil)
if err != nil {
panic(err)
}
return worker
}
func HelperNewWorkerMonitored(cfg *Config) *Worker {
worker, err := NewWorker(cfg, &WorkOpts{Monitor: true})
if err != nil {
panic(err)
}
return worker
}
func HelperNewWorkerDontStart(cfg *Config) *Worker {
worker, err := NewWorker(cfg, &WorkOpts{DontStart: true})
if err != nil {
panic(err)
}
return worker
}
func HelperNewWorkerDeaf(cfg *Config) *Worker {
worker, err := NewWorker(cfg, &WorkOpts{IsDeaf: true})
if err != nil {
panic(err)
}
return worker
}
func HelperSnapmap(cfg *Config) map[string]string {
serverSnap, err := SubmitGetServerSnapshot(cfg)
if err != nil {
panic(err)
}
return EnvToMap(serverSnap)
}
func HelperSubJob(j *Job, cfg *Config) (sub *Submitter) {
sub, err := NewSubmitter(cfg, false)
if err != nil {
panic(err)
}
sub.SubmitJob(j)
return sub
}
func HelperSubJobGetReply(j *Job, cfg *Config) (sub *Submitter, reply *Job) {
sub, err := NewSubmitter(cfg, false)
if err != nil {
panic(err)
}
reply, _, err = sub.SubmitJobGetReply(j)
if err != nil {
panic(err)
}
return sub, reply
}