/
main_test.go
189 lines (172 loc) · 4.89 KB
/
main_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2013 Jason McVetta. This is Free Software, released under the
// terms of the AGPL v3. See http://www.gnu.org/licenses/agpl-3.0.html for
// details. Resist intellectual serfdom - the ownership of ideas is akin to
// slavery.
package main
import (
"bytes"
"crypto/rand"
"encoding/base64"
"io/ioutil"
. "launchpad.net/gocheck"
"log"
"math/big"
"net/http"
"net/http/httptest"
"os"
"sync"
"testing"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) {
Suite(&TestSuite{})
TestingT(t)
}
type TestSuite struct {
dbDir string
url string
hserv *httptest.Server
}
func (s *TestSuite) SetUpSuite(c *C) {
log.SetFlags(log.Lshortfile)
}
func (s *TestSuite) SetUpTest(c *C) {
var err error
s.dbDir, err = ioutil.TempDir("", "blocker")
c.Assert(err, IsNil)
setupDb(s.dbDir)
h := muxer()
s.hserv = httptest.NewServer(h)
s.url = s.hserv.URL + "/blocker"
}
func (s *TestSuite) TearDownTest(c *C) {
s.hserv.Close()
os.RemoveAll(s.dbDir)
}
// post POSTs random data to the API.
func (s *TestSuite) post(c *C) (key string, value []byte, statusCode int) {
bi, err := rand.Int(rand.Reader, big.NewInt(int64(maxDataSize-1)))
c.Assert(err, IsNil)
size := int(bi.Int64()) + 1
// size := int(mbs * int(MiB))
// size := int(2 * MiB)
value = make([]byte, size)
_, err = rand.Read(value)
c.Assert(err, IsNil)
// We're not doing anything server-side with the bodyType
resp, err := http.Post(s.url, "", bytes.NewBuffer(value))
defer resp.Body.Close()
c.Assert(err, IsNil)
if resp.StatusCode != 200 && resp.StatusCode != 201 {
c.Fatal("Expected status 200 or 201, but got", resp.StatusCode)
}
b, err := ioutil.ReadAll(resp.Body)
c.Assert(err, IsNil)
key = string(b)
statusCode = resp.StatusCode
return
}
// TestWriteRead tests a round-trip of writing data to the API, then retrieving
// the same data from its sha1.
func (s *TestSuite) TestWriteRead(c *C) {
// Write
key, sendValue, _ := s.post(c)
// Read
url := s.url + "/" + string(key)
resp, err := http.Get(url)
defer resp.Body.Close()
c.Assert(resp.StatusCode, Equals, 200)
c.Assert(err, IsNil)
retValue, err := ioutil.ReadAll(resp.Body)
c.Assert(retValue, DeepEquals, sendValue)
}
// TestConcurrentWrites tests between 100 and 200 concurrent writes to the API.
func (s *TestSuite) TestConcurrentWrites(c *C) {
b, err := rand.Int(rand.Reader, big.NewInt(int64(99)))
c.Assert(err, IsNil)
count := 1 + int(b.Int64())
wg := sync.WaitGroup{}
wg.Add(count)
writer := func() {
s.post(c)
wg.Done()
}
for i := 0; i < count; i++ {
go writer()
}
wg.Wait()
}
// TestConcurrentSameData tests between 100 and 200 concurrent writes of
// identical data to the API.
func (s *TestSuite) TestConcurrentSameData(c *C) {
b, err := rand.Int(rand.Reader, big.NewInt(int64(100)))
c.Assert(err, IsNil)
count := 100 + int(b.Int64())
wg := sync.WaitGroup{}
wg.Add(count)
bi, err := rand.Int(rand.Reader, big.NewInt(int64(int(MiB)-1)))
c.Assert(err, IsNil)
size := int(bi.Int64()) + 1
value := make([]byte, size)
_, err = rand.Read(value)
c.Assert(err, IsNil)
writer := func() {
resp, err := http.Post(s.url, "", bytes.NewBuffer(value))
defer resp.Body.Close()
c.Assert(err, IsNil)
if resp.StatusCode != 200 && resp.StatusCode != 201 {
c.Fatal("Expected status 200 or 201, but got", resp.StatusCode)
}
// Done
wg.Done()
}
for i := 0; i < count; i++ {
go writer()
}
wg.Wait()
}
// TestGetNoKey tests for 400 error when trying to GET without a sha1 key.
func (s *TestSuite) TestGetNoKey(c *C) {
r, err := http.Get(s.url)
c.Assert(err, IsNil)
// StatusMethodNotAllowed is apparently set by pat
c.Assert(r.StatusCode, Equals, http.StatusMethodNotAllowed)
}
// TestAlreadyExists tests writing a block that already exists on disk.
func (s *TestSuite) TestAlreadyExists(c *C) {
// Write
_, sendValue, _ := s.post(c)
// And again...
resp, err := http.Post(s.url, "", bytes.NewBuffer(sendValue))
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, 200)
}
// TestDiskFull tests for proper error handling when the disk to which the
// server writes data is full.
func (s *TestSuite) TestDiskFull(c *C) {
c.Log("WARNING: TestDiskFull not yet implemented!")
}
// TestCorrupt tests server response when data is corrupted on disk.
func (s *TestSuite) TestCorrupt(c *C) {
// Write
key, _, _ := s.post(c)
// Cause intentional corruption
db.Write(string(key), []byte("foobar"))
// Expect 500 error - desirable behavior?
url := s.url + "/" + string(key)
resp, err := http.Get(url)
// defer resp.Body.Close()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, 500)
}
// TestNonexistent tests for 404 error when requesting a non existent sha1.
func (s *TestSuite) TestNonexistent(c *C) {
b := make([]byte, 8)
_, err := rand.Read(b)
c.Assert(err, IsNil)
key := base64.URLEncoding.EncodeToString(b)
url := s.url + "/" + string(key)
r, err := http.Get(url)
c.Assert(err, IsNil)
c.Assert(r.StatusCode, Equals, http.StatusNotFound)
}