github.com/attic-labs/noms@v0.0.0-20210827224422-e5fa29d95e8b/samples/go/nomsfs/rw_test.go (about)

     1  // Copyright 2016 Attic Labs, Inc. All rights reserved.
     2  // Licensed under the Apache License, version 2.0:
     3  // http://www.apache.org/licenses/LICENSE-2.0
     4  
     5  // +build darwin linux
     6  
     7  package main
     8  
     9  import (
    10  	"bytes"
    11  	"os"
    12  	"testing"
    13  
    14  	"github.com/attic-labs/noms/go/spec"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/suite"
    17  
    18  	"github.com/hanwen/go-fuse/fuse"
    19  	"github.com/hanwen/go-fuse/fuse/pathfs"
    20  )
    21  
    22  func TestRW(t *testing.T) {
    23  	suite.Run(t, &fuseTestSuite{})
    24  }
    25  
    26  func (s *fuseTestSuite) TestSimpleFile() {
    27  	datasetName := "TestSimpleFile"
    28  	str := spec.CreateValueSpecString("nbs", s.DBDir, datasetName)
    29  
    30  	var testfs pathfs.FileSystem
    31  
    32  	start(str, func(fs pathfs.FileSystem) { testfs = fs })
    33  
    34  	file, code := testfs.Create("coconut", uint32(os.O_CREATE|os.O_RDWR), 0644, nil)
    35  	assert.Equal(s.T(), fuse.OK, code)
    36  	n, code := file.Write([]byte("Lime!"), 0)
    37  	assert.Equal(s.T(), fuse.OK, code)
    38  	assert.Equal(s.T(), uint32(5), n)
    39  	assertAttr(s, testfs, "coconut", 0644|fuse.S_IFREG, 5)
    40  
    41  	data := make([]byte, 5)
    42  	rr, code := file.Read(data, 0)
    43  	assert.Equal(s.T(), fuse.OK, code)
    44  	assert.Equal(s.T(), 5, rr.Size())
    45  	assert.Equal(s.T(), "Lime!", string(data))
    46  
    47  	code = testfs.Truncate("coconut", 4, nil)
    48  	assert.Equal(s.T(), fuse.OK, code)
    49  	assertAttr(s, testfs, "coconut", 0644|fuse.S_IFREG, 4)
    50  	rr, code = file.Read(data, 0)
    51  	assert.Equal(s.T(), fuse.OK, code)
    52  	assert.Equal(s.T(), 4, rr.Size())
    53  	assert.Equal(s.T(), "Lime!", string(data))
    54  }
    55  
    56  func (s *fuseTestSuite) TestBigFile() {
    57  	datasetName := "TestBigFile"
    58  	str := spec.CreateValueSpecString("nbs", s.DBDir, datasetName)
    59  
    60  	var testfs pathfs.FileSystem
    61  
    62  	start(str, func(fs pathfs.FileSystem) { testfs = fs })
    63  
    64  	size := uint64(10 * 1024) // 10KB
    65  	data := make([]byte, size)
    66  	buf := bytes.NewBuffer(data)
    67  
    68  	for uint64(buf.Len()) < size {
    69  		buf.WriteString("All work and no play makes Jack a dull boy.\n")
    70  	}
    71  
    72  	file, code := testfs.Create("shining.txt", uint32(os.O_CREATE|os.O_RDWR), 0644, nil)
    73  	assert.Equal(s.T(), fuse.OK, code)
    74  
    75  	n, code := file.Write(buf.Bytes(), 0)
    76  	assert.Equal(s.T(), fuse.OK, code)
    77  	assert.Equal(s.T(), uint32(size), n)
    78  	assertAttr(s, testfs, "shining.txt", 0644|fuse.S_IFREG, size)
    79  }
    80  
    81  func (s *fuseTestSuite) TestOverwrite() {
    82  	datasetName := "TestOverwrite"
    83  	str := spec.CreateValueSpecString("nbs", s.DBDir, datasetName)
    84  
    85  	var testfs pathfs.FileSystem
    86  
    87  	start(str, func(fs pathfs.FileSystem) { testfs = fs })
    88  
    89  	file, code := testfs.Create("proclamation", uint32(os.O_CREATE|os.O_RDWR), 0644, nil)
    90  	assert.Equal(s.T(), fuse.OK, code)
    91  
    92  	n, code := file.Write([]byte("Four score and seven years ago..."), 0)
    93  	assert.Equal(s.T(), fuse.OK, code)
    94  	assert.Equal(s.T(), uint32(33), n)
    95  	assertAttr(s, testfs, "proclamation", 0644|fuse.S_IFREG, 33)
    96  	n, code = file.Write([]byte("beers"), 21)
    97  	assert.Equal(s.T(), fuse.OK, code)
    98  	assert.Equal(s.T(), uint32(5), n)
    99  	assertAttr(s, testfs, "proclamation", 0644|fuse.S_IFREG, 33)
   100  
   101  	data := make([]byte, 33)
   102  	rr, code := file.Read(data, 0)
   103  	assert.Equal(s.T(), fuse.OK, code)
   104  	assert.Equal(s.T(), 33, rr.Size())
   105  	assert.Equal(s.T(), "Four score and seven beers ago...", string(data))
   106  }
   107  
   108  func (s *fuseTestSuite) TestMultipleOpens() {
   109  	datasetName := "TestMultipleOpens"
   110  	str := spec.CreateValueSpecString("nbs", s.DBDir, datasetName)
   111  
   112  	var testfs pathfs.FileSystem
   113  
   114  	start(str, func(fs pathfs.FileSystem) { testfs = fs })
   115  
   116  	file1, code := testfs.Create("contend", uint32(os.O_CREATE|os.O_RDWR), 0644, nil)
   117  	assert.Equal(s.T(), fuse.OK, code)
   118  
   119  	file2, code := testfs.Open("contend", uint32(os.O_RDWR), nil)
   120  	assert.Equal(s.T(), fuse.OK, code)
   121  
   122  	file1.Write([]byte("abc contact"), 0)
   123  	file2.Write([]byte("321"), 0)
   124  
   125  	data := make([]byte, 11)
   126  	rr, code := file1.Read(data, 0)
   127  	assert.Equal(s.T(), fuse.OK, code)
   128  	assert.Equal(s.T(), 11, rr.Size())
   129  	assert.Equal(s.T(), "321 contact", string(data))
   130  }