-
Notifications
You must be signed in to change notification settings - Fork 2
/
logs_test.go
169 lines (147 loc) · 3.63 KB
/
logs_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
package container // import "github.com/docker/docker/integration/container"
import (
"context"
"fmt"
"io/ioutil"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/integration/internal/container"
"github.com/docker/docker/internal/test/request"
"github.com/docker/docker/pkg/stdcopy"
"gotest.tools/assert"
"gotest.tools/skip"
)
// Regression test for #35370
// Makes sure that when following we don't get an EOF error when there are no logs
func TestLogsFollowTailEmpty(t *testing.T) {
// FIXME(vdemeester) fails on a e2e run on linux...
skip.If(t, testEnv.IsRemoteDaemon())
defer setupTest(t)()
client := request.NewAPIClient(t)
ctx := context.Background()
id := container.Run(t, ctx, client, container.WithCmd("sleep", "100000"))
logs, err := client.ContainerLogs(ctx, id, types.ContainerLogsOptions{ShowStdout: true, Tail: "2"})
if logs != nil {
defer logs.Close()
}
assert.Check(t, err)
_, err = stdcopy.StdCopy(ioutil.Discard, ioutil.Discard, logs)
assert.Check(t, err)
}
type daemonResources [2]int
func (r daemonResources) String() string {
return fmt.Sprintf("goroutines: %d, file descriptors: %d", r[0], r[1])
}
func (r daemonResources) Delta(r2 daemonResources) (d daemonResources) {
for i := 0; i < len(r); i++ {
d[i] = r2[i] - r[i]
if d[i] < 0 { // negative values do not make sense here
d[i] = 0
}
}
return
}
// Test for #37391
func TestLogsFollowGoroutineLeak(t *testing.T) {
defer setupTest(t)()
client := request.NewAPIClient(t)
ctx := context.Background()
getDaemonResources := func() (r daemonResources) {
info, err := client.Info(ctx)
assert.NilError(t, err)
// this will fail for daemon run without -D/--debug
assert.Check(t, info.NGoroutines > 1)
assert.Check(t, info.NFd > 1)
r[0] = info.NGoroutines
r[1] = info.NFd
return
}
isZero := func(delta daemonResources) bool {
for i := 0; i < len(delta); i++ {
if delta[i] > 0 {
return false
}
}
return true
}
waitToFreeResources := func(exp daemonResources) error {
tm := time.After(10 * time.Second)
for {
select {
case <-tm:
// one last chance
r := getDaemonResources()
t.Logf("daemon resources after: %v", r)
d := exp.Delta(r)
if isZero(d) {
return nil
}
return fmt.Errorf("Leaked %v", d)
default:
d := exp.Delta(getDaemonResources())
if isZero(d) {
return nil
}
time.Sleep(200 * time.Millisecond)
}
}
}
// start a container producing lots of logs
id := container.Run(t, ctx, client, container.WithCmd("yes", "lorem ipsum"))
exp := getDaemonResources()
t.Logf("daemon resources before: %v", exp)
// consume logs
stopCh := make(chan struct{})
errCh := make(chan error)
go func() {
logs, err := client.ContainerLogs(ctx, id, types.ContainerLogsOptions{
Follow: true,
ShowStdout: true,
ShowStderr: true,
})
if err != nil {
errCh <- err
return
}
assert.Check(t, logs != nil)
rd := 0
buf := make([]byte, 1024)
defer func() {
logs.Close()
t.Logf("exit after reading %d bytes", rd)
}()
for {
select {
case <-stopCh:
errCh <- nil
return
default:
n, err := logs.Read(buf)
rd += n
if err != nil {
errCh <- err
return
}
}
}
}()
// read logs for a bit, then stop the reader
select {
case err := <-errCh:
// err can't be nil here
t.Fatalf("logs unexpectedly closed: %v", err)
case <-time.After(1 * time.Second):
close(stopCh)
}
// wait for log reader to stop
select {
case <-errCh:
case <-time.After(1 * time.Second):
t.Fatal("timeout waiting for log reader to stop")
}
err := waitToFreeResources(exp)
if err != nil {
t.Fatal(err)
}
}