seaweedfs/weed/filer2/memdb/memdb_store_test.go

150 lines
3.3 KiB
Go
Raw Normal View History

2018-05-12 20:45:29 +00:00
package memdb
import (
2019-03-15 22:26:09 +00:00
"context"
2018-05-12 20:45:29 +00:00
"github.com/chrislusf/seaweedfs/weed/filer2"
2018-05-27 18:52:26 +00:00
"testing"
2018-05-12 20:45:29 +00:00
)
func TestCreateAndFind(t *testing.T) {
2019-02-18 20:11:52 +00:00
filer := filer2.NewFiler(nil, nil)
store := &MemDbStore{}
store.Initialize(nil)
filer.SetStore(store)
2018-05-12 20:45:29 +00:00
filer.DisableDirectoryCache()
2019-03-16 16:50:21 +00:00
ctx := context.Background()
2018-05-12 20:45:29 +00:00
fullpath := filer2.FullPath("/home/chris/this/is/one/file1.jpg")
entry1 := &filer2.Entry{
FullPath: fullpath,
Attr: filer2.Attr{
Mode: 0440,
Uid: 1234,
Gid: 5678,
},
}
2019-03-16 16:50:21 +00:00
if err := filer.CreateEntry(ctx, entry1); err != nil {
2018-05-12 20:45:29 +00:00
t.Errorf("create entry %v: %v", entry1.FullPath, err)
return
}
2019-03-16 16:50:21 +00:00
entry, err := filer.FindEntry(ctx, fullpath)
2018-05-12 20:45:29 +00:00
if err != nil {
t.Errorf("find entry: %v", err)
return
}
if entry.FullPath != entry1.FullPath {
t.Errorf("find wrong entry: %v", entry.FullPath)
return
}
}
func TestCreateFileAndList(t *testing.T) {
2019-02-18 20:11:52 +00:00
filer := filer2.NewFiler(nil, nil)
store := &MemDbStore{}
store.Initialize(nil)
filer.SetStore(store)
2018-05-12 20:45:29 +00:00
filer.DisableDirectoryCache()
2019-03-16 00:20:24 +00:00
ctx := context.Background()
2018-05-12 20:45:29 +00:00
entry1 := &filer2.Entry{
FullPath: filer2.FullPath("/home/chris/this/is/one/file1.jpg"),
Attr: filer2.Attr{
Mode: 0440,
Uid: 1234,
Gid: 5678,
},
}
entry2 := &filer2.Entry{
FullPath: filer2.FullPath("/home/chris/this/is/one/file2.jpg"),
Attr: filer2.Attr{
Mode: 0440,
Uid: 1234,
Gid: 5678,
},
}
2019-03-16 00:20:24 +00:00
filer.CreateEntry(ctx, entry1)
filer.CreateEntry(ctx, entry2)
2018-05-12 20:45:29 +00:00
2018-05-12 20:50:11 +00:00
// checking the 2 files
2019-03-16 00:20:24 +00:00
entries, err := filer.ListDirectoryEntries(ctx, filer2.FullPath("/home/chris/this/is/one/"), "", false, 100)
2018-05-12 20:45:29 +00:00
if err != nil {
t.Errorf("list entries: %v", err)
return
}
if len(entries) != 2 {
t.Errorf("list entries count: %v", len(entries))
return
}
if entries[0].FullPath != entry1.FullPath {
t.Errorf("find wrong entry 1: %v", entries[0].FullPath)
return
}
if entries[1].FullPath != entry2.FullPath {
t.Errorf("find wrong entry 2: %v", entries[1].FullPath)
return
}
2018-05-13 21:02:29 +00:00
// checking the offset
2019-03-16 00:20:24 +00:00
entries, err = filer.ListDirectoryEntries(ctx, filer2.FullPath("/home/chris/this/is/one/"), "file1.jpg", false, 100)
2018-05-13 21:02:29 +00:00
if len(entries) != 1 {
t.Errorf("list entries count: %v", len(entries))
return
}
2018-05-12 20:50:11 +00:00
// checking one upper directory
2019-03-16 00:20:24 +00:00
entries, _ = filer.ListDirectoryEntries(ctx, filer2.FullPath("/home/chris/this/is"), "", false, 100)
2018-05-12 20:50:11 +00:00
if len(entries) != 1 {
t.Errorf("list entries count: %v", len(entries))
return
}
2018-05-14 06:56:16 +00:00
// checking root directory
2019-03-16 00:20:24 +00:00
entries, _ = filer.ListDirectoryEntries(ctx, filer2.FullPath("/"), "", false, 100)
2018-05-14 06:56:16 +00:00
if len(entries) != 1 {
t.Errorf("list entries count: %v", len(entries))
return
}
// add file3
file3Path := filer2.FullPath("/home/chris/this/is/file3.jpg")
2018-05-12 20:50:11 +00:00
entry3 := &filer2.Entry{
FullPath: file3Path,
2018-05-12 20:50:11 +00:00
Attr: filer2.Attr{
Mode: 0440,
Uid: 1234,
Gid: 5678,
},
}
2019-03-16 00:20:24 +00:00
filer.CreateEntry(ctx, entry3)
2018-05-12 20:50:11 +00:00
// checking one upper directory
2019-03-16 00:20:24 +00:00
entries, _ = filer.ListDirectoryEntries(ctx, filer2.FullPath("/home/chris/this/is"), "", false, 100)
2018-05-12 20:50:11 +00:00
if len(entries) != 2 {
t.Errorf("list entries count: %v", len(entries))
return
}
// delete file and count
2019-03-16 00:20:24 +00:00
filer.DeleteEntryMetaAndData(ctx, file3Path, false, false)
entries, _ = filer.ListDirectoryEntries(ctx, filer2.FullPath("/home/chris/this/is"), "", false, 100)
if len(entries) != 1 {
t.Errorf("list entries count: %v", len(entries))
return
}
2018-05-12 20:45:29 +00:00
}