-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
471 lines (438 loc) · 13.7 KB
/
main.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
461
462
463
464
465
466
467
468
469
470
471
// Licensed to NASA JPL under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. NASA JPL licenses this file to you 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
//
// http://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 (
"context"
"flag"
"fmt"
"log"
"math/rand"
"os"
"strings"
"time"
"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/core/awsutil"
"github.com/pixlise/core/v4/core/fileaccess"
"github.com/pixlise/core/v4/core/logger"
"github.com/pixlise/core/v4/core/wstestlib"
)
var auth0Params wstestlib.Auth0Info
var test1Username, test1Password, test2Username, test2Password string
// This is our complete integration test for the PIXLISE API
//
// It is intended to be run either locally on a dev laptop or in a test environment. It has several pre-requisites:
// - Running MongoDB where API is configured to talk to a database whose content we can wipe on integration test start
// - S3 buckets that API is configured to use, where we can wipe/replace files on integration test start
// - API, freshly started (so no cached things in memory yet)
// - 2 user accounts whose user/password is passed into here as arguments
//
// Integration test can then be started
var apiStorageFileAccess fileaccess.FileAccess
var apiDatasetBucket string
var apiUsersBucket string
func main() {
rand.Seed(time.Now().UnixNano())
//startupTime := time.Now()
var apiHost string
var apiDBSecret string
var expectedAPIVersion string
var testType string
flag.StringVar(&apiHost, "apiHost", "", "Host name of API we're testing. Eg: localhost:8080 or something.review.pixlise.org")
flag.StringVar(&apiDBSecret, "apiDBSecret", "", "Mongo secret of the DB the API is connected")
flag.StringVar(&apiDatasetBucket, "datasetBucket", "", "Dataset bucket the API is using")
flag.StringVar(&apiUsersBucket, "usersBucket", "", "User Data bucket the API is using")
flag.StringVar(&auth0Params.Domain, "auth0Domain", "", "Auth0 domain for management API")
flag.StringVar(&auth0Params.ClientId, "auth0ClientId", "", "Auth0 client id for management API")
flag.StringVar(&auth0Params.Secret, "auth0Secret", "", "Auth0 secret for management API")
flag.StringVar(&auth0Params.Audience, "auth0Audience", "", "Auth0 audience")
flag.StringVar(&expectedAPIVersion, "expectedAPIVersion", "", "Expected API version (version not checked if blank)")
flag.StringVar(&testType, "testType", "local", "Test type to run: local, env")
flag.StringVar(&test1Username, "test1Username", "", "Username of test account 1")
flag.StringVar(&test1Password, "test1Password", "", "Password of test account 1")
flag.StringVar(&test2Username, "test2Username", "", "Username of test account 2")
flag.StringVar(&test2Password, "test2Password", "", "Password of test account 2")
flag.Parse()
if strings.Contains(apiHost, "pixlise.org") && !strings.Contains(apiHost, "review.pixlise.org") {
fmt.Printf("Can't connect to an env for now: %v\n", apiHost)
return
}
fmt.Printf("Running integration test %v for %v\n", testType, apiHost)
// Get a session for the bucket region
sess, err := awsutil.GetSession()
if err != nil {
log.Fatalf("Failed to create AWS session. Error: %v", err)
}
s3svc, err := awsutil.GetS3(sess)
if err != nil {
log.Fatalf("Failed to create AWS S3 service. Error: %v", err)
}
apiStorageFileAccess = fileaccess.MakeS3Access(s3svc)
if len(expectedAPIVersion) > 0 {
printTestStart("API Version")
err := checkAPIVersion(apiHost, expectedAPIVersion)
printTestResult(err, "")
if err != nil {
// If API version call is broken, probably everything is...
os.Exit(1)
}
}
startTime := time.Now()
if testType == "env" {
runEnvTests(apiHost)
} else if testType == "local" || testType == "ci" {
// Connect to DB and drop the unit test database
db := wstestlib.GetDB()
err = db.Drop(context.TODO())
if err != nil {
log.Fatal(err)
}
dbCollections.InitCollections(db, &logger.StdOutLogger{}, "")
runLocalTests(apiHost, testType == "ci")
} else {
log.Fatal("Unexpected test type: " + testType)
}
fmt.Println("\n==============================")
if len(failedTestNames) == 0 {
fmt.Printf("PASSED All Tests in %vsec!\n", time.Since(startTime).Seconds())
os.Exit(0)
}
fmt.Printf("FAILED One or more tests at %vsec:\n", time.Since(startTime).Seconds())
for _, name := range failedTestNames {
fmt.Printf("- %v\n", name)
}
os.Exit(1)
}
func runEnvTests(apiHost string) {
// Old env integration test:
// Login <== we already do this?
// Verify API version
// Check alerts pre quant (then again to make sure theyre cleared) <== we won't need to do this
// Dataset listing, check that more than X come down
// Download dataset file <== no longer applicable, but we could send a bunch of requests for a given scan
// Run quants, export+verify, delete
// Check alerts after quant runs <== we won't need to do this
testImports(apiHost)
// TODO: test quant create
// TODO: download all bits for a given dataset... all proto msgs + context image download(s)
}
func runLocalTests(apiHost string, isCI bool) {
testImageGet_PreWS(apiHost) // Must be run before any web sockets log in
// testScanData(apiHost, 0 /*3 for proper testing*/)
// testQuants(apiHost)
// testDataModules(apiHost)
// Test log query (for data import)
// Run integration test against review env?
// *** NOT RUNNING THIS LOCALLY, NO LAMBDA IS STARTED *** testScanImport(apiHost)
u1Id, u2Id := testNotification(apiHost)
testImageUpload(apiHost, u1Id, u2Id)
testMemoisation(apiHost)
testImageMatchTransform(apiHost)
testSelectionMsgs(apiHost)
testQuants(apiHost, !isCI) // We only run the tests that need to start PIQUANT outside of CI for now
testDiffractionManualPeaks(apiHost)
time.Sleep(5 * time.Second)
testDiffractionStatus(apiHost)
testPiquantMsgs(apiHost)
if !isCI {
testExpressionRuntimeMsgs(apiHost)
}
testDataModules(apiHost)
time.Sleep(5 * time.Second)
testUserContent(apiHost, map[string]contentMessaging{
"elementSet": {
itemName: "elementSet",
listName: "elementSets",
invalidItemsToCreate: [][]string{
{
`"name": "User1 ElementSet1",
"lines": []`,
"Lines must contain at least 1 items",
},
},
validItemsToCreate: [][]string{
{
`"name": "User1 ElementSet1",
"lines": [
{
"Z": 14,
"K": false,
"L": false,
"M": true,
"Esc": false
},
{
"Z": 16,
"K": false,
"L": true,
"M": false,
"Esc": true
}
]`,
`"name": "User1 ElementSet1",
"lines": [
{
"Z": 14,
"M": true
},
{
"Z": 16,
"L": true,
"Esc": true
}
]`,
`"name": "User1 ElementSet1",
"atomicNumbers": [
14, 16
]`,
},
},
invalidItemsToEdit: [][]string{
{
`"name": "This name is way way too long for any element set to seriously be named this way",
"lines": [
{
"Z": 17,
"K": false,
"L": false,
"M": true,
"Esc": false
}
]`,
"Name is too long",
},
},
validItemsToEdit: [][]string{
{
`"name": "User1 ElementSet1-Edited",
"lines": [
{
"Z": 17,
"K": false,
"L": false,
"M": true,
"Esc": false
}
]`,
`"name": "User1 ElementSet1-Edited",
"lines": [
{
"Z": 17,
"M": true
}
]`,
`"name": "User1 ElementSet1-Edited",
"atomicNumbers": [
17
]`,
},
},
objectType: "OT_ELEMENT_SET",
},
"regionOfInterest": {
itemName: "regionOfInterest",
listName: "regionsOfInterest",
invalidItemsToCreate: [][]string{
{
`"name": "Invalid ROI",
"description": "Ye Invalid ROIe",
"scanId": "048300551"`,
"ROI must have location or pixel indexes defined",
},
},
validItemsToCreate: [][]string{
{
`"name": "User1 ROI1",
"description": "User1 ROI1",
"scanId": "048300551",
"scanEntryIndexesEncoded": [14, 123, -1, 126, 98]`,
`"name": "User1 ROI1",
"description": "User1 ROI1",
"scanId": "048300551",
"scanEntryIndexesEncoded": [14, 123, -1, 126, 98]`,
`"name": "User1 ROI1",
"description": "User1 ROI1",
"scanId": "048300551"`,
},
},
invalidItemsToEdit: [][]string{
{
`"scanId": "048300551",
"name": "The ROI",
"imageName": "WhatsAnImageDoingHere.png"`,
"ROI image and pixel indexes must both be defined",
},
},
validItemsToEdit: [][]string{
{
`"scanId": "048300551",
"name": "The ROI",
"description": "User1 ROI1",
"scanEntryIndexesEncoded": [14, 123, -1, 126, 98, 88]`,
`"scanId": "048300551",
"name": "The ROI",
"description": "User1 ROI1",
"scanEntryIndexesEncoded": [14, 123, -1, 126, 98, 88]`,
`"scanId": "048300551",
"description": "User1 ROI1",
"name": "The ROI"`,
},
},
objectType: "OT_ROI",
},
"expressionGroup": {
itemName: "group",
listName: "groups",
invalidItemsToCreate: [][]string{
{
`"name": "Invalid Expression Group",
"groupItems": [{"expressionId": "abcd-123", "rangeMin": "12"}]`,
"GroupItems is too short",
},
},
validItemsToCreate: [][]string{
{
`"name": "User1 ExpressionGroup",
"description": "User1 RGBMix",
"groupItems": [
{"expressionId": "red-expr-1", "rangeMin": 0, "rangeMax": 10},
{"expressionId": "green-expr-1", "rangeMin": 4, "rangeMax": 12},
{"expressionId": "blue-expr-1"}
]`,
`"name": "User1 ExpressionGroup",
"description": "User1 RGBMix",
"groupItems": [
{"expressionId": "red-expr-1", "rangeMax": 10},
{"expressionId": "green-expr-1", "rangeMin": 4, "rangeMax": 12},
{"expressionId": "blue-expr-1"}
]`,
// List and item are the same
`"name": "User1 ExpressionGroup",
"description": "User1 RGBMix",
"groupItems": [
{"expressionId": "red-expr-1", "rangeMax": 10},
{"expressionId": "green-expr-1", "rangeMin": 4, "rangeMax": 12},
{"expressionId": "blue-expr-1"}
]`,
},
},
invalidItemsToEdit: [][]string{
{
`"name": "User1 ExpressionGroup",
"description": "User1 RGBMix",
"groupItems": [
{"expressionId": "red-expr-2", "rangeMax": 10},
{"expressionId": "green-expr-1", "rangeMin": 3, "rangeMax": 12},
{"rangeMax": 133}]`,
"[2].ExpressionId is too short",
},
},
validItemsToEdit: [][]string{
{
`"description": "User1 RGBMix Modified",
"groupItems": [
{"expressionId": "red-expr-1", "rangeMax": 19},
{"expressionId": "green-expr-3", "rangeMin": 4, "rangeMax": 12},
{"expressionId": "blue-expr-6"}
]`,
`"name": "User1 ExpressionGroup",
"description": "User1 RGBMix Modified",
"groupItems": [
{"expressionId": "red-expr-1", "rangeMax": 19},
{"expressionId": "green-expr-3", "rangeMin": 4, "rangeMax": 12},
{"expressionId": "blue-expr-6"}
]`,
// List and item are the same
`"name": "User1 ExpressionGroup",
"description": "User1 RGBMix Modified",
"groupItems": [
{"expressionId": "red-expr-1", "rangeMax": 19},
{"expressionId": "green-expr-3", "rangeMin": 4, "rangeMax": 12},
{"expressionId": "blue-expr-6"}
]`,
},
},
objectType: "OT_EXPRESSION_GROUP",
},
"expression": {
itemName: "expression",
listName: "expressions",
invalidItemsToCreate: [][]string{
{
`"name": "Invalid Expression",
"sourceLanguage": "LUA"`,
"SourceCode is too short",
},
},
validItemsToCreate: [][]string{
{
`"name": "User1 Expression",
"comments": "User1 Expression",
"sourceLanguage": "LUA",
"sourceCode": "element(\"Ca\")"`,
`"name": "User1 Expression",
"sourceLanguage": "LUA",
"comments": "User1 Expression",
"sourceCode": "element(\"Ca\")"`,
`"name": "User1 Expression",
"sourceLanguage": "LUA",
"comments": "User1 Expression"`,
},
},
invalidItemsToEdit: [][]string{
{
`"name": "User1 Expression Invalid",
"comments": "User1 Expression Invalid",
"sourceLanguage": "PYTHON"`,
"Invalid source language: PYTHON",
},
},
validItemsToEdit: [][]string{
{
`"comments": "User1 Expression Modified",
"sourceCode": "element(\"Fe\")"`,
`"name": "User1 Expression",
"comments": "User1 Expression Modified",
"sourceLanguage": "LUA",
"sourceCode": "element(\"Fe\")"`,
`"name": "User1 Expression",
"comments": "User1 Expression Modified",
"sourceLanguage": "LUA"`,
},
},
objectType: "OT_EXPRESSION",
},
})
time.Sleep(5 * time.Second)
testUserSearch(apiHost)
testUserDetails(apiHost, u1Id, u2Id)
if !isCI {
testUserManagement(apiHost)
}
testUserGroups(apiHost)
time.Sleep(5 * time.Second)
testLogMsgs(apiHost)
testScanData(apiHost, 0 /*3 for proper testing*/)
time.Sleep(5 * time.Second)
testDetectorConfig(apiHost)
testTags(apiHost)
testROIUserConfiguration(apiHost)
time.Sleep(5 * time.Second)
testScreenConfiguration(apiHost)
testWidgetData(apiHost)
// Removed for now, looks like zenodo API changed recently?
//testDOI(apiHost)
}