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