github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/container_kill_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "testing" 11 ) 12 13 func TestContainerKillError(t *testing.T) { 14 client := &Client{ 15 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 16 } 17 err := client.ContainerKill(context.Background(), "nothing", "SIGKILL") 18 if err == nil || err.Error() != "Error response from daemon: Server error" { 19 t.Fatalf("expected a Server Error, got %v", err) 20 } 21 } 22 23 func TestContainerKill(t *testing.T) { 24 expectedURL := "/containers/container_id/kill" 25 client := &Client{ 26 client: newMockClient(func(req *http.Request) (*http.Response, error) { 27 if !strings.HasPrefix(req.URL.Path, expectedURL) { 28 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 29 } 30 signal := req.URL.Query().Get("signal") 31 if signal != "SIGKILL" { 32 return nil, fmt.Errorf("signal not set in URL query properly. Expected 'SIGKILL', got %s", signal) 33 } 34 return &http.Response{ 35 StatusCode: http.StatusOK, 36 Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), 37 }, nil 38 }), 39 } 40 41 err := client.ContainerKill(context.Background(), "container_id", "SIGKILL") 42 if err != nil { 43 t.Fatal(err) 44 } 45 }