github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/client/container_restart_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 TestContainerRestartError(t *testing.T) { 19 client := &Client{ 20 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 21 } 22 err := client.ContainerRestart(context.Background(), "nothing", container.StopOptions{}) 23 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 24 } 25 26 // TestContainerRestartConnectionError verifies that connection errors occurring 27 // during API-version negotiation are not shadowed by API-version errors. 28 // 29 // Regression test for https://github.com/docker/cli/issues/4890 30 func TestContainerRestartConnectionError(t *testing.T) { 31 client, err := NewClientWithOpts(WithAPIVersionNegotiation(), WithHost("tcp://no-such-host.invalid")) 32 assert.NilError(t, err) 33 34 err = client.ContainerRestart(context.Background(), "nothing", container.StopOptions{}) 35 assert.Check(t, is.ErrorType(err, IsErrConnectionFailed)) 36 } 37 38 func TestContainerRestart(t *testing.T) { 39 const expectedURL = "/v1.42/containers/container_id/restart" 40 client := &Client{ 41 client: newMockClient(func(req *http.Request) (*http.Response, error) { 42 if !strings.HasPrefix(req.URL.Path, expectedURL) { 43 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 44 } 45 s := req.URL.Query().Get("signal") 46 if s != "SIGKILL" { 47 return nil, fmt.Errorf("signal not set in URL query. Expected 'SIGKILL', got '%s'", s) 48 } 49 t := req.URL.Query().Get("t") 50 if t != "100" { 51 return nil, fmt.Errorf("t (timeout) not set in URL query properly. Expected '100', got %s", t) 52 } 53 return &http.Response{ 54 StatusCode: http.StatusOK, 55 Body: io.NopCloser(bytes.NewReader([]byte(""))), 56 }, nil 57 }), 58 version: "1.42", 59 } 60 timeout := 100 61 err := client.ContainerRestart(context.Background(), "container_id", container.StopOptions{ 62 Signal: "SIGKILL", 63 Timeout: &timeout, 64 }) 65 if err != nil { 66 t.Fatal(err) 67 } 68 }