github.com/scaleoutsean/fusego@v0.0.0-20220224074057-4a6429e46bb8/samples/statfs/statfs_darwin_test.go (about)

     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  
    15  package statfs_test
    16  
    17  import (
    18  	"fmt"
    19  	"math"
    20  	"regexp"
    21  	"syscall"
    22  
    23  	"github.com/scaleoutsean/fusego/fuseops"
    24  	. "github.com/jacobsa/ogletest"
    25  )
    26  
    27  // Sample output:
    28  //
    29  //     Filesystem  1024-blocks Used Available Capacity iused ifree %iused  Mounted on
    30  //     fake@bucket          32   16        16    50%       0     0  100%   /Users/jacobsa/tmp/mp
    31  //
    32  var gDfOutputRegexp = regexp.MustCompile(`^\S+\s+(\d+)\s+(\d+)\s+(\d+)\s+\d+%\s+\d+\s+\d+\s+\d+%.*$`)
    33  
    34  ////////////////////////////////////////////////////////////////////////
    35  // Helpers
    36  ////////////////////////////////////////////////////////////////////////
    37  
    38  func convertName(in []int8) string {
    39  	var tmp []byte
    40  	for _, v := range in {
    41  		if v == 0 {
    42  			break
    43  		}
    44  
    45  		tmp = append(tmp, byte(v))
    46  	}
    47  
    48  	return string(tmp)
    49  }
    50  
    51  ////////////////////////////////////////////////////////////////////////
    52  // Tests
    53  ////////////////////////////////////////////////////////////////////////
    54  
    55  func (t *StatFSTest) Syscall_ZeroValues() {
    56  	var err error
    57  	var stat syscall.Statfs_t
    58  
    59  	// Call without configuring a canned response, meaning the OS will see the
    60  	// zero value for each field. The assertions below act as documentation for
    61  	// the OS's behavior in this case.
    62  	err = syscall.Statfs(t.Dir, &stat)
    63  	AssertEq(nil, err)
    64  
    65  	ExpectEq(4096, stat.Bsize)
    66  	ExpectEq(65536, stat.Iosize)
    67  	ExpectEq(0, stat.Blocks)
    68  	ExpectEq(0, stat.Bfree)
    69  	ExpectEq(0, stat.Bavail)
    70  	ExpectEq(0, stat.Files)
    71  	ExpectEq(0, stat.Ffree)
    72  	ExpectEq("osxfuse", convertName(stat.Fstypename[:]))
    73  	ExpectEq(t.canonicalDir, convertName(stat.Mntonname[:]))
    74  	ExpectEq(fsName, convertName(stat.Mntfromname[:]))
    75  }
    76  
    77  func (t *StatFSTest) Syscall_NonZeroValues() {
    78  	var err error
    79  	var stat syscall.Statfs_t
    80  
    81  	// Set up the canned response.
    82  	canned := fuseops.StatFSOp{
    83  		BlockSize: 1 << 15,
    84  		IoSize:    1 << 16,
    85  
    86  		Blocks:          1<<51 + 3,
    87  		BlocksFree:      1<<43 + 5,
    88  		BlocksAvailable: 1<<41 + 7,
    89  
    90  		Inodes:     1<<59 + 11,
    91  		InodesFree: 1<<58 + 13,
    92  	}
    93  
    94  	t.fs.SetStatFSResponse(canned)
    95  
    96  	// Stat.
    97  	err = syscall.Statfs(t.Dir, &stat)
    98  	AssertEq(nil, err)
    99  
   100  	ExpectEq(canned.BlockSize, stat.Bsize)
   101  	ExpectEq(canned.IoSize, stat.Iosize)
   102  	ExpectEq(canned.Blocks, stat.Blocks)
   103  	ExpectEq(canned.BlocksFree, stat.Bfree)
   104  	ExpectEq(canned.BlocksAvailable, stat.Bavail)
   105  	ExpectEq(canned.Inodes, stat.Files)
   106  	ExpectEq(canned.InodesFree, stat.Ffree)
   107  	ExpectEq("osxfuse", convertName(stat.Fstypename[:]))
   108  	ExpectEq(t.canonicalDir, convertName(stat.Mntonname[:]))
   109  	ExpectEq(fsName, convertName(stat.Mntfromname[:]))
   110  }
   111  
   112  func (t *StatFSTest) BlockSizes() {
   113  	var err error
   114  
   115  	// Test a bunch of block sizes that the OS does or doesn't support
   116  	// faithfully, checking what it transforms them too.
   117  	testCases := []struct {
   118  		fsBlockSize   uint32
   119  		expectedBsize uint32
   120  	}{
   121  		0:  {0, 4096},
   122  		1:  {1, 128},
   123  		2:  {3, 128},
   124  		3:  {511, 512},
   125  		4:  {512, 512},
   126  		5:  {513, 1024},
   127  		6:  {1023, 1024},
   128  		7:  {1024, 1024},
   129  		8:  {4095, 4096},
   130  		9:  {1 << 16, 1 << 16},
   131  		10: {1 << 17, 1 << 17},
   132  		11: {1 << 18, 1 << 18},
   133  		12: {1 << 19, 1 << 19},
   134  
   135  		13: {1<<20 - 1, 1 << 20},
   136  		14: {1 << 20, 1 << 20},
   137  		15: {1<<20 + 1, 1 << 20},
   138  
   139  		16: {1 << 21, 1 << 20},
   140  		17: {1 << 22, 1 << 20},
   141  
   142  		18: {math.MaxInt32 - 1, 1 << 20},
   143  		19: {math.MaxInt32, 1 << 20},
   144  		20: {math.MaxInt32 + 1, 128},
   145  		21: {math.MaxInt32 + 1<<15, 1 << 15},
   146  		22: {math.MaxUint32, 1 << 20},
   147  	}
   148  
   149  	for i, tc := range testCases {
   150  		desc := fmt.Sprintf("Case %d: block size %d", i, tc.fsBlockSize)
   151  
   152  		// Set up.
   153  		canned := fuseops.StatFSOp{
   154  			BlockSize: tc.fsBlockSize,
   155  			Blocks:    10,
   156  		}
   157  
   158  		t.fs.SetStatFSResponse(canned)
   159  
   160  		// Check.
   161  		var stat syscall.Statfs_t
   162  		err = syscall.Statfs(t.Dir, &stat)
   163  		AssertEq(nil, err)
   164  
   165  		ExpectEq(tc.expectedBsize, stat.Bsize, "%s", desc)
   166  	}
   167  }
   168  
   169  func (t *StatFSTest) IoSizes() {
   170  	var err error
   171  
   172  	// Test a bunch of io sizes that the OS does or doesn't support faithfully,
   173  	// checking what it transforms them too.
   174  	testCases := []struct {
   175  		fsIoSize       uint32
   176  		expectedIosize uint32
   177  	}{
   178  		0: {0, 65536},
   179  		1: {1, 4096},
   180  		2: {3, 4096},
   181  		3: {4095, 4096},
   182  		4: {4096, 4096},
   183  		5: {4097, 8192},
   184  		6: {8191, 8192},
   185  		7: {8192, 8192},
   186  		8: {8193, 16384},
   187  
   188  		9:  {1 << 18, 1 << 18},
   189  		10: {1 << 20, 1 << 20},
   190  		11: {1 << 23, 1 << 23},
   191  
   192  		12: {1<<25 - 1, 1 << 25},
   193  		13: {1 << 25, 1 << 25},
   194  		14: {1<<25 + 1, 1 << 25},
   195  
   196  		15: {math.MaxInt32 - 1, 1 << 25},
   197  		16: {math.MaxInt32, 1 << 25},
   198  		17: {math.MaxInt32 + 1, 4096},
   199  		18: {math.MaxInt32 + 1<<15, 1 << 15},
   200  		19: {math.MaxUint32, 1 << 25},
   201  	}
   202  
   203  	for i, tc := range testCases {
   204  		desc := fmt.Sprintf("Case %d: IO size %d", i, tc.fsIoSize)
   205  
   206  		// Set up.
   207  		canned := fuseops.StatFSOp{
   208  			IoSize: tc.fsIoSize,
   209  			Blocks: 10,
   210  		}
   211  
   212  		t.fs.SetStatFSResponse(canned)
   213  
   214  		// Check.
   215  		var stat syscall.Statfs_t
   216  		err = syscall.Statfs(t.Dir, &stat)
   217  		AssertEq(nil, err)
   218  
   219  		ExpectEq(tc.expectedIosize, stat.Iosize, "%s", desc)
   220  	}
   221  }