github.com/attic-labs/noms@v0.0.0-20210827224422-e5fa29d95e8b/samples/go/nomsfs/dir_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  	"os"
    11  	"sort"
    12  	"testing"
    13  
    14  	"github.com/attic-labs/noms/go/spec"
    15  	"github.com/stretchr/testify/suite"
    16  
    17  	"github.com/hanwen/go-fuse/fuse"
    18  	"github.com/hanwen/go-fuse/fuse/pathfs"
    19  )
    20  
    21  func TestDir(t *testing.T) {
    22  	suite.Run(t, &fuseTestSuite{})
    23  }
    24  
    25  func findDir(fs pathfs.FileSystem, path string, delimiter string) (ret []string) {
    26  	ents, _ := fs.OpenDir(path, nil)
    27  	for _, ent := range ents {
    28  		child := path + delimiter + ent.Name
    29  		attr, _ := fs.GetAttr(child, nil)
    30  		if attr.Mode&fuse.S_IFDIR != 0 {
    31  			ret = append(ret, child+"/")
    32  			ret = append(ret, findDir(fs, child, "/")...)
    33  		} else {
    34  			ret = append(ret, child)
    35  		}
    36  	}
    37  
    38  	return ret
    39  }
    40  
    41  func find(fs pathfs.FileSystem) []string {
    42  	return findDir(fs, "", "")
    43  }
    44  
    45  func (s *fuseTestSuite) TestHierarchy() {
    46  	datasetName := "TestHierarchy"
    47  	str := spec.CreateValueSpecString("nbs", s.DBDir, datasetName)
    48  
    49  	var testfs pathfs.FileSystem
    50  
    51  	start(str, func(fs pathfs.FileSystem) { testfs = fs })
    52  
    53  	hierarchy := []string{
    54  		"bin/",
    55  		"bin/sh",
    56  		"usr/",
    57  		"usr/bin/",
    58  		"usr/bin/cat",
    59  		"usr/bin/bash",
    60  		"usr/lib/",
    61  		"usr/lib/libc.so.1",
    62  		"usr/dict/",
    63  		"usr/dict/words",
    64  		"usr/dict/words2",
    65  	}
    66  
    67  	for _, path := range hierarchy {
    68  		if ll := len(path); path[ll-1] == '/' {
    69  			code := testfs.Mkdir(path[:ll-1], 0555, nil)
    70  			s.Equal(fuse.OK, code)
    71  		} else {
    72  			_, code := testfs.Create(path, uint32(os.O_CREATE)|uint32(os.O_WRONLY), 0444, nil)
    73  			s.Equal(fuse.OK, code)
    74  		}
    75  	}
    76  
    77  	h := find(testfs)
    78  
    79  	sort.Strings(hierarchy)
    80  	sort.Strings(h)
    81  
    82  	s.Equal(hierarchy, h)
    83  }
    84  
    85  func (s *fuseTestSuite) TestDirError() {
    86  	datasetName := "TestDirError"
    87  	str := spec.CreateValueSpecString("nbs", s.DBDir, datasetName)
    88  
    89  	var testfs pathfs.FileSystem
    90  
    91  	start(str, func(fs pathfs.FileSystem) { testfs = fs })
    92  
    93  	code := testfs.Mkdir("foo/bar", 0755, nil)
    94  	s.Equal(fuse.ENOENT, code)
    95  	_, code = testfs.Create("foo", uint32(os.O_CREATE)|uint32(os.O_WRONLY), 0644, nil)
    96  	s.Equal(fuse.OK, code)
    97  	code = testfs.Mkdir("foo/bar", 0755, nil)
    98  	s.Equal(fuse.ENOTDIR, code)
    99  
   100  	_, code = testfs.OpenDir("foo", nil)
   101  	s.Equal(fuse.ENOTDIR, code)
   102  }
   103  
   104  func (s *fuseTestSuite) TestRenaming() {
   105  	datasetName := "TestRenaming"
   106  	str := spec.CreateValueSpecString("nbs", s.DBDir, datasetName)
   107  
   108  	var testfs pathfs.FileSystem
   109  
   110  	start(str, func(fs pathfs.FileSystem) { testfs = fs })
   111  
   112  	code := testfs.Mkdir("foo", 0755, nil)
   113  	s.Equal(fuse.OK, code)
   114  	code = testfs.Mkdir("foo/bar", 0755, nil)
   115  	s.Equal(fuse.OK, code)
   116  	code = testfs.Mkdir("foo/baz", 0755, nil)
   117  	s.Equal(fuse.OK, code)
   118  	_, code = testfs.Create("foo/bar/buzz", uint32(os.O_CREATE)|uint32(os.O_WRONLY), 0644, nil)
   119  	s.Equal(fuse.OK, code)
   120  
   121  	code = testfs.Rename("foo/bar/buzz", "foo/baz/buzz", nil)
   122  	s.Equal(fuse.OK, code)
   123  	code = testfs.Rename("foo/baz/buzz", "buzz", nil)
   124  	s.Equal(fuse.OK, code)
   125  	code = testfs.Rename("buzz", "foo/buzz", nil)
   126  	s.Equal(fuse.OK, code)
   127  }
   128  
   129  func (s *fuseTestSuite) TestRenameWhileOpen() {
   130  	datasetName := "TestRenameWhileOpen"
   131  	str := spec.CreateValueSpecString("nbs", s.DBDir, datasetName)
   132  
   133  	var testfs pathfs.FileSystem
   134  
   135  	start(str, func(fs pathfs.FileSystem) { testfs = fs })
   136  
   137  	code := testfs.Mkdir("foo", 0755, nil)
   138  	s.Equal(fuse.OK, code)
   139  	code = testfs.Mkdir("foo/bar", 0755, nil)
   140  	s.Equal(fuse.OK, code)
   141  	file, code := testfs.Create("foo/bar/file.txt", uint32(os.O_CREATE)|uint32(os.O_WRONLY), 0644, nil)
   142  	s.Equal(fuse.OK, code)
   143  
   144  	// Validate renaming a file between opening it and writing to it.
   145  	code = testfs.Rename("foo/bar/file.txt", "file.txt", nil)
   146  	s.Equal(fuse.OK, code)
   147  
   148  	n, code := file.Write([]byte("howdy!"), 0)
   149  	s.Equal(uint32(6), n)
   150  	s.Equal(fuse.OK, code)
   151  	assertAttr(s, testfs, "file.txt", 0644|fuse.S_IFREG, 6)
   152  }