github.com/scaleoutsean/fusego@v0.0.0-20220224074057-4a6429e46bb8/samples/errorfs/error_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 errorfs_test
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os"
    20  	"path"
    21  	"reflect"
    22  	"syscall"
    23  	"testing"
    24  
    25  	"github.com/scaleoutsean/fusego/fuseops"
    26  	"github.com/scaleoutsean/fusego/fuseutil"
    27  	"github.com/scaleoutsean/fusego/samples"
    28  	"github.com/scaleoutsean/fusego/samples/errorfs"
    29  	. "github.com/jacobsa/oglematchers"
    30  	. "github.com/jacobsa/ogletest"
    31  )
    32  
    33  func TestErrorFS(t *testing.T) { RunTests(t) }
    34  
    35  ////////////////////////////////////////////////////////////////////////
    36  // Boilerplate
    37  ////////////////////////////////////////////////////////////////////////
    38  
    39  type ErrorFSTest struct {
    40  	samples.SampleTest
    41  	fs errorfs.FS
    42  }
    43  
    44  func init() { RegisterTestSuite(&ErrorFSTest{}) }
    45  
    46  var _ SetUpInterface = &ErrorFSTest{}
    47  var _ TearDownInterface = &ErrorFSTest{}
    48  
    49  func (t *ErrorFSTest) SetUp(ti *TestInfo) {
    50  	var err error
    51  
    52  	// Create the file system.
    53  	t.fs, err = errorfs.New()
    54  	AssertEq(nil, err)
    55  
    56  	t.Server = fuseutil.NewFileSystemServer(t.fs)
    57  
    58  	// Mount it.
    59  	t.SampleTest.SetUp(ti)
    60  }
    61  
    62  ////////////////////////////////////////////////////////////////////////
    63  // Tests
    64  ////////////////////////////////////////////////////////////////////////
    65  
    66  func (t *ErrorFSTest) OpenFile() {
    67  	t.fs.SetError(reflect.TypeOf(&fuseops.OpenFileOp{}), syscall.EOWNERDEAD)
    68  
    69  	f, err := os.Open(path.Join(t.Dir, "foo"))
    70  	defer f.Close()
    71  	ExpectThat(err, Error(MatchesRegexp("open.*: .*owner died")))
    72  }
    73  
    74  func (t *ErrorFSTest) ReadFile() {
    75  	t.fs.SetError(reflect.TypeOf(&fuseops.ReadFileOp{}), syscall.EOWNERDEAD)
    76  
    77  	// Open
    78  	f, err := os.Open(path.Join(t.Dir, "foo"))
    79  	defer f.Close()
    80  	AssertEq(nil, err)
    81  
    82  	// Read
    83  	_, err = ioutil.ReadAll(f)
    84  	ExpectThat(err, Error(MatchesRegexp("read.*: .*owner died")))
    85  }
    86  
    87  func (t *ErrorFSTest) OpenDir() {
    88  	t.fs.SetError(reflect.TypeOf(&fuseops.OpenDirOp{}), syscall.EOWNERDEAD)
    89  
    90  	f, err := os.Open(t.Dir)
    91  	defer f.Close()
    92  	ExpectThat(err, Error(MatchesRegexp("open.*: .*owner died")))
    93  }
    94  
    95  func (t *ErrorFSTest) ReadDir() {
    96  	t.fs.SetError(reflect.TypeOf(&fuseops.ReadDirOp{}), syscall.EOWNERDEAD)
    97  
    98  	// Open
    99  	f, err := os.Open(t.Dir)
   100  	defer f.Close()
   101  	AssertEq(nil, err)
   102  
   103  	// Read
   104  	_, err = f.Readdirnames(1)
   105  	ExpectThat(err, Error(MatchesRegexp("read.*: .*owner died")))
   106  }