volume_inspect_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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"
  11. "github.com/docker/docker/internal/testutil"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. "golang.org/x/net/context"
  15. )
  16. func TestVolumeInspectError(t *testing.T) {
  17. client := &Client{
  18. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  19. }
  20. _, err := client.VolumeInspect(context.Background(), "nothing")
  21. testutil.ErrorContains(t, err, "Error response from daemon: Server error")
  22. }
  23. func TestVolumeInspectNotFound(t *testing.T) {
  24. client := &Client{
  25. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  26. }
  27. _, err := client.VolumeInspect(context.Background(), "unknown")
  28. assert.True(t, IsErrNotFound(err))
  29. }
  30. func TestVolumeInspectWithEmptyID(t *testing.T) {
  31. expectedURL := "/volumes/"
  32. client := &Client{
  33. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  34. assert.Equal(t, req.URL.Path, expectedURL)
  35. return &http.Response{
  36. StatusCode: http.StatusNotFound,
  37. Body: ioutil.NopCloser(bytes.NewReader(nil)),
  38. }, nil
  39. }),
  40. }
  41. _, err := client.VolumeInspect(context.Background(), "")
  42. testutil.ErrorContains(t, err, "No such volume: ")
  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 != "GET" {
  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: ioutil.NopCloser(bytes.NewReader(content)),
  66. }, nil
  67. }),
  68. }
  69. volume, err := client.VolumeInspect(context.Background(), "volume_id")
  70. require.NoError(t, err)
  71. assert.Equal(t, expected, volume)
  72. }