docker_cli_external_volume_driver_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "strings"
  12. "testing"
  13. "time"
  14. "github.com/docker/docker/api/types"
  15. volumetypes "github.com/docker/docker/api/types/volume"
  16. "github.com/docker/docker/integration-cli/daemon"
  17. "github.com/docker/docker/pkg/plugins"
  18. "github.com/docker/docker/pkg/stringid"
  19. testdaemon "github.com/docker/docker/testutil/daemon"
  20. "github.com/docker/docker/volume"
  21. "gotest.tools/v3/assert"
  22. )
  23. const volumePluginName = "test-external-volume-driver"
  24. type eventCounter struct {
  25. activations int
  26. creations int
  27. removals int
  28. mounts int
  29. unmounts int
  30. paths int
  31. lists int
  32. gets int
  33. caps int
  34. }
  35. type DockerExternalVolumeSuite struct {
  36. ds *DockerSuite
  37. d *daemon.Daemon
  38. *volumePlugin
  39. }
  40. func (s *DockerExternalVolumeSuite) SetUpTest(c *testing.T) {
  41. testRequires(c, testEnv.IsLocalDaemon)
  42. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  43. s.ec = &eventCounter{}
  44. }
  45. func (s *DockerExternalVolumeSuite) TearDownTest(c *testing.T) {
  46. if s.d != nil {
  47. s.d.Stop(c)
  48. s.ds.TearDownTest(c)
  49. }
  50. }
  51. func (s *DockerExternalVolumeSuite) SetUpSuite(c *testing.T) {
  52. s.volumePlugin = newVolumePlugin(c, volumePluginName)
  53. }
  54. type volumePlugin struct {
  55. ec *eventCounter
  56. *httptest.Server
  57. vols map[string]vol
  58. }
  59. type vol struct {
  60. Name string
  61. Mountpoint string
  62. Ninja bool // hack used to trigger a null volume return on `Get`
  63. Status map[string]interface{}
  64. Options map[string]string
  65. }
  66. func (p *volumePlugin) Close() {
  67. p.Server.Close()
  68. }
  69. func newVolumePlugin(c *testing.T, name string) *volumePlugin {
  70. mux := http.NewServeMux()
  71. s := &volumePlugin{Server: httptest.NewServer(mux), ec: &eventCounter{}, vols: make(map[string]vol)}
  72. type pluginRequest struct {
  73. Name string
  74. Opts map[string]string
  75. ID string
  76. }
  77. type pluginResp struct {
  78. Mountpoint string `json:",omitempty"`
  79. Err string `json:",omitempty"`
  80. }
  81. read := func(b io.ReadCloser) (pluginRequest, error) {
  82. defer b.Close()
  83. var pr pluginRequest
  84. err := json.NewDecoder(b).Decode(&pr)
  85. return pr, err
  86. }
  87. send := func(w http.ResponseWriter, data interface{}) {
  88. switch t := data.(type) {
  89. case error:
  90. http.Error(w, t.Error(), 500)
  91. case string:
  92. w.Header().Set("Content-Type", plugins.VersionMimetype)
  93. fmt.Fprintln(w, t)
  94. default:
  95. w.Header().Set("Content-Type", plugins.VersionMimetype)
  96. json.NewEncoder(w).Encode(&data)
  97. }
  98. }
  99. mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
  100. s.ec.activations++
  101. send(w, `{"Implements": ["VolumeDriver"]}`)
  102. })
  103. mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
  104. s.ec.creations++
  105. pr, err := read(r.Body)
  106. if err != nil {
  107. send(w, err)
  108. return
  109. }
  110. _, isNinja := pr.Opts["ninja"]
  111. status := map[string]interface{}{"Hello": "world"}
  112. s.vols[pr.Name] = vol{Name: pr.Name, Ninja: isNinja, Status: status, Options: pr.Opts}
  113. send(w, nil)
  114. })
  115. mux.HandleFunc("/VolumeDriver.List", func(w http.ResponseWriter, r *http.Request) {
  116. s.ec.lists++
  117. vols := make([]vol, 0, len(s.vols))
  118. for _, v := range s.vols {
  119. if v.Ninja {
  120. continue
  121. }
  122. vols = append(vols, v)
  123. }
  124. send(w, map[string][]vol{"Volumes": vols})
  125. })
  126. mux.HandleFunc("/VolumeDriver.Get", func(w http.ResponseWriter, r *http.Request) {
  127. s.ec.gets++
  128. pr, err := read(r.Body)
  129. if err != nil {
  130. send(w, err)
  131. return
  132. }
  133. v, exists := s.vols[pr.Name]
  134. if !exists {
  135. send(w, `{"Err": "no such volume"}`)
  136. }
  137. if v.Ninja {
  138. send(w, map[string]vol{})
  139. return
  140. }
  141. v.Mountpoint = hostVolumePath(pr.Name)
  142. send(w, map[string]vol{"Volume": v})
  143. })
  144. mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
  145. s.ec.removals++
  146. pr, err := read(r.Body)
  147. if err != nil {
  148. send(w, err)
  149. return
  150. }
  151. v, ok := s.vols[pr.Name]
  152. if !ok {
  153. send(w, nil)
  154. return
  155. }
  156. if err := os.RemoveAll(hostVolumePath(v.Name)); err != nil {
  157. send(w, &pluginResp{Err: err.Error()})
  158. return
  159. }
  160. delete(s.vols, v.Name)
  161. send(w, nil)
  162. })
  163. mux.HandleFunc("/VolumeDriver.Path", func(w http.ResponseWriter, r *http.Request) {
  164. s.ec.paths++
  165. pr, err := read(r.Body)
  166. if err != nil {
  167. send(w, err)
  168. return
  169. }
  170. p := hostVolumePath(pr.Name)
  171. send(w, &pluginResp{Mountpoint: p})
  172. })
  173. mux.HandleFunc("/VolumeDriver.Mount", func(w http.ResponseWriter, r *http.Request) {
  174. s.ec.mounts++
  175. pr, err := read(r.Body)
  176. if err != nil {
  177. send(w, err)
  178. return
  179. }
  180. if v, exists := s.vols[pr.Name]; exists {
  181. // Use this to simulate a mount failure
  182. if _, exists := v.Options["invalidOption"]; exists {
  183. send(w, fmt.Errorf("invalid argument"))
  184. return
  185. }
  186. }
  187. p := hostVolumePath(pr.Name)
  188. if err := os.MkdirAll(p, 0o755); err != nil {
  189. send(w, &pluginResp{Err: err.Error()})
  190. return
  191. }
  192. if err := os.WriteFile(filepath.Join(p, "test"), []byte(s.Server.URL), 0o644); err != nil {
  193. send(w, err)
  194. return
  195. }
  196. if err := os.WriteFile(filepath.Join(p, "mountID"), []byte(pr.ID), 0o644); err != nil {
  197. send(w, err)
  198. return
  199. }
  200. send(w, &pluginResp{Mountpoint: p})
  201. })
  202. mux.HandleFunc("/VolumeDriver.Unmount", func(w http.ResponseWriter, r *http.Request) {
  203. s.ec.unmounts++
  204. _, err := read(r.Body)
  205. if err != nil {
  206. send(w, err)
  207. return
  208. }
  209. send(w, nil)
  210. })
  211. mux.HandleFunc("/VolumeDriver.Capabilities", func(w http.ResponseWriter, r *http.Request) {
  212. s.ec.caps++
  213. _, err := read(r.Body)
  214. if err != nil {
  215. send(w, err)
  216. return
  217. }
  218. send(w, `{"Capabilities": { "Scope": "global" }}`)
  219. })
  220. err := os.MkdirAll("/etc/docker/plugins", 0o755)
  221. assert.NilError(c, err)
  222. err = os.WriteFile("/etc/docker/plugins/"+name+".spec", []byte(s.Server.URL), 0o644)
  223. assert.NilError(c, err)
  224. return s
  225. }
  226. func (s *DockerExternalVolumeSuite) TearDownSuite(c *testing.T) {
  227. s.volumePlugin.Close()
  228. err := os.RemoveAll("/etc/docker/plugins")
  229. assert.NilError(c, err)
  230. }
  231. func (s *DockerExternalVolumeSuite) TestVolumeCLICreateOptionConflict(c *testing.T) {
  232. dockerCmd(c, "volume", "create", "test")
  233. out, _, err := dockerCmdWithError("volume", "create", "test", "--driver", volumePluginName)
  234. assert.Assert(c, err != nil, "volume create exception name already in use with another driver")
  235. assert.Assert(c, strings.Contains(out, "must be unique"))
  236. out, _ = dockerCmd(c, "volume", "inspect", "--format={{ .Driver }}", "test")
  237. _, _, err = dockerCmdWithError("volume", "create", "test", "--driver", strings.TrimSpace(out))
  238. assert.NilError(c, err)
  239. }
  240. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverNamed(c *testing.T) {
  241. s.d.StartWithBusybox(c)
  242. out, err := s.d.Cmd("run", "--rm", "--name", "test-data", "-v", "external-volume-test:/tmp/external-volume-test", "--volume-driver", volumePluginName, "busybox:latest", "cat", "/tmp/external-volume-test/test")
  243. assert.NilError(c, err, out)
  244. assert.Assert(c, strings.Contains(out, s.Server.URL))
  245. _, err = s.d.Cmd("volume", "rm", "external-volume-test")
  246. assert.NilError(c, err)
  247. p := hostVolumePath("external-volume-test")
  248. _, err = os.Lstat(p)
  249. assert.ErrorContains(c, err, "")
  250. assert.Assert(c, os.IsNotExist(err), "Expected volume path in host to not exist: %s, %v\n", p, err)
  251. assert.Equal(c, s.ec.activations, 1)
  252. assert.Equal(c, s.ec.creations, 1)
  253. assert.Equal(c, s.ec.removals, 1)
  254. assert.Equal(c, s.ec.mounts, 1)
  255. assert.Equal(c, s.ec.unmounts, 1)
  256. }
  257. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverUnnamed(c *testing.T) {
  258. s.d.StartWithBusybox(c)
  259. out, err := s.d.Cmd("run", "--rm", "--name", "test-data", "-v", "/tmp/external-volume-test", "--volume-driver", volumePluginName, "busybox:latest", "cat", "/tmp/external-volume-test/test")
  260. assert.NilError(c, err, out)
  261. assert.Assert(c, strings.Contains(out, s.Server.URL))
  262. assert.Equal(c, s.ec.activations, 1)
  263. assert.Equal(c, s.ec.creations, 1)
  264. assert.Equal(c, s.ec.removals, 1)
  265. assert.Equal(c, s.ec.mounts, 1)
  266. assert.Equal(c, s.ec.unmounts, 1)
  267. }
  268. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverVolumesFrom(c *testing.T) {
  269. s.d.StartWithBusybox(c)
  270. out, err := s.d.Cmd("run", "--name", "vol-test1", "-v", "/foo", "--volume-driver", volumePluginName, "busybox:latest")
  271. assert.NilError(c, err, out)
  272. out, err = s.d.Cmd("run", "--rm", "--volumes-from", "vol-test1", "--name", "vol-test2", "busybox", "ls", "/tmp")
  273. assert.NilError(c, err, out)
  274. out, err = s.d.Cmd("rm", "-fv", "vol-test1")
  275. assert.NilError(c, err, out)
  276. assert.Equal(c, s.ec.activations, 1)
  277. assert.Equal(c, s.ec.creations, 1)
  278. assert.Equal(c, s.ec.removals, 1)
  279. assert.Equal(c, s.ec.mounts, 2)
  280. assert.Equal(c, s.ec.unmounts, 2)
  281. }
  282. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverDeleteContainer(c *testing.T) {
  283. s.d.StartWithBusybox(c)
  284. out, err := s.d.Cmd("run", "--name", "vol-test1", "-v", "/foo", "--volume-driver", volumePluginName, "busybox:latest")
  285. assert.NilError(c, err, out)
  286. out, err = s.d.Cmd("rm", "-fv", "vol-test1")
  287. assert.NilError(c, err, out)
  288. assert.Equal(c, s.ec.activations, 1)
  289. assert.Equal(c, s.ec.creations, 1)
  290. assert.Equal(c, s.ec.removals, 1)
  291. assert.Equal(c, s.ec.mounts, 1)
  292. assert.Equal(c, s.ec.unmounts, 1)
  293. }
  294. func hostVolumePath(name string) string {
  295. return fmt.Sprintf("/var/lib/docker/volumes/%s", name)
  296. }
  297. // Make sure a request to use a down driver doesn't block other requests
  298. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverLookupNotBlocked(c *testing.T) {
  299. specPath := "/etc/docker/plugins/down-driver.spec"
  300. err := os.WriteFile(specPath, []byte("tcp://127.0.0.7:9999"), 0o644)
  301. assert.NilError(c, err)
  302. defer os.RemoveAll(specPath)
  303. chCmd1 := make(chan struct{})
  304. chCmd2 := make(chan error, 1)
  305. cmd1 := exec.Command(dockerBinary, "volume", "create", "-d", "down-driver")
  306. cmd2 := exec.Command(dockerBinary, "volume", "create")
  307. assert.Assert(c, cmd1.Start() == nil)
  308. defer cmd1.Process.Kill()
  309. time.Sleep(100 * time.Millisecond) // ensure API has been called
  310. assert.Assert(c, cmd2.Start() == nil)
  311. go func() {
  312. cmd1.Wait()
  313. close(chCmd1)
  314. }()
  315. go func() {
  316. chCmd2 <- cmd2.Wait()
  317. }()
  318. select {
  319. case <-chCmd1:
  320. cmd2.Process.Kill()
  321. c.Fatalf("volume create with down driver finished unexpectedly")
  322. case err := <-chCmd2:
  323. assert.NilError(c, err)
  324. case <-time.After(5 * time.Second):
  325. cmd2.Process.Kill()
  326. c.Fatal("volume creates are blocked by previous create requests when previous driver is down")
  327. }
  328. }
  329. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverRetryNotImmediatelyExists(c *testing.T) {
  330. s.d.StartWithBusybox(c)
  331. driverName := "test-external-volume-driver-retry"
  332. errchan := make(chan error, 1)
  333. started := make(chan struct{})
  334. go func() {
  335. close(started)
  336. if out, err := s.d.Cmd("run", "--rm", "--name", "test-data-retry", "-v", "external-volume-test:/tmp/external-volume-test", "--volume-driver", driverName, "busybox:latest"); err != nil {
  337. errchan <- fmt.Errorf("%v:\n%s", err, out)
  338. }
  339. close(errchan)
  340. }()
  341. <-started
  342. // wait for a retry to occur, then create spec to allow plugin to register
  343. time.Sleep(2 * time.Second)
  344. p := newVolumePlugin(c, driverName)
  345. defer p.Close()
  346. select {
  347. case err := <-errchan:
  348. assert.NilError(c, err)
  349. case <-time.After(8 * time.Second):
  350. c.Fatal("volume creates fail when plugin not immediately available")
  351. }
  352. _, err := s.d.Cmd("volume", "rm", "external-volume-test")
  353. assert.NilError(c, err)
  354. assert.Equal(c, p.ec.activations, 1)
  355. assert.Equal(c, p.ec.creations, 1)
  356. assert.Equal(c, p.ec.removals, 1)
  357. assert.Equal(c, p.ec.mounts, 1)
  358. assert.Equal(c, p.ec.unmounts, 1)
  359. }
  360. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverBindExternalVolume(c *testing.T) {
  361. dockerCmd(c, "volume", "create", "-d", volumePluginName, "foo")
  362. dockerCmd(c, "run", "-d", "--name", "testing", "-v", "foo:/bar", "busybox", "top")
  363. var mounts []struct {
  364. Name string
  365. Driver string
  366. }
  367. out := inspectFieldJSON(c, "testing", "Mounts")
  368. assert.Assert(c, json.NewDecoder(strings.NewReader(out)).Decode(&mounts) == nil)
  369. assert.Equal(c, len(mounts), 1, out)
  370. assert.Equal(c, mounts[0].Name, "foo")
  371. assert.Equal(c, mounts[0].Driver, volumePluginName)
  372. }
  373. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverList(c *testing.T) {
  374. dockerCmd(c, "volume", "create", "-d", volumePluginName, "abc3")
  375. out, _ := dockerCmd(c, "volume", "ls")
  376. ls := strings.Split(strings.TrimSpace(out), "\n")
  377. assert.Equal(c, len(ls), 2, fmt.Sprintf("\n%s", out))
  378. vol := strings.Fields(ls[len(ls)-1])
  379. assert.Equal(c, len(vol), 2, fmt.Sprintf("%v", vol))
  380. assert.Equal(c, vol[0], volumePluginName)
  381. assert.Equal(c, vol[1], "abc3")
  382. assert.Equal(c, s.ec.lists, 1)
  383. }
  384. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverGet(c *testing.T) {
  385. out, _, err := dockerCmdWithError("volume", "inspect", "dummy")
  386. assert.ErrorContains(c, err, "", out)
  387. assert.Assert(c, strings.Contains(out, "No such volume"))
  388. assert.Equal(c, s.ec.gets, 1)
  389. dockerCmd(c, "volume", "create", "test", "-d", volumePluginName)
  390. out, _ = dockerCmd(c, "volume", "inspect", "test")
  391. type vol struct {
  392. Status map[string]string
  393. }
  394. var st []vol
  395. assert.Assert(c, json.Unmarshal([]byte(out), &st) == nil)
  396. assert.Equal(c, len(st), 1)
  397. assert.Equal(c, len(st[0].Status), 1, fmt.Sprintf("%v", st[0]))
  398. assert.Equal(c, st[0].Status["Hello"], "world", fmt.Sprintf("%v", st[0].Status))
  399. }
  400. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverWithDaemonRestart(c *testing.T) {
  401. dockerCmd(c, "volume", "create", "-d", volumePluginName, "abc1")
  402. s.d.Restart(c)
  403. dockerCmd(c, "run", "--name=test", "-v", "abc1:/foo", "busybox", "true")
  404. var mounts []types.MountPoint
  405. inspectFieldAndUnmarshall(c, "test", "Mounts", &mounts)
  406. assert.Equal(c, len(mounts), 1)
  407. assert.Equal(c, mounts[0].Driver, volumePluginName)
  408. }
  409. // Ensures that the daemon handles when the plugin responds to a `Get` request with a null volume and a null error.
  410. // Prior the daemon would panic in this scenario.
  411. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverGetEmptyResponse(c *testing.T) {
  412. s.d.Start(c)
  413. out, err := s.d.Cmd("volume", "create", "-d", volumePluginName, "abc2", "--opt", "ninja=1")
  414. assert.NilError(c, err, out)
  415. out, err = s.d.Cmd("volume", "inspect", "abc2")
  416. assert.ErrorContains(c, err, "", out)
  417. assert.Assert(c, strings.Contains(out, "No such volume"))
  418. }
  419. // Ensure only cached paths are used in volume list to prevent N+1 calls to `VolumeDriver.Path`
  420. //
  421. // TODO(@cpuguy83): This test is testing internal implementation. In all the cases here, there may not even be a path available because the volume is not even mounted. Consider removing this test.
  422. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverPathCalls(c *testing.T) {
  423. s.d.Start(c)
  424. assert.Equal(c, s.ec.paths, 0)
  425. out, err := s.d.Cmd("volume", "create", "test", "--driver=test-external-volume-driver")
  426. assert.NilError(c, err, out)
  427. assert.Equal(c, s.ec.paths, 0)
  428. out, err = s.d.Cmd("volume", "ls")
  429. assert.NilError(c, err, out)
  430. assert.Equal(c, s.ec.paths, 0)
  431. }
  432. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverMountID(c *testing.T) {
  433. s.d.StartWithBusybox(c)
  434. out, err := s.d.Cmd("run", "--rm", "-v", "external-volume-test:/tmp/external-volume-test", "--volume-driver", volumePluginName, "busybox:latest", "cat", "/tmp/external-volume-test/test")
  435. assert.NilError(c, err, out)
  436. assert.Assert(c, strings.TrimSpace(out) != "")
  437. }
  438. // Check that VolumeDriver.Capabilities gets called, and only called once
  439. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverCapabilities(c *testing.T) {
  440. s.d.Start(c)
  441. assert.Equal(c, s.ec.caps, 0)
  442. for i := 0; i < 3; i++ {
  443. out, err := s.d.Cmd("volume", "create", "-d", volumePluginName, fmt.Sprintf("test%d", i))
  444. assert.NilError(c, err, out)
  445. assert.Equal(c, s.ec.caps, 1)
  446. out, err = s.d.Cmd("volume", "inspect", "--format={{.Scope}}", fmt.Sprintf("test%d", i))
  447. assert.NilError(c, err)
  448. assert.Equal(c, strings.TrimSpace(out), volume.GlobalScope)
  449. }
  450. }
  451. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverOutOfBandDelete(c *testing.T) {
  452. driverName := stringid.GenerateRandomID()
  453. p := newVolumePlugin(c, driverName)
  454. defer p.Close()
  455. s.d.StartWithBusybox(c)
  456. out, err := s.d.Cmd("volume", "create", "-d", driverName, "--name", "test")
  457. assert.NilError(c, err, out)
  458. out, err = s.d.Cmd("volume", "create", "-d", "local", "--name", "test")
  459. assert.ErrorContains(c, err, "", out)
  460. assert.Assert(c, strings.Contains(out, "must be unique"))
  461. // simulate out of band volume deletion on plugin level
  462. delete(p.vols, "test")
  463. // test re-create with same driver
  464. out, err = s.d.Cmd("volume", "create", "-d", driverName, "--opt", "foo=bar", "--name", "test")
  465. assert.NilError(c, err, out)
  466. out, err = s.d.Cmd("volume", "inspect", "test")
  467. assert.NilError(c, err, out)
  468. var vs []volumetypes.Volume
  469. err = json.Unmarshal([]byte(out), &vs)
  470. assert.NilError(c, err)
  471. assert.Equal(c, len(vs), 1)
  472. assert.Equal(c, vs[0].Driver, driverName)
  473. assert.Assert(c, vs[0].Options != nil)
  474. assert.Equal(c, vs[0].Options["foo"], "bar")
  475. assert.Equal(c, vs[0].Driver, driverName)
  476. // simulate out of band volume deletion on plugin level
  477. delete(p.vols, "test")
  478. // test create with different driver
  479. out, err = s.d.Cmd("volume", "create", "-d", "local", "--name", "test")
  480. assert.NilError(c, err, out)
  481. out, err = s.d.Cmd("volume", "inspect", "test")
  482. assert.NilError(c, err, out)
  483. vs = nil
  484. err = json.Unmarshal([]byte(out), &vs)
  485. assert.NilError(c, err)
  486. assert.Equal(c, len(vs), 1)
  487. assert.Equal(c, len(vs[0].Options), 0)
  488. assert.Equal(c, vs[0].Driver, "local")
  489. }
  490. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverUnmountOnMountFail(c *testing.T) {
  491. s.d.StartWithBusybox(c)
  492. s.d.Cmd("volume", "create", "-d", "test-external-volume-driver", "--opt=invalidOption=1", "--name=testumount")
  493. out, _ := s.d.Cmd("run", "-v", "testumount:/foo", "busybox", "true")
  494. assert.Equal(c, s.ec.unmounts, 0, out)
  495. out, _ = s.d.Cmd("run", "-w", "/foo", "-v", "testumount:/foo", "busybox", "true")
  496. assert.Equal(c, s.ec.unmounts, 0, out)
  497. }
  498. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverUnmountOnCp(c *testing.T) {
  499. s.d.StartWithBusybox(c)
  500. s.d.Cmd("volume", "create", "-d", "test-external-volume-driver", "--name=test")
  501. out, _ := s.d.Cmd("run", "-d", "--name=test", "-v", "test:/foo", "busybox", "/bin/sh", "-c", "touch /test && top")
  502. assert.Equal(c, s.ec.mounts, 1, out)
  503. out, _ = s.d.Cmd("cp", "test:/test", "/tmp/test")
  504. assert.Equal(c, s.ec.mounts, 2, out)
  505. assert.Equal(c, s.ec.unmounts, 1, out)
  506. out, _ = s.d.Cmd("kill", "test")
  507. assert.Equal(c, s.ec.unmounts, 2, out)
  508. }