mount_helper_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package mount
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "syscall"
  20. "testing"
  21. )
  22. func TestDoCleanupMountPoint(t *testing.T) {
  23. const testMount = "test-mount"
  24. const defaultPerm = 0750
  25. tests := map[string]struct {
  26. corruptedMnt bool
  27. // Function that prepares the directory structure for the test under
  28. // the given base directory.
  29. // Returns a fake MountPoint, a fake error for the mount point,
  30. // and error if the prepare function encountered a fatal error.
  31. prepare func(base string) (MountPoint, error, error)
  32. expectErr bool
  33. }{
  34. "mount-ok": {
  35. prepare: func(base string) (MountPoint, error, error) {
  36. path := filepath.Join(base, testMount)
  37. if err := os.MkdirAll(path, defaultPerm); err != nil {
  38. return MountPoint{}, nil, err
  39. }
  40. return MountPoint{Device: "/dev/sdb", Path: path}, nil, nil
  41. },
  42. },
  43. "mount-corrupted": {
  44. prepare: func(base string) (MountPoint, error, error) {
  45. path := filepath.Join(base, testMount)
  46. if err := os.MkdirAll(path, defaultPerm); err != nil {
  47. return MountPoint{}, nil, err
  48. }
  49. return MountPoint{Device: "/dev/sdb", Path: path}, os.NewSyscallError("fake", syscall.ESTALE), nil
  50. },
  51. corruptedMnt: true,
  52. },
  53. "mount-err-not-corrupted": {
  54. prepare: func(base string) (MountPoint, error, error) {
  55. path := filepath.Join(base, testMount)
  56. if err := os.MkdirAll(path, defaultPerm); err != nil {
  57. return MountPoint{}, nil, err
  58. }
  59. return MountPoint{Device: "/dev/sdb", Path: path}, os.NewSyscallError("fake", syscall.ETIMEDOUT), nil
  60. },
  61. expectErr: true,
  62. },
  63. }
  64. for name, tt := range tests {
  65. t.Run(name, func(t *testing.T) {
  66. tmpDir, err := ioutil.TempDir("", "unmount-mount-point-test")
  67. if err != nil {
  68. t.Fatalf("failed to create tmpdir: %v", err)
  69. }
  70. defer os.RemoveAll(tmpDir)
  71. if tt.prepare == nil {
  72. t.Fatalf("prepare function required")
  73. }
  74. mountPoint, mountError, err := tt.prepare(tmpDir)
  75. if err != nil {
  76. t.Fatalf("failed to prepare test: %v", err)
  77. }
  78. fake := &FakeMounter{
  79. MountPoints: []MountPoint{mountPoint},
  80. MountCheckErrors: map[string]error{mountPoint.Path: mountError},
  81. }
  82. err = doCleanupMountPoint(mountPoint.Path, fake, true, tt.corruptedMnt)
  83. if tt.expectErr {
  84. if err == nil {
  85. t.Errorf("test %s failed, expected error, got none", name)
  86. }
  87. if err := validateDirExists(mountPoint.Path); err != nil {
  88. t.Errorf("test %s failed, mount path doesn't exist: %v", name, err)
  89. }
  90. }
  91. if !tt.expectErr {
  92. if err != nil {
  93. t.Errorf("test %s failed: %v", name, err)
  94. }
  95. if err := validateDirNotExists(mountPoint.Path); err != nil {
  96. t.Errorf("test %s failed, mount path still exists: %v", name, err)
  97. }
  98. }
  99. })
  100. }
  101. }
  102. func validateDirExists(dir string) error {
  103. _, err := ioutil.ReadDir(dir)
  104. if err != nil {
  105. return err
  106. }
  107. return nil
  108. }
  109. func validateDirNotExists(dir string) error {
  110. _, err := ioutil.ReadDir(dir)
  111. if os.IsNotExist(err) {
  112. return nil
  113. }
  114. if err != nil {
  115. return err
  116. }
  117. return fmt.Errorf("dir %q still exists", dir)
  118. }