tstor/daemons/archive/archive_test.go
royalcat 832f5b9710
Some checks failed
docker / build-docker (push) Has been cancelled
wip
2025-01-20 05:18:15 +03:00

155 lines
3.5 KiB
Go

package archive_test
import (
"archive/zip"
"bytes"
"context"
"io"
"io/fs"
"testing"
"git.kmsign.ru/royalcat/tstor/daemons/archive"
"git.kmsign.ru/royalcat/tstor/src/vfs"
"github.com/stretchr/testify/require"
)
// 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)
zReader, size := createTestZip(require)
ctx := context.Background()
// TODO add single dir collapse test
zfs, err := archive.NewArchive(ctx, "test", "test", zReader, size, archive.ZipLoader)
require.NoError(err)
files, err := zfs.ReadDir(ctx, "/path/to/test/file")
require.NoError(err)
require.Len(files, 1)
e := files[0]
require.Equal("1.txt", e.Name())
require.NotNil(e)
out := make([]byte, 5)
f, err := zfs.Open(ctx, "/path/to/test/file/1.txt")
require.NoError(err)
n, err := f.Read(ctx, out)
require.ErrorIs(err, io.EOF)
require.Equal(5, n)
require.Equal([]byte("Hello"), out)
outSpace := make([]byte, 1)
n, err = f.Read(ctx, outSpace)
require.ErrorIs(err, io.EOF)
require.Equal(1, n)
require.Equal([]byte(" "), outSpace)
n, err = f.Read(ctx, out)
require.ErrorIs(err, io.EOF)
require.Equal(5, n)
require.Equal([]byte("World"), out)
}
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{
data: bytes.NewReader(b),
}
}
var _ vfs.File = &closeableByteReader{}
type closeableByteReader struct {
data *bytes.Reader
}
// 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)
}
// 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")
}