volume_inspect_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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"
  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. if !errdefs.IsSystem(err) {
  23. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  24. }
  25. }
  26. func TestVolumeInspectNotFound(t *testing.T) {
  27. client := &Client{
  28. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  29. }
  30. _, err := client.VolumeInspect(context.Background(), "unknown")
  31. assert.Check(t, IsErrNotFound(err))
  32. }
  33. func TestVolumeInspectWithEmptyID(t *testing.T) {
  34. client := &Client{
  35. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  36. return nil, errors.New("should not make request")
  37. }),
  38. }
  39. _, _, err := client.VolumeInspectWithRaw(context.Background(), "")
  40. if !IsErrNotFound(err) {
  41. t.Fatalf("Expected NotFoundError, got %v", err)
  42. }
  43. }
  44. func TestVolumeInspect(t *testing.T) {
  45. expectedURL := "/volumes/volume_id"
  46. expected := types.Volume{
  47. Name: "name",
  48. Driver: "driver",
  49. Mountpoint: "mountpoint",
  50. }
  51. client := &Client{
  52. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  53. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  54. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  55. }
  56. if req.Method != http.MethodGet {
  57. return nil, fmt.Errorf("expected GET method, got %s", req.Method)
  58. }
  59. content, err := json.Marshal(expected)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return &http.Response{
  64. StatusCode: http.StatusOK,
  65. Body: io.NopCloser(bytes.NewReader(content)),
  66. }, nil
  67. }),
  68. }
  69. volume, err := client.VolumeInspect(context.Background(), "volume_id")
  70. assert.NilError(t, err)
  71. assert.Check(t, is.DeepEqual(expected, volume))
  72. }