github.com/scaleoutsean/fusego@v0.0.0-20220224074057-4a6429e46bb8/samples/forgetfs/forget_fs_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 forgetfs_test
    16  
    17  import (
    18  	"io"
    19  	"os"
    20  	"path"
    21  	"testing"
    22  
    23  	"github.com/scaleoutsean/fusego/samples"
    24  	"github.com/scaleoutsean/fusego/samples/forgetfs"
    25  	. "github.com/jacobsa/ogletest"
    26  )
    27  
    28  func TestForgetFS(t *testing.T) { RunTests(t) }
    29  
    30  ////////////////////////////////////////////////////////////////////////
    31  // Boilerplate
    32  ////////////////////////////////////////////////////////////////////////
    33  
    34  type ForgetFSTest struct {
    35  	samples.SampleTest
    36  	fs *forgetfs.ForgetFS
    37  }
    38  
    39  func init() { RegisterTestSuite(&ForgetFSTest{}) }
    40  
    41  func (t *ForgetFSTest) SetUp(ti *TestInfo) {
    42  	t.fs = forgetfs.NewFileSystem()
    43  	t.Server = t.fs
    44  	t.SampleTest.SetUp(ti)
    45  }
    46  
    47  func (t *ForgetFSTest) TearDown() {
    48  	// Unmount.
    49  	t.SampleTest.TearDown()
    50  
    51  	// Crash if anything is left.
    52  	t.fs.Check()
    53  }
    54  
    55  ////////////////////////////////////////////////////////////////////////
    56  // Tests
    57  ////////////////////////////////////////////////////////////////////////
    58  
    59  func (t *ForgetFSTest) Open_Foo() {
    60  	var err error
    61  
    62  	f, err := os.Open(path.Join(t.Dir, "foo"))
    63  	AssertEq(nil, err)
    64  
    65  	err = f.Close()
    66  	AssertEq(nil, err)
    67  }
    68  
    69  func (t *ForgetFSTest) Open_Bar() {
    70  	var err error
    71  
    72  	f, err := os.Open(path.Join(t.Dir, "bar"))
    73  	AssertEq(nil, err)
    74  
    75  	err = f.Close()
    76  	AssertEq(nil, err)
    77  }
    78  
    79  func (t *ForgetFSTest) Open_ManyTimes() {
    80  	// Set up a slice of files that will be closed when we're done.
    81  	var toClose []io.Closer
    82  	defer func() {
    83  		for _, c := range toClose {
    84  			ExpectEq(nil, c.Close())
    85  		}
    86  	}()
    87  
    88  	// Open foo many times.
    89  	for i := 0; i < 100; i++ {
    90  		f, err := os.Open(path.Join(t.Dir, "foo"))
    91  		AssertEq(nil, err)
    92  		toClose = append(toClose, f)
    93  	}
    94  
    95  	// Open bar many times.
    96  	for i := 0; i < 100; i++ {
    97  		f, err := os.Open(path.Join(t.Dir, "bar"))
    98  		AssertEq(nil, err)
    99  		toClose = append(toClose, f)
   100  	}
   101  }
   102  
   103  func (t *ForgetFSTest) Stat_Foo() {
   104  	var fi os.FileInfo
   105  	var err error
   106  
   107  	fi, err = os.Stat(path.Join(t.Dir, "foo"))
   108  	AssertEq(nil, err)
   109  	AssertEq("foo", fi.Name())
   110  	AssertEq(os.FileMode(0777), fi.Mode())
   111  }
   112  
   113  func (t *ForgetFSTest) Stat_Bar() {
   114  	var fi os.FileInfo
   115  	var err error
   116  
   117  	fi, err = os.Stat(path.Join(t.Dir, "bar"))
   118  	AssertEq(nil, err)
   119  	AssertEq("bar", fi.Name())
   120  	AssertEq(0777|os.ModeDir, fi.Mode())
   121  }
   122  
   123  func (t *ForgetFSTest) Stat_ManyTimes() {
   124  	var err error
   125  
   126  	// Stat foo many times.
   127  	for i := 0; i < 100; i++ {
   128  		_, err = os.Stat(path.Join(t.Dir, "foo"))
   129  		AssertEq(nil, err)
   130  	}
   131  
   132  	// Stat bar many times.
   133  	for i := 0; i < 100; i++ {
   134  		_, err = os.Stat(path.Join(t.Dir, "bar"))
   135  		AssertEq(nil, err)
   136  	}
   137  }
   138  
   139  func (t *ForgetFSTest) CreateFile() {
   140  	// Create and close many files within the root.
   141  	for i := 0; i < 100; i++ {
   142  		f, err := os.Create(path.Join(t.Dir, "blah"))
   143  		AssertEq(nil, err)
   144  		AssertEq(nil, f.Close())
   145  	}
   146  
   147  	// Create and close many files within the sub-directory.
   148  	for i := 0; i < 100; i++ {
   149  		f, err := os.Create(path.Join(t.Dir, "bar", "blah"))
   150  		AssertEq(nil, err)
   151  		AssertEq(nil, f.Close())
   152  	}
   153  }
   154  
   155  func (t *ForgetFSTest) MkDir() {
   156  	// Create many directories within the root.
   157  	for i := 0; i < 100; i++ {
   158  		err := os.Mkdir(path.Join(t.Dir, "blah"), 0777)
   159  		AssertEq(nil, err)
   160  	}
   161  
   162  	// Create many directories within the sub-directory.
   163  	for i := 0; i < 100; i++ {
   164  		err := os.Mkdir(path.Join(t.Dir, "bar", "blah"), 0777)
   165  		AssertEq(nil, err)
   166  	}
   167  }