docker_cli_external_volume_driver_test.go 18 KB

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