-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstorage_test.go
81 lines (61 loc) · 1.88 KB
/
storage_test.go
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
package pitreos
import (
"context"
"fmt"
"io"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestDStoreStorage_ListBackups(t *testing.T) {
path := "/tmp/test"
_ = os.RemoveAll(path)
ctx := context.Background()
storage, err := NewDStoreStorage(ctx, fmt.Sprintf("file://%s", path))
require.NoError(t, err)
err = storage.WriteBackupIndex("b1", []byte{1, 2, 3})
require.NoError(t, err)
err = storage.WriteBackupIndex("b2", []byte{1, 2, 3})
require.NoError(t, err)
err = storage.WriteBackupIndex("b3", []byte{1, 2, 3})
require.NoError(t, err)
out, err := storage.ListBackups(2, "")
require.NoError(t, err)
require.Equal(t, []string{"b1", "b2"}, out)
out, err = storage.ListBackups(3, "")
require.NoError(t, err)
require.Equal(t, []string{"b1", "b2", "b3"}, out)
}
func TestDStoreStorage_WriteBackupIndex_OpenBackupIndex(t *testing.T) {
path := "/tmp/test"
_ = os.RemoveAll(path)
ctx := context.Background()
storage, err := NewDStoreStorage(ctx, fmt.Sprintf("file://%s", path))
require.NoError(t, err)
storage.WriteBackupIndex("b1", []byte{1, 2, 3})
rc, err := storage.OpenBackupIndex("b1")
require.NoError(t, err)
b := make([]byte, 8)
l, err := rc.Read(b)
require.Error(t, io.EOF)
require.Equal(t, 3, l)
require.Equal(t, []byte{1, 2, 3, 0, 0, 0, 0, 0}, b)
}
func TestNewDStoreStorage_WriteChunk_ChunkExists_OpenChunk(t *testing.T) {
path := "/tmp/test"
_ = os.RemoveAll(path)
ctx := context.Background()
storage, err := NewDStoreStorage(ctx, fmt.Sprintf("file://%s", path))
require.NoError(t, err)
err = storage.WriteChunk("hash.1", []byte{1, 2, 3})
require.NoError(t, err)
exist, err := storage.ChunkExists("hash.1")
require.NoError(t, err)
require.True(t, exist)
rc, err := storage.OpenChunk("hash.1")
b := make([]byte, 8)
l, err := rc.Read(b)
require.Error(t, io.EOF)
require.Equal(t, 3, l)
require.Equal(t, []byte{1, 2, 3, 0, 0, 0, 0, 0}, b)
}