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