docker_cli_external_volume_driver_unix_test.go 18 KB

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