Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/op/storage_test.go
1560 views
1
package op_test
2
3
import (
4
"context"
5
"testing"
6
7
"github.com/alist-org/alist/v3/internal/conf"
8
"github.com/alist-org/alist/v3/internal/db"
9
"github.com/alist-org/alist/v3/internal/model"
10
"github.com/alist-org/alist/v3/internal/op"
11
"github.com/alist-org/alist/v3/pkg/utils"
12
mapset "github.com/deckarep/golang-set/v2"
13
"gorm.io/driver/sqlite"
14
"gorm.io/gorm"
15
)
16
17
func init() {
18
dB, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
19
if err != nil {
20
panic("failed to connect database")
21
}
22
conf.Conf = conf.DefaultConfig()
23
db.Init(dB)
24
}
25
26
func TestCreateStorage(t *testing.T) {
27
var storages = []struct {
28
storage model.Storage
29
isErr bool
30
}{
31
{storage: model.Storage{Driver: "Local", MountPath: "/local", Addition: `{"root_folder_path":"."}`}, isErr: false},
32
{storage: model.Storage{Driver: "Local", MountPath: "/local", Addition: `{"root_folder_path":"."}`}, isErr: true},
33
{storage: model.Storage{Driver: "None", MountPath: "/none", Addition: `{"root_folder_path":"."}`}, isErr: true},
34
}
35
for _, storage := range storages {
36
_, err := op.CreateStorage(context.Background(), storage.storage)
37
if err != nil {
38
if !storage.isErr {
39
t.Errorf("failed to create storage: %+v", err)
40
} else {
41
t.Logf("expect failed to create storage: %+v", err)
42
}
43
}
44
}
45
}
46
47
func TestGetStorageVirtualFilesByPath(t *testing.T) {
48
setupStorages(t)
49
virtualFiles := op.GetStorageVirtualFilesByPath("/a")
50
var names []string
51
for _, virtualFile := range virtualFiles {
52
names = append(names, virtualFile.GetName())
53
}
54
var expectedNames = []string{"b", "c", "d"}
55
if utils.SliceEqual(names, expectedNames) {
56
t.Logf("passed")
57
} else {
58
t.Errorf("expected: %+v, got: %+v", expectedNames, names)
59
}
60
}
61
62
func TestGetBalancedStorage(t *testing.T) {
63
set := mapset.NewSet[string]()
64
for i := 0; i < 5; i++ {
65
storage := op.GetBalancedStorage("/a/d/e1")
66
set.Add(storage.GetStorage().MountPath)
67
}
68
expected := mapset.NewSet([]string{"/a/d/e1", "/a/d/e1.balance"}...)
69
if !expected.Equal(set) {
70
t.Errorf("expected: %+v, got: %+v", expected, set)
71
}
72
}
73
74
func setupStorages(t *testing.T) {
75
var storages = []model.Storage{
76
{Driver: "Local", MountPath: "/a/b", Order: 0, Addition: `{"root_folder_path":"."}`},
77
{Driver: "Local", MountPath: "/adc", Order: 0, Addition: `{"root_folder_path":"."}`},
78
{Driver: "Local", MountPath: "/a/c", Order: 1, Addition: `{"root_folder_path":"."}`},
79
{Driver: "Local", MountPath: "/a/d", Order: 2, Addition: `{"root_folder_path":"."}`},
80
{Driver: "Local", MountPath: "/a/d/e1", Order: 3, Addition: `{"root_folder_path":"."}`},
81
{Driver: "Local", MountPath: "/a/d/e", Order: 4, Addition: `{"root_folder_path":"."}`},
82
{Driver: "Local", MountPath: "/a/d/e1.balance", Order: 4, Addition: `{"root_folder_path":"."}`},
83
}
84
for _, storage := range storages {
85
_, err := op.CreateStorage(context.Background(), storage)
86
if err != nil {
87
t.Fatalf("failed to create storage: %+v", err)
88
}
89
}
90
}
91
92