-
Notifications
You must be signed in to change notification settings - Fork 43
/
local_test.go
149 lines (113 loc) · 3.7 KB
/
local_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
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
145
146
147
148
149
package desync
import (
"context"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestLocalStoreCompressed(t *testing.T) {
store := t.TempDir()
s, err := NewLocalStore(store, StoreOptions{})
require.NoError(t, err)
// Make up some data and store it
dataIn := []byte("some data")
chunkIn := NewChunk(dataIn)
id := chunkIn.ID()
// Store the chunk
err = s.StoreChunk(chunkIn)
require.NoError(t, err)
// Check it's in the store
hasChunk, err := s.HasChunk(id)
require.NoError(t, err)
require.True(t, hasChunk, "chunk not found in store")
// Pull the data the "official" way
chunkOut, err := s.GetChunk(id)
require.NoError(t, err)
dataOut, err := chunkOut.Data()
require.NoError(t, err)
// Compare the data that went in with what came out
require.Equal(t, dataIn, dataOut)
// Now let's look at the file in the store directly to make sure it's compressed
_, name := s.nameFromID(id)
b, err := ioutil.ReadFile(name)
require.NoError(t, err)
require.NotEqual(t, dataIn, b, "chunk is not compressed")
}
func TestLocalStoreUncompressed(t *testing.T) {
store := t.TempDir()
s, err := NewLocalStore(store, StoreOptions{Uncompressed: true})
require.NoError(t, err)
// Make up some data and store it
dataIn := []byte("some data")
chunkIn := NewChunk(dataIn)
id := chunkIn.ID()
err = s.StoreChunk(chunkIn)
require.NoError(t, err)
// Check it's in the store
hasChunk, err := s.HasChunk(id)
require.NoError(t, err)
require.True(t, hasChunk, "chunk not found in store")
// Pull the data the "official" way
chunkOut, err := s.GetChunk(id)
require.NoError(t, err)
dataOut, err := chunkOut.Data()
require.NoError(t, err)
// Compare the data that went in with what came out
require.Equal(t, dataIn, dataOut)
// Now let's look at the file in the store directly to make sure it's uncompressed
_, name := s.nameFromID(id)
b, err := ioutil.ReadFile(name)
require.NoError(t, err)
require.Equal(t, dataIn, b, "chunk is compressed")
}
func TestLocalStoreErrorHandling(t *testing.T) {
store := t.TempDir()
s, err := NewLocalStore(store, StoreOptions{})
require.NoError(t, err)
// Make up some data and store it
dataIn := []byte("some data")
chunkIn := NewChunk(dataIn)
id := chunkIn.ID()
err = s.StoreChunk(chunkIn)
require.NoError(t, err)
// Now put an invalid chunk into the store
idInvalid, err := ChunkIDFromString("1000000000000000000000000000000000000000000000000000000000000000")
require.NoError(t, err)
dirInvalid, nameInvalid := s.nameFromID(idInvalid)
_ = os.Mkdir(dirInvalid, 0755)
err = ioutil.WriteFile(nameInvalid, []byte("invalid data"), 0644)
require.NoError(t, err)
// Also add a blank chunk
idBlank, err := ChunkIDFromString("2000000000000000000000000000000000000000000000000000000000000000")
require.NoError(t, err)
dirBlank, nameBlank := s.nameFromID(idBlank)
_ = os.Mkdir(dirBlank, 0755)
err = ioutil.WriteFile(nameBlank, nil, 0644)
require.NoError(t, err)
// Let's see if we can retrieve the good chunk and get errors from the bad ones
_, err = s.GetChunk(id)
require.NoError(t, err)
_, err = s.GetChunk(idInvalid)
if _, ok := err.(ChunkInvalid); !ok {
t.Fatal(err)
}
_, err = s.GetChunk(idBlank)
if _, ok := err.(ChunkInvalid); !ok {
t.Fatal(err)
}
// Run the verify with repair enabled which should get rid of the invalid and blank chunks
err = s.Verify(context.Background(), 1, true, ioutil.Discard)
require.NoError(t, err)
// Let's see if we can still retrieve the good chunk and get Not Found for the others
_, err = s.GetChunk(id)
require.NoError(t, err)
_, err = s.GetChunk(idInvalid)
if _, ok := err.(ChunkMissing); !ok {
t.Fatal(err)
}
_, err = s.GetChunk(idBlank)
if _, ok := err.(ChunkMissing); !ok {
t.Fatal(err)
}
}