Browse code

Fix flaky test TestJSONFormatProgress (#21124)

In TestJSONFormatProgress, the progress string was used for comparison.
However, the progress string (progress.String()) uses time.Now().UTC()
to generate the timeLeftBox which is not a fixed value and cannot be
compared reliably.

This PR fixes the issue by stripping the timeLeftBox field before doing
the comparison.

This PR fixes #21124.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2016/03/12 14:50:02
Showing 1 changed files
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"encoding/json"
5 5
 	"errors"
6 6
 	"reflect"
7
+	"strings"
7 8
 	"testing"
8 9
 
9 10
 	"github.com/docker/docker/pkg/jsonmessage"
... ...
@@ -84,9 +85,19 @@ func TestJSONFormatProgress(t *testing.T) {
84 84
 	if msg.Status != "action" {
85 85
 		t.Fatalf("Status must be 'action', got: %s", msg.Status)
86 86
 	}
87
-	if msg.ProgressMessage != progress.String() {
88
-		t.Fatalf("ProgressMessage must be %s, got: %s", progress.String(), msg.ProgressMessage)
87
+
88
+	// The progress will always be in the format of:
89
+	// [=========================>                         ]     15 B/30 B 404933h7m11s
90
+	// The last entry '404933h7m11s' is the timeLeftBox.
91
+	// However, the timeLeftBox field may change as progress.String() depends on time.Now().
92
+	// Therefore, we have to strip the timeLeftBox from the strings to do the comparison.
93
+
94
+	// Compare the progress strings before the timeLeftBox
95
+	expectedProgress := "[=========================>                         ]     15 B/30 B"
96
+	if !strings.HasPrefix(msg.ProgressMessage, expectedProgress) {
97
+		t.Fatalf("ProgressMessage without the timeLeftBox must be %s, got: %s", expectedProgress, msg.ProgressMessage)
89 98
 	}
99
+
90 100
 	if !reflect.DeepEqual(msg.Progress, progress) {
91 101
 		t.Fatal("Original progress not equals progress from FormatProgress")
92 102
 	}