2024-03-28 13:09:42 +00:00
|
|
|
package vfs_test
|
2020-09-27 19:23:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
2024-03-20 21:47:51 +00:00
|
|
|
"context"
|
2020-09-27 19:23:47 +00:00
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2024-03-20 21:47:51 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/pkg/ctxio"
|
2024-03-28 13:09:42 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/src/host/vfs"
|
2020-09-27 19:23:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2024-03-28 13:09:42 +00:00
|
|
|
// TODO
|
|
|
|
// func TestArchiveFactories(t *testing.T) {
|
|
|
|
// t.Parallel()
|
|
|
|
|
|
|
|
// ctx := context.Background()
|
|
|
|
|
|
|
|
// require := require.New(t)
|
|
|
|
|
|
|
|
// require.Contains(vfs.ArchiveFactories, ".zip")
|
|
|
|
// require.Contains(vfs.ArchiveFactories, ".rar")
|
|
|
|
// require.Contains(vfs.ArchiveFactories, ".7z")
|
|
|
|
|
|
|
|
// fs, err := vfs.ArchiveFactories[".zip"](ctx, &vfs.DummyFile{})
|
|
|
|
// require.NoError(err)
|
|
|
|
// require.NotNil(fs)
|
|
|
|
|
|
|
|
// fs, err = vfs.ArchiveFactories[".rar"](ctx, &vfs.DummyFile{})
|
|
|
|
// require.NoError(err)
|
|
|
|
// require.NotNil(fs)
|
|
|
|
|
|
|
|
// fs, err = vfs.ArchiveFactories[".7z"](ctx, &vfs.DummyFile{})
|
|
|
|
// require.NoError(err)
|
|
|
|
// require.NotNil(fs)
|
|
|
|
// }
|
|
|
|
|
2020-09-27 19:23:47 +00:00
|
|
|
var fileContent []byte = []byte("Hello World")
|
|
|
|
|
|
|
|
func TestZipFilesystem(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-12-21 23:15:39 +00:00
|
|
|
zReader, size := createTestZip(require)
|
2020-09-27 19:23:47 +00:00
|
|
|
|
2024-03-20 21:47:51 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-01-28 20:22:49 +00:00
|
|
|
// TODO add single dir collapse test
|
2024-03-28 13:09:42 +00:00
|
|
|
zfs, err := vfs.NewArchive(ctx, "test", zReader, size, vfs.ZipLoader)
|
|
|
|
require.NoError(err)
|
2020-09-27 19:23:47 +00:00
|
|
|
|
2024-03-20 21:47:51 +00:00
|
|
|
files, err := zfs.ReadDir(ctx, "/path/to/test/file")
|
2020-09-27 19:23:47 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
require.Len(files, 1)
|
2023-12-21 23:15:39 +00:00
|
|
|
e := files[0]
|
|
|
|
require.Equal("1.txt", e.Name())
|
|
|
|
require.NotNil(e)
|
2020-09-27 19:23:47 +00:00
|
|
|
|
|
|
|
out := make([]byte, 11)
|
2024-03-20 21:47:51 +00:00
|
|
|
f, err := zfs.Open(ctx, "/path/to/test/file/1.txt")
|
2023-12-21 23:15:39 +00:00
|
|
|
require.NoError(err)
|
2024-03-20 21:47:51 +00:00
|
|
|
n, err := f.Read(ctx, out)
|
2020-09-27 19:23:47 +00:00
|
|
|
require.Equal(io.EOF, err)
|
|
|
|
require.Equal(11, n)
|
|
|
|
require.Equal(fileContent, out)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-20 21:47:51 +00:00
|
|
|
func createTestZip(require *require.Assertions) (ctxio.ReaderAt, int64) {
|
2020-09-27 19:23:47 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
|
|
|
|
zWriter := zip.NewWriter(buf)
|
|
|
|
|
|
|
|
f1, err := zWriter.Create("path/to/test/file/1.txt")
|
|
|
|
require.NoError(err)
|
|
|
|
_, err = f1.Write(fileContent)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
err = zWriter.Close()
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
return newCBR(buf.Bytes()), int64(buf.Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
type closeableByteReader struct {
|
2024-03-20 21:47:51 +00:00
|
|
|
data *bytes.Reader
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newCBR(b []byte) *closeableByteReader {
|
|
|
|
return &closeableByteReader{
|
2024-03-20 21:47:51 +00:00
|
|
|
data: bytes.NewReader(b),
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 21:47:51 +00:00
|
|
|
// ReadAt implements ctxio.ReaderAt.
|
|
|
|
func (c *closeableByteReader) ReadAt(ctx context.Context, p []byte, off int64) (n int, err error) {
|
|
|
|
return c.data.ReadAt(p, off)
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|