github.com/hanwen/go-fuse@v1.0.0/unionfs/cachingfs_test.go (about)

     1  // Copyright 2016 the Go-FUSE Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package unionfs
     6  
     7  import (
     8  	"os"
     9  	"syscall"
    10  	"testing"
    11  
    12  	"github.com/hanwen/go-fuse/fuse"
    13  	"github.com/hanwen/go-fuse/fuse/pathfs"
    14  	"github.com/hanwen/go-fuse/internal/testutil"
    15  )
    16  
    17  func modeMapEq(m1, m2 map[string]uint32) bool {
    18  	if len(m1) != len(m2) {
    19  		return false
    20  	}
    21  
    22  	for k, v := range m1 {
    23  		val, ok := m2[k]
    24  		if !ok || val != v {
    25  			return false
    26  		}
    27  	}
    28  	return true
    29  }
    30  
    31  func TestCachingFs(t *testing.T) {
    32  	wd := testutil.TempDir()
    33  	defer os.RemoveAll(wd)
    34  
    35  	fs := pathfs.NewLoopbackFileSystem(wd)
    36  	cfs := NewCachingFileSystem(fs, 0)
    37  
    38  	os.Mkdir(wd+"/orig", 0755)
    39  	fi, code := cfs.GetAttr("orig", nil)
    40  	if !code.Ok() {
    41  		t.Fatal("GetAttr failure", code)
    42  	}
    43  	if !fi.IsDir() {
    44  		t.Error("unexpected attr", fi)
    45  	}
    46  
    47  	os.Symlink("orig", wd+"/symlink")
    48  
    49  	val, code := cfs.Readlink("symlink", nil)
    50  	if val != "orig" {
    51  		t.Error("unexpected readlink", val)
    52  	}
    53  	if !code.Ok() {
    54  		t.Error("code !ok ", code)
    55  	}
    56  
    57  	stream, code := cfs.OpenDir("", nil)
    58  	if !code.Ok() {
    59  		t.Fatal("Readdir fail", code)
    60  	}
    61  
    62  	results := make(map[string]uint32)
    63  	for _, v := range stream {
    64  		results[v.Name] = v.Mode &^ 07777
    65  	}
    66  	expected := map[string]uint32{
    67  		"symlink": syscall.S_IFLNK,
    68  		"orig":    fuse.S_IFDIR,
    69  	}
    70  	if !modeMapEq(results, expected) {
    71  		t.Error("Unexpected readdir result", results, expected)
    72  	}
    73  }