Browse code

Fix flakey test for log file rotate.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>

Brian Goff authored on 2020/05/15 06:25:20
Showing 1 changed files
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	"io"
7 7
 	"io/ioutil"
8 8
 	"os"
9
+	"path/filepath"
9 10
 	"strings"
10 11
 	"testing"
11 12
 	"time"
... ...
@@ -14,6 +15,7 @@ import (
14 14
 	"github.com/docker/docker/pkg/pubsub"
15 15
 	"github.com/docker/docker/pkg/tailfile"
16 16
 	"gotest.tools/v3/assert"
17
+	"gotest.tools/v3/poll"
17 18
 )
18 19
 
19 20
 type testDecoder struct {
... ...
@@ -259,21 +261,15 @@ func TestCheckCapacityAndRotate(t *testing.T) {
259 259
 	defer l.Close()
260 260
 
261 261
 	assert.NilError(t, l.WriteLogEntry(&logger.Message{Line: []byte("hello world!")}))
262
-
263
-	dStringer := dirStringer{dir}
264
-
265 262
 	_, err = os.Stat(f.Name() + ".1")
266
-	assert.Assert(t, os.IsNotExist(err), dStringer)
263
+	assert.Assert(t, os.IsNotExist(err), dirStringer{dir})
267 264
 
268 265
 	assert.NilError(t, l.WriteLogEntry(&logger.Message{Line: []byte("hello world!")}))
269
-	_, err = os.Stat(f.Name() + ".1")
270
-	assert.NilError(t, err, dStringer)
266
+	poll.WaitOn(t, checkFileExists(f.Name()+".1"), poll.WithTimeout(30*time.Second))
271 267
 
272 268
 	assert.NilError(t, l.WriteLogEntry(&logger.Message{Line: []byte("hello world!")}))
273
-	_, err = os.Stat(f.Name() + ".1")
274
-	assert.NilError(t, err, dStringer)
275
-	_, err = os.Stat(f.Name() + ".2.gz")
276
-	assert.NilError(t, err, dStringer)
269
+	poll.WaitOn(t, checkFileExists(f.Name()+".1"), poll.WithTimeout(30*time.Second))
270
+	poll.WaitOn(t, checkFileExists(f.Name()+".2.gz"), poll.WithTimeout(30*time.Second))
277 271
 
278 272
 	// Now let's simulate a failed rotation where the file was able to be closed but something else happened elsewhere
279 273
 	// down the line.
... ...
@@ -299,3 +295,18 @@ func (d dirStringer) String() string {
299 299
 	}
300 300
 	return s.String()
301 301
 }
302
+
303
+func checkFileExists(name string) poll.Check {
304
+	return func(t poll.LogT) poll.Result {
305
+		_, err := os.Stat(name)
306
+		switch {
307
+		case err == nil:
308
+			return poll.Success()
309
+		case os.IsNotExist(err):
310
+			return poll.Continue("waiting for %s to exist", name)
311
+		default:
312
+			t.Logf("%s", dirStringer{filepath.Dir(name)})
313
+			return poll.Error(err)
314
+		}
315
+	}
316
+}