-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
207 lines (165 loc) · 4.81 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//go:build unit
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"testing"
db "github.com/btmorr/leifdb/internal/database"
"github.com/btmorr/leifdb/internal/node"
"github.com/btmorr/leifdb/internal/raft"
"github.com/btmorr/leifdb/internal/util"
"github.com/gin-gonic/gin"
)
// setupServer configures a Database, Node, and router for test, and creates
// a test directory that is automatically cleaned up after each test
func setupServer(t *testing.T) (*gin.Engine, *node.Node) {
addr := "localhost:16990"
clientAddr := "localhost:8080"
testDir, err := util.CreateTmpDir(".tmp-leifdb")
if err != nil {
log.Fatalln("Error creating test dir:", err)
}
t.Cleanup(func() {
util.RemoveTmpDir(testDir)
})
store := db.NewDatabase()
config := node.NewNodeConfig(testDir, addr, clientAddr, make([]string, 0, 0))
n, _ := node.NewNode(config, store)
router := buildRouter(n)
n.State = node.Leader
return router, n
}
func TestHealthRoute(t *testing.T) {
router, _ := setupServer(t)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/health", nil)
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Error("Non-200 health status")
}
}
func TestReadAfterWrite(t *testing.T) {
router, _ := setupServer(t)
v := "testy"
body1 := WriteRequest{Value: v}
b1, _ := json.Marshal(body1)
br1 := bytes.NewReader(b1)
w := httptest.NewRecorder()
req, _ := http.NewRequest("PUT", "/db/stuff", br1)
router.ServeHTTP(w, req)
fmt.Printf("Response: %+v\n", w)
if w.Code != http.StatusOK {
t.Error("Non-200 health status in PUT:", w.Code)
}
w2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/db/stuff", nil)
router.ServeHTTP(w2, req2)
if w2.Code != http.StatusOK {
t.Error("Non-200 health status in GET:", w2.Code)
}
raw, _ := ioutil.ReadAll(w2.Body)
var data ReadResponse
if err := json.Unmarshal(raw, &data); err != nil {
t.Error(err.Error())
}
if data.Value != v {
t.Errorf("Incorrect response: %+v", data)
}
}
func TestWriteRedirect(t *testing.T) {
router, n := setupServer(t)
// Change our node so we become a follower
n.State = node.Follower
n.SetTerm(n.Term+1, &raft.Node{
Id: "localhost:16991",
ClientAddr: "localhost:8081",
})
v := "testy"
body1 := WriteRequest{Value: v}
b1, _ := json.Marshal(body1)
br1 := bytes.NewReader(b1)
w := httptest.NewRecorder()
req, _ := http.NewRequest("PUT", "/db/stuff", br1)
router.ServeHTTP(w, req)
if w.Code != http.StatusTemporaryRedirect {
t.Errorf("Expected a temporary (307) redirect but got: %d\n", w.Code)
}
location := w.Header().Get("Location")
expected := "http://localhost:8081/db/stuff"
if location != expected {
t.Errorf("Expected to be redirected to %s but got %s\n", expected, location)
}
}
func TestDelete(t *testing.T) {
router, _ := setupServer(t)
v := "testy"
body1 := WriteRequest{Value: v}
b1, _ := json.Marshal(body1)
br1 := bytes.NewReader(b1)
uri := "/db/stuff"
w := httptest.NewRecorder()
req, _ := http.NewRequest("PUT", uri, br1)
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Error("Non-200 health status in PUT:", w.Code)
}
w2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", uri, nil)
router.ServeHTTP(w2, req2)
if w2.Code != http.StatusOK {
t.Error("Non-200 health status in GET:", w2.Code)
}
raw, _ := ioutil.ReadAll(w2.Body)
var data ReadResponse
if err := json.Unmarshal(raw, &data); err != nil {
t.Error(err.Error())
}
if data.Value != v {
t.Errorf("Incorrect response: %+v", data)
}
w3 := httptest.NewRecorder()
req3, _ := http.NewRequest("DELETE", uri, nil)
router.ServeHTTP(w3, req3)
if w3.Code != http.StatusOK {
t.Error("Non-200 health status in DELETE:", w3.Code)
}
w4 := httptest.NewRecorder()
req4, _ := http.NewRequest("GET", uri, nil)
router.ServeHTTP(w4, req4)
if w4.Code != http.StatusOK {
t.Error("Non-200 health status in GET (for key not found):", w4.Code)
}
raw4, _ := ioutil.ReadAll(w4.Body)
var data4 ReadResponse
if err := json.Unmarshal(raw4, &data4); err != nil {
t.Errorf("%s while processing %s", err.Error(), string(raw4))
}
if data4.Value != "" {
t.Errorf("Incorrect response: %+v", data4)
}
}
func TestDeleteRedirect(t *testing.T) {
router, n := setupServer(t)
// Change our node so we become a follower
n.State = node.Follower
n.SetTerm(n.Term+1, &raft.Node{
Id: "localhost:16991",
ClientAddr: "localhost:8081",
})
w := httptest.NewRecorder()
req, _ := http.NewRequest("DELETE", "/db/stuff", nil)
router.ServeHTTP(w, req)
if w.Code != http.StatusTemporaryRedirect {
t.Errorf("Expected a temporary (307) redirect but got: %d\n", w.Code)
}
location := w.Header().Get("Location")
expected := "http://localhost:8081/db/stuff"
if location != expected {
t.Errorf("Expected to be redirected to %s but got %s\n", expected, location)
}
}