volume_inspect_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/volume"
  12. "github.com/docker/docker/errdefs"
  13. "github.com/pkg/errors"
  14. "gotest.tools/v3/assert"
  15. is "gotest.tools/v3/assert/cmp"
  16. )
  17. func TestVolumeInspectError(t *testing.T) {
  18. client := &Client{
  19. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  20. }
  21. _, err := client.VolumeInspect(context.Background(), "nothing")
  22. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  23. }
  24. func TestVolumeInspectNotFound(t *testing.T) {
  25. client := &Client{
  26. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  27. }
  28. _, err := client.VolumeInspect(context.Background(), "unknown")
  29. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  30. }
  31. func TestVolumeInspectWithEmptyID(t *testing.T) {
  32. client := &Client{
  33. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  34. return nil, errors.New("should not make request")
  35. }),
  36. }
  37. _, _, err := client.VolumeInspectWithRaw(context.Background(), "")
  38. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  39. }
  40. func TestVolumeInspect(t *testing.T) {
  41. expectedURL := "/volumes/volume_id"
  42. expected := volume.Volume{
  43. Name: "name",
  44. Driver: "driver",
  45. Mountpoint: "mountpoint",
  46. }
  47. client := &Client{
  48. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  49. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  50. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  51. }
  52. if req.Method != http.MethodGet {
  53. return nil, fmt.Errorf("expected GET method, got %s", req.Method)
  54. }
  55. content, err := json.Marshal(expected)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return &http.Response{
  60. StatusCode: http.StatusOK,
  61. Body: io.NopCloser(bytes.NewReader(content)),
  62. }, nil
  63. }),
  64. }
  65. vol, err := client.VolumeInspect(context.Background(), "volume_id")
  66. assert.NilError(t, err)
  67. assert.Check(t, is.DeepEqual(expected, vol))
  68. }