docker_cli_external_volume_driver_unix_test.go 18 KB

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