This repository has been archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
/
file_blackbox_test.go
163 lines (140 loc) · 3.31 KB
/
file_blackbox_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package torus_test
import (
"bytes"
"crypto/rand"
"io"
"io/ioutil"
"testing"
"github.com/coreos/torus"
"github.com/coreos/torus/blockset"
"github.com/coreos/torus/models"
_ "github.com/coreos/torus/metadata/temp"
_ "github.com/coreos/torus/storage"
)
func makeTestData(size int) []byte {
out := make([]byte, size)
_, err := rand.Read(out)
if err != nil {
panic(err)
}
return out
}
func makeFile(name string, t *testing.T) (*torus.Server, *torus.File) {
srv := torus.NewMemoryServer()
vol := &models.Volume{
Name: name,
Id: 3,
Type: "test",
}
globals := srv.MDS.GlobalMetadata()
bs, err := blockset.CreateBlocksetFromSpec(globals.DefaultBlockSpec, srv.Blocks)
if err != nil {
t.Fatal(err)
}
inode := models.NewEmptyINode()
inode.INode = 1
inode.Volume = vol.Id
inode.Blocks, err = torus.MarshalBlocksetToProto(bs)
f, err := srv.CreateFile(vol, inode, bs)
if err != nil {
t.Fatal(err)
}
return srv, f
}
func newFile(name string, t *testing.T) *torus.File {
_, f := makeFile(name, t)
return f
}
func TestReadAt(t *testing.T) {
f := newFile("TestReadAt", t)
defer f.Close()
const data = "hello, world\n"
io.WriteString(f, data)
b := make([]byte, 5)
n, err := f.ReadAt(b, 7)
if err != nil || n != len(b) {
t.Fatalf("ReadAt 7: %d, %v", n, err)
}
if string(b) != "world" {
t.Fatalf("ReadAt 7: have %q want %q", string(b), "world")
}
_, err = f.SyncAllWrites()
if err != nil {
t.Fatalf("error on sync: %v", err)
}
b = make([]byte, 5)
n, err = f.ReadAt(b, 7)
if err != nil || n != len(b) {
t.Fatalf("ReadAt 7: %d, %v", n, err)
}
if string(b) != "world" {
t.Fatalf("ReadAt 7: have %q want %q", string(b), "world")
}
}
func TestReadAtOffset(t *testing.T) {
f := newFile("TestReadAtOffset", t)
defer f.Close()
const data = "hello, world\n"
io.WriteString(f, data)
f.Seek(0, 0)
b := make([]byte, 5)
n, err := f.ReadAt(b, 7)
if err != nil || n != len(b) {
t.Fatalf("ReadAt 7: %d, %v", n, err)
}
if string(b) != "world" {
t.Fatalf("ReadAt 7: have %q want %q", string(b), "world")
}
n, err = f.Read(b)
if err != nil || n != len(b) {
t.Fatalf("Read: %d, %v", n, err)
}
if string(b) != "hello" {
t.Fatalf("Read: have %q want %q", string(b), "hello")
}
}
func TestWriteAt(t *testing.T) {
f := newFile("TestWriteAt", t)
defer f.Close()
const data = "hello, world\n"
io.WriteString(f, data)
n, err := f.WriteAt([]byte("WORLD"), 7)
if err != nil || n != 5 {
t.Fatalf("WriteAt 7: %d, %v", n, err)
}
f.Seek(0, 0)
b, err := ioutil.ReadAll(f)
if err != nil {
t.Fatalf("ReadAll %v: %v", f, err)
}
if string(b) != "hello, WORLD\n" {
t.Fatalf("after write: have %q want %q", string(b), "hello, WORLD\n")
}
}
func TestWriteAtBulk(t *testing.T) {
f := newFile("TestWriteAt", t)
defer f.Close()
const data = "hello, world\n"
io.WriteString(f, data)
_, err := f.SyncAllWrites()
if err != nil {
t.Fatalf("can't sync: %v", err)
}
// Odd-shape to stress block edges.
big := makeTestData(549)
n, err := f.WriteAt(big, 7)
if err != nil || n != len(big) {
t.Fatalf("WriteAt 7: %d, %v", n, err)
}
f.Seek(0, 0)
b, err := ioutil.ReadAll(f)
if err != nil {
t.Fatalf("ReadAll %v: %v", f, err)
}
if string(b[:6]) != "hello," {
t.Fatalf("after write: have %q want %q", string(b), "hello,")
}
if !bytes.Equal(b[7:], big) {
t.Fatal("byte strings aren't equal")
}
}