removeall_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright 2017 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 removeall
  14. import (
  15. "errors"
  16. "os"
  17. "path"
  18. "strings"
  19. "testing"
  20. utiltesting "k8s.io/client-go/util/testing"
  21. "k8s.io/kubernetes/pkg/util/mount"
  22. )
  23. type fakeMounter struct {
  24. mount.FakeMounter
  25. }
  26. // IsLikelyNotMountPoint overrides mount.FakeMounter.IsLikelyNotMountPoint for our use.
  27. func (f *fakeMounter) IsLikelyNotMountPoint(file string) (bool, error) {
  28. name := path.Base(file)
  29. if strings.HasPrefix(name, "mount") {
  30. return false, nil
  31. }
  32. if strings.HasPrefix(name, "err") {
  33. return false, errors.New("mock error")
  34. }
  35. return true, nil
  36. }
  37. func TestRemoveAllOneFilesystem(t *testing.T) {
  38. tests := []struct {
  39. name string
  40. // Items of the test directory. Directories end with "/".
  41. // Directories starting with "mount" are considered to be mount points.
  42. // Directories starting with "err" will cause an error in
  43. // IsLikelyNotMountPoint.
  44. items []string
  45. expectError bool
  46. }{
  47. {
  48. "empty dir",
  49. []string{},
  50. false,
  51. },
  52. {
  53. "non-mount",
  54. []string{
  55. "dir/",
  56. "dir/file",
  57. "dir2/",
  58. "file2",
  59. },
  60. false,
  61. },
  62. {
  63. "mount",
  64. []string{
  65. "dir/",
  66. "dir/file",
  67. "dir2/",
  68. "file2",
  69. "mount/",
  70. "mount/file3",
  71. },
  72. true,
  73. },
  74. {
  75. "innermount",
  76. []string{
  77. "dir/",
  78. "dir/file",
  79. "dir/dir2/",
  80. "dir/dir2/file2",
  81. "dir/dir2/mount/",
  82. "dir/dir2/mount/file3",
  83. },
  84. true,
  85. },
  86. {
  87. "error",
  88. []string{
  89. "dir/",
  90. "dir/file",
  91. "dir2/",
  92. "file2",
  93. "err/",
  94. "err/file3",
  95. },
  96. true,
  97. },
  98. }
  99. for _, test := range tests {
  100. tmpDir, err := utiltesting.MkTmpdir("removeall-" + test.name + "-")
  101. if err != nil {
  102. t.Fatalf("Can't make a tmp dir: %v", err)
  103. }
  104. defer os.RemoveAll(tmpDir)
  105. // Create the directory structure
  106. for _, item := range test.items {
  107. if strings.HasSuffix(item, "/") {
  108. item = strings.TrimRight(item, "/")
  109. if err = os.Mkdir(path.Join(tmpDir, item), 0777); err != nil {
  110. t.Fatalf("error creating %s: %v", item, err)
  111. }
  112. } else {
  113. f, err := os.Create(path.Join(tmpDir, item))
  114. if err != nil {
  115. t.Fatalf("error creating %s: %v", item, err)
  116. }
  117. f.Close()
  118. }
  119. }
  120. mounter := &fakeMounter{}
  121. err = RemoveAllOneFilesystem(mounter, tmpDir)
  122. if err == nil && test.expectError {
  123. t.Errorf("test %q failed: expected error and got none", test.name)
  124. }
  125. if err != nil && !test.expectError {
  126. t.Errorf("test %q failed: unexpected error: %v", test.name, err)
  127. }
  128. }
  129. }