github.com/kobeld/docker@v1.12.0-rc1/api/client/formatter/formatter_test.go (about) 1 package formatter 2 3 import ( 4 "bytes" 5 "fmt" 6 "testing" 7 "time" 8 9 "github.com/docker/engine-api/types" 10 ) 11 12 func TestContainerContextWrite(t *testing.T) { 13 unixTime := time.Now().AddDate(0, 0, -1).Unix() 14 expectedTime := time.Unix(unixTime, 0).String() 15 16 contexts := []struct { 17 context ContainerContext 18 expected string 19 }{ 20 // Errors 21 { 22 ContainerContext{ 23 Context: Context{ 24 Format: "{{InvalidFunction}}", 25 }, 26 }, 27 `Template parsing error: template: :1: function "InvalidFunction" not defined 28 `, 29 }, 30 { 31 ContainerContext{ 32 Context: Context{ 33 Format: "{{nil}}", 34 }, 35 }, 36 `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command 37 `, 38 }, 39 // Table Format 40 { 41 ContainerContext{ 42 Context: Context{ 43 Format: "table", 44 }, 45 }, 46 `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 47 containerID1 ubuntu "" 24 hours ago foobar_baz 48 containerID2 ubuntu "" 24 hours ago foobar_bar 49 `, 50 }, 51 { 52 ContainerContext{ 53 Context: Context{ 54 Format: "table {{.Image}}", 55 }, 56 }, 57 "IMAGE\nubuntu\nubuntu\n", 58 }, 59 { 60 ContainerContext{ 61 Context: Context{ 62 Format: "table {{.Image}}", 63 }, 64 Size: true, 65 }, 66 "IMAGE\nubuntu\nubuntu\n", 67 }, 68 { 69 ContainerContext{ 70 Context: Context{ 71 Format: "table {{.Image}}", 72 Quiet: true, 73 }, 74 }, 75 "IMAGE\nubuntu\nubuntu\n", 76 }, 77 { 78 ContainerContext{ 79 Context: Context{ 80 Format: "table", 81 Quiet: true, 82 }, 83 }, 84 "containerID1\ncontainerID2\n", 85 }, 86 // Raw Format 87 { 88 ContainerContext{ 89 Context: Context{ 90 Format: "raw", 91 }, 92 }, 93 fmt.Sprintf(`container_id: containerID1 94 image: ubuntu 95 command: "" 96 created_at: %s 97 status: 98 names: foobar_baz 99 labels: 100 ports: 101 102 container_id: containerID2 103 image: ubuntu 104 command: "" 105 created_at: %s 106 status: 107 names: foobar_bar 108 labels: 109 ports: 110 111 `, expectedTime, expectedTime), 112 }, 113 { 114 ContainerContext{ 115 Context: Context{ 116 Format: "raw", 117 }, 118 Size: true, 119 }, 120 fmt.Sprintf(`container_id: containerID1 121 image: ubuntu 122 command: "" 123 created_at: %s 124 status: 125 names: foobar_baz 126 labels: 127 ports: 128 size: 0 B 129 130 container_id: containerID2 131 image: ubuntu 132 command: "" 133 created_at: %s 134 status: 135 names: foobar_bar 136 labels: 137 ports: 138 size: 0 B 139 140 `, expectedTime, expectedTime), 141 }, 142 { 143 ContainerContext{ 144 Context: Context{ 145 Format: "raw", 146 Quiet: true, 147 }, 148 }, 149 "container_id: containerID1\ncontainer_id: containerID2\n", 150 }, 151 // Custom Format 152 { 153 ContainerContext{ 154 Context: Context{ 155 Format: "{{.Image}}", 156 }, 157 }, 158 "ubuntu\nubuntu\n", 159 }, 160 { 161 ContainerContext{ 162 Context: Context{ 163 Format: "{{.Image}}", 164 }, 165 Size: true, 166 }, 167 "ubuntu\nubuntu\n", 168 }, 169 } 170 171 for _, context := range contexts { 172 containers := []types.Container{ 173 {ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unixTime}, 174 {ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unixTime}, 175 } 176 out := bytes.NewBufferString("") 177 context.context.Output = out 178 context.context.Containers = containers 179 context.context.Write() 180 actual := out.String() 181 if actual != context.expected { 182 t.Fatalf("Expected \n%s, got \n%s", context.expected, actual) 183 } 184 // Clean buffer 185 out.Reset() 186 } 187 } 188 189 func TestContainerContextWriteWithNoContainers(t *testing.T) { 190 out := bytes.NewBufferString("") 191 containers := []types.Container{} 192 193 contexts := []struct { 194 context ContainerContext 195 expected string 196 }{ 197 { 198 ContainerContext{ 199 Context: Context{ 200 Format: "{{.Image}}", 201 Output: out, 202 }, 203 }, 204 "", 205 }, 206 { 207 ContainerContext{ 208 Context: Context{ 209 Format: "table {{.Image}}", 210 Output: out, 211 }, 212 }, 213 "IMAGE\n", 214 }, 215 { 216 ContainerContext{ 217 Context: Context{ 218 Format: "{{.Image}}", 219 Output: out, 220 }, 221 Size: true, 222 }, 223 "", 224 }, 225 { 226 ContainerContext{ 227 Context: Context{ 228 Format: "table {{.Image}}", 229 Output: out, 230 }, 231 Size: true, 232 }, 233 "IMAGE\n", 234 }, 235 { 236 ContainerContext{ 237 Context: Context{ 238 Format: "table {{.Image}}\t{{.Size}}", 239 Output: out, 240 }, 241 }, 242 "IMAGE SIZE\n", 243 }, 244 { 245 ContainerContext{ 246 Context: Context{ 247 Format: "table {{.Image}}\t{{.Size}}", 248 Output: out, 249 }, 250 Size: true, 251 }, 252 "IMAGE SIZE\n", 253 }, 254 } 255 256 for _, context := range contexts { 257 context.context.Containers = containers 258 context.context.Write() 259 actual := out.String() 260 if actual != context.expected { 261 t.Fatalf("Expected \n%s, got \n%s", context.expected, actual) 262 } 263 // Clean buffer 264 out.Reset() 265 } 266 } 267 268 func TestImageContextWrite(t *testing.T) { 269 unixTime := time.Now().AddDate(0, 0, -1).Unix() 270 expectedTime := time.Unix(unixTime, 0).String() 271 272 contexts := []struct { 273 context ImageContext 274 expected string 275 }{ 276 // Errors 277 { 278 ImageContext{ 279 Context: Context{ 280 Format: "{{InvalidFunction}}", 281 }, 282 }, 283 `Template parsing error: template: :1: function "InvalidFunction" not defined 284 `, 285 }, 286 { 287 ImageContext{ 288 Context: Context{ 289 Format: "{{nil}}", 290 }, 291 }, 292 `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command 293 `, 294 }, 295 // Table Format 296 { 297 ImageContext{ 298 Context: Context{ 299 Format: "table", 300 }, 301 }, 302 `REPOSITORY TAG IMAGE ID CREATED SIZE 303 image tag1 imageID1 24 hours ago 0 B 304 image <none> imageID1 24 hours ago 0 B 305 image tag2 imageID2 24 hours ago 0 B 306 <none> <none> imageID3 24 hours ago 0 B 307 `, 308 }, 309 { 310 ImageContext{ 311 Context: Context{ 312 Format: "table {{.Repository}}", 313 }, 314 }, 315 "REPOSITORY\nimage\nimage\nimage\n<none>\n", 316 }, 317 { 318 ImageContext{ 319 Context: Context{ 320 Format: "table {{.Repository}}", 321 }, 322 Digest: true, 323 }, 324 `REPOSITORY DIGEST 325 image <none> 326 image sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf 327 image <none> 328 <none> <none> 329 `, 330 }, 331 { 332 ImageContext{ 333 Context: Context{ 334 Format: "table {{.Repository}}", 335 Quiet: true, 336 }, 337 }, 338 "REPOSITORY\nimage\nimage\nimage\n<none>\n", 339 }, 340 { 341 ImageContext{ 342 Context: Context{ 343 Format: "table", 344 Quiet: true, 345 }, 346 }, 347 "imageID1\nimageID1\nimageID2\nimageID3\n", 348 }, 349 { 350 ImageContext{ 351 Context: Context{ 352 Format: "table", 353 Quiet: false, 354 }, 355 Digest: true, 356 }, 357 `REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE 358 image tag1 <none> imageID1 24 hours ago 0 B 359 image <none> sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf imageID1 24 hours ago 0 B 360 image tag2 <none> imageID2 24 hours ago 0 B 361 <none> <none> <none> imageID3 24 hours ago 0 B 362 `, 363 }, 364 { 365 ImageContext{ 366 Context: Context{ 367 Format: "table", 368 Quiet: true, 369 }, 370 Digest: true, 371 }, 372 "imageID1\nimageID1\nimageID2\nimageID3\n", 373 }, 374 // Raw Format 375 { 376 ImageContext{ 377 Context: Context{ 378 Format: "raw", 379 }, 380 }, 381 fmt.Sprintf(`repository: image 382 tag: tag1 383 image_id: imageID1 384 created_at: %s 385 virtual_size: 0 B 386 387 repository: image 388 tag: <none> 389 image_id: imageID1 390 created_at: %s 391 virtual_size: 0 B 392 393 repository: image 394 tag: tag2 395 image_id: imageID2 396 created_at: %s 397 virtual_size: 0 B 398 399 repository: <none> 400 tag: <none> 401 image_id: imageID3 402 created_at: %s 403 virtual_size: 0 B 404 405 `, expectedTime, expectedTime, expectedTime, expectedTime), 406 }, 407 { 408 ImageContext{ 409 Context: Context{ 410 Format: "raw", 411 }, 412 Digest: true, 413 }, 414 fmt.Sprintf(`repository: image 415 tag: tag1 416 digest: <none> 417 image_id: imageID1 418 created_at: %s 419 virtual_size: 0 B 420 421 repository: image 422 tag: <none> 423 digest: sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf 424 image_id: imageID1 425 created_at: %s 426 virtual_size: 0 B 427 428 repository: image 429 tag: tag2 430 digest: <none> 431 image_id: imageID2 432 created_at: %s 433 virtual_size: 0 B 434 435 repository: <none> 436 tag: <none> 437 digest: <none> 438 image_id: imageID3 439 created_at: %s 440 virtual_size: 0 B 441 442 `, expectedTime, expectedTime, expectedTime, expectedTime), 443 }, 444 { 445 ImageContext{ 446 Context: Context{ 447 Format: "raw", 448 Quiet: true, 449 }, 450 }, 451 `image_id: imageID1 452 image_id: imageID1 453 image_id: imageID2 454 image_id: imageID3 455 `, 456 }, 457 // Custom Format 458 { 459 ImageContext{ 460 Context: Context{ 461 Format: "{{.Repository}}", 462 }, 463 }, 464 "image\nimage\nimage\n<none>\n", 465 }, 466 { 467 ImageContext{ 468 Context: Context{ 469 Format: "{{.Repository}}", 470 }, 471 Digest: true, 472 }, 473 "image\nimage\nimage\n<none>\n", 474 }, 475 } 476 477 for _, context := range contexts { 478 images := []types.Image{ 479 {ID: "imageID1", RepoTags: []string{"image:tag1"}, RepoDigests: []string{"image@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf"}, Created: unixTime}, 480 {ID: "imageID2", RepoTags: []string{"image:tag2"}, Created: unixTime}, 481 {ID: "imageID3", RepoTags: []string{"<none>:<none>"}, RepoDigests: []string{"<none>@<none>"}, Created: unixTime}, 482 } 483 out := bytes.NewBufferString("") 484 context.context.Output = out 485 context.context.Images = images 486 context.context.Write() 487 actual := out.String() 488 if actual != context.expected { 489 t.Fatalf("Expected \n%s, got \n%s", context.expected, actual) 490 } 491 // Clean buffer 492 out.Reset() 493 } 494 } 495 496 func TestImageContextWriteWithNoImage(t *testing.T) { 497 out := bytes.NewBufferString("") 498 images := []types.Image{} 499 500 contexts := []struct { 501 context ImageContext 502 expected string 503 }{ 504 { 505 ImageContext{ 506 Context: Context{ 507 Format: "{{.Repository}}", 508 Output: out, 509 }, 510 }, 511 "", 512 }, 513 { 514 ImageContext{ 515 Context: Context{ 516 Format: "table {{.Repository}}", 517 Output: out, 518 }, 519 }, 520 "REPOSITORY\n", 521 }, 522 { 523 ImageContext{ 524 Context: Context{ 525 Format: "{{.Repository}}", 526 Output: out, 527 }, 528 Digest: true, 529 }, 530 "", 531 }, 532 { 533 ImageContext{ 534 Context: Context{ 535 Format: "table {{.Repository}}", 536 Output: out, 537 }, 538 Digest: true, 539 }, 540 "REPOSITORY DIGEST\n", 541 }, 542 } 543 544 for _, context := range contexts { 545 context.context.Images = images 546 context.context.Write() 547 actual := out.String() 548 if actual != context.expected { 549 t.Fatalf("Expected \n%s, got \n%s", context.expected, actual) 550 } 551 // Clean buffer 552 out.Reset() 553 } 554 }