container_diff_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types/container"
  12. "github.com/docker/docker/errdefs"
  13. "gotest.tools/v3/assert"
  14. is "gotest.tools/v3/assert/cmp"
  15. )
  16. func TestContainerDiffError(t *testing.T) {
  17. client := &Client{
  18. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  19. }
  20. _, err := client.ContainerDiff(context.Background(), "nothing")
  21. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  22. }
  23. func TestContainerDiff(t *testing.T) {
  24. const expectedURL = "/containers/container_id/changes"
  25. expected := []container.FilesystemChange{
  26. {
  27. Kind: container.ChangeModify,
  28. Path: "/path/1",
  29. },
  30. {
  31. Kind: container.ChangeAdd,
  32. Path: "/path/2",
  33. },
  34. {
  35. Kind: container.ChangeDelete,
  36. Path: "/path/3",
  37. },
  38. }
  39. client := &Client{
  40. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  41. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  42. return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
  43. }
  44. b, err := json.Marshal(expected)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return &http.Response{
  49. StatusCode: http.StatusOK,
  50. Body: io.NopCloser(bytes.NewReader(b)),
  51. }, nil
  52. }),
  53. }
  54. changes, err := client.ContainerDiff(context.Background(), "container_id")
  55. assert.Check(t, err)
  56. assert.Check(t, is.DeepEqual(changes, expected))
  57. }