-
Notifications
You must be signed in to change notification settings - Fork 23
/
main_test.go
355 lines (296 loc) · 10.6 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package main
import (
"bytes"
"compress/gzip"
"crypto/sha256"
"db"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
"github.com/LeelaChessZero/lczero-client/src/client"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type StoreSuite struct {
suite.Suite
router *gin.Engine
w *httptest.ResponseRecorder
}
func (s *StoreSuite) SetupSuite() {
db.Init(false)
s.router = setupRouter()
}
func (s *StoreSuite) SetupTest() {
err := db.GetDB().DropTable(
&db.User{},
&db.TrainingRun{},
&db.Network{},
&db.Match{},
&db.MatchGame{},
&db.TrainingGame{},
).Error
if err != nil {
log.Fatal(err)
}
db.SetupDB()
network := db.Network{Sha: "abcd", Path: "/tmp/network", TrainingRunID: 1}
if err := db.GetDB().Create(&network).Error; err != nil {
log.Fatal(err)
}
training_run := db.TrainingRun{Description: "Testing", BestNetworkID: network.ID, Active: true}
if err := db.GetDB().Create(&training_run).Error; err != nil {
log.Fatal(err)
}
user := db.User{Username: "defaut", Password: "1234"}
if err := db.GetDB().Create(&user).Error; err != nil {
log.Fatal(err)
}
s.w = httptest.NewRecorder()
}
func (s *StoreSuite) TearDownSuite() {
db.Close()
}
// This is the actual "test" as seen by Go, which runs the tests defined below
func TestStoreSuite(t *testing.T) {
s := new(StoreSuite)
suite.Run(t, s)
}
func postParams(params map[string]string) *strings.Reader {
data := url.Values{}
for key, val := range params {
data.Set(key, val)
}
return strings.NewReader(data.Encode())
}
func initMatch(matchDone bool) {
candidate_network := db.Network{Sha: "efgh", Path: "/tmp/network2"}
if err := db.GetDB().Create(&candidate_network).Error; err != nil {
log.Fatal(err)
}
match := db.Match{
TrainingRunID: 1,
Parameters: `["--visits 10"]`,
CandidateID: candidate_network.ID,
CurrentBestID: 1,
Done: matchDone,
GameCap: 6,
}
if err := db.GetDB().Create(&match).Error; err != nil {
log.Fatal(err)
}
}
// For backwards compatibility in short term.
func (s *StoreSuite) TestNextGameNoUser() {
req, _ := http.NewRequest("POST", "/next_game", nil)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
s.router.ServeHTTP(s.w, req)
assert.Equal(s.T(), 200, s.w.Code, s.w.Body.String())
assert.JSONEqf(s.T(), `{"params":"","type":"train","trainingId":1,"networkId":1,"sha":"abcd"}`, s.w.Body.String(), "Body incorrect")
}
// Make sure old users don't get match games
func (s *StoreSuite) TestNextGameNoUserMatch() {
initMatch(false)
req, _ := http.NewRequest("POST", "/next_game", nil)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
s.router.ServeHTTP(s.w, req)
assert.Equal(s.T(), 200, s.w.Code, s.w.Body.String())
assert.JSONEqf(s.T(), `{"params":"","type":"train","trainingId":1,"networkId":1,"sha":"abcd"}`, s.w.Body.String(), "Body incorrect")
}
func (s *StoreSuite) TestNextGameUserNoMatch() {
req, _ := http.NewRequest("POST", "/next_game", postParams(map[string]string{"user": "default", "password": "1234", "version": "2"}))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
s.router.ServeHTTP(s.w, req)
assert.Equal(s.T(), 200, s.w.Code, s.w.Body.String())
assert.JSONEqf(s.T(), `{"params":"","type":"train","trainingId":1,"networkId":1,"sha":"abcd"}`, s.w.Body.String(), "Body incorrect")
}
func (s *StoreSuite) TestNextGameUserMatch() {
initMatch(false)
req, _ := http.NewRequest("POST", "/next_game", postParams(map[string]string{"user": "default", "password": "1234", "version": "2"}))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
s.router.ServeHTTP(s.w, req)
assert.Equal(s.T(), 200, s.w.Code, s.w.Body.String())
assert.JSONEqf(s.T(), `{"params":"[\"--visits 10\"]","type":"match","matchGameId":1,"sha":"abcd","candidateSha":"efgh","flip":true}`, s.w.Body.String(), "Body incorrect")
}
func (s *StoreSuite) TestNextGameUserMatchDone() {
initMatch(true)
req, _ := http.NewRequest("POST", "/next_game", postParams(map[string]string{"user": "default", "password": "1234", "version": "2"}))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
s.router.ServeHTTP(s.w, req)
// Shouldn't get a match back
assert.Equal(s.T(), 200, s.w.Code, s.w.Body.String())
assert.JSONEqf(s.T(), `{"params":"","type":"train","trainingId":1,"networkId":1,"sha":"abcd"}`, s.w.Body.String(), "Body incorrect")
}
func (s *StoreSuite) TestUploadGameNewUser() {
extraParams := map[string]string{
"user": "foo",
"password": "asdf",
"training_id": "1",
"network_id": "1",
"version": "1",
}
tmpfile, _ := ioutil.TempFile("", "example")
defer os.Remove(tmpfile.Name())
req, err := client.BuildUploadRequest("/upload_game", extraParams, "file", tmpfile.Name())
if err != nil {
log.Fatal(err)
}
s.router.ServeHTTP(s.w, req)
assert.Equal(s.T(), 200, s.w.Code, s.w.Body.String())
// Check we create the new user
user := db.User{}
err = db.GetDB().Where("username = ?", "foo").First(&user).Error
if err != nil {
log.Fatal(err)
}
// Check we update the game count properly
network := db.Network{}
err = db.GetDB().Where("id = ?", 1).First(&network).Error
if err != nil {
log.Fatal(err)
}
assert.Equal(s.T(), 1, network.GamesPlayed)
}
func uploadTestNetwork(s *StoreSuite, contentString string, networkId int) {
s.w = httptest.NewRecorder()
content := []byte(contentString)
var buf bytes.Buffer
zw := gzip.NewWriterLevel(&buf, BestCompression)
zw.Write(content)
zw.Close()
extraParams := map[string]string{
"training_id": "1",
"layers": "6",
"filters": "64",
}
tmpfile, _ := ioutil.TempFile("", "example")
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(buf.Bytes()); err != nil {
log.Fatal(err)
}
req, err := client.BuildUploadRequest("/upload_network", extraParams, "file", tmpfile.Name())
if err != nil {
log.Fatal(err)
}
s.router.ServeHTTP(s.w, req)
assert.Equal(s.T(), 200, s.w.Code, s.w.Body.String())
// Trying to upload the same network should now fail
s.w = httptest.NewRecorder()
req, err = client.BuildUploadRequest("/upload_network", extraParams, "file", tmpfile.Name())
if err != nil {
log.Fatal(err)
}
s.router.ServeHTTP(s.w, req)
assert.Equal(s.T(), 400, s.w.Code, s.w.Body.String())
// /next_game shouldn't return new network now, since it hasn't passed yet.
s.w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", "/next_game", nil)
s.router.ServeHTTP(s.w, req)
assert.Equal(s.T(), 200, s.w.Code, s.w.Body.String())
assert.JSONEqf(s.T(), `{"params":"", "type":"train","trainingId":1,"networkId":1,"sha":"abcd"}`, s.w.Body.String(), "Body incorrect")
sha := sha256.Sum256(content)
// And let's download it now.
s.w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", fmt.Sprintf("/get_network?sha=%x", sha), nil)
s.router.ServeHTTP(s.w, req)
assert.Equal(s.T(), 200, s.w.Code, s.w.Body.String())
// Should match the contents
zr, err := gzip.NewReader(s.w.Body)
if err != nil {
log.Fatal(err)
}
buf.Reset()
if _, err := io.Copy(&buf, zr); err != nil {
log.Fatal(err)
}
assert.Equal(s.T(), contentString, buf.String(), "Contents don't match")
}
func (s *StoreSuite) TestUploadNetwork() {
uploadTestNetwork(s, "this_is_a_network", 2)
// We should get a match game.
s.w = httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/next_game", postParams(map[string]string{"user": "default", "password": "1234", "version": "2"}))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
s.router.ServeHTTP(s.w, req)
assert.Equal(s.T(), 200, s.w.Code, s.w.Body.String())
sha := sha256.Sum256([]byte("this_is_a_network"))
assert.JSONEqf(s.T(), fmt.Sprintf(`{"params":"","type":"match","matchGameId":1,"sha":"abcd","candidateSha":"%x","flip":true}`, sha), s.w.Body.String(), "Body incorrect")
uploadTestNetwork(s, "network2", 3)
}
func testMatchResult(s *StoreSuite, promote bool) {
initMatch(false)
for i := 0; i < 6; i++ {
// get the match game
s.w = httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/next_game", postParams(map[string]string{"user": "default", "password": "1234", "version": "2"}))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
s.router.ServeHTTP(s.w, req)
match_game_id := fmt.Sprintf("%d", i+1)
flip := (i & 1) == 0
assert.Equal(s.T(), 200, s.w.Code, s.w.Body.String())
assert.JSONEqf(s.T(), fmt.Sprintf(`{"params":"[\"--visits 10\"]","type":"match","matchGameId":%s,"sha":"abcd","candidateSha":"efgh","flip":%t}`, match_game_id, flip), s.w.Body.String(), "Body incorrect")
// Now, post a result from the match
s.w = httptest.NewRecorder()
result := -1
if promote {
result = 1
}
req, _ = http.NewRequest("POST", "/match_result", postParams(map[string]string{
"user": "default",
"password": "1234",
"version": "2",
"match_game_id": match_game_id,
"result": fmt.Sprintf("%d", result),
"pgn": "asdf",
}))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
s.router.ServeHTTP(s.w, req)
assert.Equal(s.T(), 200, s.w.Code, s.w.Body.String())
// Check that the match game is present now.
match_game := db.MatchGame{}
err := db.GetDB().Where("id = ?", 1).First(&match_game).Error
if err != nil {
log.Fatal(err)
}
assert.Equal(s.T(), result, match_game.Result)
assert.Equal(s.T(), "asdf", match_game.Pgn)
assert.Equal(s.T(), true, match_game.Done)
// And now that the match is updated.
match := db.Match{}
err = db.GetDB().Where("id = ?", 1).First(&match).Error
if err != nil {
log.Fatal(err)
}
}
// Match should be done now
match := db.Match{}
err := db.GetDB().Where("id = ?", 1).First(&match).Error
if err != nil {
log.Fatal(err)
}
assert.Equal(s.T(), true, match.Done)
// And, now, we shouldn't get a match game back
s.w = httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/next_game", postParams(map[string]string{"user": "default", "password": "1234", "version": "2"}))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
s.router.ServeHTTP(s.w, req)
assert.Equal(s.T(), 200, s.w.Code, s.w.Body.String())
if promote {
assert.JSONEqf(s.T(), `{"params":"","type":"train","trainingId":1,"networkId":2,"sha":"efgh"}`, s.w.Body.String(), "Body incorrect")
} else {
assert.JSONEqf(s.T(), `{"params":"","type":"train","trainingId":1,"networkId":1,"sha":"abcd"}`, s.w.Body.String(), "Body incorrect")
}
}
func (s *StoreSuite) TestPostMatchResultFailed() {
testMatchResult(s, false)
}
func (s *StoreSuite) TestPostMatchResultSuccess() {
testMatchResult(s, true)
}