Browse code

* standardize timeouts for log reads and writes for logger adapter tests * use an assertion framework in logger adapter tests

Signed-off-by: Royce Remer <royceremer@gmail.com>

Royce Remer authored on 2017/06/19 10:47:45
Showing 1 changed files
... ...
@@ -1,17 +1,16 @@
1 1
 package logger
2 2
 
3 3
 import (
4
-	"bytes"
5 4
 	"encoding/binary"
6 5
 	"io"
7 6
 	"io/ioutil"
8 7
 	"os"
9
-	"runtime"
10 8
 	"testing"
11 9
 	"time"
12 10
 
13 11
 	"github.com/docker/docker/api/types/plugins/logdriver"
14 12
 	protoio "github.com/gogo/protobuf/io"
13
+	"github.com/stretchr/testify/assert"
15 14
 )
16 15
 
17 16
 // mockLoggingPlugin implements the loggingPlugin interface for testing purposes
... ...
@@ -89,9 +88,8 @@ func (l *mockLoggingPlugin) ReadLogs(info Info, config ReadConfig) (io.ReadClose
89 89
 func newMockPluginAdapter(t *testing.T) Logger {
90 90
 	r, w := io.Pipe()
91 91
 	f, err := ioutil.TempFile("", "mock-plugin-adapter")
92
-	if err != nil {
93
-		t.Fatal(err)
94
-	}
92
+	assert.NoError(t, err)
93
+
95 94
 	enc := logdriver.NewLogEntryEncoder(w)
96 95
 	a := &pluginAdapterWithRead{
97 96
 		&pluginAdapter{
... ...
@@ -118,15 +116,11 @@ func TestAdapterReadLogs(t *testing.T) {
118 118
 	}
119 119
 	for _, msg := range testMsg {
120 120
 		m := msg.copy()
121
-		if err := l.Log(m); err != nil {
122
-			t.Fatal(err)
123
-		}
121
+		assert.NoError(t, l.Log(m))
124 122
 	}
125 123
 
126 124
 	lr, ok := l.(LogReader)
127
-	if !ok {
128
-		t.Fatal("expected log reader")
129
-	}
125
+	assert.NotNil(t, ok)
130 126
 
131 127
 	lw := lr.ReadLogs(ReadConfig{})
132 128
 
... ...
@@ -134,16 +128,14 @@ func TestAdapterReadLogs(t *testing.T) {
134 134
 		select {
135 135
 		case msg := <-lw.Msg:
136 136
 			testMessageEqual(t, &x, msg)
137
-		case <-time.After(10 * time.Millisecond):
137
+		case <-time.After(10 * time.Second):
138 138
 			t.Fatal("timeout reading logs")
139 139
 		}
140 140
 	}
141 141
 
142 142
 	select {
143 143
 	case _, ok := <-lw.Msg:
144
-		if ok {
145
-			t.Fatal("expected message channel to be closed")
146
-		}
144
+		assert.False(t, ok, "expected message channel to be closed")
147 145
 	case <-time.After(10 * time.Second):
148 146
 		t.Fatal("timeout waiting for message channel to close")
149 147
 
... ...
@@ -161,16 +153,11 @@ func TestAdapterReadLogs(t *testing.T) {
161 161
 	}
162 162
 
163 163
 	x := Message{Line: []byte("Too infinity and beyond!"), Timestamp: time.Now()}
164
-
165
-	if err := l.Log(x.copy()); err != nil {
166
-		t.Fatal(err)
167
-	}
164
+	assert.NoError(t, l.Log(x.copy()))
168 165
 
169 166
 	select {
170 167
 	case msg, ok := <-lw.Msg:
171
-		if !ok {
172
-			t.Fatal("message channel unexpectedly closed")
173
-		}
168
+		assert.NotNil(t, ok, "message channel unexpectedly closed")
174 169
 		testMessageEqual(t, &x, msg)
175 170
 	case <-time.After(10 * time.Second):
176 171
 		t.Fatal("timeout reading logs")
... ...
@@ -179,30 +166,15 @@ func TestAdapterReadLogs(t *testing.T) {
179 179
 	l.Close()
180 180
 	select {
181 181
 	case msg, ok := <-lw.Msg:
182
-		if ok {
183
-			t.Fatal("expected message channel to be closed")
184
-		}
185
-		if msg != nil {
186
-			t.Fatal("expected nil message")
187
-		}
182
+		assert.False(t, ok, "expected message channel to be closed")
183
+		assert.Nil(t, msg)
188 184
 	case <-time.After(10 * time.Second):
189 185
 		t.Fatal("timeout waiting for logger to close")
190 186
 	}
191 187
 }
192 188
 
193 189
 func testMessageEqual(t *testing.T, a, b *Message) {
194
-	_, _, n, _ := runtime.Caller(1)
195
-	errFmt := "line %d: expected same messages:\nwant: %+v\nhave: %+v"
196
-
197
-	if !bytes.Equal(a.Line, b.Line) {
198
-		t.Fatalf(errFmt, n, *a, *b)
199
-	}
200
-
201
-	if a.Timestamp.UnixNano() != b.Timestamp.UnixNano() {
202
-		t.Fatalf(errFmt, n, *a, *b)
203
-	}
204
-
205
-	if a.Source != b.Source {
206
-		t.Fatalf(errFmt, n, *a, *b)
207
-	}
190
+	assert.Equal(t, a.Line, b.Line)
191
+	assert.Equal(t, a.Timestamp.UnixNano(), b.Timestamp.UnixNano())
192
+	assert.Equal(t, a.Source, b.Source)
208 193
 }