-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
75 lines (69 loc) · 1.42 KB
/
manager.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
package itt
import (
"bytes"
"testing"
"github.com/fsouza/go-dockerclient"
)
type Manager struct {
PortMappings map[string]string // Randomized port mappings from exposed port -> random
containers []Container
ids []string
t *testing.T
}
func (m *Manager) Fatal(args ...interface{}) {
m.Close()
m.getLogs()
m.t.Fatal(args)
}
func (m *Manager) Fatalf(format string, args ...interface{}) {
m.Close()
m.t.Fatalf(format, args)
}
func (m *Manager) FailNow() {
m.Close()
m.getLogs()
m.t.FailNow()
}
func (m *Manager) Close() {
var recovered interface{}
if r := recover(); r != nil {
recovered = r
m.t.Logf("Recovered in close %s", r)
}
//m.t.Logf("Starting close on %d containers\n", len(m.ids))
for _, id := range m.ids {
err := client.KillContainer(docker.KillContainerOptions{ID: id})
if err != nil {
m.t.Error(err)
}
}
if recovered != nil {
panic(recovered)
}
}
func (m *Manager) Write(p []byte) (int, error) {
m.t.Log("Writing!")
m.t.Log(p)
return len(p), nil
}
func (m *Manager) getLogs() {
for _, id := range m.ids {
buf := bytes.Buffer{}
// go func(cid string) {
logReq := docker.LogsOptions{
Container: id,
OutputStream: &buf,
ErrorStream: &buf,
Stdout: true,
Stderr: true,
Timestamps: true,
}
err := client.Logs(logReq)
if err != nil {
m.t.Error(err)
return
}
m.t.Errorf("%s:\n%s", id, buf.String())
// }(id)
}
}