github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/client/container_stop_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "io" 8 "net/http" 9 "strings" 10 "testing" 11 "time" 12 13 "github.com/docker/docker/errdefs" 14 ) 15 16 func TestContainerStopError(t *testing.T) { 17 client := &Client{ 18 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 19 } 20 timeout := 0 * time.Second 21 err := client.ContainerStop(context.Background(), "nothing", &timeout) 22 if !errdefs.IsSystem(err) { 23 t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err) 24 } 25 } 26 27 func TestContainerStop(t *testing.T) { 28 expectedURL := "/containers/container_id/stop" 29 client := &Client{ 30 client: newMockClient(func(req *http.Request) (*http.Response, error) { 31 if !strings.HasPrefix(req.URL.Path, expectedURL) { 32 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 33 } 34 t := req.URL.Query().Get("t") 35 if t != "100" { 36 return nil, fmt.Errorf("t (timeout) not set in URL query properly. Expected '100', got %s", t) 37 } 38 return &http.Response{ 39 StatusCode: http.StatusOK, 40 Body: io.NopCloser(bytes.NewReader([]byte(""))), 41 }, nil 42 }), 43 } 44 timeout := 100 * time.Second 45 err := client.ContainerStop(context.Background(), "container_id", &timeout) 46 if err != nil { 47 t.Fatal(err) 48 } 49 }