container_remove_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package client
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "testing"
  9. "github.com/docker/docker/api/types"
  10. "golang.org/x/net/context"
  11. )
  12. func TestContainerRemoveError(t *testing.T) {
  13. client := &Client{
  14. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  15. }
  16. err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{})
  17. if err == nil || err.Error() != "Error response from daemon: Server error" {
  18. t.Fatalf("expected a Server Error, got %v", err)
  19. }
  20. }
  21. func TestContainerRemove(t *testing.T) {
  22. expectedURL := "/containers/container_id"
  23. client := &Client{
  24. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  25. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  26. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  27. }
  28. query := req.URL.Query()
  29. volume := query.Get("v")
  30. if volume != "1" {
  31. return nil, fmt.Errorf("v (volume) not set in URL query properly. Expected '1', got %s", volume)
  32. }
  33. force := query.Get("force")
  34. if force != "1" {
  35. return nil, fmt.Errorf("force not set in URL query properly. Expected '1', got %s", force)
  36. }
  37. link := query.Get("link")
  38. if link != "" {
  39. return nil, fmt.Errorf("link should have not be present in query, go %s", link)
  40. }
  41. return &http.Response{
  42. StatusCode: http.StatusOK,
  43. Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
  44. }, nil
  45. }),
  46. }
  47. err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{
  48. RemoveVolumes: true,
  49. Force: true,
  50. })
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. }