-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathzeusmaster_test.go
More file actions
310 lines (260 loc) · 6.24 KB
/
zeusmaster_test.go
File metadata and controls
310 lines (260 loc) · 6.24 KB
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
package zeusmaster_test
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path"
"path/filepath"
"testing"
"time"
"github.com/burke/zeus/go/filemonitor"
slog "github.com/burke/zeus/go/shinylog"
"github.com/burke/zeus/go/unixsocket"
"github.com/burke/zeus/go/zeusclient"
"github.com/burke/zeus/go/zeusmaster"
)
var testFiles = map[string]string{
"zeus.json": `
{
"command": "ruby -r./custom_plan -eZeus.go",
"plan": {
"boot": {
"data": {
"data_srv": {}
},
"code": {
"code_srv": {}
},
"cmd": []
}
}
}
`,
"custom_plan.rb": `
$LOAD_PATH.unshift(File.readlink('./lib'))
require 'zeus'
class CustomPlan < Zeus::Plan
def self.command(name, &block)
define_method(name) do
begin
self.instance_eval(&block)
rescue => e
STDERR.puts "#{name} terminated with exception: #{e.message}"
STDERR.puts e.backtrace.map {|line| " #{line}"}
raise
end
end
end
command :boot do
require_relative 'srv'
end
command :data do
redirect_log('data')
require_relative 'data'
end
command :code do
redirect_log('code')
require_relative 'code'
end
command :cmd do
puts "bijagua"
STDERR.puts "bazinga"
end
command :data_srv do
redirect_log('data_srv')
serve('data.sock')
end
command :code_srv do
redirect_log('code_srv')
serve('code.sock')
end
def redirect_log(cmd)
log_file = File.open("zeus_test_#{cmd}.log", 'a')
log_file.sync = true
STDOUT.reopen(log_file)
STDERR.reopen(log_file)
STDOUT.sync = STDERR.sync = true
end
end
Zeus.plan = CustomPlan.new
`,
"data.rb": `
require 'yaml'
$response = YAML::load_file('data.yaml')['response']
`,
"data.yaml": `
response: YAML the Camel is a Mammal with Enamel
`,
"other-data.yaml": `
response: Hi
`,
"code.rb": `
$response = "Hello, world!"
`,
"other-code.rb": `
$response = "there!"
`,
"srv.rb": `
$response = "pong"
def serve(sock_path)
sock = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0)
sock.connect(Socket.pack_sockaddr_un(sock_path))
b = sock.send($response, 0)
puts "Wrote #{b} bytes to #{sock_path}"
end
`,
}
func writeTestFiles(dir string) error {
for name, contents := range testFiles {
if err := ioutil.WriteFile(path.Join(dir, name), []byte(contents), 0644); err != nil {
return fmt.Errorf("error writing %s: %v", name, err)
}
}
gempath := os.Getenv("ZEUS_TEST_GEMPATH")
if gempath == "" {
var err error
gempath, err = filepath.Abs("rubygem")
if err != nil {
return fmt.Errorf("error finding gempath: %v", err)
}
}
if err := os.Symlink(filepath.Join(gempath, "lib"), filepath.Join(dir, "lib")); err != nil {
return fmt.Errorf("error linking zeus gem: %v", err)
}
return nil
}
func enableTracing() {
slog.SetTraceLogger(slog.NewTraceLogger(os.Stderr))
}
func TestZeusBoots(t *testing.T) {
dir, err := ioutil.TempDir("", "zeus_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
if err := writeTestFiles(dir); err != nil {
t.Fatal(err)
}
unixsocket.SetZeusSockName(filepath.Join(dir, ".zeus.sock"))
connections := map[string]*net.UnixConn{
"cmd": nil,
"data": nil,
"code": nil,
}
for name := range connections {
sockName := filepath.Join(dir, fmt.Sprintf("%s.sock", name))
c, err := net.ListenUnixgram("unixgram", &net.UnixAddr{
Name: sockName, Net: "unixgram",
})
if err != nil {
t.Fatalf("Error opening %q socket: %v", sockName, err)
}
defer c.Close()
connections[name] = c
}
me, err := os.FindProcess(os.Getpid())
if err != nil {
t.Fatal(err)
}
defer me.Signal(os.Interrupt)
if err := os.Chdir(dir); err != nil {
t.Fatal(err)
}
// TODO: Find a way to redirect stdout so we can look for crashed
// processes.
enableTracing()
zexit := make(chan int)
go func() {
zexit <- zeusmaster.Run(filepath.Join(dir, "zeus.json"), filemonitor.DefaultFileChangeDelay, false)
}()
expects := map[string]string{
// TODO: Use the zeusclient to spawn a command to test
// that path.
// "cmd": "pong",
"data": "YAML the Camel is a Mammal with Enamel",
"code": "Hello, world!",
}
for name, want := range expects {
if err := readAndCompare(connections[name], want); err != nil {
t.Fatalf("%s: %v", name, err)
}
}
time.Sleep(400 * time.Millisecond)
for _, f := range []string{"code.rb", "data.yaml"} {
from := filepath.Join(dir, fmt.Sprintf("other-%s", f))
to := filepath.Join(dir, f)
if err := os.Rename(from, to); err != nil {
t.Fatalf("Error renaming %s: %v", f, err)
}
}
expects = map[string]string{
"data": "Hi",
"code": "there!",
}
for name, want := range expects {
if err := readAndCompare(connections[name], want); err != nil {
t.Fatalf("%s: %v", name, err)
}
}
readCloser := make(chan struct{})
defer func() { close(readCloser) }()
cmdReader, cmdWriter, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
cmdErrReader, cmdErrWriter, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
cexit := make(chan int, 1)
go func() {
cexit <- zeusclient.Run([]string{"cmd"}, hangingReader{readCloser}, cmdWriter, cmdErrWriter)
time.Sleep(100 * time.Millisecond)
cmdWriter.Close()
cmdErrWriter.Close()
}()
have, err := ioutil.ReadAll(cmdReader)
if err != nil {
t.Fatal(err)
}
if want := "bijagua\n"; string(have) != want {
t.Errorf("expected %q, got %q", want, have)
}
if code := <-cexit; code != 0 {
t.Errorf("cmd exited with %d", code)
}
have, err = ioutil.ReadAll(cmdErrReader)
if err != nil {
t.Fatal(err)
}
if want := "bazinga\n"; string(have) != want {
t.Errorf("expected stderr %q, got %q", want, have)
}
// The zeusmaster catches the interrupt and exits gracefully
me.Signal(os.Interrupt)
if code := <-zexit; code != 0 {
t.Fatalf("Zeus exited with %d", code)
}
}
func readAndCompare(conn *net.UnixConn, want string) error {
buf := make([]byte, 128)
// File system events can take a long time to propagate
conn.SetDeadline(time.Now().Add(2 * time.Second))
if _, _, err := conn.ReadFrom(buf); err != nil {
return err
}
if have := string(bytes.Trim(buf, "\x00")); have != want {
return fmt.Errorf("expected %q, got %q", want, have)
}
return nil
}
type hangingReader struct {
close chan struct{}
}
func (r hangingReader) Read([]byte) (int, error) {
<-r.close
return 0, io.EOF
}