github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/container_resize_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "strings" 9 "testing" 10 11 "github.com/docker/docker/api/types" 12 "golang.org/x/net/context" 13 ) 14 15 func TestContainerResizeError(t *testing.T) { 16 client := &Client{ 17 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 18 } 19 err := client.ContainerResize(context.Background(), "container_id", types.ResizeOptions{}) 20 if err == nil || err.Error() != "Error response from daemon: Server error" { 21 t.Fatalf("expected a Server Error, got %v", err) 22 } 23 } 24 25 func TestContainerExecResizeError(t *testing.T) { 26 client := &Client{ 27 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 28 } 29 err := client.ContainerExecResize(context.Background(), "exec_id", types.ResizeOptions{}) 30 if err == nil || err.Error() != "Error response from daemon: Server error" { 31 t.Fatalf("expected a Server Error, got %v", err) 32 } 33 } 34 35 func TestContainerResize(t *testing.T) { 36 client := &Client{ 37 client: newMockClient(resizeTransport("/containers/container_id/resize")), 38 } 39 40 err := client.ContainerResize(context.Background(), "container_id", types.ResizeOptions{ 41 Height: 500, 42 Width: 600, 43 }) 44 if err != nil { 45 t.Fatal(err) 46 } 47 } 48 49 func TestContainerExecResize(t *testing.T) { 50 client := &Client{ 51 client: newMockClient(resizeTransport("/exec/exec_id/resize")), 52 } 53 54 err := client.ContainerExecResize(context.Background(), "exec_id", types.ResizeOptions{ 55 Height: 500, 56 Width: 600, 57 }) 58 if err != nil { 59 t.Fatal(err) 60 } 61 } 62 63 func resizeTransport(expectedURL string) func(req *http.Request) (*http.Response, error) { 64 return func(req *http.Request) (*http.Response, error) { 65 if !strings.HasPrefix(req.URL.Path, expectedURL) { 66 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 67 } 68 query := req.URL.Query() 69 h := query.Get("h") 70 if h != "500" { 71 return nil, fmt.Errorf("h not set in URL query properly. Expected '500', got %s", h) 72 } 73 w := query.Get("w") 74 if w != "600" { 75 return nil, fmt.Errorf("w not set in URL query properly. Expected '600', got %s", w) 76 } 77 return &http.Response{ 78 StatusCode: http.StatusOK, 79 Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), 80 }, nil 81 } 82 }