docker_cli_external_volume_driver_test.go 19 KB

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