github.com/scaleoutsean/fusego@v0.0.0-20220224074057-4a6429e46bb8/samples/statfs/statfs_linux_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                  1K-blocks Used Available Use% Mounted on
    30  //     some_fuse_file_system       512       64   384       15%  /tmp/sample_test001288095
    31  //
    32  var gDfOutputRegexp = regexp.MustCompile(`^\S+\s+(\d+)\s+(\d+)\s+(\d+)\s+\d+%.*$`)
    33  
    34  ////////////////////////////////////////////////////////////////////////
    35  // Tests
    36  ////////////////////////////////////////////////////////////////////////
    37  
    38  func (t *StatFSTest) Syscall_ZeroValues() {
    39  	var err error
    40  	var stat syscall.Statfs_t
    41  
    42  	// Call without configuring a canned response, meaning the OS will see the
    43  	// zero value for each field. The assertions below act as documentation for
    44  	// the OS's behavior in this case.
    45  	err = syscall.Statfs(t.Dir, &stat)
    46  	AssertEq(nil, err)
    47  
    48  	ExpectEq(0, stat.Bsize)
    49  	ExpectEq(0, stat.Frsize)
    50  	ExpectEq(0, stat.Blocks)
    51  	ExpectEq(0, stat.Bfree)
    52  	ExpectEq(0, stat.Bavail)
    53  	ExpectEq(0, stat.Files)
    54  	ExpectEq(0, stat.Ffree)
    55  }
    56  
    57  func (t *StatFSTest) Syscall_NonZeroValues() {
    58  	var err error
    59  	var stat syscall.Statfs_t
    60  
    61  	// Set up the canned response.
    62  	canned := fuseops.StatFSOp{
    63  		BlockSize: 1 << 15,
    64  		IoSize:    1 << 16,
    65  
    66  		Blocks:          1<<51 + 3,
    67  		BlocksFree:      1<<43 + 5,
    68  		BlocksAvailable: 1<<41 + 7,
    69  
    70  		Inodes:     1<<59 + 11,
    71  		InodesFree: 1<<58 + 13,
    72  	}
    73  
    74  	t.fs.SetStatFSResponse(canned)
    75  
    76  	// Stat.
    77  	err = syscall.Statfs(t.Dir, &stat)
    78  	AssertEq(nil, err)
    79  
    80  	ExpectEq(canned.BlockSize, stat.Frsize)
    81  	ExpectEq(canned.IoSize, stat.Bsize)
    82  	ExpectEq(canned.Blocks, stat.Blocks)
    83  	ExpectEq(canned.BlocksFree, stat.Bfree)
    84  	ExpectEq(canned.BlocksAvailable, stat.Bavail)
    85  	ExpectEq(canned.Inodes, stat.Files)
    86  	ExpectEq(canned.InodesFree, stat.Ffree)
    87  }
    88  
    89  func (t *StatFSTest) BlockSizes() {
    90  	var err error
    91  
    92  	// Test a bunch of weird block sizes that OS X would be cranky about.
    93  	blockSizes := []uint32{
    94  		0,
    95  		1,
    96  		3,
    97  		17,
    98  		1<<20 - 1,
    99  		1<<20 + 0,
   100  		1<<20 + 1,
   101  		math.MaxInt32,
   102  		math.MaxInt32 + 1,
   103  		math.MaxUint32,
   104  	}
   105  
   106  	for _, bs := range blockSizes {
   107  		desc := fmt.Sprintf("block size %d", bs)
   108  
   109  		// Set up.
   110  		canned := fuseops.StatFSOp{
   111  			BlockSize: bs,
   112  			Blocks:    10,
   113  		}
   114  
   115  		t.fs.SetStatFSResponse(canned)
   116  
   117  		// Check.
   118  		var stat syscall.Statfs_t
   119  		err = syscall.Statfs(t.Dir, &stat)
   120  		AssertEq(nil, err)
   121  
   122  		ExpectEq(bs, stat.Frsize, "%s", desc)
   123  	}
   124  }
   125  
   126  func (t *StatFSTest) IoSizes() {
   127  	var err error
   128  
   129  	// Test a bunch of weird IO sizes that OS X would be cranky about.
   130  	ioSizes := []uint32{
   131  		0,
   132  		1,
   133  		3,
   134  		17,
   135  		1<<20 - 1,
   136  		1<<20 + 0,
   137  		1<<20 + 1,
   138  		math.MaxInt32,
   139  		math.MaxInt32 + 1,
   140  		math.MaxUint32,
   141  	}
   142  
   143  	for _, bs := range ioSizes {
   144  		desc := fmt.Sprintf("IO size %d", bs)
   145  
   146  		// Set up.
   147  		canned := fuseops.StatFSOp{
   148  			IoSize: bs,
   149  			Blocks: 10,
   150  		}
   151  
   152  		t.fs.SetStatFSResponse(canned)
   153  
   154  		// Check.
   155  		var stat syscall.Statfs_t
   156  		err = syscall.Statfs(t.Dir, &stat)
   157  		AssertEq(nil, err)
   158  
   159  		ExpectEq(bs, stat.Bsize, "%s", desc)
   160  	}
   161  }