github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/container_stop_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "strings" 9 "testing" 10 "time" 11 12 "golang.org/x/net/context" 13 ) 14 15 func TestContainerStopError(t *testing.T) { 16 client := &Client{ 17 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 18 } 19 timeout := 0 * time.Second 20 err := client.ContainerStop(context.Background(), "nothing", &timeout) 21 if err == nil || err.Error() != "Error response from daemon: Server error" { 22 t.Fatalf("expected a Server Error, got %v", err) 23 } 24 } 25 26 func TestContainerStop(t *testing.T) { 27 expectedURL := "/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 t := req.URL.Query().Get("t") 34 if t != "100" { 35 return nil, fmt.Errorf("t (timeout) not set in URL query properly. Expected '100', got %s", t) 36 } 37 return &http.Response{ 38 StatusCode: http.StatusOK, 39 Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), 40 }, nil 41 }), 42 } 43 timeout := 100 * time.Second 44 err := client.ContainerStop(context.Background(), "container_id", &timeout) 45 if err != nil { 46 t.Fatal(err) 47 } 48 }