volumes_linux_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package daemon
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/docker/docker/api/types/mount"
  6. )
  7. func TestBindDaemonRoot(t *testing.T) {
  8. t.Parallel()
  9. d := &Daemon{root: "/a/b/c/daemon"}
  10. for _, test := range []struct {
  11. desc string
  12. opts *mount.BindOptions
  13. needsProp bool
  14. err bool
  15. }{
  16. {desc: "nil propagation settings", opts: nil, needsProp: true, err: false},
  17. {desc: "empty propagation settings", opts: &mount.BindOptions{}, needsProp: true, err: false},
  18. {desc: "private propagation", opts: &mount.BindOptions{Propagation: mount.PropagationPrivate}, err: true},
  19. {desc: "rprivate propagation", opts: &mount.BindOptions{Propagation: mount.PropagationRPrivate}, err: true},
  20. {desc: "slave propagation", opts: &mount.BindOptions{Propagation: mount.PropagationSlave}, err: true},
  21. {desc: "rslave propagation", opts: &mount.BindOptions{Propagation: mount.PropagationRSlave}, err: false, needsProp: false},
  22. {desc: "shared propagation", opts: &mount.BindOptions{Propagation: mount.PropagationShared}, err: true},
  23. {desc: "rshared propagation", opts: &mount.BindOptions{Propagation: mount.PropagationRSlave}, err: false, needsProp: false},
  24. } {
  25. t.Run(test.desc, func(t *testing.T) {
  26. test := test
  27. for desc, source := range map[string]string{
  28. "source is root": d.root,
  29. "source is subpath": filepath.Join(d.root, "a", "b"),
  30. "source is parent": filepath.Dir(d.root),
  31. "source is /": "/",
  32. } {
  33. t.Run(desc, func(t *testing.T) {
  34. mnt := mount.Mount{
  35. Type: mount.TypeBind,
  36. Source: source,
  37. BindOptions: test.opts,
  38. }
  39. needsProp, err := d.validateBindDaemonRoot(mnt)
  40. if (err != nil) != test.err {
  41. t.Fatalf("expected err=%v, got: %v", test.err, err)
  42. }
  43. if test.err {
  44. return
  45. }
  46. if test.needsProp != needsProp {
  47. t.Fatalf("expected needsProp=%v, got: %v", test.needsProp, needsProp)
  48. }
  49. })
  50. }
  51. })
  52. }
  53. }