safe_format_and_mount_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. Copyright 2014 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. "runtime"
  19. "testing"
  20. fakeexec "k8s.io/utils/exec/testing"
  21. )
  22. type ErrorMounter struct {
  23. *FakeMounter
  24. errIndex int
  25. err []error
  26. }
  27. func (mounter *ErrorMounter) Mount(source string, target string, fstype string, options []string) error {
  28. i := mounter.errIndex
  29. mounter.errIndex++
  30. if mounter.err != nil && mounter.err[i] != nil {
  31. return mounter.err[i]
  32. }
  33. return mounter.FakeMounter.Mount(source, target, fstype, options)
  34. }
  35. type ExecArgs struct {
  36. command string
  37. args []string
  38. output string
  39. err error
  40. }
  41. func TestSafeFormatAndMount(t *testing.T) {
  42. if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
  43. t.Skipf("not supported on GOOS=%s", runtime.GOOS)
  44. }
  45. mntDir, err := ioutil.TempDir(os.TempDir(), "mount")
  46. if err != nil {
  47. t.Fatalf("failed to create tmp dir: %v", err)
  48. }
  49. defer os.RemoveAll(mntDir)
  50. tests := []struct {
  51. description string
  52. fstype string
  53. mountOptions []string
  54. execScripts []ExecArgs
  55. mountErrs []error
  56. expectedError error
  57. }{
  58. {
  59. description: "Test a read only mount",
  60. fstype: "ext4",
  61. mountOptions: []string{"ro"},
  62. },
  63. {
  64. description: "Test a normal mount",
  65. fstype: "ext4",
  66. execScripts: []ExecArgs{
  67. {"fsck", []string{"-a", "/dev/foo"}, "", nil},
  68. },
  69. },
  70. {
  71. description: "Test 'fsck' fails with exit status 4",
  72. fstype: "ext4",
  73. execScripts: []ExecArgs{
  74. {"fsck", []string{"-a", "/dev/foo"}, "", &fakeexec.FakeExitError{Status: 4}},
  75. },
  76. expectedError: fmt.Errorf("'fsck' found errors on device /dev/foo but could not correct them: ."),
  77. },
  78. {
  79. description: "Test 'fsck' fails with exit status 1 (errors found and corrected)",
  80. fstype: "ext4",
  81. execScripts: []ExecArgs{
  82. {"fsck", []string{"-a", "/dev/foo"}, "", &fakeexec.FakeExitError{Status: 1}},
  83. },
  84. },
  85. {
  86. description: "Test 'fsck' fails with exit status other than 1 and 4 (likely unformatted device)",
  87. fstype: "ext4",
  88. execScripts: []ExecArgs{
  89. {"fsck", []string{"-a", "/dev/foo"}, "", &fakeexec.FakeExitError{Status: 8}},
  90. },
  91. },
  92. {
  93. description: "Test that 'blkid' is called and fails",
  94. fstype: "ext4",
  95. mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'")},
  96. execScripts: []ExecArgs{
  97. {"fsck", []string{"-a", "/dev/foo"}, "", nil},
  98. {"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nTYPE=ext4\n", nil},
  99. },
  100. expectedError: fmt.Errorf("unknown filesystem type '(null)'"),
  101. },
  102. {
  103. description: "Test that 'blkid' is called and confirms unformatted disk, format fails",
  104. fstype: "ext4",
  105. mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'")},
  106. execScripts: []ExecArgs{
  107. {"fsck", []string{"-a", "/dev/foo"}, "", nil},
  108. {"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &fakeexec.FakeExitError{Status: 2}},
  109. {"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", fmt.Errorf("formatting failed")},
  110. },
  111. expectedError: fmt.Errorf("formatting failed"),
  112. },
  113. {
  114. description: "Test that 'blkid' is called and confirms unformatted disk, format passes, second mount fails",
  115. fstype: "ext4",
  116. mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'"), fmt.Errorf("Still cannot mount")},
  117. execScripts: []ExecArgs{
  118. {"fsck", []string{"-a", "/dev/foo"}, "", nil},
  119. {"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &fakeexec.FakeExitError{Status: 2}},
  120. {"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", nil},
  121. },
  122. expectedError: fmt.Errorf("Still cannot mount"),
  123. },
  124. {
  125. description: "Test that 'blkid' is called and confirms unformatted disk, format passes, second mount passes",
  126. fstype: "ext4",
  127. mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'"), nil},
  128. execScripts: []ExecArgs{
  129. {"fsck", []string{"-a", "/dev/foo"}, "", nil},
  130. {"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &fakeexec.FakeExitError{Status: 2}},
  131. {"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", nil},
  132. },
  133. expectedError: nil,
  134. },
  135. {
  136. description: "Test that 'blkid' is called and confirms unformatted disk, format passes, second mount passes with ext3",
  137. fstype: "ext3",
  138. mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'"), nil},
  139. execScripts: []ExecArgs{
  140. {"fsck", []string{"-a", "/dev/foo"}, "", nil},
  141. {"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &fakeexec.FakeExitError{Status: 2}},
  142. {"mkfs.ext3", []string{"-F", "-m0", "/dev/foo"}, "", nil},
  143. },
  144. expectedError: nil,
  145. },
  146. {
  147. description: "test that none ext4 fs does not get called with ext4 options.",
  148. fstype: "xfs",
  149. mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'"), nil},
  150. execScripts: []ExecArgs{
  151. {"fsck", []string{"-a", "/dev/foo"}, "", nil},
  152. {"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &fakeexec.FakeExitError{Status: 2}},
  153. {"mkfs.xfs", []string{"/dev/foo"}, "", nil},
  154. },
  155. expectedError: nil,
  156. },
  157. {
  158. description: "Test that 'blkid' is called and reports ext4 partition",
  159. fstype: "ext3",
  160. mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'")},
  161. execScripts: []ExecArgs{
  162. {"fsck", []string{"-a", "/dev/foo"}, "", nil},
  163. {"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nPTTYPE=dos\n", nil},
  164. },
  165. expectedError: fmt.Errorf("failed to mount the volume as \"ext3\", it already contains unknown data, probably partitions. Mount error: unknown filesystem type '(null)'"),
  166. },
  167. {
  168. description: "Test that 'blkid' is called but has some usage or other errors (an exit code of 4 is returned)",
  169. fstype: "xfs",
  170. mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'"), nil},
  171. execScripts: []ExecArgs{
  172. {"fsck", []string{"-a", "/dev/foo"}, "", nil},
  173. {"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &fakeexec.FakeExitError{Status: 4}},
  174. {"mkfs.xfs", []string{"/dev/foo"}, "", nil},
  175. },
  176. expectedError: fmt.Errorf("exit 4"),
  177. },
  178. }
  179. for _, test := range tests {
  180. execCallCount := 0
  181. execCallback := func(cmd string, args ...string) ([]byte, error) {
  182. if len(test.execScripts) <= execCallCount {
  183. t.Errorf("Unexpected command: %s %v", cmd, args)
  184. return nil, nil
  185. }
  186. script := test.execScripts[execCallCount]
  187. execCallCount++
  188. if script.command != cmd {
  189. t.Errorf("Unexpected command %s. Expecting %s", cmd, script.command)
  190. }
  191. for j := range args {
  192. if args[j] != script.args[j] {
  193. t.Errorf("Unexpected args %v. Expecting %v", args, script.args)
  194. }
  195. }
  196. return []byte(script.output), script.err
  197. }
  198. fakeMounter := ErrorMounter{&FakeMounter{}, 0, test.mountErrs}
  199. fakeExec := NewFakeExec(execCallback)
  200. mounter := SafeFormatAndMount{
  201. Interface: &fakeMounter,
  202. Exec: fakeExec,
  203. }
  204. device := "/dev/foo"
  205. dest := mntDir
  206. err := mounter.FormatAndMount(device, dest, test.fstype, test.mountOptions)
  207. if test.expectedError == nil {
  208. if err != nil {
  209. t.Errorf("test \"%s\" unexpected non-error: %v", test.description, err)
  210. }
  211. // Check that something was mounted on the directory
  212. isNotMountPoint, err := fakeMounter.IsLikelyNotMountPoint(dest)
  213. if err != nil || isNotMountPoint {
  214. t.Errorf("test \"%s\" the directory was not mounted", test.description)
  215. }
  216. //check that the correct device was mounted
  217. mountedDevice, _, err := GetDeviceNameFromMount(fakeMounter.FakeMounter, dest)
  218. if err != nil || mountedDevice != device {
  219. t.Errorf("test \"%s\" the correct device was not mounted", test.description)
  220. }
  221. } else {
  222. if err == nil || test.expectedError.Error() != err.Error() {
  223. t.Errorf("test \"%s\" unexpected error: \n [%v]. \nExpecting [%v]", test.description, err, test.expectedError)
  224. }
  225. }
  226. }
  227. }