github.com/rish1988/moby@v25.0.2+incompatible/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 12 "github.com/docker/docker/api/types/container" 13 "github.com/docker/docker/errdefs" 14 "gotest.tools/v3/assert" 15 is "gotest.tools/v3/assert/cmp" 16 ) 17 18 func TestContainerStopError(t *testing.T) { 19 client := &Client{ 20 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 21 } 22 err := client.ContainerStop(context.Background(), "nothing", container.StopOptions{}) 23 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 24 } 25 26 func TestContainerStop(t *testing.T) { 27 const expectedURL = "/v1.42/containers/container_id/stop" 28 client := &Client{ 29 client: newMockClient(func(req *http.Request) (*http.Response, error) { 30 if !strings.HasPrefix(req.URL.Path, expectedURL) { 31 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 32 } 33 s := req.URL.Query().Get("signal") 34 if s != "SIGKILL" { 35 return nil, fmt.Errorf("signal not set in URL query. Expected 'SIGKILL', got '%s'", s) 36 } 37 t := req.URL.Query().Get("t") 38 if t != "100" { 39 return nil, fmt.Errorf("t (timeout) not set in URL query properly. Expected '100', got %s", t) 40 } 41 return &http.Response{ 42 StatusCode: http.StatusOK, 43 Body: io.NopCloser(bytes.NewReader([]byte(""))), 44 }, nil 45 }), 46 version: "1.42", 47 } 48 timeout := 100 49 err := client.ContainerStop(context.Background(), "container_id", container.StopOptions{ 50 Signal: "SIGKILL", 51 Timeout: &timeout, 52 }) 53 if err != nil { 54 t.Fatal(err) 55 } 56 }