docker_cli_external_volume_driver_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  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. "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/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. return
  143. })
  144. mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
  145. s.ec.removals++
  146. pr, err := read(r.Body)
  147. if err != nil {
  148. send(w, err)
  149. return
  150. }
  151. v, ok := s.vols[pr.Name]
  152. if !ok {
  153. send(w, nil)
  154. return
  155. }
  156. if err := os.RemoveAll(hostVolumePath(v.Name)); err != nil {
  157. send(w, &pluginResp{Err: err.Error()})
  158. return
  159. }
  160. delete(s.vols, v.Name)
  161. send(w, nil)
  162. })
  163. mux.HandleFunc("/VolumeDriver.Path", func(w http.ResponseWriter, r *http.Request) {
  164. s.ec.paths++
  165. pr, err := read(r.Body)
  166. if err != nil {
  167. send(w, err)
  168. return
  169. }
  170. p := hostVolumePath(pr.Name)
  171. send(w, &pluginResp{Mountpoint: p})
  172. })
  173. mux.HandleFunc("/VolumeDriver.Mount", func(w http.ResponseWriter, r *http.Request) {
  174. s.ec.mounts++
  175. pr, err := read(r.Body)
  176. if err != nil {
  177. send(w, err)
  178. return
  179. }
  180. if v, exists := s.vols[pr.Name]; exists {
  181. // Use this to simulate a mount failure
  182. if _, exists := v.Options["invalidOption"]; exists {
  183. send(w, fmt.Errorf("invalid argument"))
  184. return
  185. }
  186. }
  187. p := hostVolumePath(pr.Name)
  188. if err := os.MkdirAll(p, 0755); err != nil {
  189. send(w, &pluginResp{Err: err.Error()})
  190. return
  191. }
  192. if err := ioutil.WriteFile(filepath.Join(p, "test"), []byte(s.Server.URL), 0644); err != nil {
  193. send(w, err)
  194. return
  195. }
  196. if err := ioutil.WriteFile(filepath.Join(p, "mountID"), []byte(pr.ID), 0644); err != nil {
  197. send(w, err)
  198. return
  199. }
  200. send(w, &pluginResp{Mountpoint: p})
  201. })
  202. mux.HandleFunc("/VolumeDriver.Unmount", func(w http.ResponseWriter, r *http.Request) {
  203. s.ec.unmounts++
  204. _, err := read(r.Body)
  205. if err != nil {
  206. send(w, err)
  207. return
  208. }
  209. send(w, nil)
  210. })
  211. mux.HandleFunc("/VolumeDriver.Capabilities", func(w http.ResponseWriter, r *http.Request) {
  212. s.ec.caps++
  213. _, err := read(r.Body)
  214. if err != nil {
  215. send(w, err)
  216. return
  217. }
  218. send(w, `{"Capabilities": { "Scope": "global" }}`)
  219. })
  220. err := os.MkdirAll("/etc/docker/plugins", 0755)
  221. assert.NilError(c, err)
  222. err = ioutil.WriteFile("/etc/docker/plugins/"+name+".spec", []byte(s.Server.URL), 0644)
  223. assert.NilError(c, err)
  224. return s
  225. }
  226. func (s *DockerExternalVolumeSuite) TearDownSuite(c *testing.T) {
  227. s.volumePlugin.Close()
  228. err := os.RemoveAll("/etc/docker/plugins")
  229. assert.NilError(c, err)
  230. }
  231. func (s *DockerExternalVolumeSuite) TestVolumeCLICreateOptionConflict(c *testing.T) {
  232. dockerCmd(c, "volume", "create", "test")
  233. out, _, err := dockerCmdWithError("volume", "create", "test", "--driver", volumePluginName)
  234. assert.Assert(c, err != nil, "volume create exception name already in use with another driver")
  235. assert.Assert(c, strings.Contains(out, "must be unique"))
  236. out, _ = dockerCmd(c, "volume", "inspect", "--format={{ .Driver }}", "test")
  237. _, _, err = dockerCmdWithError("volume", "create", "test", "--driver", strings.TrimSpace(out))
  238. assert.NilError(c, err)
  239. }
  240. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverNamed(c *testing.T) {
  241. s.d.StartWithBusybox(c)
  242. out, err := s.d.Cmd("run", "--rm", "--name", "test-data", "-v", "external-volume-test:/tmp/external-volume-test", "--volume-driver", volumePluginName, "busybox:latest", "cat", "/tmp/external-volume-test/test")
  243. assert.NilError(c, err, out)
  244. assert.Assert(c, strings.Contains(out, s.Server.URL))
  245. _, err = s.d.Cmd("volume", "rm", "external-volume-test")
  246. assert.NilError(c, err)
  247. p := hostVolumePath("external-volume-test")
  248. _, err = os.Lstat(p)
  249. assert.ErrorContains(c, err, "")
  250. assert.Assert(c, os.IsNotExist(err), "Expected volume path in host to not exist: %s, %v\n", p, err)
  251. assert.Equal(c, s.ec.activations, 1)
  252. assert.Equal(c, s.ec.creations, 1)
  253. assert.Equal(c, s.ec.removals, 1)
  254. assert.Equal(c, s.ec.mounts, 1)
  255. assert.Equal(c, s.ec.unmounts, 1)
  256. }
  257. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverUnnamed(c *testing.T) {
  258. s.d.StartWithBusybox(c)
  259. out, err := s.d.Cmd("run", "--rm", "--name", "test-data", "-v", "/tmp/external-volume-test", "--volume-driver", volumePluginName, "busybox:latest", "cat", "/tmp/external-volume-test/test")
  260. assert.NilError(c, err, out)
  261. assert.Assert(c, strings.Contains(out, s.Server.URL))
  262. assert.Equal(c, s.ec.activations, 1)
  263. assert.Equal(c, s.ec.creations, 1)
  264. assert.Equal(c, s.ec.removals, 1)
  265. assert.Equal(c, s.ec.mounts, 1)
  266. assert.Equal(c, s.ec.unmounts, 1)
  267. }
  268. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverVolumesFrom(c *testing.T) {
  269. s.d.StartWithBusybox(c)
  270. out, err := s.d.Cmd("run", "--name", "vol-test1", "-v", "/foo", "--volume-driver", volumePluginName, "busybox:latest")
  271. assert.NilError(c, err, out)
  272. out, err = s.d.Cmd("run", "--rm", "--volumes-from", "vol-test1", "--name", "vol-test2", "busybox", "ls", "/tmp")
  273. assert.NilError(c, err, out)
  274. out, err = s.d.Cmd("rm", "-fv", "vol-test1")
  275. assert.NilError(c, err, out)
  276. assert.Equal(c, s.ec.activations, 1)
  277. assert.Equal(c, s.ec.creations, 1)
  278. assert.Equal(c, s.ec.removals, 1)
  279. assert.Equal(c, s.ec.mounts, 2)
  280. assert.Equal(c, s.ec.unmounts, 2)
  281. }
  282. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverDeleteContainer(c *testing.T) {
  283. s.d.StartWithBusybox(c)
  284. out, err := s.d.Cmd("run", "--name", "vol-test1", "-v", "/foo", "--volume-driver", volumePluginName, "busybox:latest")
  285. assert.NilError(c, err, out)
  286. out, err = s.d.Cmd("rm", "-fv", "vol-test1")
  287. assert.NilError(c, err, out)
  288. assert.Equal(c, s.ec.activations, 1)
  289. assert.Equal(c, s.ec.creations, 1)
  290. assert.Equal(c, s.ec.removals, 1)
  291. assert.Equal(c, s.ec.mounts, 1)
  292. assert.Equal(c, s.ec.unmounts, 1)
  293. }
  294. func hostVolumePath(name string) string {
  295. return fmt.Sprintf("/var/lib/docker/volumes/%s", name)
  296. }
  297. // Make sure a request to use a down driver doesn't block other requests
  298. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverLookupNotBlocked(c *testing.T) {
  299. specPath := "/etc/docker/plugins/down-driver.spec"
  300. err := ioutil.WriteFile(specPath, []byte("tcp://127.0.0.7:9999"), 0644)
  301. assert.NilError(c, err)
  302. defer os.RemoveAll(specPath)
  303. chCmd1 := make(chan struct{})
  304. chCmd2 := make(chan error)
  305. cmd1 := exec.Command(dockerBinary, "volume", "create", "-d", "down-driver")
  306. cmd2 := exec.Command(dockerBinary, "volume", "create")
  307. assert.Assert(c, cmd1.Start() == nil)
  308. defer cmd1.Process.Kill()
  309. time.Sleep(100 * time.Millisecond) // ensure API has been called
  310. assert.Assert(c, cmd2.Start() == nil)
  311. go func() {
  312. cmd1.Wait()
  313. close(chCmd1)
  314. }()
  315. go func() {
  316. chCmd2 <- cmd2.Wait()
  317. }()
  318. select {
  319. case <-chCmd1:
  320. cmd2.Process.Kill()
  321. c.Fatalf("volume create with down driver finished unexpectedly")
  322. case err := <-chCmd2:
  323. assert.NilError(c, err)
  324. case <-time.After(5 * time.Second):
  325. cmd2.Process.Kill()
  326. c.Fatal("volume creates are blocked by previous create requests when previous driver is down")
  327. }
  328. }
  329. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverRetryNotImmediatelyExists(c *testing.T) {
  330. s.d.StartWithBusybox(c)
  331. driverName := "test-external-volume-driver-retry"
  332. errchan := make(chan error)
  333. started := make(chan struct{})
  334. go func() {
  335. close(started)
  336. if out, err := s.d.Cmd("run", "--rm", "--name", "test-data-retry", "-v", "external-volume-test:/tmp/external-volume-test", "--volume-driver", driverName, "busybox:latest"); err != nil {
  337. errchan <- fmt.Errorf("%v:\n%s", err, out)
  338. }
  339. close(errchan)
  340. }()
  341. <-started
  342. // wait for a retry to occur, then create spec to allow plugin to register
  343. time.Sleep(2 * time.Second)
  344. p := newVolumePlugin(c, driverName)
  345. defer p.Close()
  346. select {
  347. case err := <-errchan:
  348. assert.NilError(c, err)
  349. case <-time.After(8 * time.Second):
  350. c.Fatal("volume creates fail when plugin not immediately available")
  351. }
  352. _, err := s.d.Cmd("volume", "rm", "external-volume-test")
  353. assert.NilError(c, err)
  354. assert.Equal(c, p.ec.activations, 1)
  355. assert.Equal(c, p.ec.creations, 1)
  356. assert.Equal(c, p.ec.removals, 1)
  357. assert.Equal(c, p.ec.mounts, 1)
  358. assert.Equal(c, p.ec.unmounts, 1)
  359. }
  360. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverBindExternalVolume(c *testing.T) {
  361. dockerCmd(c, "volume", "create", "-d", volumePluginName, "foo")
  362. dockerCmd(c, "run", "-d", "--name", "testing", "-v", "foo:/bar", "busybox", "top")
  363. var mounts []struct {
  364. Name string
  365. Driver string
  366. }
  367. out := inspectFieldJSON(c, "testing", "Mounts")
  368. assert.Assert(c, json.NewDecoder(strings.NewReader(out)).Decode(&mounts) == nil)
  369. assert.Equal(c, len(mounts), 1, fmt.Sprintf("%s", out))
  370. assert.Equal(c, mounts[0].Name, "foo")
  371. assert.Equal(c, mounts[0].Driver, volumePluginName)
  372. }
  373. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverList(c *testing.T) {
  374. dockerCmd(c, "volume", "create", "-d", volumePluginName, "abc3")
  375. out, _ := dockerCmd(c, "volume", "ls")
  376. ls := strings.Split(strings.TrimSpace(out), "\n")
  377. assert.Equal(c, len(ls), 2, fmt.Sprintf("\n%s", out))
  378. vol := strings.Fields(ls[len(ls)-1])
  379. assert.Equal(c, len(vol), 2, fmt.Sprintf("%v", vol))
  380. assert.Equal(c, vol[0], volumePluginName)
  381. assert.Equal(c, vol[1], "abc3")
  382. assert.Equal(c, s.ec.lists, 1)
  383. }
  384. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverGet(c *testing.T) {
  385. out, _, err := dockerCmdWithError("volume", "inspect", "dummy")
  386. assert.ErrorContains(c, err, "", out)
  387. assert.Assert(c, strings.Contains(out, "No such volume"))
  388. assert.Equal(c, s.ec.gets, 1)
  389. dockerCmd(c, "volume", "create", "test", "-d", volumePluginName)
  390. out, _ = dockerCmd(c, "volume", "inspect", "test")
  391. type vol struct {
  392. Status map[string]string
  393. }
  394. var st []vol
  395. assert.Assert(c, json.Unmarshal([]byte(out), &st) == nil)
  396. assert.Equal(c, len(st), 1)
  397. assert.Equal(c, len(st[0].Status), 1, fmt.Sprintf("%v", st[0]))
  398. assert.Equal(c, st[0].Status["Hello"], "world", fmt.Sprintf("%v", st[0].Status))
  399. }
  400. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverWithDaemonRestart(c *testing.T) {
  401. dockerCmd(c, "volume", "create", "-d", volumePluginName, "abc1")
  402. s.d.Restart(c)
  403. dockerCmd(c, "run", "--name=test", "-v", "abc1:/foo", "busybox", "true")
  404. var mounts []types.MountPoint
  405. inspectFieldAndUnmarshall(c, "test", "Mounts", &mounts)
  406. assert.Equal(c, len(mounts), 1)
  407. assert.Equal(c, mounts[0].Driver, volumePluginName)
  408. }
  409. // Ensures that the daemon handles when the plugin responds to a `Get` request with a null volume and a null error.
  410. // Prior the daemon would panic in this scenario.
  411. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverGetEmptyResponse(c *testing.T) {
  412. s.d.Start(c)
  413. out, err := s.d.Cmd("volume", "create", "-d", volumePluginName, "abc2", "--opt", "ninja=1")
  414. assert.NilError(c, err, out)
  415. out, err = s.d.Cmd("volume", "inspect", "abc2")
  416. assert.ErrorContains(c, err, "", out)
  417. assert.Assert(c, strings.Contains(out, "No such volume"))
  418. }
  419. // Ensure only cached paths are used in volume list to prevent N+1 calls to `VolumeDriver.Path`
  420. //
  421. // TODO(@cpuguy83): This test is testing internal implementation. In all the cases here, there may not even be a path
  422. // available because the volume is not even mounted. Consider removing this test.
  423. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverPathCalls(c *testing.T) {
  424. s.d.Start(c)
  425. assert.Equal(c, s.ec.paths, 0)
  426. out, err := s.d.Cmd("volume", "create", "test", "--driver=test-external-volume-driver")
  427. assert.NilError(c, err, out)
  428. assert.Equal(c, s.ec.paths, 0)
  429. out, err = s.d.Cmd("volume", "ls")
  430. assert.NilError(c, err, out)
  431. assert.Equal(c, s.ec.paths, 0)
  432. }
  433. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverMountID(c *testing.T) {
  434. s.d.StartWithBusybox(c)
  435. 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")
  436. assert.NilError(c, err, out)
  437. assert.Assert(c, strings.TrimSpace(out) != "")
  438. }
  439. // Check that VolumeDriver.Capabilities gets called, and only called once
  440. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverCapabilities(c *testing.T) {
  441. s.d.Start(c)
  442. assert.Equal(c, s.ec.caps, 0)
  443. for i := 0; i < 3; i++ {
  444. out, err := s.d.Cmd("volume", "create", "-d", volumePluginName, fmt.Sprintf("test%d", i))
  445. assert.NilError(c, err, out)
  446. assert.Equal(c, s.ec.caps, 1)
  447. out, err = s.d.Cmd("volume", "inspect", "--format={{.Scope}}", fmt.Sprintf("test%d", i))
  448. assert.NilError(c, err)
  449. assert.Equal(c, strings.TrimSpace(out), volume.GlobalScope)
  450. }
  451. }
  452. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverOutOfBandDelete(c *testing.T) {
  453. driverName := stringid.GenerateRandomID()
  454. p := newVolumePlugin(c, driverName)
  455. defer p.Close()
  456. s.d.StartWithBusybox(c)
  457. out, err := s.d.Cmd("volume", "create", "-d", driverName, "--name", "test")
  458. assert.NilError(c, err, out)
  459. out, err = s.d.Cmd("volume", "create", "-d", "local", "--name", "test")
  460. assert.ErrorContains(c, err, "", out)
  461. assert.Assert(c, strings.Contains(out, "must be unique"))
  462. // simulate out of band volume deletion on plugin level
  463. delete(p.vols, "test")
  464. // test re-create with same driver
  465. out, err = s.d.Cmd("volume", "create", "-d", driverName, "--opt", "foo=bar", "--name", "test")
  466. assert.NilError(c, err, out)
  467. out, err = s.d.Cmd("volume", "inspect", "test")
  468. assert.NilError(c, err, out)
  469. var vs []types.Volume
  470. err = json.Unmarshal([]byte(out), &vs)
  471. assert.NilError(c, err)
  472. assert.Equal(c, len(vs), 1)
  473. assert.Equal(c, vs[0].Driver, driverName)
  474. assert.Assert(c, vs[0].Options != nil)
  475. assert.Equal(c, vs[0].Options["foo"], "bar")
  476. assert.Equal(c, vs[0].Driver, driverName)
  477. // simulate out of band volume deletion on plugin level
  478. delete(p.vols, "test")
  479. // test create with different driver
  480. out, err = s.d.Cmd("volume", "create", "-d", "local", "--name", "test")
  481. assert.NilError(c, err, out)
  482. out, err = s.d.Cmd("volume", "inspect", "test")
  483. assert.NilError(c, err, out)
  484. vs = nil
  485. err = json.Unmarshal([]byte(out), &vs)
  486. assert.NilError(c, err)
  487. assert.Equal(c, len(vs), 1)
  488. assert.Equal(c, len(vs[0].Options), 0)
  489. assert.Equal(c, vs[0].Driver, "local")
  490. }
  491. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverUnmountOnMountFail(c *testing.T) {
  492. s.d.StartWithBusybox(c)
  493. s.d.Cmd("volume", "create", "-d", "test-external-volume-driver", "--opt=invalidOption=1", "--name=testumount")
  494. out, _ := s.d.Cmd("run", "-v", "testumount:/foo", "busybox", "true")
  495. assert.Equal(c, s.ec.unmounts, 0, fmt.Sprintf("%s", out))
  496. out, _ = s.d.Cmd("run", "-w", "/foo", "-v", "testumount:/foo", "busybox", "true")
  497. assert.Equal(c, s.ec.unmounts, 0, fmt.Sprintf("%s", out))
  498. }
  499. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverUnmountOnCp(c *testing.T) {
  500. s.d.StartWithBusybox(c)
  501. s.d.Cmd("volume", "create", "-d", "test-external-volume-driver", "--name=test")
  502. out, _ := s.d.Cmd("run", "-d", "--name=test", "-v", "test:/foo", "busybox", "/bin/sh", "-c", "touch /test && top")
  503. assert.Equal(c, s.ec.mounts, 1, fmt.Sprintf("%s", out))
  504. out, _ = s.d.Cmd("cp", "test:/test", "/tmp/test")
  505. assert.Equal(c, s.ec.mounts, 2, fmt.Sprintf("%s", out))
  506. assert.Equal(c, s.ec.unmounts, 1, fmt.Sprintf("%s", out))
  507. out, _ = s.d.Cmd("kill", "test")
  508. assert.Equal(c, s.ec.unmounts, 2, fmt.Sprintf("%s", out))
  509. }