container_remove_test.go 2.0 KB

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