github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/dos/filesystem_test.go (about) 1 package dos_test 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "io" 8 "io/fs" 9 "log" 10 "os" 11 "path/filepath" 12 "testing" 13 14 "github.com/google/uuid" 15 "github.com/spf13/afero" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 19 "github.com/datawire/dlib/dlog" 20 "github.com/telepresenceio/telepresence/v2/pkg/dos" 21 "github.com/telepresenceio/telepresence/v2/pkg/dos/aferofs" 22 ) 23 24 func TestWithFS(t *testing.T) { 25 appFS := afero.NewMemMapFs() 26 cData := []byte("file c\n") 27 dData := []byte("file d\n") 28 require.NoError(t, appFS.MkdirAll("a/b", 0o755)) 29 require.NoError(t, afero.WriteFile(appFS, "/a/b/c.txt", cData, 0o644)) 30 require.NoError(t, afero.WriteFile(appFS, "/a/d.txt", dData, 0o644)) 31 32 ctx := dos.WithFS(dlog.NewTestContext(t, false), dos.WorkingDirWrapper(aferofs.Wrap(appFS))) 33 34 require.NoError(t, dos.Chdir(ctx, "/a/b")) 35 data, err := dos.ReadFile(ctx, "c.txt") 36 require.NoError(t, err) 37 require.Equal(t, cData, data) 38 39 require.NoError(t, dos.Chdir(ctx, "../..")) 40 f, err := dos.Open(ctx, "/a/d.txt") 41 require.NoError(t, err) 42 data, err = io.ReadAll(f) 43 require.NoError(t, err) 44 require.NoError(t, f.Close()) 45 require.Equal(t, dData, data) 46 } 47 48 func TestFileNil(t *testing.T) { 49 // This function will return a File interface that points to a nil *os.File. That's 50 // not the same as a File interface which is nil. 51 neverDoThis := func(name string) (dos.File, error) { 52 return os.Open(name) 53 } 54 55 dlog.NewTestContext(t, false) 56 uuid, err := uuid.NewUUID() 57 badFile := filepath.Join(fmt.Sprintf("%c%s", filepath.Separator, uuid), "does", "not", "exist") 58 require.NoError(t, err) 59 f, err := neverDoThis(badFile) 60 assert.Error(t, err) 61 assert.True(t, f != nil) // Do NOT change this to assert.NotNil(t, f) because that test is flawed. 62 assert.Nil(t, f) // Told you so. It is flawed! 63 64 f, err = dos.Open(context.Background(), badFile) 65 assert.Error(t, err) 66 assert.True(t, f == nil) // Do NOT change this to assert.Nil(t, f) because that test is flawed. 67 f, err = dos.OpenFile(context.Background(), badFile, os.O_RDONLY, 0o600) 68 assert.Error(t, err) 69 assert.True(t, f == nil) // Do NOT change this to assert.Nil(t, f) because that test is flawed. 70 f, err = dos.Create(context.Background(), badFile) 71 assert.Error(t, err) 72 assert.True(t, f == nil) // Do NOT change this to assert.Nil(t, f) because that test is flawed. 73 } 74 75 // Example using afero.MemMapFs. 76 func ExampleWithFS() { 77 appFS := afero.NewCopyOnWriteFs(afero.NewOsFs(), afero.NewMemMapFs()) 78 ctx := dos.WithFS(context.Background(), aferofs.Wrap(appFS)) 79 80 if err := dos.MkdirAll(ctx, "/etc", 0o700); err != nil { 81 log.Fatal(err) 82 } 83 hosts, err := dos.Create(ctx, "/etc/example.conf") 84 if err != nil { 85 log.Fatal(err) 86 } 87 fmt.Fprintln(hosts, "example = conf") 88 hosts.Close() 89 90 if hosts, err = dos.Open(ctx, "/etc/example.conf"); err != nil { 91 log.Fatal(err) 92 } 93 _, err = io.Copy(os.Stdout, hosts) 94 _ = hosts.Close() 95 if err != nil { 96 log.Fatal(err) 97 } 98 99 if hosts, err = dos.Open(context.Background(), "/etc/example.conf"); err != nil { 100 if errors.Is(err, fs.ErrNotExist) { 101 fmt.Println("file does not exist") 102 } else { 103 fmt.Println(err) 104 } 105 } 106 // Output: 107 // example = conf 108 // file does not exist 109 }