mt.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. Copyright 2015 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 mounttest
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "time"
  19. "github.com/spf13/cobra"
  20. )
  21. // CmdMounttest is used by agnhost Cobra.
  22. var CmdMounttest = &cobra.Command{
  23. Use: "mounttest",
  24. Short: "Creates files with given permissions and outputs FS type, owner, mode, permissions, contents of files",
  25. Long: "Creates files with specific file permissions, and outputs the filesystem type, owner, mode, permissions, content of the given files, if they exist.",
  26. Args: cobra.MaximumNArgs(0),
  27. Run: main,
  28. }
  29. var (
  30. fsTypePath = ""
  31. fileModePath = ""
  32. filePermPath = ""
  33. fileOwnerPath = ""
  34. newFilePath0644 = ""
  35. newFilePath0666 = ""
  36. newFilePath0660 = ""
  37. newFilePath0777 = ""
  38. readFileContentPath = ""
  39. readFileContentInLoopPath = ""
  40. retryDuration = 180
  41. breakOnExpectedContent = true
  42. )
  43. func init() {
  44. CmdMounttest.Flags().StringVar(&fsTypePath, "fs_type", "", "Path to print the fs type for")
  45. CmdMounttest.Flags().StringVar(&fileModePath, "file_mode", "", "Path to print the mode bits of")
  46. CmdMounttest.Flags().StringVar(&filePermPath, "file_perm", "", "Path to print the perms of")
  47. CmdMounttest.Flags().StringVar(&fileOwnerPath, "file_owner", "", "Path to print the owning UID and GID of")
  48. CmdMounttest.Flags().StringVar(&newFilePath0644, "new_file_0644", "", "Path to write to and read from with perm 0644")
  49. CmdMounttest.Flags().StringVar(&newFilePath0666, "new_file_0666", "", "Path to write to and read from with perm 0666")
  50. CmdMounttest.Flags().StringVar(&newFilePath0660, "new_file_0660", "", "Path to write to and read from with perm 0660")
  51. CmdMounttest.Flags().StringVar(&newFilePath0777, "new_file_0777", "", "Path to write to and read from with perm 0777")
  52. CmdMounttest.Flags().StringVar(&readFileContentPath, "file_content", "", "Path to read the file content from")
  53. CmdMounttest.Flags().StringVar(&readFileContentInLoopPath, "file_content_in_loop", "", "Path to read the file content in loop from")
  54. CmdMounttest.Flags().IntVar(&retryDuration, "retry_time", 180, "Retry time during the loop")
  55. CmdMounttest.Flags().BoolVar(&breakOnExpectedContent, "break_on_expected_content", true, "Break out of loop on expected content, (use with --file_content_in_loop flag only)")
  56. }
  57. // This program performs some tests on the filesystem as dictated by the
  58. // flags passed by the user.
  59. func main(cmd *cobra.Command, args []string) {
  60. var (
  61. err error
  62. errs = []error{}
  63. )
  64. // Clear the umask so we can set any mode bits we want.
  65. umask(0000)
  66. // NOTE: the ordering of execution of the various command line
  67. // flags is intentional and allows a single command to:
  68. //
  69. // 1. Check the fstype of a path
  70. // 2. Write a new file within that path
  71. // 3. Check that the file's content can be read
  72. //
  73. // Changing the ordering of the following code will break tests.
  74. err = fsType(fsTypePath)
  75. if err != nil {
  76. errs = append(errs, err)
  77. }
  78. err = readWriteNewFile(newFilePath0644, 0644)
  79. if err != nil {
  80. errs = append(errs, err)
  81. }
  82. err = readWriteNewFile(newFilePath0666, 0666)
  83. if err != nil {
  84. errs = append(errs, err)
  85. }
  86. err = readWriteNewFile(newFilePath0660, 0660)
  87. if err != nil {
  88. errs = append(errs, err)
  89. }
  90. err = readWriteNewFile(newFilePath0777, 0777)
  91. if err != nil {
  92. errs = append(errs, err)
  93. }
  94. err = fileMode(fileModePath)
  95. if err != nil {
  96. errs = append(errs, err)
  97. }
  98. err = filePerm(filePermPath)
  99. if err != nil {
  100. errs = append(errs, err)
  101. }
  102. err = fileOwner(fileOwnerPath)
  103. if err != nil {
  104. errs = append(errs, err)
  105. }
  106. err = readFileContent(readFileContentPath)
  107. if err != nil {
  108. errs = append(errs, err)
  109. }
  110. err = readFileContentInLoop(readFileContentInLoopPath, retryDuration, breakOnExpectedContent)
  111. if err != nil {
  112. errs = append(errs, err)
  113. }
  114. if len(errs) != 0 {
  115. os.Exit(1)
  116. }
  117. os.Exit(0)
  118. }
  119. func readFileContent(path string) error {
  120. if path == "" {
  121. return nil
  122. }
  123. contentBytes, err := ioutil.ReadFile(path)
  124. if err != nil {
  125. fmt.Printf("error reading file content for %q: %v\n", path, err)
  126. return err
  127. }
  128. fmt.Printf("content of file %q: %v\n", path, string(contentBytes))
  129. return nil
  130. }
  131. const initialContent string = "mount-tester new file\n"
  132. func readWriteNewFile(path string, perm os.FileMode) error {
  133. if path == "" {
  134. return nil
  135. }
  136. err := ioutil.WriteFile(path, []byte(initialContent), perm)
  137. if err != nil {
  138. fmt.Printf("error writing new file %q: %v\n", path, err)
  139. return err
  140. }
  141. return readFileContent(path)
  142. }
  143. func readFileContentInLoop(path string, retryDuration int, breakOnExpectedContent bool) error {
  144. if path == "" {
  145. return nil
  146. }
  147. return testFileContent(path, retryDuration, breakOnExpectedContent)
  148. }
  149. func testFileContent(filePath string, retryDuration int, breakOnExpectedContent bool) error {
  150. var (
  151. contentBytes []byte
  152. err error
  153. )
  154. retryTime := time.Second * time.Duration(retryDuration)
  155. for start := time.Now(); time.Since(start) < retryTime; time.Sleep(2 * time.Second) {
  156. contentBytes, err = ioutil.ReadFile(filePath)
  157. if err != nil {
  158. fmt.Printf("Error reading file %s: %v, retrying\n", filePath, err)
  159. continue
  160. }
  161. fmt.Printf("content of file %q: %v\n", filePath, string(contentBytes))
  162. if breakOnExpectedContent {
  163. if string(contentBytes) != initialContent {
  164. fmt.Printf("Unexpected content. Expected: %s. Retrying", initialContent)
  165. continue
  166. }
  167. break
  168. }
  169. }
  170. return err
  171. }