testutils.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package testutils
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net"
  7. "net/http"
  8. "time"
  9. "github.com/docker/docker/pkg/plugingetter"
  10. "github.com/docker/docker/pkg/plugins"
  11. "github.com/docker/docker/volume"
  12. )
  13. // NoopVolume is a volume that doesn't perform any operation
  14. type NoopVolume struct{}
  15. // Name is the name of the volume
  16. func (NoopVolume) Name() string { return "noop" }
  17. // DriverName is the name of the driver
  18. func (NoopVolume) DriverName() string { return "noop" }
  19. // Path is the filesystem path to the volume
  20. func (NoopVolume) Path() string { return "noop" }
  21. // Mount mounts the volume in the container
  22. func (NoopVolume) Mount(_ string) (string, error) { return "noop", nil }
  23. // Unmount unmounts the volume from the container
  24. func (NoopVolume) Unmount(_ string) error { return nil }
  25. // Status provides low-level details about the volume
  26. func (NoopVolume) Status() map[string]interface{} { return nil }
  27. // CreatedAt provides the time the volume (directory) was created at
  28. func (NoopVolume) CreatedAt() (time.Time, error) { return time.Now(), nil }
  29. // FakeVolume is a fake volume with a random name
  30. type FakeVolume struct {
  31. name string
  32. driverName string
  33. }
  34. // NewFakeVolume creates a new fake volume for testing
  35. func NewFakeVolume(name string, driverName string) volume.Volume {
  36. return FakeVolume{name: name, driverName: driverName}
  37. }
  38. // Name is the name of the volume
  39. func (f FakeVolume) Name() string { return f.name }
  40. // DriverName is the name of the driver
  41. func (f FakeVolume) DriverName() string { return f.driverName }
  42. // Path is the filesystem path to the volume
  43. func (FakeVolume) Path() string { return "fake" }
  44. // Mount mounts the volume in the container
  45. func (FakeVolume) Mount(_ string) (string, error) { return "fake", nil }
  46. // Unmount unmounts the volume from the container
  47. func (FakeVolume) Unmount(_ string) error { return nil }
  48. // Status provides low-level details about the volume
  49. func (FakeVolume) Status() map[string]interface{} { return nil }
  50. // CreatedAt provides the time the volume (directory) was created at
  51. func (FakeVolume) CreatedAt() (time.Time, error) { return time.Now(), nil }
  52. // FakeDriver is a driver that generates fake volumes
  53. type FakeDriver struct {
  54. name string
  55. vols map[string]volume.Volume
  56. }
  57. // NewFakeDriver creates a new FakeDriver with the specified name
  58. func NewFakeDriver(name string) volume.Driver {
  59. return &FakeDriver{
  60. name: name,
  61. vols: make(map[string]volume.Volume),
  62. }
  63. }
  64. // Name is the name of the driver
  65. func (d *FakeDriver) Name() string { return d.name }
  66. // Create initializes a fake volume.
  67. // It returns an error if the options include an "error" key with a message
  68. func (d *FakeDriver) Create(name string, opts map[string]string) (volume.Volume, error) {
  69. if opts != nil && opts["error"] != "" {
  70. return nil, fmt.Errorf(opts["error"])
  71. }
  72. v := NewFakeVolume(name, d.name)
  73. d.vols[name] = v
  74. return v, nil
  75. }
  76. // Remove deletes a volume.
  77. func (d *FakeDriver) Remove(v volume.Volume) error {
  78. if _, exists := d.vols[v.Name()]; !exists {
  79. return fmt.Errorf("no such volume")
  80. }
  81. delete(d.vols, v.Name())
  82. return nil
  83. }
  84. // List lists the volumes
  85. func (d *FakeDriver) List() ([]volume.Volume, error) {
  86. var vols []volume.Volume
  87. for _, v := range d.vols {
  88. vols = append(vols, v)
  89. }
  90. return vols, nil
  91. }
  92. // Get gets the volume
  93. func (d *FakeDriver) Get(name string) (volume.Volume, error) {
  94. if v, exists := d.vols[name]; exists {
  95. return v, nil
  96. }
  97. return nil, fmt.Errorf("no such volume")
  98. }
  99. // Scope returns the local scope
  100. func (*FakeDriver) Scope() string {
  101. return "local"
  102. }
  103. type fakePlugin struct {
  104. client *plugins.Client
  105. name string
  106. refs int
  107. }
  108. // MakeFakePlugin creates a fake plugin from the passed in driver
  109. // Note: currently only "Create" is implemented because that's all that's needed
  110. // so far. If you need it to test something else, add it here, but probably you
  111. // shouldn't need to use this except for very specific cases with v2 plugin handling.
  112. func MakeFakePlugin(d volume.Driver, l net.Listener) (plugingetter.CompatPlugin, error) {
  113. c, err := plugins.NewClient(l.Addr().Network()+"://"+l.Addr().String(), nil)
  114. if err != nil {
  115. return nil, err
  116. }
  117. mux := http.NewServeMux()
  118. mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
  119. createReq := struct {
  120. Name string
  121. Opts map[string]string
  122. }{}
  123. if err := json.NewDecoder(r.Body).Decode(&createReq); err != nil {
  124. fmt.Fprintf(w, `{"Err": "%s"}`, err.Error())
  125. return
  126. }
  127. _, err := d.Create(createReq.Name, createReq.Opts)
  128. if err != nil {
  129. fmt.Fprintf(w, `{"Err": "%s"}`, err.Error())
  130. return
  131. }
  132. w.Write([]byte("{}"))
  133. })
  134. go http.Serve(l, mux)
  135. return &fakePlugin{client: c, name: d.Name()}, nil
  136. }
  137. func (p *fakePlugin) Client() *plugins.Client {
  138. return p.client
  139. }
  140. func (p *fakePlugin) Name() string {
  141. return p.name
  142. }
  143. func (p *fakePlugin) IsV1() bool {
  144. return false
  145. }
  146. func (p *fakePlugin) BasePath() string {
  147. return ""
  148. }
  149. type fakePluginGetter struct {
  150. plugins map[string]plugingetter.CompatPlugin
  151. }
  152. // NewFakePluginGetter returns a plugin getter for fake plugins
  153. func NewFakePluginGetter(pls ...plugingetter.CompatPlugin) plugingetter.PluginGetter {
  154. idx := make(map[string]plugingetter.CompatPlugin, len(pls))
  155. for _, p := range pls {
  156. idx[p.Name()] = p
  157. }
  158. return &fakePluginGetter{plugins: idx}
  159. }
  160. // This ignores the second argument since we only care about volume drivers here,
  161. // there shouldn't be any other kind of plugin in here
  162. func (g *fakePluginGetter) Get(name, _ string, mode int) (plugingetter.CompatPlugin, error) {
  163. p, ok := g.plugins[name]
  164. if !ok {
  165. return nil, errors.New("not found")
  166. }
  167. p.(*fakePlugin).refs += mode
  168. return p, nil
  169. }
  170. func (g *fakePluginGetter) GetAllByCap(capability string) ([]plugingetter.CompatPlugin, error) {
  171. panic("GetAllByCap shouldn't be called")
  172. }
  173. func (g *fakePluginGetter) GetAllManagedPluginsByCap(capability string) []plugingetter.CompatPlugin {
  174. panic("GetAllManagedPluginsByCap should not be called")
  175. }
  176. func (g *fakePluginGetter) Handle(capability string, callback func(string, *plugins.Client)) {
  177. panic("Handle should not be called")
  178. }
  179. // FakeRefs checks ref count on a fake plugin.
  180. func FakeRefs(p plugingetter.CompatPlugin) int {
  181. // this should panic if something other than a `*fakePlugin` is passed in
  182. return p.(*fakePlugin).refs
  183. }