2022-08-31 13:01:15 +00:00
|
|
|
package op_test
|
2022-07-10 06:45:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2022-12-18 11:51:20 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/conf"
|
2022-08-03 06:26:59 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/db"
|
2022-07-10 06:45:39 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
2022-08-31 13:01:15 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/op"
|
2022-07-10 06:45:39 +00:00
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
2022-12-18 11:51:20 +00:00
|
|
|
mapset "github.com/deckarep/golang-set/v2"
|
2022-07-10 06:45:39 +00:00
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
dB, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to connect database")
|
|
|
|
}
|
2022-12-18 11:51:20 +00:00
|
|
|
conf.Conf = conf.DefaultConfig()
|
2022-07-10 06:45:39 +00:00
|
|
|
db.Init(dB)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateStorage(t *testing.T) {
|
|
|
|
var storages = []struct {
|
|
|
|
storage model.Storage
|
|
|
|
isErr bool
|
|
|
|
}{
|
2022-12-18 11:51:20 +00:00
|
|
|
{storage: model.Storage{Driver: "Local", MountPath: "/local", Addition: `{"root_folder_path":"."}`}, isErr: false},
|
|
|
|
{storage: model.Storage{Driver: "Local", MountPath: "/local", Addition: `{"root_folder_path":"."}`}, isErr: true},
|
|
|
|
{storage: model.Storage{Driver: "None", MountPath: "/none", Addition: `{"root_folder_path":"."}`}, isErr: true},
|
2022-07-10 06:45:39 +00:00
|
|
|
}
|
|
|
|
for _, storage := range storages {
|
2022-11-13 12:17:10 +00:00
|
|
|
_, err := op.CreateStorage(context.Background(), storage.storage)
|
2022-07-10 06:45:39 +00:00
|
|
|
if err != nil {
|
|
|
|
if !storage.isErr {
|
|
|
|
t.Errorf("failed to create storage: %+v", err)
|
|
|
|
} else {
|
|
|
|
t.Logf("expect failed to create storage: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetStorageVirtualFilesByPath(t *testing.T) {
|
|
|
|
setupStorages(t)
|
2022-08-31 13:01:15 +00:00
|
|
|
virtualFiles := op.GetStorageVirtualFilesByPath("/a")
|
2022-07-10 06:45:39 +00:00
|
|
|
var names []string
|
|
|
|
for _, virtualFile := range virtualFiles {
|
|
|
|
names = append(names, virtualFile.GetName())
|
|
|
|
}
|
|
|
|
var expectedNames = []string{"b", "c", "d"}
|
|
|
|
if utils.SliceEqual(names, expectedNames) {
|
|
|
|
t.Logf("passed")
|
|
|
|
} else {
|
|
|
|
t.Errorf("expected: %+v, got: %+v", expectedNames, names)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBalancedStorage(t *testing.T) {
|
2022-12-18 11:51:20 +00:00
|
|
|
set := mapset.NewSet[string]()
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
storage := op.GetBalancedStorage("/a/d/e1")
|
|
|
|
set.Add(storage.GetStorage().MountPath)
|
2022-07-10 06:45:39 +00:00
|
|
|
}
|
2022-12-18 11:51:20 +00:00
|
|
|
expected := mapset.NewSet([]string{"/a/d/e1", "/a/d/e1.balance"}...)
|
|
|
|
if !expected.Equal(set) {
|
|
|
|
t.Errorf("expected: %+v, got: %+v", expected, set)
|
2022-07-10 06:45:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupStorages(t *testing.T) {
|
|
|
|
var storages = []model.Storage{
|
2022-12-18 11:51:20 +00:00
|
|
|
{Driver: "Local", MountPath: "/a/b", Order: 0, Addition: `{"root_folder_path":"."}`},
|
|
|
|
{Driver: "Local", MountPath: "/adc", Order: 0, Addition: `{"root_folder_path":"."}`},
|
|
|
|
{Driver: "Local", MountPath: "/a/c", Order: 1, Addition: `{"root_folder_path":"."}`},
|
|
|
|
{Driver: "Local", MountPath: "/a/d", Order: 2, Addition: `{"root_folder_path":"."}`},
|
|
|
|
{Driver: "Local", MountPath: "/a/d/e1", Order: 3, Addition: `{"root_folder_path":"."}`},
|
|
|
|
{Driver: "Local", MountPath: "/a/d/e", Order: 4, Addition: `{"root_folder_path":"."}`},
|
|
|
|
{Driver: "Local", MountPath: "/a/d/e1.balance", Order: 4, Addition: `{"root_folder_path":"."}`},
|
2022-07-10 06:45:39 +00:00
|
|
|
}
|
|
|
|
for _, storage := range storages {
|
2022-11-13 12:17:10 +00:00
|
|
|
_, err := op.CreateStorage(context.Background(), storage)
|
2022-07-10 06:45:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create storage: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|