-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain_test.go
460 lines (390 loc) · 12.1 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//go:build integration
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"context"
"encoding/json"
"github.com/stretchr/testify/assert"
database "cloud.google.com/go/spanner/admin/database/apiv1"
instance "cloud.google.com/go/spanner/admin/instance/apiv1"
"embed"
"fmt"
"github.com/googleforgames/global-multiplayer-demo/profile-service/models"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
databasepb "google.golang.org/genproto/googleapis/spanner/admin/database/v1"
instancepb "google.golang.org/genproto/googleapis/spanner/admin/instance/v1"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"testing"
)
//go:embed test_data/schema.sql
var SCHEMAFILE embed.FS
var TESTNETWORK = "globalgame-spanner-test"
// These integration tests run against the Spanner emulator. The emulator
// must be running and accessible prior to integration tests running.
type Emulator struct {
testcontainers.Container
Endpoint string
Project string
Instance string
Database string
}
type Service struct {
testcontainers.Container
Endpoint string
}
func teardown(ctx context.Context, emulator *Emulator, service *Service) {
emulator.Terminate(ctx)
service.Terminate(ctx)
}
func setupSpannerEmulator(ctx context.Context) (*Emulator, error) {
req := testcontainers.ContainerRequest{
Image: "gcr.io/cloud-spanner-emulator/emulator:1.5.0",
ExposedPorts: []string{"9010/tcp"},
Networks: []string{
TESTNETWORK,
},
NetworkAliases: map[string][]string{
TESTNETWORK: []string{
"emulator",
},
},
Name: "emulator",
WaitingFor: wait.ForLog("gRPC server listening at"),
}
spannerEmulator, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
return nil, err
}
// Retrieve the container IP
ip, err := spannerEmulator.Host(ctx)
if err != nil {
return nil, err
}
// Retrieve the container port
mappedPort, err := spannerEmulator.MappedPort(ctx, "9010")
if err != nil {
return nil, err
}
// OS environment needed for setting up instance and database
grpcEndpoint := fmt.Sprintf("%s:%s", ip, mappedPort.Port())
os.Setenv("SPANNER_EMULATOR_HOST", grpcEndpoint)
var ec = Emulator{
Container: spannerEmulator,
Endpoint: "emulator:9010",
Project: "test-project",
Instance: "test-instance",
Database: "test-database",
}
// Create instance
err = setupInstance(ctx, ec)
if err != nil {
return nil, err
}
// Define the database and schema
err = setupDatabase(ctx, ec)
if err != nil {
return nil, err
}
return &ec, nil
}
func setupInstance(ctx context.Context, ec Emulator) error {
instanceAdmin, err := instance.NewInstanceAdminClient(ctx)
if err != nil {
log.Fatal(err)
}
defer instanceAdmin.Close()
op, err := instanceAdmin.CreateInstance(ctx, &instancepb.CreateInstanceRequest{
Parent: fmt.Sprintf("projects/%s", ec.Project),
InstanceId: ec.Instance,
Instance: &instancepb.Instance{
Config: fmt.Sprintf("projects/%s/instanceConfigs/%s", ec.Project, "emulator-config"),
DisplayName: ec.Instance,
NodeCount: 1,
},
})
if err != nil {
return fmt.Errorf("could not create instance %s: %v", fmt.Sprintf("projects/%s/instances/%s", ec.Project, ec.Instance), err)
}
// Wait for the instance creation to finish.
i, err := op.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting for instance creation to finish failed: %v", err)
}
// The instance may not be ready to serve yet.
if i.State != instancepb.Instance_READY {
fmt.Printf("instance state is not READY yet. Got state %v\n", i.State)
}
fmt.Printf("Created emulator instance [%s]\n", ec.Instance)
return nil
}
func setupDatabase(ctx context.Context, ec Emulator) error {
// get schema statements from file
schema, _ := SCHEMAFILE.ReadFile("test_data/schema.sql")
// Remove trailing semi-colon/newline so Emulator can parse DDL statements
schemaString := strings.TrimSuffix(string(schema), ";\n")
schemaStatements := strings.Split(schemaString, ";")
adminClient, err := database.NewDatabaseAdminClient(ctx)
if err != nil {
return err
}
defer adminClient.Close()
op, err := adminClient.CreateDatabase(ctx, &databasepb.CreateDatabaseRequest{
Parent: fmt.Sprintf("projects/%s/instances/%s", ec.Project, ec.Instance),
CreateStatement: "CREATE DATABASE `" + ec.Database + "`",
ExtraStatements: schemaStatements,
})
if err != nil {
fmt.Printf("Error: [%s]", err)
return err
}
if _, err := op.Wait(ctx); err != nil {
fmt.Printf("Error: [%s]", err)
return err
}
fmt.Printf("Created emulator database [%s]\n", ec.Database)
return nil
}
func setupService(ctx context.Context, ec *Emulator) (*Service, error) {
var service = "profile-service"
req := testcontainers.ContainerRequest{
Image: fmt.Sprintf("%s:latest", service),
Name: service,
ExposedPorts: []string{"80:80/tcp"}, // Bind to 80 on localhost to avoid not knowing about the container port
Networks: []string{TESTNETWORK},
NetworkAliases: map[string][]string{
TESTNETWORK: []string{
service,
},
},
Env: map[string]string{
"SPANNER_PROJECT_ID": ec.Project,
"SPANNER_INSTANCE_ID": ec.Instance,
"SPANNER_DATABASE_ID": ec.Database,
"SERVICE_HOST": "0.0.0.0",
"SERVICE_PORT": "80",
"SPANNER_EMULATOR_HOST": ec.Endpoint,
},
WaitingFor: wait.ForLog("Listening and serving HTTP on 0.0.0.0:80"),
}
serviceContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
return nil, err
}
// Retrieve the container endpoint
endpoint, err := serviceContainer.Endpoint(ctx, "")
if err != nil {
return nil, err
}
return &Service{
Container: serviceContainer,
Endpoint: endpoint,
}, nil
}
func httpPUT(url string, data io.Reader) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequest(http.MethodPut, url, data)
if err != nil {
return nil, err
}
// set the request header Content-Type for json
req.Header.Set("Content-Type", "application/json")
response, err := client.Do(req)
if err != nil {
return nil, err
}
return response, nil
}
func TestMain(m *testing.M) {
ctx := context.Background()
// Setup the docker network so containers can talk to each other
net, err := testcontainers.GenericNetwork(ctx, testcontainers.GenericNetworkRequest{
NetworkRequest: testcontainers.NetworkRequest{
Name: TESTNETWORK,
Attachable: true,
CheckDuplicate: true,
},
})
defer net.Remove(ctx)
if err != nil {
fmt.Printf("Error setting up docker test network: %s\n", err)
os.Exit(1)
}
// Setup the emulator container and default instance/database
spannerEmulator, err := setupSpannerEmulator(ctx)
if err != nil {
fmt.Printf("Error setting up emulator: %s\n", err)
os.Exit(1)
}
// Run service
service, err := setupService(ctx, spannerEmulator)
if err != nil {
fmt.Printf("Error setting up service: %s\n", err)
os.Exit(1)
}
defer teardown(ctx, spannerEmulator, service)
os.Exit(m.Run())
}
var test_player = models.Player{
Player_google_id: "123456",
Player_name: "test player",
Profile_image: "default",
Region: "amer",
}
var test_stats = []models.SingleGameStats{
{
Won: true,
Score: 500,
Kills: 1,
Deaths: 0,
},
{
Won: false,
Score: 100,
Kills: 5,
Deaths: 20,
},
{
Won: true,
Score: 1000,
Kills: 20,
Deaths: 1,
},
}
func TestAddPlayers(t *testing.T) {
pJson, err := json.Marshal(test_player)
if err != nil {
t.Fatal(err.Error())
}
bufferJson := bytes.NewBuffer(pJson)
// Test adding non-existing players
response, err := http.Post("http://localhost/players", "application/json", bufferJson)
assert.Nil(t, err)
assert.Equal(t, 201, response.StatusCode)
// Validate response is the original player_google_id
var data string
body, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err.Error())
}
json.Unmarshal(body, &data)
assert.Equal(t, test_player.Player_google_id, data)
// Test adding same player, should be statuscode 400 since player exists from previous call
response, err = http.Post("http://localhost/players", "application/json", bufferJson)
assert.Nil(t, err)
assert.Equal(t, 400, response.StatusCode)
// Test add player with same name. Should succeed with 201 code
newPlayer := test_player
newPlayer.Player_google_id = "654321"
pJson, err = json.Marshal(newPlayer)
if err != nil {
t.Fatal(err.Error())
}
response, err = http.Post("http://localhost/players", "application/json", bytes.NewBuffer(pJson))
assert.Nil(t, err)
assert.Equal(t, 201, response.StatusCode)
}
func TestGetPlayers(t *testing.T) {
// Get the testPlayer's data and validate response code (assuming the result was not empty)
response, err := http.Get(fmt.Sprintf("http://localhost/players/%s", test_player.Player_google_id))
if err != nil {
t.Fatal(err.Error())
}
assert.Equal(t, 200, response.StatusCode)
body, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err.Error())
}
var pData models.Player
json.Unmarshal(body, &pData)
var skill_level int64 = 0
assert.Equal(t, test_player.Player_google_id, pData.Player_google_id)
assert.Equal(t, test_player.Player_name, pData.Player_name)
assert.Equal(t, test_player.Profile_image, pData.Profile_image)
assert.Equal(t, test_player.Region, pData.Region)
assert.Equal(t, skill_level, pData.Skill_level)
assert.Equal(t, "U", pData.Tier)
}
func TestUpdatePlayer(t *testing.T) {
test_player.Region = "asia"
pJson, err := json.Marshal(test_player)
assert.Nil(t, err)
response, err := httpPUT("http://localhost/players", bytes.NewBuffer(pJson))
if err != nil {
t.Fatal(err.Error())
}
assert.Equal(t, 200, response.StatusCode)
body, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err.Error())
}
var pData models.Player
json.Unmarshal(body, &pData)
assert.Equal(t, test_player.Player_google_id, pData.Player_google_id)
assert.Equal(t, test_player.Player_name, pData.Player_name)
assert.Equal(t, test_player.Profile_image, pData.Profile_image)
assert.Equal(t, test_player.Region, pData.Region)
}
func TestUpdatePlayerStats(t *testing.T) {
for _, new_stats := range test_stats {
new_stats.Player_google_id = test_player.Player_google_id
statsJson, err := json.Marshal(new_stats)
assert.Nil(t, err)
response, err := httpPUT(fmt.Sprintf("http://localhost/players/%s/stats", test_player.Player_google_id), bytes.NewBuffer(statsJson))
if err != nil {
t.Fatal(err.Error())
}
assert.Equal(t, 200, response.StatusCode)
}
}
func TestGetPlayerStats(t *testing.T) {
// Get the testPlayer's stats and validate response code (assuming the result was not empty)
response, err := http.Get(fmt.Sprintf("http://localhost/players/%s/stats", test_player.Player_google_id))
if err != nil {
t.Fatal(err.Error())
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err.Error())
}
var pData models.Player
if err = json.Unmarshal(body, &pData); err != nil {
t.Fatal(err.Error())
}
var pStats models.PlayerStats
if err = json.Unmarshal([]byte(pData.Stats.String()), &pStats); err != nil {
t.Fatal(err.Error())
}
assert.Equal(t, test_player.Player_google_id, pData.Player_google_id)
assert.Equal(t, int64(3), pStats.Games_played)
assert.Equal(t, int64(2), pStats.Games_won)
assert.Equal(t, int64(1600), pStats.Total_score)
assert.Equal(t, int64(26), pStats.Total_kills)
assert.Equal(t, int64(21), pStats.Total_deaths)
assert.Equal(t, int64(1), pData.Skill_level)
assert.Equal(t, "U", pData.Tier)
}