tstor/daemons/archive/archive_test.go

156 lines
3.5 KiB
Go
Raw Normal View History

2025-01-20 02:18:15 +00:00
package archive_test
import (
"archive/zip"
"bytes"
2024-03-20 21:47:51 +00:00
"context"
2024-12-09 20:44:01 +00:00
"io"
"io/fs"
"testing"
2025-01-20 02:18:15 +00:00
"git.kmsign.ru/royalcat/tstor/daemons/archive"
2024-06-02 19:53:33 +00:00
"git.kmsign.ru/royalcat/tstor/src/vfs"
"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)
// }
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)
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
2025-01-20 02:18:15 +00:00
zfs, err := archive.NewArchive(ctx, "test", "test", zReader, size, archive.ZipLoader)
2024-03-28 13:09:42 +00:00
require.NoError(err)
2024-03-20 21:47:51 +00:00
files, err := zfs.ReadDir(ctx, "/path/to/test/file")
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)
2024-06-20 08:20:43 +00:00
out := make([]byte, 5)
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)
2024-12-09 20:44:01 +00:00
require.ErrorIs(err, io.EOF)
2024-06-20 08:20:43 +00:00
require.Equal(5, n)
require.Equal([]byte("Hello"), out)
outSpace := make([]byte, 1)
n, err = f.Read(ctx, outSpace)
2024-12-09 20:44:01 +00:00
require.ErrorIs(err, io.EOF)
2024-06-20 08:20:43 +00:00
require.Equal(1, n)
require.Equal([]byte(" "), outSpace)
n, err = f.Read(ctx, out)
2024-12-09 20:44:01 +00:00
require.ErrorIs(err, io.EOF)
2024-06-20 08:20:43 +00:00
require.Equal(5, n)
require.Equal([]byte("World"), out)
}
2024-12-09 20:44:01 +00:00
func createTestZip(require *require.Assertions) (vfs.File, int64) {
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())
}
func newCBR(b []byte) *closeableByteReader {
return &closeableByteReader{
2024-03-20 21:47:51 +00:00
data: bytes.NewReader(b),
}
}
2024-12-09 20:44:01 +00:00
var _ vfs.File = &closeableByteReader{}
type closeableByteReader struct {
data *bytes.Reader
}
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)
}
2024-12-09 20:44:01 +00:00
// Close implements vfs.File.
func (c *closeableByteReader) Close(ctx context.Context) error {
panic("unimplemented")
}
// Info implements vfs.File.
func (c *closeableByteReader) Info() (fs.FileInfo, error) {
panic("unimplemented")
}
// IsDir implements vfs.File.
func (c *closeableByteReader) IsDir() bool {
panic("unimplemented")
}
// Name implements vfs.File.
func (c *closeableByteReader) Name() string {
panic("unimplemented")
}
// Read implements vfs.File.
func (c *closeableByteReader) Read(ctx context.Context, p []byte) (n int, err error) {
return c.data.Read(p)
}
// Seek implements vfs.File.
func (c *closeableByteReader) Seek(offset int64, whence int) (int64, error) {
return c.data.Seek(offset, whence)
}
// Size implements vfs.File.
func (c *closeableByteReader) Size() int64 {
return c.data.Size()
}
// Type implements vfs.File.
func (c *closeableByteReader) Type() fs.FileMode {
panic("unimplemented")
}