2024-11-18 22:34:07 +01:00
|
|
|
package fsys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"io"
|
2024-11-19 02:54:41 +01:00
|
|
|
"io/fs"
|
2024-11-18 22:34:07 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStorageS3(t *testing.T) {
|
|
|
|
|
2024-11-18 23:00:58 +01:00
|
|
|
stor, err := New(Config{
|
2024-11-18 22:34:07 +01:00
|
|
|
Type: "s3",
|
|
|
|
S3BucketName: "test",
|
|
|
|
S3AccessID: "root",
|
|
|
|
S3AccessKey: "password123",
|
|
|
|
S3Endpoint: "127.0.0.1:9000",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
t.Log("== Write ==")
|
2024-11-20 16:19:07 +01:00
|
|
|
|
2024-11-18 22:34:07 +01:00
|
|
|
err = stor.Write("test.txt", []byte("hi"), WriteOptions{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-11-20 16:19:07 +01:00
|
|
|
err = stor.Write("testing/1.txt", []byte("hi"), WriteOptions{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = stor.Write("testing/2.txt", []byte("hi"), WriteOptions{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = stor.Write("testing/test/2.txt", []byte("hi"), WriteOptions{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-11-19 02:54:41 +01:00
|
|
|
t.Log("== Stat ==")
|
2024-11-20 16:19:07 +01:00
|
|
|
|
2024-11-19 02:54:41 +01:00
|
|
|
fileInfo, err := stor.Stat("test.txt")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test.txt", fileInfo.Name)
|
|
|
|
assert.Equal(t, int64(len("hi")), fileInfo.Size)
|
|
|
|
assert.Equal(t, fs.FileMode(0777), fileInfo.Mode)
|
|
|
|
|
2024-11-18 22:39:35 +01:00
|
|
|
t.Log("== Read ==")
|
2024-11-20 16:19:07 +01:00
|
|
|
|
2024-11-18 22:39:35 +01:00
|
|
|
fileContent, err := stor.Read("test.txt")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("hi"), fileContent)
|
|
|
|
|
2024-11-20 19:45:51 +01:00
|
|
|
_, err = stor.Read("nonexistent")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, ErrFileNotFound, err)
|
|
|
|
|
2024-11-20 16:19:07 +01:00
|
|
|
t.Log("== ReadDir ==")
|
|
|
|
|
|
|
|
files, err := stor.ReadDir("testing")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 3, len(files))
|
|
|
|
|
2024-11-19 14:56:43 +01:00
|
|
|
t.Log("== Copy ==")
|
2024-11-20 16:19:07 +01:00
|
|
|
|
2024-11-19 14:56:43 +01:00
|
|
|
err = stor.Copy("test.txt", "test2.txt")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
copyFileInfo, err := stor.Stat("test2.txt")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, fs.FileMode(0777), copyFileInfo.Mode)
|
|
|
|
assert.Equal(t, "test2.txt", copyFileInfo.Name)
|
2024-11-19 15:20:53 +01:00
|
|
|
assert.Equal(t, int64(len("hi")), copyFileInfo.Size)
|
|
|
|
|
2024-11-20 21:43:34 +01:00
|
|
|
t.Log("== CopyDir ==")
|
|
|
|
|
|
|
|
err = stor.CopyDir("testing", "testing2")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
copyFiles, err := stor.ReadDir("testing2")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 3, len(copyFiles))
|
|
|
|
|
|
|
|
t.Log("== DeleteDir ==")
|
|
|
|
|
|
|
|
err = stor.DeleteDir("testing2")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-11-19 15:20:53 +01:00
|
|
|
t.Log("== Move ==")
|
2024-11-20 16:19:07 +01:00
|
|
|
|
2024-11-19 15:20:53 +01:00
|
|
|
err = stor.Move("test2.txt", "test3.txt")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
moveFileInfo, err := stor.Stat("test3.txt")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, fs.FileMode(0777), moveFileInfo.Mode)
|
|
|
|
assert.Equal(t, "test3.txt", moveFileInfo.Name)
|
|
|
|
assert.Equal(t, int64(len("hi")), moveFileInfo.Size)
|
2024-11-19 14:56:43 +01:00
|
|
|
|
2024-11-18 22:34:07 +01:00
|
|
|
t.Log("== Open ==")
|
2024-11-20 16:19:07 +01:00
|
|
|
|
2024-11-18 22:34:07 +01:00
|
|
|
file, err := stor.Open("test.txt")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test.txt", file.Name)
|
|
|
|
|
2024-11-18 22:39:35 +01:00
|
|
|
fileContent, err = io.ReadAll(file)
|
2024-11-18 22:34:07 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("hi"), fileContent)
|
|
|
|
|
|
|
|
err = file.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-11-18 22:48:49 +01:00
|
|
|
t.Log("== Delete ==")
|
2024-11-20 16:19:07 +01:00
|
|
|
|
2024-11-18 22:48:49 +01:00
|
|
|
err = stor.Delete("test.txt")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-11-18 22:34:07 +01:00
|
|
|
}
|