forked from colinmarc/hdfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot_test.go
113 lines (88 loc) · 2.63 KB
/
snapshot_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
package hdfs
import (
"io/ioutil"
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func writeAndVerifyTestFile(t *testing.T, snapshotDir, filepath string) {
c := getClientForSuperUser(t)
baleet(t, filepath)
mkdirp(t, snapshotDir)
f, err := c.CreateFile(filepath, 1, 1048576, 0744)
require.NoError(t, err)
// fill the file a bit
b := make([]byte, 128)
for i := 0; i < 128; i++ {
b[i] = 'a'
}
_, err = f.Write(b)
require.NoError(t, err)
f.Close()
nf, err := c.Open(filepath)
require.NoError(t, err)
br, err := ioutil.ReadAll(nf)
require.NoError(t, err)
nf.Close()
assert.Equal(t, b, br)
}
func baleetSnapshot(t *testing.T, dir, snapshot string) {
c := getClientForSuperUser(t)
c.DeleteSnapshot(dir, snapshot)
}
func TestAllowSnapshot(t *testing.T) {
c := getClientForSuperUser(t)
baleetSnapshot(t, "/_test/allowsnaps", "snap")
mkdirp(t, "/_test/allowsnaps")
err := c.AllowSnapshots("/_test/allowsnaps")
require.NoError(t, err)
path, err := c.CreateSnapshot("/_test/allowsnaps", "snap")
require.NoError(t, err)
assert.Equal(t, "/_test/allowsnaps/.snapshot/snap", path)
}
func TestDisallowSnapshot(t *testing.T) {
c := getClientForSuperUser(t)
baleetSnapshot(t, "/_test/allowsnaps", "snap")
mkdirp(t, "/_test/allowsnaps")
err := c.DisallowSnapshots("/_test/allowsnaps")
require.NoError(t, err)
_, err = c.CreateSnapshot("/_test/allowsnaps", "snap")
require.Error(t, err)
}
func TestSnapshot(t *testing.T) {
const name = "TestSnapshot"
const dir = "/_test/snapshot"
const filename = "file_to_restore.txt"
const filepath = "/_test/snapshot/file_to_restore.txt"
c := getClientForSuperUser(t)
baleetSnapshot(t, dir, name)
writeAndVerifyTestFile(t, dir, filepath)
err := c.AllowSnapshots(dir)
require.NoError(t, err)
snapshotPath, err := c.CreateSnapshot(dir, name)
require.NoError(t, err)
err = c.Remove(filepath)
require.NoError(t, err)
_, err = c.Stat(filepath)
assertPathError(t, err, "stat", filepath, os.ErrNotExist)
st, err := c.Stat(path.Join(snapshotPath, filename))
require.NoError(t, err)
assert.Equal(t, int64(128), st.Size())
}
func TestDeleteSnapshot(t *testing.T) {
c := getClientForSuperUser(t)
baleetSnapshot(t, "/_test/deletesnaps", "snap")
mkdirp(t, "/_test/deletesnaps")
err := c.AllowSnapshots("/_test/deletesnaps")
require.NoError(t, err)
path, err := c.CreateSnapshot("/_test/deletesnaps", "snap")
require.NoError(t, err)
fs, err := c.Stat(path)
assert.True(t, fs.IsDir())
err = c.DeleteSnapshot("/_test/deletesnaps", "snap")
require.NoError(t, err)
_, err = c.Stat(path)
assertPathError(t, err, "stat", path, os.ErrNotExist)
}