container_diff_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api/types/container"
  11. "golang.org/x/net/context"
  12. )
  13. func TestContainerDiffError(t *testing.T) {
  14. client := &Client{
  15. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  16. }
  17. _, err := client.ContainerDiff(context.Background(), "nothing")
  18. if err == nil || err.Error() != "Error response from daemon: Server error" {
  19. t.Fatalf("expected a Server Error, got %v", err)
  20. }
  21. }
  22. func TestContainerDiff(t *testing.T) {
  23. expectedURL := "/containers/container_id/changes"
  24. client := &Client{
  25. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  26. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  27. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  28. }
  29. b, err := json.Marshal([]container.ContainerChangeResponseItem{
  30. {
  31. Kind: 0,
  32. Path: "/path/1",
  33. },
  34. {
  35. Kind: 1,
  36. Path: "/path/2",
  37. },
  38. })
  39. if err != nil {
  40. return nil, err
  41. }
  42. return &http.Response{
  43. StatusCode: http.StatusOK,
  44. Body: ioutil.NopCloser(bytes.NewReader(b)),
  45. }, nil
  46. }),
  47. }
  48. changes, err := client.ContainerDiff(context.Background(), "container_id")
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if len(changes) != 2 {
  53. t.Fatalf("expected an array of 2 changes, got %v", changes)
  54. }
  55. }