forked from singnet/platform-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish_example_service_test.go
527 lines (408 loc) · 13.7 KB
/
publish_example_service_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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package main
import (
"errors"
"fmt"
"log"
"strings"
"time"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/gherkin"
)
var outputFile string
var outputContainsStrings []string
var platformContractsDir string
var exampleServiceDir string
var snetConfigFile string
var accountPrivateKey string
var identiyPrivateKey string
var agentFactoryAddress string
var registryAddress string
var organizationAddress string
var agentAddress string
func init() {
platformContractsDir = envSingnetRepos + "/platform-contracts"
exampleServiceDir = envSingnetRepos + "/example-service"
snetConfigFile = envHome + "/.snet/config"
}
func ethereumNetworkIsRunningOnPort(port int) error {
outputFile = logPath + "/ganache.log"
outputContainsStrings = []string{"Listening on 127.0.0.1:" + toString(port)}
args := []string{"--mnemonic", "gauge enact biology destroy normal tunnel slight slide wide sauce ladder produce"}
command := ExecCommand{
Command: "./node_modules/.bin/ganache-cli",
Directory: platformContractsDir,
OutputFile: outputFile,
Args: args,
}
err := runCommandAsync(command)
if err != nil {
return err
}
exists, err := checkWithTimeout(5000, 500, checkFileContainsStrings)
if err != nil {
return err
}
if !exists {
return errors.New("Etherium networks is not started")
}
organizationAddress, err = getPropertyFromFile(outputFile, "(1)")
if err != nil {
return err
}
accountPrivateKey, err = getPropertyWithIndexFromFile(outputFile, "(2)", 1)
if err != nil {
return err
}
if len(accountPrivateKey) < 3 {
return errors.New("Len of account privite key is to small: " + accountPrivateKey)
}
accountPrivateKey = accountPrivateKey[2:len(accountPrivateKey)]
identiyPrivateKey, err = getPropertyWithIndexFromFile(outputFile, "(0)", 1)
if err != nil {
return err
}
return nil
}
func contractsAreDeployedUsingTruffle() error {
command := ExecCommand{
Command: "./node_modules/.bin/truffle",
Directory: platformContractsDir,
Args: []string{"compile"},
}
err := runCommand(command)
if err != nil {
return err
}
output := "migrate.out"
command.Args = []string{"migrate", "--network", "local"}
command.OutputFile = output
err = runCommand(command)
registryAddress, err = getPropertyFromFile(output, "Registry:")
if err != nil {
return err
}
agentFactoryAddress, err = getPropertyFromFile(output, "AgentFactory:")
if err != nil {
return err
}
return err
}
func ipfsIsRunning(portAPI int, portGateway int) error {
env := []string{"IPFS_PATH=" + envGoPath + "/ipfs"}
command := ExecCommand{
Command: "ipfs",
Env: env,
Args: []string{"init"},
}
err := runCommand(command)
if err != nil {
return err
}
command.Args = []string{"bootstrap", "rm", "--all"}
err = runCommand(command)
if err != nil {
return err
}
addressAPI := "/ip4/127.0.0.1/tcp/" + toString(portAPI)
command.Args = []string{"config", "Addresses.API", addressAPI}
err = runCommand(command)
if err != nil {
return err
}
addressGateway := "/ip4/0.0.0.0/tcp/" + toString(portGateway)
command.Args = []string{"config", "Addresses.Gateway", addressGateway}
err = runCommand(command)
if err != nil {
return err
}
outputFile = logPath + "/ipfs.log"
command.OutputFile = outputFile
command.Args = []string{"daemon"}
err = runCommandAsync(command)
if err != nil {
return err
}
outputContainsStrings = []string{
"Daemon is ready",
"server listening on " + addressAPI,
"server listening on " + addressGateway,
}
exists, err := checkWithTimeout(5000, 500, checkFileContainsStrings)
if err != nil {
return err
}
if !exists {
return errors.New("Etherium networks is not started")
}
return nil
}
func identityIsCreatedWithUserAndPrivateKey(user string, privateKey string) error {
command := ExecCommand{
Command: "snet",
Args: []string{"identity", "create", user, "key", "--private-key", identiyPrivateKey},
}
err := runCommand(command)
if err != nil {
return err
}
command.Args = []string{"identity", "snet-user"}
return runCommand(command)
}
func snetIsConfiguredWithEthereumRPCEndpoint(endpointEthereumRPC int) error {
config := `
[network.local]
default_eth_rpc_endpoint = http://localhost:` + toString(endpointEthereumRPC)
err := appendToFile(snetConfigFile, config)
if err != nil {
return err
}
command := ExecCommand{
Command: "snet",
Args: []string{"network", "local"},
}
err = runCommand(command)
if err != nil {
return err
}
outputFile = snetConfigFile
outputContainsStrings = []string{"session"}
exists, e := checkWithTimeout(5000, 500, checkFileContainsStrings)
if !exists {
return errors.New("snet config file is not created: " + snetConfigFile)
}
return e
}
func snetIsConfiguredWithIPFSEndpoint(endpointIPFS int) error {
command := ExecCommand{
Command: "sed",
Args: []string{"-ie", "/ipfs/,+2d", snetConfigFile},
}
err := runCommand(command)
if err != nil {
return err
}
config := `
[ipfs]
default_ipfs_endpoint = http://localhost:` + toString(endpointIPFS)
return appendToFile(snetConfigFile, config)
}
func organizationIsAdded(table *gherkin.DataTable) error {
organization := getTableValue(table, "organization")
args := []string{
"contract", "Registry",
"--at", registryAddress,
"createOrganization", organization,
"[\"" + organizationAddress + "\"]",
"--transact",
"--yes",
}
command := ExecCommand{
Command: "snet",
Args: args,
}
return runCommand(command)
}
func exampleserviceIsRegistered(table *gherkin.DataTable) error {
name := getTableValue(table, "name")
price := getTableValue(table, "price")
endpoint := getTableValue(table, "endpoint")
tags := getTableValue(table, "tags")
description := getTableValue(table, "description")
command := ExecCommand{
Command: "snet",
Directory: exampleServiceDir,
Input: []string{"", "", name, "", price, endpoint, tags, description},
Args: []string{"service", "init"},
}
return runCommand(command)
}
func exampleserviceIsPublishedToNetwork() error {
serviceFile := "./service.json"
args := []string{
"service", "publish", "local",
"--config", serviceFile,
"--agent-factory-at", agentFactoryAddress,
"--registry-at", registryAddress,
"--yes",
}
command := ExecCommand{
Command: "snet",
Directory: exampleServiceDir,
Args: args,
}
err := runCommand(command)
if err != nil {
return err
}
agentAddress, err = getPropertyFromFile(
exampleServiceDir+"/"+serviceFile,
"\"agentAddress\":",
)
if err != nil {
return err
}
if len(agentAddress) < 2 {
return errors.New("Len of accoagent address is to small: " + agentAddress)
}
agentAddress = agentAddress[1 : len(agentAddress)-1]
return nil
}
func exampleserviceIsRunWithSnetdaemon(table *gherkin.DataTable) error {
daemonPort := getTableValue(table, "daemon port")
ethereumEndpointPort := getTableValue(table, "ethereum endpoint port")
passthroughEndpointPort := getTableValue(table, "passthrough endpoint port")
snetdConfigTemplate := `
{
"AGENT_CONTRACT_ADDRESS": "%s",
"AUTO_SSL_DOMAIN": "",
"AUTO_SSL_CACHE_DIR": "",
"BLOCKCHAIN_ENABLED": true,
"CONFIG_PATH": "",
"DAEMON_LISTENING_PORT": %s,
"DAEMON_TYPE": "grpc",
"DB_PATH": "./db",
"ETHEREUM_JSON_RPC_ENDPOINT": "http://localhost:%s",
"EXECUTABLE_PATH": "",
"LOG_LEVEL": 5,
"PASSTHROUGH_ENABLED": true,
"PASSTHROUGH_ENDPOINT": "http://localhost:%s",
"POLL_SLEEP": "",
"PRIVATE_KEY": "%s",
"SERVICE_TYPE": "jsonrpc",
"SSL_CERT": "",
"SSL_KEY": "",
"WIRE_ENCODING": "json"
}`
snetdConfig := fmt.Sprintf(snetdConfigTemplate,
agentAddress, daemonPort, ethereumEndpointPort, passthroughEndpointPort, accountPrivateKey)
file := exampleServiceDir + "/snetd.config.json"
err := writeToFile(file, snetdConfig)
if err != nil {
return err
}
linkFile(envSingnetRepos+"/snet-daemon/build/snetd-linux-amd64", exampleServiceDir+"/snetd-linux-amd64")
outputFile = logPath + "/example-service.log"
outputContainsStrings = []string{}
command := ExecCommand{
Command: exampleServiceDir + "/scripts/run-snet-service",
Directory: exampleServiceDir,
OutputFile: outputFile,
}
err = runCommandAsync(command)
if err != nil {
return err
}
_, err = checkWithTimeout(5000, 500, checkFileContainsStrings)
if err != nil {
return err
}
time.Sleep(2 * time.Second)
command = ExecCommand{
Command: exampleServiceDir + "/scripts/test-call",
Directory: exampleServiceDir,
}
return runCommand(command)
}
func singularityNETJobIsCreated(table *gherkin.DataTable) error {
maxPrice := getTableValue(table, "max price")
args := []string{
"agent",
"--at", agentAddress,
"create-jobs",
"--funded",
"--signed",
"--max-price", maxPrice,
"--yes",
}
command := ExecCommand{
Command: "snet",
Directory: exampleServiceDir,
Args: args,
}
err := runCommand(command)
if err != nil {
return err
}
args = []string{
"client", "call", "classify",
fmt.Sprintf(`{"image_type": "jpg", "image": "%s"}`, testImage),
"--agent-at", agentAddress,
}
command = ExecCommand{
Command: "snet",
Directory: exampleServiceDir,
Args: args,
}
return runCommand(command)
}
func FeatureContext(s *godog.Suite) {
s.Step(`^Ethereum network is running on port (\d+)$`, ethereumNetworkIsRunningOnPort)
s.Step(`^Contracts are deployed using Truffle$`, contractsAreDeployedUsingTruffle)
s.Step(`^IPFS is running with API port (\d+) and Gateway port (\d+)$`, ipfsIsRunning)
s.Step(`^Identity is created with user "([^"]*)" and private key "([^"]*)"$`,
identityIsCreatedWithUserAndPrivateKey)
s.Step(`^snet is configured with Ethereum RPC endpoint (\d+)$`, snetIsConfiguredWithEthereumRPCEndpoint)
s.Step(`^snet is configured with IPFS endpoint (\d+)$`, snetIsConfiguredWithIPFSEndpoint)
s.Step(`^Organization is added:$`, organizationIsAdded)
s.Step(`^example-service is registered$`, exampleserviceIsRegistered)
s.Step(`^example-service is published to network$`, exampleserviceIsPublishedToNetwork)
s.Step(`^example-service is run with snet-daemon$`, exampleserviceIsRunWithSnetdaemon)
s.Step(`^SingularityNET job is created$`, singularityNETJobIsCreated)
}
func checkFileContainsStrings() (bool, error) {
log.Printf("check output file: '%s'\n", outputFile)
log.Printf("check output file contains string: '%s'\n", strings.Join(outputContainsStrings, ","))
out, err := readFile(outputFile)
if err != nil {
return false, err
}
if out != "" {
log.Printf("Output: %s\n", out)
}
if strings.Contains(out, "Error") {
return false, errors.New("Output contains error")
}
for _, str := range outputContainsStrings {
if !strings.Contains(out, str) {
return false, nil
}
}
return true, nil
}
func getTableValue(table *gherkin.DataTable, column string) string {
names := table.Rows[0].Cells
for i, cell := range names {
if cell.Value == column {
return table.Rows[1].Cells[i].Value
}
}
log.Printf("column: %s has not been found in table", column)
return ""
}
var testImage = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxAPDw0PDQ0PDg0PDQ0PDQ0PDQ8ODQ0NFRIWFhUSExUYHyghGB4lJxMWITEhJSor" +
"Li4uGB8zODMsNygtLisBCgoKDQ0NDxAPFSsdFRktL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