aboutsummaryrefslogtreecommitdiff
path: root/weed/filer2/memdb/memdb_store_test.go
blob: 31da8998f0163c28421e409673eff507d7aedc19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package memdb

import (
	"github.com/chrislusf/seaweedfs/weed/filer2"
	"testing"
)

func TestCreateAndFind(t *testing.T) {
	filer := filer2.NewFiler(nil, nil)
	store := &MemDbStore{}
	store.Initialize(nil)
	filer.SetStore(store)
	filer.DisableDirectoryCache()

	fullpath := filer2.FullPath("/home/chris/this/is/one/file1.jpg")

	entry1 := &filer2.Entry{
		FullPath: fullpath,
		Attr: filer2.Attr{
			Mode: 0440,
			Uid:  1234,
			Gid:  5678,
		},
	}

	if err := filer.CreateEntry(entry1); err != nil {
		t.Errorf("create entry %v: %v", entry1.FullPath, err)
		return
	}

	entry, err := filer.FindEntry(fullpath)

	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) {
	filer := filer2.NewFiler(nil, nil)
	store := &MemDbStore{}
	store.Initialize(nil)
	filer.SetStore(store)
	filer.DisableDirectoryCache()

	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,
		},
	}

	filer.CreateEntry(entry1)
	filer.CreateEntry(entry2)

	// checking the 2 files
	entries, err := filer.ListDirectoryEntries(filer2.FullPath("/home/chris/this/is/one/"), "", false, 100)

	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
	}

	// checking the offset
	entries, err = filer.ListDirectoryEntries(filer2.FullPath("/home/chris/this/is/one/"), "file1.jpg", false, 100)
	if len(entries) != 1 {
		t.Errorf("list entries count: %v", len(entries))
		return
	}

	// checking one upper directory
	entries, _ = filer.ListDirectoryEntries(filer2.FullPath("/home/chris/this/is"), "", false, 100)
	if len(entries) != 1 {
		t.Errorf("list entries count: %v", len(entries))
		return
	}

	// checking root directory
	entries, _ = filer.ListDirectoryEntries(filer2.FullPath("/"), "", false, 100)
	if len(entries) != 1 {
		t.Errorf("list entries count: %v", len(entries))
		return
	}

	// add file3
	file3Path := filer2.FullPath("/home/chris/this/is/file3.jpg")
	entry3 := &filer2.Entry{
		FullPath: file3Path,
		Attr: filer2.Attr{
			Mode: 0440,
			Uid:  1234,
			Gid:  5678,
		},
	}
	filer.CreateEntry(entry3)

	// checking one upper directory
	entries, _ = filer.ListDirectoryEntries(filer2.FullPath("/home/chris/this/is"), "", false, 100)
	if len(entries) != 2 {
		t.Errorf("list entries count: %v", len(entries))
		return
	}

	// delete file and count
	filer.DeleteEntryMetaAndData(file3Path, false, false)
	entries, _ = filer.ListDirectoryEntries(filer2.FullPath("/home/chris/this/is"), "", false, 100)
	if len(entries) != 1 {
		t.Errorf("list entries count: %v", len(entries))
		return
	}

}