/usr/share/gocode/src/github.com/fsouza/go-dockerclient/image_test.go is in golang-github-fsouza-go-dockerclient-dev 0.0+git20150905-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 | // Copyright 2015 go-dockerclient authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package docker
import (
"bytes"
"encoding/base64"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"os"
"reflect"
"strings"
"testing"
"time"
)
func newTestClient(rt *FakeRoundTripper) Client {
endpoint := "http://localhost:4243"
u, _ := parseEndpoint("http://localhost:4243", false)
testAPIVersion, _ := NewAPIVersion("1.17")
client := Client{
HTTPClient: &http.Client{Transport: rt},
endpoint: endpoint,
endpointURL: u,
SkipServerVersionCheck: true,
serverAPIVersion: testAPIVersion,
}
return client
}
type stdoutMock struct {
*bytes.Buffer
}
func (m stdoutMock) Close() error {
return nil
}
type stdinMock struct {
*bytes.Buffer
}
func (m stdinMock) Close() error {
return nil
}
func TestListImages(t *testing.T) {
body := `[
{
"Repository":"base",
"Tag":"ubuntu-12.10",
"Id":"b750fe79269d",
"Created":1364102658
},
{
"Repository":"base",
"Tag":"ubuntu-quantal",
"Id":"b750fe79269d",
"Created":1364102658
},
{
"RepoTag": [
"ubuntu:12.04",
"ubuntu:precise",
"ubuntu:latest"
],
"Id": "8dbd9e392a964c",
"Created": 1365714795,
"Size": 131506275,
"VirtualSize": 131506275
},
{
"RepoTag": [
"ubuntu:12.10",
"ubuntu:quantal"
],
"ParentId": "27cf784147099545",
"Id": "b750fe79269d2e",
"Created": 1364102658,
"Size": 24653,
"VirtualSize": 180116135
}
]`
var expected []APIImages
err := json.Unmarshal([]byte(body), &expected)
if err != nil {
t.Fatal(err)
}
client := newTestClient(&FakeRoundTripper{message: body, status: http.StatusOK})
images, err := client.ListImages(ListImagesOptions{})
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(images, expected) {
t.Errorf("ListImages: Wrong return value. Want %#v. Got %#v.", expected, images)
}
}
func TestListImagesParameters(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "null", status: http.StatusOK}
client := newTestClient(fakeRT)
_, err := client.ListImages(ListImagesOptions{All: false})
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
if req.Method != "GET" {
t.Errorf("ListImages({All: false}: Wrong HTTP method. Want GET. Got %s.", req.Method)
}
if all := req.URL.Query().Get("all"); all != "0" && all != "" {
t.Errorf("ListImages({All: false}): Wrong parameter. Want all=0 or not present at all. Got all=%s", all)
}
fakeRT.Reset()
_, err = client.ListImages(ListImagesOptions{All: true})
if err != nil {
t.Fatal(err)
}
req = fakeRT.requests[0]
if all := req.URL.Query().Get("all"); all != "1" {
t.Errorf("ListImages({All: true}): Wrong parameter. Want all=1. Got all=%s", all)
}
fakeRT.Reset()
_, err = client.ListImages(ListImagesOptions{Filters: map[string][]string{
"dangling": {"true"},
}})
if err != nil {
t.Fatal(err)
}
req = fakeRT.requests[0]
body := req.URL.Query().Get("filters")
var filters map[string][]string
err = json.Unmarshal([]byte(body), &filters)
if err != nil {
t.Fatal(err)
}
if len(filters["dangling"]) != 1 || filters["dangling"][0] != "true" {
t.Errorf("ListImages(dangling=[true]): Wrong filter map. Want dangling=[true], got dangling=%v", filters["dangling"])
}
}
func TestImageHistory(t *testing.T) {
body := `[
{
"Id": "25daec02219d2d852f7526137213a9b199926b4b24e732eab5b8bc6c49bd470e",
"Tags": [
"debian:7.6",
"debian:latest",
"debian:7",
"debian:wheezy"
],
"Created": 1409856216,
"CreatedBy": "/bin/sh -c #(nop) CMD [/bin/bash]"
},
{
"Id": "41026a5347fb5be6ed16115bf22df8569697139f246186de9ae8d4f67c335dce",
"Created": 1409856213,
"CreatedBy": "/bin/sh -c #(nop) ADD file:1ee9e97209d00e3416a4543b23574cc7259684741a46bbcbc755909b8a053a38 in /",
"Size": 85178663
},
{
"Id": "511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158",
"Tags": [
"scratch:latest"
],
"Created": 1371157430
}
]`
var expected []ImageHistory
err := json.Unmarshal([]byte(body), &expected)
if err != nil {
t.Fatal(err)
}
client := newTestClient(&FakeRoundTripper{message: body, status: http.StatusOK})
history, err := client.ImageHistory("debian:latest")
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(history, expected) {
t.Errorf("ImageHistory: Wrong return value. Want %#v. Got %#v.", expected, history)
}
}
func TestRemoveImage(t *testing.T) {
name := "test"
fakeRT := &FakeRoundTripper{message: "", status: http.StatusNoContent}
client := newTestClient(fakeRT)
err := client.RemoveImage(name)
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expectedMethod := "DELETE"
if req.Method != expectedMethod {
t.Errorf("RemoveImage(%q): Wrong HTTP method. Want %s. Got %s.", name, expectedMethod, req.Method)
}
u, _ := url.Parse(client.getURL("/images/" + name))
if req.URL.Path != u.Path {
t.Errorf("RemoveImage(%q): Wrong request path. Want %q. Got %q.", name, u.Path, req.URL.Path)
}
}
func TestRemoveImageNotFound(t *testing.T) {
client := newTestClient(&FakeRoundTripper{message: "no such image", status: http.StatusNotFound})
err := client.RemoveImage("test:")
if err != ErrNoSuchImage {
t.Errorf("RemoveImage: wrong error. Want %#v. Got %#v.", ErrNoSuchImage, err)
}
}
func TestRemoveImageExtended(t *testing.T) {
name := "test"
fakeRT := &FakeRoundTripper{message: "", status: http.StatusNoContent}
client := newTestClient(fakeRT)
err := client.RemoveImageExtended(name, RemoveImageOptions{Force: true, NoPrune: true})
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expectedMethod := "DELETE"
if req.Method != expectedMethod {
t.Errorf("RemoveImage(%q): Wrong HTTP method. Want %s. Got %s.", name, expectedMethod, req.Method)
}
u, _ := url.Parse(client.getURL("/images/" + name))
if req.URL.Path != u.Path {
t.Errorf("RemoveImage(%q): Wrong request path. Want %q. Got %q.", name, u.Path, req.URL.Path)
}
expectedQuery := "force=1&noprune=1"
if query := req.URL.Query().Encode(); query != expectedQuery {
t.Errorf("PushImage: Wrong query string. Want %q. Got %q.", expectedQuery, query)
}
}
func TestInspectImage(t *testing.T) {
body := `{
"Id":"b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc",
"Parent":"27cf784147099545",
"Created":"2013-03-23T22:24:18.818426Z",
"Container":"3d67245a8d72ecf13f33dffac9f79dcdf70f75acb84d308770391510e0c23ad0",
"ContainerConfig":{"Memory":1},
"VirtualSize":12345
}`
created, err := time.Parse(time.RFC3339Nano, "2013-03-23T22:24:18.818426Z")
if err != nil {
t.Fatal(err)
}
expected := Image{
ID: "b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc",
Parent: "27cf784147099545",
Created: created,
Container: "3d67245a8d72ecf13f33dffac9f79dcdf70f75acb84d308770391510e0c23ad0",
ContainerConfig: Config{
Memory: 1,
},
VirtualSize: 12345,
}
fakeRT := &FakeRoundTripper{message: body, status: http.StatusOK}
client := newTestClient(fakeRT)
image, err := client.InspectImage(expected.ID)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(*image, expected) {
t.Errorf("InspectImage(%q): Wrong image returned. Want %#v. Got %#v.", expected.ID, expected, *image)
}
req := fakeRT.requests[0]
if req.Method != "GET" {
t.Errorf("InspectImage(%q): Wrong HTTP method. Want GET. Got %s.", expected.ID, req.Method)
}
u, _ := url.Parse(client.getURL("/images/" + expected.ID + "/json"))
if req.URL.Path != u.Path {
t.Errorf("InspectImage(%q): Wrong request URL. Want %q. Got %q.", expected.ID, u.Path, req.URL.Path)
}
}
func TestInspectImageNotFound(t *testing.T) {
client := newTestClient(&FakeRoundTripper{message: "no such image", status: http.StatusNotFound})
name := "test"
image, err := client.InspectImage(name)
if image != nil {
t.Errorf("InspectImage(%q): expected <nil> image, got %#v.", name, image)
}
if err != ErrNoSuchImage {
t.Errorf("InspectImage(%q): wrong error. Want %#v. Got %#v.", name, ErrNoSuchImage, err)
}
}
func TestPushImage(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "Pushing 1/100", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
err := client.PushImage(PushImageOptions{Name: "test", OutputStream: &buf}, AuthConfiguration{})
if err != nil {
t.Fatal(err)
}
expected := "Pushing 1/100"
if buf.String() != expected {
t.Errorf("PushImage: Wrong output. Want %q. Got %q.", expected, buf.String())
}
req := fakeRT.requests[0]
if req.Method != "POST" {
t.Errorf("PushImage: Wrong HTTP method. Want POST. Got %s.", req.Method)
}
u, _ := url.Parse(client.getURL("/images/test/push"))
if req.URL.Path != u.Path {
t.Errorf("PushImage: Wrong request path. Want %q. Got %q.", u.Path, req.URL.Path)
}
if query := req.URL.Query().Encode(); query != "" {
t.Errorf("PushImage: Wrong query string. Want no parameters, got %q.", query)
}
auth, err := base64.URLEncoding.DecodeString(req.Header.Get("X-Registry-Auth"))
if err != nil {
t.Errorf("PushImage: caught error decoding auth. %#v", err.Error())
}
if strings.TrimSpace(string(auth)) != "{}" {
t.Errorf("PushImage: wrong body. Want %q. Got %q.",
base64.URLEncoding.EncodeToString([]byte("{}")), req.Header.Get("X-Registry-Auth"))
}
}
func TestPushImageWithRawJSON(t *testing.T) {
body := `
{"status":"Pushing..."}
{"status":"Pushing", "progress":"1/? (n/a)", "progressDetail":{"current":1}}}
{"status":"Image successfully pushed"}
`
fakeRT := &FakeRoundTripper{
message: body,
status: http.StatusOK,
header: map[string]string{
"Content-Type": "application/json",
},
}
client := newTestClient(fakeRT)
var buf bytes.Buffer
err := client.PushImage(PushImageOptions{
Name: "test",
OutputStream: &buf,
RawJSONStream: true,
}, AuthConfiguration{})
if err != nil {
t.Fatal(err)
}
if buf.String() != body {
t.Errorf("PushImage: Wrong raw output. Want %q. Got %q.", body, buf.String())
}
}
func TestPushImageWithAuthentication(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "Pushing 1/100", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
inputAuth := AuthConfiguration{
Username: "gopher",
Password: "gopher123",
Email: "gopher@tsuru.io",
}
err := client.PushImage(PushImageOptions{Name: "test", OutputStream: &buf}, inputAuth)
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
var gotAuth AuthConfiguration
auth, err := base64.URLEncoding.DecodeString(req.Header.Get("X-Registry-Auth"))
if err != nil {
t.Errorf("PushImage: caught error decoding auth. %#v", err.Error())
}
err = json.Unmarshal(auth, &gotAuth)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(gotAuth, inputAuth) {
t.Errorf("PushImage: wrong auth configuration. Want %#v. Got %#v.", inputAuth, gotAuth)
}
}
func TestPushImageCustomRegistry(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "Pushing 1/100", status: http.StatusOK}
client := newTestClient(fakeRT)
var authConfig AuthConfiguration
var buf bytes.Buffer
opts := PushImageOptions{
Name: "test", Registry: "docker.tsuru.io",
OutputStream: &buf,
}
err := client.PushImage(opts, authConfig)
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expectedQuery := "registry=docker.tsuru.io"
if query := req.URL.Query().Encode(); query != expectedQuery {
t.Errorf("PushImage: Wrong query string. Want %q. Got %q.", expectedQuery, query)
}
}
func TestPushImageNoName(t *testing.T) {
client := Client{}
err := client.PushImage(PushImageOptions{}, AuthConfiguration{})
if err != ErrNoSuchImage {
t.Errorf("PushImage: got wrong error. Want %#v. Got %#v.", ErrNoSuchImage, err)
}
}
func TestPullImage(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "Pulling 1/100", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
err := client.PullImage(PullImageOptions{Repository: "base", OutputStream: &buf},
AuthConfiguration{})
if err != nil {
t.Fatal(err)
}
expected := "Pulling 1/100"
if buf.String() != expected {
t.Errorf("PullImage: Wrong output. Want %q. Got %q.", expected, buf.String())
}
req := fakeRT.requests[0]
if req.Method != "POST" {
t.Errorf("PullImage: Wrong HTTP method. Want POST. Got %s.", req.Method)
}
u, _ := url.Parse(client.getURL("/images/create"))
if req.URL.Path != u.Path {
t.Errorf("PullImage: Wrong request path. Want %q. Got %q.", u.Path, req.URL.Path)
}
expectedQuery := "fromImage=base"
if query := req.URL.Query().Encode(); query != expectedQuery {
t.Errorf("PullImage: Wrong query strin. Want %q. Got %q.", expectedQuery, query)
}
}
func TestPullImageWithRawJSON(t *testing.T) {
body := `
{"status":"Pulling..."}
{"status":"Pulling", "progress":"1 B/ 100 B", "progressDetail":{"current":1, "total":100}}
`
fakeRT := &FakeRoundTripper{
message: body,
status: http.StatusOK,
header: map[string]string{
"Content-Type": "application/json",
},
}
client := newTestClient(fakeRT)
var buf bytes.Buffer
err := client.PullImage(PullImageOptions{
Repository: "base",
OutputStream: &buf,
RawJSONStream: true,
}, AuthConfiguration{})
if err != nil {
t.Fatal(err)
}
if buf.String() != body {
t.Errorf("PullImage: Wrong raw output. Want %q. Got %q", body, buf.String())
}
}
func TestPullImageWithoutOutputStream(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "Pulling 1/100", status: http.StatusOK}
client := newTestClient(fakeRT)
opts := PullImageOptions{
Repository: "base",
Registry: "docker.tsuru.io",
}
err := client.PullImage(opts, AuthConfiguration{})
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expected := map[string][]string{"fromImage": {"base"}, "registry": {"docker.tsuru.io"}}
got := map[string][]string(req.URL.Query())
if !reflect.DeepEqual(got, expected) {
t.Errorf("PullImage: wrong query string. Want %#v. Got %#v.", expected, got)
}
}
func TestPullImageCustomRegistry(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "Pulling 1/100", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
opts := PullImageOptions{
Repository: "base",
Registry: "docker.tsuru.io",
OutputStream: &buf,
}
err := client.PullImage(opts, AuthConfiguration{})
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expected := map[string][]string{"fromImage": {"base"}, "registry": {"docker.tsuru.io"}}
got := map[string][]string(req.URL.Query())
if !reflect.DeepEqual(got, expected) {
t.Errorf("PullImage: wrong query string. Want %#v. Got %#v.", expected, got)
}
}
func TestPullImageTag(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "Pulling 1/100", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
opts := PullImageOptions{
Repository: "base",
Registry: "docker.tsuru.io",
Tag: "latest",
OutputStream: &buf,
}
err := client.PullImage(opts, AuthConfiguration{})
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expected := map[string][]string{"fromImage": {"base"}, "registry": {"docker.tsuru.io"}, "tag": {"latest"}}
got := map[string][]string(req.URL.Query())
if !reflect.DeepEqual(got, expected) {
t.Errorf("PullImage: wrong query string. Want %#v. Got %#v.", expected, got)
}
}
func TestPullImageNoRepository(t *testing.T) {
var opts PullImageOptions
client := Client{}
err := client.PullImage(opts, AuthConfiguration{})
if err != ErrNoSuchImage {
t.Errorf("PullImage: got wrong error. Want %#v. Got %#v.", ErrNoSuchImage, err)
}
}
func TestImportImageFromUrl(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
opts := ImportImageOptions{
Source: "http://mycompany.com/file.tar",
Repository: "testimage",
Tag: "tag",
OutputStream: &buf,
}
err := client.ImportImage(opts)
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expected := map[string][]string{"fromSrc": {opts.Source}, "repo": {opts.Repository}, "tag": {opts.Tag}}
got := map[string][]string(req.URL.Query())
if !reflect.DeepEqual(got, expected) {
t.Errorf("ImportImage: wrong query string. Want %#v. Got %#v.", expected, got)
}
}
func TestImportImageFromInput(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
in := bytes.NewBufferString("tar content")
var buf bytes.Buffer
opts := ImportImageOptions{
Source: "-", Repository: "testimage",
InputStream: in, OutputStream: &buf,
Tag: "tag",
}
err := client.ImportImage(opts)
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expected := map[string][]string{"fromSrc": {opts.Source}, "repo": {opts.Repository}, "tag": {opts.Tag}}
got := map[string][]string(req.URL.Query())
if !reflect.DeepEqual(got, expected) {
t.Errorf("ImportImage: wrong query string. Want %#v. Got %#v.", expected, got)
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Errorf("ImportImage: caugth error while reading body %#v", err.Error())
}
e := "tar content"
if string(body) != e {
t.Errorf("ImportImage: wrong body. Want %#v. Got %#v.", e, string(body))
}
}
func TestImportImageDoesNotPassesInputIfSourceIsNotDash(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
in := bytes.NewBufferString("foo")
opts := ImportImageOptions{
Source: "http://test.com/container.tar", Repository: "testimage",
InputStream: in, OutputStream: &buf,
}
err := client.ImportImage(opts)
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expected := map[string][]string{"fromSrc": {opts.Source}, "repo": {opts.Repository}}
got := map[string][]string(req.URL.Query())
if !reflect.DeepEqual(got, expected) {
t.Errorf("ImportImage: wrong query string. Want %#v. Got %#v.", expected, got)
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Errorf("ImportImage: caugth error while reading body %#v", err.Error())
}
if string(body) != "" {
t.Errorf("ImportImage: wrong body. Want nothing. Got %#v.", string(body))
}
}
func TestImportImageShouldPassTarContentToBodyWhenSourceIsFilePath(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
tarPath := "testing/data/container.tar"
opts := ImportImageOptions{
Source: tarPath, Repository: "testimage",
OutputStream: &buf,
}
err := client.ImportImage(opts)
if err != nil {
t.Fatal(err)
}
tar, err := os.Open(tarPath)
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
tarContent, err := ioutil.ReadAll(tar)
body, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(tarContent, body) {
t.Errorf("ImportImage: wrong body. Want %#v content. Got %#v.", tarPath, body)
}
}
func TestImportImageShouldChangeSourceToDashWhenItsAFilePath(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
tarPath := "testing/data/container.tar"
opts := ImportImageOptions{
Source: tarPath, Repository: "testimage",
OutputStream: &buf,
}
err := client.ImportImage(opts)
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expected := map[string][]string{"fromSrc": {"-"}, "repo": {opts.Repository}}
got := map[string][]string(req.URL.Query())
if !reflect.DeepEqual(got, expected) {
t.Errorf("ImportImage: wrong query string. Want %#v. Got %#v.", expected, got)
}
}
func TestBuildImageParameters(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
opts := BuildImageOptions{
Name: "testImage",
NoCache: true,
SuppressOutput: true,
Pull: true,
RmTmpContainer: true,
ForceRmTmpContainer: true,
Memory: 1024,
Memswap: 2048,
CPUShares: 10,
CPUSetCPUs: "0-3",
InputStream: &buf,
OutputStream: &buf,
}
err := client.BuildImage(opts)
if err != nil && strings.Index(err.Error(), "build image fail") == -1 {
t.Fatal(err)
}
req := fakeRT.requests[0]
expected := map[string][]string{
"t": {opts.Name},
"nocache": {"1"},
"q": {"1"},
"pull": {"1"},
"rm": {"1"},
"forcerm": {"1"},
"memory": {"1024"},
"memswap": {"2048"},
"cpushares": {"10"},
"cpusetcpus": {"0-3"},
}
got := map[string][]string(req.URL.Query())
if !reflect.DeepEqual(got, expected) {
t.Errorf("BuildImage: wrong query string. Want %#v. Got %#v.", expected, got)
}
}
func TestBuildImageParametersForRemoteBuild(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
opts := BuildImageOptions{
Name: "testImage",
Remote: "testing/data/container.tar",
SuppressOutput: true,
OutputStream: &buf,
}
err := client.BuildImage(opts)
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expected := map[string][]string{"t": {opts.Name}, "remote": {opts.Remote}, "q": {"1"}}
got := map[string][]string(req.URL.Query())
if !reflect.DeepEqual(got, expected) {
t.Errorf("BuildImage: wrong query string. Want %#v. Got %#v.", expected, got)
}
}
func TestBuildImageMissingRepoAndNilInput(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
opts := BuildImageOptions{
Name: "testImage",
SuppressOutput: true,
OutputStream: &buf,
}
err := client.BuildImage(opts)
if err != ErrMissingRepo {
t.Errorf("BuildImage: wrong error returned. Want %#v. Got %#v.", ErrMissingRepo, err)
}
}
func TestBuildImageMissingOutputStream(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
opts := BuildImageOptions{Name: "testImage"}
err := client.BuildImage(opts)
if err != ErrMissingOutputStream {
t.Errorf("BuildImage: wrong error returned. Want %#v. Got %#v.", ErrMissingOutputStream, err)
}
}
func TestBuildImageWithRawJSON(t *testing.T) {
body := `
{"stream":"Step 0 : FROM ubuntu:latest\n"}
{"stream":" ---\u003e 4300eb9d3c8d\n"}
{"stream":"Step 1 : MAINTAINER docker <eng@docker.com>\n"}
{"stream":" ---\u003e Using cache\n"}
{"stream":" ---\u003e 3a3ed758c370\n"}
{"stream":"Step 2 : CMD /usr/bin/top\n"}
{"stream":" ---\u003e Running in 36b1479cc2e4\n"}
{"stream":" ---\u003e 4b6188aebe39\n"}
{"stream":"Removing intermediate container 36b1479cc2e4\n"}
{"stream":"Successfully built 4b6188aebe39\n"}
`
fakeRT := &FakeRoundTripper{
message: body,
status: http.StatusOK,
header: map[string]string{
"Content-Type": "application/json",
},
}
client := newTestClient(fakeRT)
var buf bytes.Buffer
opts := BuildImageOptions{
Name: "testImage",
RmTmpContainer: true,
InputStream: &buf,
OutputStream: &buf,
RawJSONStream: true,
}
err := client.BuildImage(opts)
if err != nil {
t.Fatal(err)
}
if buf.String() != body {
t.Errorf("BuildImage: Wrong raw output. Want %q. Got %q.", body, buf.String())
}
}
func TestBuildImageRemoteWithoutName(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
opts := BuildImageOptions{
Remote: "testing/data/container.tar",
SuppressOutput: true,
OutputStream: &buf,
}
err := client.BuildImage(opts)
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expected := map[string][]string{"t": {opts.Remote}, "remote": {opts.Remote}, "q": {"1"}}
got := map[string][]string(req.URL.Query())
if !reflect.DeepEqual(got, expected) {
t.Errorf("BuildImage: wrong query string. Want %#v. Got %#v.", expected, got)
}
}
func TestTagImageParameters(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
opts := TagImageOptions{Repo: "testImage"}
err := client.TagImage("base", opts)
if err != nil && strings.Index(err.Error(), "tag image fail") == -1 {
t.Fatal(err)
}
req := fakeRT.requests[0]
expected := "http://localhost:4243/images/base/tag?repo=testImage"
got := req.URL.String()
if !reflect.DeepEqual(got, expected) {
t.Errorf("TagImage: wrong query string. Want %#v. Got %#v.", expected, got)
}
}
func TestTagImageMissingRepo(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
opts := TagImageOptions{Repo: "testImage"}
err := client.TagImage("", opts)
if err != ErrNoSuchImage {
t.Errorf("TestTag: wrong error returned. Want %#v. Got %#v.",
ErrNoSuchImage, err)
}
}
func TestIsUrl(t *testing.T) {
url := "http://foo.bar/"
result := isURL(url)
if !result {
t.Errorf("isURL: wrong match. Expected %#v to be a url. Got %#v.", url, result)
}
url = "/foo/bar.tar"
result = isURL(url)
if result {
t.Errorf("isURL: wrong match. Expected %#v to not be a url. Got %#v", url, result)
}
}
func TestLoadImage(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
tar, err := os.Open("testing/data/container.tar")
if err != nil {
t.Fatal(err)
} else {
defer tar.Close()
}
opts := LoadImageOptions{InputStream: tar}
err = client.LoadImage(opts)
if nil != err {
t.Error(err)
}
req := fakeRT.requests[0]
if req.Method != "POST" {
t.Errorf("LoadImage: wrong method. Expected %q. Got %q.", "POST", req.Method)
}
if req.URL.Path != "/images/load" {
t.Errorf("LoadImage: wrong URL. Expected %q. Got %q.", "/images/load", req.URL.Path)
}
}
func TestExportImage(t *testing.T) {
var buf bytes.Buffer
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
opts := ExportImageOptions{Name: "testimage", OutputStream: &buf}
err := client.ExportImage(opts)
if nil != err {
t.Error(err)
}
req := fakeRT.requests[0]
if req.Method != "GET" {
t.Errorf("ExportImage: wrong method. Expected %q. Got %q.", "GET", req.Method)
}
expectedPath := "/images/testimage/get"
if req.URL.Path != expectedPath {
t.Errorf("ExportIMage: wrong path. Expected %q. Got %q.", expectedPath, req.URL.Path)
}
}
func TestExportImages(t *testing.T) {
var buf bytes.Buffer
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
opts := ExportImagesOptions{Names: []string{"testimage1", "testimage2:latest"}, OutputStream: &buf}
err := client.ExportImages(opts)
if nil != err {
t.Error(err)
}
req := fakeRT.requests[0]
if req.Method != "GET" {
t.Errorf("ExportImage: wrong method. Expected %q. Got %q.", "GET", req.Method)
}
expected := "http://localhost:4243/images/get?names=testimage1&names=testimage2%3Alatest"
got := req.URL.String()
if !reflect.DeepEqual(got, expected) {
t.Errorf("ExportIMage: wrong path. Expected %q. Got %q.", expected, got)
}
}
func TestExportImagesNoNames(t *testing.T) {
var buf bytes.Buffer
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
opts := ExportImagesOptions{Names: []string{}, OutputStream: &buf}
err := client.ExportImages(opts)
if err == nil {
t.Error("Expected an error")
}
if err != ErrMustSpecifyNames {
t.Error(err)
}
}
func TestSearchImages(t *testing.T) {
body := `[
{
"description":"A container with Cassandra 2.0.3",
"is_official":true,
"is_automated":true,
"name":"poklet/cassandra",
"star_count":17
},
{
"description":"A container with Cassandra 2.0.3",
"is_official":true,
"is_automated":false,
"name":"poklet/cassandra",
"star_count":17
}
,
{
"description":"A container with Cassandra 2.0.3",
"is_official":false,
"is_automated":true,
"name":"poklet/cassandra",
"star_count":17
}
]`
var expected []APIImageSearch
err := json.Unmarshal([]byte(body), &expected)
if err != nil {
t.Fatal(err)
}
client := newTestClient(&FakeRoundTripper{message: body, status: http.StatusOK})
result, err := client.SearchImages("cassandra")
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("SearchImages: Wrong return value. Want %#v. Got %#v.", expected, result)
}
}
|