github.com/scaleoutsean/fusego@v0.0.0-20220224074057-4a6429e46bb8/samples/in_process.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 samples
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"io"
    21  	"io/ioutil"
    22  	"log"
    23  	"os"
    24  	"time"
    25  
    26  	"github.com/scaleoutsean/fusego"
    27  	"github.com/jacobsa/ogletest"
    28  	"github.com/jacobsa/timeutil"
    29  )
    30  
    31  // A struct that implements common behavior needed by tests in the samples/
    32  // directory. Use it as an embedded field in your test fixture, calling its
    33  // SetUp method from your SetUp method after setting the Server field.
    34  type SampleTest struct {
    35  	// The server under test and the configuration with which it should be
    36  	// mounted. These must be set by the user of this type before calling SetUp;
    37  	// all the other fields below are set by SetUp itself.
    38  	Server      fuse.Server
    39  	MountConfig fuse.MountConfig
    40  
    41  	// A context object that can be used for long-running operations.
    42  	Ctx context.Context
    43  
    44  	// A clock with a fixed initial time. The test's set up method may use this
    45  	// to wire the server with a clock, if desired.
    46  	Clock timeutil.SimulatedClock
    47  
    48  	// The directory at which the file system is mounted.
    49  	Dir string
    50  
    51  	// Anothing non-nil in this slice will be closed by TearDown. The test will
    52  	// fail if closing fails.
    53  	ToClose []io.Closer
    54  
    55  	mfs *fuse.MountedFileSystem
    56  }
    57  
    58  // Mount t.Server and initialize the other exported fields of the struct.
    59  // Panics on error.
    60  //
    61  // REQUIRES: t.Server has been set.
    62  func (t *SampleTest) SetUp(ti *ogletest.TestInfo) {
    63  	cfg := t.MountConfig
    64  	if *fDebug {
    65  		cfg.DebugLogger = log.New(os.Stderr, "fuse: ", 0)
    66  	}
    67  
    68  	err := t.initialize(ti.Ctx, t.Server, &cfg)
    69  	if err != nil {
    70  		panic(err)
    71  	}
    72  }
    73  
    74  // Like SetUp, but doens't panic.
    75  func (t *SampleTest) initialize(
    76  	ctx context.Context,
    77  	server fuse.Server,
    78  	config *fuse.MountConfig) error {
    79  	// Initialize the context used by the test.
    80  	t.Ctx = ctx
    81  
    82  	// Make the server share that context, if the test hasn't already set some
    83  	// other one.
    84  	if config.OpContext == nil {
    85  		config.OpContext = ctx
    86  	}
    87  
    88  	// Initialize the clock.
    89  	t.Clock.SetTime(time.Date(2012, 8, 15, 22, 56, 0, 0, time.Local))
    90  
    91  	// Set up a temporary directory.
    92  	var err error
    93  	t.Dir, err = ioutil.TempDir("", "sample_test")
    94  	if err != nil {
    95  		return fmt.Errorf("TempDir: %v", err)
    96  	}
    97  
    98  	// Mount the file system.
    99  	t.mfs, err = fuse.Mount(t.Dir, server, config)
   100  	if err != nil {
   101  		return fmt.Errorf("Mount: %v", err)
   102  	}
   103  
   104  	return nil
   105  }
   106  
   107  // Unmount the file system and clean up. Panics on error.
   108  func (t *SampleTest) TearDown() {
   109  	err := t.destroy()
   110  	if err != nil {
   111  		panic(err)
   112  	}
   113  }
   114  
   115  // Like TearDown, but doesn't panic.
   116  func (t *SampleTest) destroy() (err error) {
   117  	// Close what is necessary.
   118  	for _, c := range t.ToClose {
   119  		if c == nil {
   120  			continue
   121  		}
   122  
   123  		ogletest.ExpectEq(nil, c.Close())
   124  	}
   125  
   126  	// Was the file system mounted?
   127  	if t.mfs == nil {
   128  		return nil
   129  	}
   130  
   131  	// Unmount the file system.
   132  	if err := unmount(t.Dir); err != nil {
   133  		return fmt.Errorf("unmount: %v", err)
   134  	}
   135  
   136  	// Unlink the mount point.
   137  	if err := os.Remove(t.Dir); err != nil {
   138  		return fmt.Errorf("Unlinking mount point: %v", err)
   139  	}
   140  
   141  	// Join the file system.
   142  	if err := t.mfs.Join(t.Ctx); err != nil {
   143  		return fmt.Errorf("mfs.Join: %v", err)
   144  	}
   145  
   146  	return nil
   147  }