exec_mount_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // +build linux
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package exec
  15. import (
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. "k8s.io/kubernetes/pkg/util/mount"
  21. )
  22. var (
  23. sourcePath = "/mnt/srv"
  24. destinationPath = "/mnt/dst"
  25. fsType = "xfs"
  26. mountOptions = []string{"vers=1", "foo=bar"}
  27. )
  28. func TestMount(t *testing.T) {
  29. exec := mount.NewFakeExec(func(cmd string, args ...string) ([]byte, error) {
  30. if cmd != "mount" {
  31. t.Errorf("expected mount command, got %q", cmd)
  32. }
  33. // mount -t fstype -o options source target
  34. expectedArgs := []string{"-t", fsType, "-o", strings.Join(mountOptions, ","), sourcePath, destinationPath}
  35. if !reflect.DeepEqual(expectedArgs, args) {
  36. t.Errorf("expected arguments %q, got %q", strings.Join(expectedArgs, " "), strings.Join(args, " "))
  37. }
  38. return nil, nil
  39. })
  40. wrappedMounter := &fakeMounter{FakeMounter: &mount.FakeMounter{}, t: t}
  41. mounter := NewExecMounter(exec, wrappedMounter)
  42. mounter.Mount(sourcePath, destinationPath, fsType, mountOptions)
  43. }
  44. func TestBindMount(t *testing.T) {
  45. cmdCount := 0
  46. exec := mount.NewFakeExec(func(cmd string, args ...string) ([]byte, error) {
  47. cmdCount++
  48. if cmd != "mount" {
  49. t.Errorf("expected mount command, got %q", cmd)
  50. }
  51. var expectedArgs []string
  52. switch cmdCount {
  53. case 1:
  54. // mount -t fstype -o "bind" source target
  55. expectedArgs = []string{"-t", fsType, "-o", "bind", sourcePath, destinationPath}
  56. case 2:
  57. // mount -t fstype -o "remount,opts" source target
  58. expectedArgs = []string{"-t", fsType, "-o", "bind,remount," + strings.Join(mountOptions, ","), sourcePath, destinationPath}
  59. }
  60. if !reflect.DeepEqual(expectedArgs, args) {
  61. t.Errorf("expected arguments %q, got %q", strings.Join(expectedArgs, " "), strings.Join(args, " "))
  62. }
  63. return nil, nil
  64. })
  65. wrappedMounter := &fakeMounter{FakeMounter: &mount.FakeMounter{}, t: t}
  66. mounter := NewExecMounter(exec, wrappedMounter)
  67. bindOptions := append(mountOptions, "bind")
  68. mounter.Mount(sourcePath, destinationPath, fsType, bindOptions)
  69. }
  70. func TestUnmount(t *testing.T) {
  71. exec := mount.NewFakeExec(func(cmd string, args ...string) ([]byte, error) {
  72. if cmd != "umount" {
  73. t.Errorf("expected unmount command, got %q", cmd)
  74. }
  75. // unmount $target
  76. expectedArgs := []string{destinationPath}
  77. if !reflect.DeepEqual(expectedArgs, args) {
  78. t.Errorf("expected arguments %q, got %q", strings.Join(expectedArgs, " "), strings.Join(args, " "))
  79. }
  80. return nil, nil
  81. })
  82. wrappedMounter := &fakeMounter{&mount.FakeMounter{}, t}
  83. mounter := NewExecMounter(exec, wrappedMounter)
  84. mounter.Unmount(destinationPath)
  85. }
  86. /* Fake wrapped mounter */
  87. type fakeMounter struct {
  88. *mount.FakeMounter
  89. t *testing.T
  90. }
  91. func (fm *fakeMounter) Mount(source string, target string, fstype string, options []string) error {
  92. // Mount() of wrapped mounter should never be called. We call exec instead.
  93. fm.t.Errorf("Unexpected wrapped mount call")
  94. return fmt.Errorf("Unexpected wrapped mount call")
  95. }
  96. func (fm *fakeMounter) Unmount(target string) error {
  97. // umount() of wrapped mounter should never be called. We call exec instead.
  98. fm.t.Errorf("Unexpected wrapped mount call")
  99. return fmt.Errorf("Unexpected wrapped mount call")
  100. }