statfs_linux_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package statfs_test
  15. import (
  16. "fmt"
  17. "math"
  18. "regexp"
  19. "syscall"
  20. "github.com/jacobsa/fuse/fuseops"
  21. . "github.com/jacobsa/ogletest"
  22. )
  23. // Sample output:
  24. //
  25. // Filesystem 1K-blocks Used Available Use% Mounted on
  26. // some_fuse_file_system 512 64 384 15% /tmp/sample_test001288095
  27. //
  28. var gDfOutputRegexp = regexp.MustCompile(`^\S+\s+(\d+)\s+(\d+)\s+(\d+)\s+\d+%.*$`)
  29. ////////////////////////////////////////////////////////////////////////
  30. // Tests
  31. ////////////////////////////////////////////////////////////////////////
  32. func (t *StatFSTest) Syscall_ZeroValues() {
  33. var err error
  34. var stat syscall.Statfs_t
  35. // Call without configuring a canned response, meaning the OS will see the
  36. // zero value for each field. The assertions below act as documentation for
  37. // the OS's behavior in this case.
  38. err = syscall.Statfs(t.Dir, &stat)
  39. AssertEq(nil, err)
  40. ExpectEq(0, stat.Bsize)
  41. ExpectEq(0, stat.Frsize)
  42. ExpectEq(0, stat.Blocks)
  43. ExpectEq(0, stat.Bfree)
  44. ExpectEq(0, stat.Bavail)
  45. ExpectEq(0, stat.Files)
  46. ExpectEq(0, stat.Ffree)
  47. }
  48. func (t *StatFSTest) Syscall_NonZeroValues() {
  49. var err error
  50. var stat syscall.Statfs_t
  51. // Set up the canned response.
  52. canned := fuseops.StatFSOp{
  53. BlockSize: 1 << 15,
  54. IoSize: 1 << 16,
  55. Blocks: 1<<51 + 3,
  56. BlocksFree: 1<<43 + 5,
  57. BlocksAvailable: 1<<41 + 7,
  58. Inodes: 1<<59 + 11,
  59. InodesFree: 1<<58 + 13,
  60. }
  61. t.fs.SetStatFSResponse(canned)
  62. // Stat.
  63. err = syscall.Statfs(t.Dir, &stat)
  64. AssertEq(nil, err)
  65. ExpectEq(canned.BlockSize, stat.Frsize)
  66. ExpectEq(canned.IoSize, stat.Bsize)
  67. ExpectEq(canned.Blocks, stat.Blocks)
  68. ExpectEq(canned.BlocksFree, stat.Bfree)
  69. ExpectEq(canned.BlocksAvailable, stat.Bavail)
  70. ExpectEq(canned.Inodes, stat.Files)
  71. ExpectEq(canned.InodesFree, stat.Ffree)
  72. }
  73. func (t *StatFSTest) BlockSizes() {
  74. var err error
  75. // Test a bunch of weird block sizes that OS X would be cranky about.
  76. blockSizes := []uint32{
  77. 0,
  78. 1,
  79. 3,
  80. 17,
  81. 1<<20 - 1,
  82. 1<<20 + 0,
  83. 1<<20 + 1,
  84. math.MaxInt32,
  85. math.MaxInt32 + 1,
  86. math.MaxUint32,
  87. }
  88. for _, bs := range blockSizes {
  89. desc := fmt.Sprintf("block size %d", bs)
  90. // Set up.
  91. canned := fuseops.StatFSOp{
  92. BlockSize: bs,
  93. Blocks: 10,
  94. }
  95. t.fs.SetStatFSResponse(canned)
  96. // Check.
  97. var stat syscall.Statfs_t
  98. err = syscall.Statfs(t.Dir, &stat)
  99. AssertEq(nil, err)
  100. ExpectEq(bs, stat.Frsize, "%s", desc)
  101. }
  102. }
  103. func (t *StatFSTest) IoSizes() {
  104. var err error
  105. // Test a bunch of weird IO sizes that OS X would be cranky about.
  106. ioSizes := []uint32{
  107. 0,
  108. 1,
  109. 3,
  110. 17,
  111. 1<<20 - 1,
  112. 1<<20 + 0,
  113. 1<<20 + 1,
  114. math.MaxInt32,
  115. math.MaxInt32 + 1,
  116. math.MaxUint32,
  117. }
  118. for _, bs := range ioSizes {
  119. desc := fmt.Sprintf("IO size %d", bs)
  120. // Set up.
  121. canned := fuseops.StatFSOp{
  122. IoSize: bs,
  123. Blocks: 10,
  124. }
  125. t.fs.SetStatFSResponse(canned)
  126. // Check.
  127. var stat syscall.Statfs_t
  128. err = syscall.Statfs(t.Dir, &stat)
  129. AssertEq(nil, err)
  130. ExpectEq(bs, stat.Bsize, "%s", desc)
  131. }
  132. }