Signed-off-by: Vincent Demeester <vincent@sbr.pm>
| ... | ... |
@@ -1,7 +1,14 @@ |
| 1 | 1 |
package jsonmessage |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
+ "bytes" |
|
| 5 |
+ "fmt" |
|
| 4 | 6 |
"testing" |
| 7 |
+ "time" |
|
| 8 |
+ |
|
| 9 |
+ "github.com/docker/docker/pkg/term" |
|
| 10 |
+ "github.com/docker/docker/pkg/timeutils" |
|
| 11 |
+ "strings" |
|
| 5 | 12 |
) |
| 6 | 13 |
|
| 7 | 14 |
func TestError(t *testing.T) {
|
| ... | ... |
@@ -23,16 +30,181 @@ func TestProgress(t *testing.T) {
|
| 23 | 23 |
t.Fatalf("Expected %q, got %q", expected, jp2.String())
|
| 24 | 24 |
} |
| 25 | 25 |
|
| 26 |
- expected = "[=========================> ] 50 B/100 B" |
|
| 27 |
- jp3 := JSONProgress{Current: 50, Total: 100}
|
|
| 28 |
- if jp3.String() != expected {
|
|
| 29 |
- t.Fatalf("Expected %q, got %q", expected, jp3.String())
|
|
| 26 |
+ expectedStart := "[==========> ] 20 B/100 B" |
|
| 27 |
+ jp3 := JSONProgress{Current: 20, Total: 100, Start: time.Now().Unix()}
|
|
| 28 |
+ // Just look at the start of the string |
|
| 29 |
+ // (the remaining time is really hard to test -_-) |
|
| 30 |
+ if jp3.String()[:len(expectedStart)] != expectedStart {
|
|
| 31 |
+ t.Fatalf("Expected to start with %q, got %q", expectedStart, jp3.String())
|
|
| 30 | 32 |
} |
| 31 | 33 |
|
| 32 |
- // this number can't be negetive gh#7136 |
|
| 33 |
- expected = "[==================================================>] 50 B/40 B" |
|
| 34 |
- jp4 := JSONProgress{Current: 50, Total: 40}
|
|
| 34 |
+ expected = "[=========================> ] 50 B/100 B" |
|
| 35 |
+ jp4 := JSONProgress{Current: 50, Total: 100}
|
|
| 35 | 36 |
if jp4.String() != expected {
|
| 36 | 37 |
t.Fatalf("Expected %q, got %q", expected, jp4.String())
|
| 37 | 38 |
} |
| 39 |
+ |
|
| 40 |
+ // this number can't be negative gh#7136 |
|
| 41 |
+ expected = "[==================================================>] 50 B/40 B" |
|
| 42 |
+ jp5 := JSONProgress{Current: 50, Total: 40}
|
|
| 43 |
+ if jp5.String() != expected {
|
|
| 44 |
+ t.Fatalf("Expected %q, got %q", expected, jp5.String())
|
|
| 45 |
+ } |
|
| 46 |
+} |
|
| 47 |
+ |
|
| 48 |
+func TestJSONMessageDisplay(t *testing.T) {
|
|
| 49 |
+ now := time.Now().Unix() |
|
| 50 |
+ messages := map[JSONMessage][]string{
|
|
| 51 |
+ // Empty |
|
| 52 |
+ JSONMessage{}: {"\n", "\n"},
|
|
| 53 |
+ // Status |
|
| 54 |
+ JSONMessage{
|
|
| 55 |
+ Status: "status", |
|
| 56 |
+ }: {
|
|
| 57 |
+ "status\n", |
|
| 58 |
+ "status\n", |
|
| 59 |
+ }, |
|
| 60 |
+ // General |
|
| 61 |
+ JSONMessage{
|
|
| 62 |
+ Time: now, |
|
| 63 |
+ ID: "ID", |
|
| 64 |
+ From: "From", |
|
| 65 |
+ Status: "status", |
|
| 66 |
+ }: {
|
|
| 67 |
+ fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now, 0).Format(timeutils.RFC3339NanoFixed)),
|
|
| 68 |
+ fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now, 0).Format(timeutils.RFC3339NanoFixed)),
|
|
| 69 |
+ }, |
|
| 70 |
+ // Stream over status |
|
| 71 |
+ JSONMessage{
|
|
| 72 |
+ Status: "status", |
|
| 73 |
+ Stream: "stream", |
|
| 74 |
+ }: {
|
|
| 75 |
+ "stream", |
|
| 76 |
+ "stream", |
|
| 77 |
+ }, |
|
| 78 |
+ // With progress message |
|
| 79 |
+ JSONMessage{
|
|
| 80 |
+ Status: "status", |
|
| 81 |
+ ProgressMessage: "progressMessage", |
|
| 82 |
+ }: {
|
|
| 83 |
+ "status progressMessage", |
|
| 84 |
+ "status progressMessage", |
|
| 85 |
+ }, |
|
| 86 |
+ // With progress, stream empty |
|
| 87 |
+ JSONMessage{
|
|
| 88 |
+ Status: "status", |
|
| 89 |
+ Stream: "", |
|
| 90 |
+ Progress: &JSONProgress{Current: 1},
|
|
| 91 |
+ }: {
|
|
| 92 |
+ "", |
|
| 93 |
+ fmt.Sprintf("%c[2K\rstatus 1 B\r", 27),
|
|
| 94 |
+ }, |
|
| 95 |
+ } |
|
| 96 |
+ |
|
| 97 |
+ // The tests :) |
|
| 98 |
+ for jsonMessage, expectedMessages := range messages {
|
|
| 99 |
+ // Without terminal |
|
| 100 |
+ data := bytes.NewBuffer([]byte{})
|
|
| 101 |
+ if err := jsonMessage.Display(data, false); err != nil {
|
|
| 102 |
+ t.Fatal(err) |
|
| 103 |
+ } |
|
| 104 |
+ if data.String() != expectedMessages[0] {
|
|
| 105 |
+ t.Fatalf("Expected [%v], got [%v]", expectedMessages[0], data.String())
|
|
| 106 |
+ } |
|
| 107 |
+ // With terminal |
|
| 108 |
+ data = bytes.NewBuffer([]byte{})
|
|
| 109 |
+ if err := jsonMessage.Display(data, true); err != nil {
|
|
| 110 |
+ t.Fatal(err) |
|
| 111 |
+ } |
|
| 112 |
+ if data.String() != expectedMessages[1] {
|
|
| 113 |
+ t.Fatalf("Expected [%v], got [%v]", expectedMessages[1], data.String())
|
|
| 114 |
+ } |
|
| 115 |
+ } |
|
| 116 |
+} |
|
| 117 |
+ |
|
| 118 |
+// Test JSONMessage with an Error. It will return an error with the text as error, not the meaning of the HTTP code. |
|
| 119 |
+func TestJSONMessageDisplayWithJSONError(t *testing.T) {
|
|
| 120 |
+ data := bytes.NewBuffer([]byte{})
|
|
| 121 |
+ jsonMessage := JSONMessage{Error: &JSONError{404, "Can't find it"}}
|
|
| 122 |
+ |
|
| 123 |
+ err := jsonMessage.Display(data, true) |
|
| 124 |
+ if err == nil || err.Error() != "Can't find it" {
|
|
| 125 |
+ t.Fatalf("Expected a JSONError 404, got [%v]", err)
|
|
| 126 |
+ } |
|
| 127 |
+ |
|
| 128 |
+ jsonMessage = JSONMessage{Error: &JSONError{401, "Anything"}}
|
|
| 129 |
+ err = jsonMessage.Display(data, true) |
|
| 130 |
+ if err == nil || err.Error() != "Authentication is required." {
|
|
| 131 |
+ t.Fatalf("Expected an error [Authentication is required.], got [%v]", err)
|
|
| 132 |
+ } |
|
| 133 |
+} |
|
| 134 |
+ |
|
| 135 |
+func TestDisplayJSONMessagesStreamInvalidJSON(t *testing.T) {
|
|
| 136 |
+ var ( |
|
| 137 |
+ inFd uintptr |
|
| 138 |
+ ) |
|
| 139 |
+ data := bytes.NewBuffer([]byte{})
|
|
| 140 |
+ reader := strings.NewReader("This is not a 'valid' JSON []")
|
|
| 141 |
+ inFd, _ = term.GetFdInfo(reader) |
|
| 142 |
+ |
|
| 143 |
+ if err := DisplayJSONMessagesStream(reader, data, inFd, false); err == nil && err.Error()[:17] != "invalid character" {
|
|
| 144 |
+ t.Fatalf("Should have thrown an error (invalid character in ..), got [%v]", err)
|
|
| 145 |
+ } |
|
| 146 |
+} |
|
| 147 |
+ |
|
| 148 |
+func TestDisplayJSONMessagesStream(t *testing.T) {
|
|
| 149 |
+ var ( |
|
| 150 |
+ inFd uintptr |
|
| 151 |
+ ) |
|
| 152 |
+ |
|
| 153 |
+ messages := map[string][]string{
|
|
| 154 |
+ // empty string |
|
| 155 |
+ "": {
|
|
| 156 |
+ "", |
|
| 157 |
+ ""}, |
|
| 158 |
+ // Without progress & ID |
|
| 159 |
+ "{ \"status\": \"status\" }": {
|
|
| 160 |
+ "status\n", |
|
| 161 |
+ "status\n", |
|
| 162 |
+ }, |
|
| 163 |
+ // Without progress, with ID |
|
| 164 |
+ "{ \"id\": \"ID\",\"status\": \"status\" }": {
|
|
| 165 |
+ "ID: status\n", |
|
| 166 |
+ fmt.Sprintf("ID: status\n%c[%dB", 27, 0),
|
|
| 167 |
+ }, |
|
| 168 |
+ // With progress |
|
| 169 |
+ "{ \"id\": \"ID\", \"status\": \"status\", \"progress\": \"ProgressMessage\" }": {
|
|
| 170 |
+ "ID: status ProgressMessage", |
|
| 171 |
+ fmt.Sprintf("\n%c[%dAID: status ProgressMessage%c[%dB", 27, 0, 27, 0),
|
|
| 172 |
+ }, |
|
| 173 |
+ // With progressDetail |
|
| 174 |
+ "{ \"id\": \"ID\", \"status\": \"status\", \"progressDetail\": { \"Current\": 1} }": {
|
|
| 175 |
+ "", // progressbar is disabled in non-terminal |
|
| 176 |
+ fmt.Sprintf("\n%c[%dA%c[2K\rID: status 1 B\r%c[%dB", 27, 0, 27, 27, 0),
|
|
| 177 |
+ }, |
|
| 178 |
+ } |
|
| 179 |
+ for jsonMessage, expectedMessages := range messages {
|
|
| 180 |
+ data := bytes.NewBuffer([]byte{})
|
|
| 181 |
+ reader := strings.NewReader(jsonMessage) |
|
| 182 |
+ inFd, _ = term.GetFdInfo(reader) |
|
| 183 |
+ |
|
| 184 |
+ // Without terminal |
|
| 185 |
+ if err := DisplayJSONMessagesStream(reader, data, inFd, false); err != nil {
|
|
| 186 |
+ t.Fatal(err) |
|
| 187 |
+ } |
|
| 188 |
+ if data.String() != expectedMessages[0] {
|
|
| 189 |
+ t.Fatalf("Expected an [%v], got [%v]", expectedMessages[0], data.String())
|
|
| 190 |
+ } |
|
| 191 |
+ |
|
| 192 |
+ // With terminal |
|
| 193 |
+ data = bytes.NewBuffer([]byte{})
|
|
| 194 |
+ reader = strings.NewReader(jsonMessage) |
|
| 195 |
+ if err := DisplayJSONMessagesStream(reader, data, inFd, true); err != nil {
|
|
| 196 |
+ t.Fatal(err) |
|
| 197 |
+ } |
|
| 198 |
+ if data.String() != expectedMessages[1] {
|
|
| 199 |
+ t.Fatalf("Expected an [%v], got [%v]", expectedMessages[1], data.String())
|
|
| 200 |
+ } |
|
| 201 |
+ } |
|
| 202 |
+ |
|
| 38 | 203 |
} |