container_remove_test.go 1.9 KB

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