docker_cli_external_volume_driver_test.go 19 KB

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