docker_cli_external_volume_driver_unix_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. // +build !windows
  2. package main
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "os/exec"
  12. "path/filepath"
  13. "strings"
  14. "time"
  15. "github.com/docker/docker/api/types"
  16. "github.com/docker/docker/integration-cli/checker"
  17. "github.com/docker/docker/integration-cli/daemon"
  18. testdaemon "github.com/docker/docker/internal/test/daemon"
  19. "github.com/docker/docker/pkg/stringid"
  20. "github.com/docker/docker/volume"
  21. "github.com/go-check/check"
  22. "gotest.tools/assert"
  23. )
  24. const volumePluginName = "test-external-volume-driver"
  25. func init() {
  26. check.Suite(&DockerExternalVolumeSuite{
  27. ds: &DockerSuite{},
  28. })
  29. }
  30. type eventCounter struct {
  31. activations int
  32. creations int
  33. removals int
  34. mounts int
  35. unmounts int
  36. paths int
  37. lists int
  38. gets int
  39. caps int
  40. }
  41. type DockerExternalVolumeSuite struct {
  42. ds *DockerSuite
  43. d *daemon.Daemon
  44. *volumePlugin
  45. }
  46. func (s *DockerExternalVolumeSuite) SetUpTest(c *testing.T) {
  47. testRequires(c, testEnv.IsLocalDaemon)
  48. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  49. s.ec = &eventCounter{}
  50. }
  51. func (s *DockerExternalVolumeSuite) TearDownTest(c *testing.T) {
  52. if s.d != nil {
  53. s.d.Stop(c)
  54. s.ds.TearDownTest(c)
  55. }
  56. }
  57. func (s *DockerExternalVolumeSuite) SetUpSuite(c *testing.T) {
  58. s.volumePlugin = newVolumePlugin(c, volumePluginName)
  59. }
  60. type volumePlugin struct {
  61. ec *eventCounter
  62. *httptest.Server
  63. vols map[string]vol
  64. }
  65. type vol struct {
  66. Name string
  67. Mountpoint string
  68. Ninja bool // hack used to trigger a null volume return on `Get`
  69. Status map[string]interface{}
  70. Options map[string]string
  71. }
  72. func (p *volumePlugin) Close() {
  73. p.Server.Close()
  74. }
  75. func newVolumePlugin(c *testing.T, name string) *volumePlugin {
  76. mux := http.NewServeMux()
  77. s := &volumePlugin{Server: httptest.NewServer(mux), ec: &eventCounter{}, vols: make(map[string]vol)}
  78. type pluginRequest struct {
  79. Name string
  80. Opts map[string]string
  81. ID string
  82. }
  83. type pluginResp struct {
  84. Mountpoint string `json:",omitempty"`
  85. Err string `json:",omitempty"`
  86. }
  87. read := func(b io.ReadCloser) (pluginRequest, error) {
  88. defer b.Close()
  89. var pr pluginRequest
  90. err := json.NewDecoder(b).Decode(&pr)
  91. return pr, err
  92. }
  93. send := func(w http.ResponseWriter, data interface{}) {
  94. switch t := data.(type) {
  95. case error:
  96. http.Error(w, t.Error(), 500)
  97. case string:
  98. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  99. fmt.Fprintln(w, t)
  100. default:
  101. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  102. json.NewEncoder(w).Encode(&data)
  103. }
  104. }
  105. mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
  106. s.ec.activations++
  107. send(w, `{"Implements": ["VolumeDriver"]}`)
  108. })
  109. mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
  110. s.ec.creations++
  111. pr, err := read(r.Body)
  112. if err != nil {
  113. send(w, err)
  114. return
  115. }
  116. _, isNinja := pr.Opts["ninja"]
  117. status := map[string]interface{}{"Hello": "world"}
  118. s.vols[pr.Name] = vol{Name: pr.Name, Ninja: isNinja, Status: status, Options: pr.Opts}
  119. send(w, nil)
  120. })
  121. mux.HandleFunc("/VolumeDriver.List", func(w http.ResponseWriter, r *http.Request) {
  122. s.ec.lists++
  123. vols := make([]vol, 0, len(s.vols))
  124. for _, v := range s.vols {
  125. if v.Ninja {
  126. continue
  127. }
  128. vols = append(vols, v)
  129. }
  130. send(w, map[string][]vol{"Volumes": vols})
  131. })
  132. mux.HandleFunc("/VolumeDriver.Get", func(w http.ResponseWriter, r *http.Request) {
  133. s.ec.gets++
  134. pr, err := read(r.Body)
  135. if err != nil {
  136. send(w, err)
  137. return
  138. }
  139. v, exists := s.vols[pr.Name]
  140. if !exists {
  141. send(w, `{"Err": "no such volume"}`)
  142. }
  143. if v.Ninja {
  144. send(w, map[string]vol{})
  145. return
  146. }
  147. v.Mountpoint = hostVolumePath(pr.Name)
  148. send(w, map[string]vol{"Volume": v})
  149. return
  150. })
  151. mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
  152. s.ec.removals++
  153. pr, err := read(r.Body)
  154. if err != nil {
  155. send(w, err)
  156. return
  157. }
  158. v, ok := s.vols[pr.Name]
  159. if !ok {
  160. send(w, nil)
  161. return
  162. }
  163. if err := os.RemoveAll(hostVolumePath(v.Name)); err != nil {
  164. send(w, &pluginResp{Err: err.Error()})
  165. return
  166. }
  167. delete(s.vols, v.Name)
  168. send(w, nil)
  169. })
  170. mux.HandleFunc("/VolumeDriver.Path", func(w http.ResponseWriter, r *http.Request) {
  171. s.ec.paths++
  172. pr, err := read(r.Body)
  173. if err != nil {
  174. send(w, err)
  175. return
  176. }
  177. p := hostVolumePath(pr.Name)
  178. send(w, &pluginResp{Mountpoint: p})
  179. })
  180. mux.HandleFunc("/VolumeDriver.Mount", func(w http.ResponseWriter, r *http.Request) {
  181. s.ec.mounts++
  182. pr, err := read(r.Body)
  183. if err != nil {
  184. send(w, err)
  185. return
  186. }
  187. if v, exists := s.vols[pr.Name]; exists {
  188. // Use this to simulate a mount failure
  189. if _, exists := v.Options["invalidOption"]; exists {
  190. send(w, fmt.Errorf("invalid argument"))
  191. return
  192. }
  193. }
  194. p := hostVolumePath(pr.Name)
  195. if err := os.MkdirAll(p, 0755); err != nil {
  196. send(w, &pluginResp{Err: err.Error()})
  197. return
  198. }
  199. if err := ioutil.WriteFile(filepath.Join(p, "test"), []byte(s.Server.URL), 0644); err != nil {
  200. send(w, err)
  201. return
  202. }
  203. if err := ioutil.WriteFile(filepath.Join(p, "mountID"), []byte(pr.ID), 0644); err != nil {
  204. send(w, err)
  205. return
  206. }
  207. send(w, &pluginResp{Mountpoint: p})
  208. })
  209. mux.HandleFunc("/VolumeDriver.Unmount", func(w http.ResponseWriter, r *http.Request) {
  210. s.ec.unmounts++
  211. _, err := read(r.Body)
  212. if err != nil {
  213. send(w, err)
  214. return
  215. }
  216. send(w, nil)
  217. })
  218. mux.HandleFunc("/VolumeDriver.Capabilities", func(w http.ResponseWriter, r *http.Request) {
  219. s.ec.caps++
  220. _, err := read(r.Body)
  221. if err != nil {
  222. send(w, err)
  223. return
  224. }
  225. send(w, `{"Capabilities": { "Scope": "global" }}`)
  226. })
  227. err := os.MkdirAll("/etc/docker/plugins", 0755)
  228. assert.NilError(c, err)
  229. err = ioutil.WriteFile("/etc/docker/plugins/"+name+".spec", []byte(s.Server.URL), 0644)
  230. assert.NilError(c, err)
  231. return s
  232. }
  233. func (s *DockerExternalVolumeSuite) TearDownSuite(c *testing.T) {
  234. s.volumePlugin.Close()
  235. err := os.RemoveAll("/etc/docker/plugins")
  236. assert.NilError(c, err)
  237. }
  238. func (s *DockerExternalVolumeSuite) TestVolumeCLICreateOptionConflict(c *testing.T) {
  239. dockerCmd(c, "volume", "create", "test")
  240. out, _, err := dockerCmdWithError("volume", "create", "test", "--driver", volumePluginName)
  241. assert.Assert(c, err, checker.NotNil, check.Commentf("volume create exception name already in use with another driver"))
  242. assert.Assert(c, out, checker.Contains, "must be unique")
  243. out, _ = dockerCmd(c, "volume", "inspect", "--format={{ .Driver }}", "test")
  244. _, _, err = dockerCmdWithError("volume", "create", "test", "--driver", strings.TrimSpace(out))
  245. assert.NilError(c, err)
  246. }
  247. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverNamed(c *testing.T) {
  248. s.d.StartWithBusybox(c)
  249. 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")
  250. assert.NilError(c, err, out)
  251. assert.Assert(c, out, checker.Contains, s.Server.URL)
  252. _, err = s.d.Cmd("volume", "rm", "external-volume-test")
  253. assert.NilError(c, err)
  254. p := hostVolumePath("external-volume-test")
  255. _, err = os.Lstat(p)
  256. assert.ErrorContains(c, err, "")
  257. assert.Assert(c, os.IsNotExist(err), checker.True, check.Commentf("Expected volume path in host to not exist: %s, %v\n", p, err))
  258. assert.Equal(c, s.ec.activations, 1)
  259. assert.Equal(c, s.ec.creations, 1)
  260. assert.Equal(c, s.ec.removals, 1)
  261. assert.Equal(c, s.ec.mounts, 1)
  262. assert.Equal(c, s.ec.unmounts, 1)
  263. }
  264. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverUnnamed(c *testing.T) {
  265. s.d.StartWithBusybox(c)
  266. 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")
  267. assert.NilError(c, err, out)
  268. assert.Assert(c, out, checker.Contains, s.Server.URL)
  269. assert.Equal(c, s.ec.activations, 1)
  270. assert.Equal(c, s.ec.creations, 1)
  271. assert.Equal(c, s.ec.removals, 1)
  272. assert.Equal(c, s.ec.mounts, 1)
  273. assert.Equal(c, s.ec.unmounts, 1)
  274. }
  275. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverVolumesFrom(c *testing.T) {
  276. s.d.StartWithBusybox(c)
  277. out, err := s.d.Cmd("run", "--name", "vol-test1", "-v", "/foo", "--volume-driver", volumePluginName, "busybox:latest")
  278. assert.NilError(c, err, out)
  279. out, err = s.d.Cmd("run", "--rm", "--volumes-from", "vol-test1", "--name", "vol-test2", "busybox", "ls", "/tmp")
  280. assert.NilError(c, err, out)
  281. out, err = s.d.Cmd("rm", "-fv", "vol-test1")
  282. assert.NilError(c, err, out)
  283. assert.Equal(c, s.ec.activations, 1)
  284. assert.Equal(c, s.ec.creations, 1)
  285. assert.Equal(c, s.ec.removals, 1)
  286. assert.Equal(c, s.ec.mounts, 2)
  287. assert.Equal(c, s.ec.unmounts, 2)
  288. }
  289. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverDeleteContainer(c *testing.T) {
  290. s.d.StartWithBusybox(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 := ioutil.WriteFile(specPath, []byte("tcp://127.0.0.7:9999"), 0644)
  308. assert.NilError(c, err)
  309. defer os.RemoveAll(specPath)
  310. chCmd1 := make(chan struct{})
  311. chCmd2 := make(chan error)
  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. s.d.StartWithBusybox(c)
  338. driverName := "test-external-volume-driver-retry"
  339. errchan := make(chan error)
  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, check.Commentf("%s", 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, check.Commentf("\n%s", out))
  385. vol := strings.Fields(ls[len(ls)-1])
  386. assert.Equal(c, len(vol), 2, check.Commentf("%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, out, checker.Contains, "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, check.Commentf("%v", st[0]))
  405. assert.Equal(c, st[0].Status["Hello"], "world", check.Commentf("%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, out, checker.Contains, "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
  429. // 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. s.d.StartWithBusybox(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. driverName := stringid.GenerateRandomID()
  461. p := newVolumePlugin(c, driverName)
  462. defer p.Close()
  463. s.d.StartWithBusybox(c)
  464. out, err := s.d.Cmd("volume", "create", "-d", driverName, "--name", "test")
  465. assert.NilError(c, err, out)
  466. out, err = s.d.Cmd("volume", "create", "-d", "local", "--name", "test")
  467. assert.ErrorContains(c, err, "", out)
  468. assert.Assert(c, out, checker.Contains, "must be unique")
  469. // simulate out of band volume deletion on plugin level
  470. delete(p.vols, "test")
  471. // test re-create with same driver
  472. out, err = s.d.Cmd("volume", "create", "-d", driverName, "--opt", "foo=bar", "--name", "test")
  473. assert.NilError(c, err, out)
  474. out, err = s.d.Cmd("volume", "inspect", "test")
  475. assert.NilError(c, err, out)
  476. var vs []types.Volume
  477. err = json.Unmarshal([]byte(out), &vs)
  478. assert.NilError(c, err)
  479. assert.Equal(c, len(vs), 1)
  480. assert.Equal(c, vs[0].Driver, driverName)
  481. assert.Assert(c, vs[0].Options, checker.NotNil)
  482. assert.Equal(c, vs[0].Options["foo"], "bar")
  483. assert.Equal(c, vs[0].Driver, driverName)
  484. // simulate out of band volume deletion on plugin level
  485. delete(p.vols, "test")
  486. // test create with different driver
  487. out, err = s.d.Cmd("volume", "create", "-d", "local", "--name", "test")
  488. assert.NilError(c, err, out)
  489. out, err = s.d.Cmd("volume", "inspect", "test")
  490. assert.NilError(c, err, out)
  491. vs = nil
  492. err = json.Unmarshal([]byte(out), &vs)
  493. assert.NilError(c, err)
  494. assert.Equal(c, len(vs), 1)
  495. assert.Equal(c, len(vs[0].Options), 0)
  496. assert.Equal(c, vs[0].Driver, "local")
  497. }
  498. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverUnmountOnMountFail(c *testing.T) {
  499. s.d.StartWithBusybox(c)
  500. s.d.Cmd("volume", "create", "-d", "test-external-volume-driver", "--opt=invalidOption=1", "--name=testumount")
  501. out, _ := s.d.Cmd("run", "-v", "testumount:/foo", "busybox", "true")
  502. assert.Equal(c, s.ec.unmounts, 0, check.Commentf("%s", out))
  503. out, _ = s.d.Cmd("run", "-w", "/foo", "-v", "testumount:/foo", "busybox", "true")
  504. assert.Equal(c, s.ec.unmounts, 0, check.Commentf("%s", out))
  505. }
  506. func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverUnmountOnCp(c *testing.T) {
  507. s.d.StartWithBusybox(c)
  508. s.d.Cmd("volume", "create", "-d", "test-external-volume-driver", "--name=test")
  509. out, _ := s.d.Cmd("run", "-d", "--name=test", "-v", "test:/foo", "busybox", "/bin/sh", "-c", "touch /test && top")
  510. assert.Equal(c, s.ec.mounts, 1, check.Commentf("%s", out))
  511. out, _ = s.d.Cmd("cp", "test:/test", "/tmp/test")
  512. assert.Equal(c, s.ec.mounts, 2, check.Commentf("%s", out))
  513. assert.Equal(c, s.ec.unmounts, 1, check.Commentf("%s", out))
  514. out, _ = s.d.Cmd("kill", "test")
  515. assert.Equal(c, s.ec.unmounts, 2, check.Commentf("%s", out))
  516. }