-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutils_test.go
More file actions
162 lines (134 loc) · 3.52 KB
/
utils_test.go
File metadata and controls
162 lines (134 loc) · 3.52 KB
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
package substrate
import (
"crypto/md5"
"encoding/hex"
"errors"
"os"
"testing"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/stretchr/testify/require"
)
type AccountUser string
const (
AccountAlice = "alice"
AccountAliceStash = "alice_stash"
AccountBob = "bob"
)
var (
someDocumentUrl = "somedocument"
testName = "test-substrate"
AliceMnemonics = "//Alice"
AliceStashMnemonics = "//Alice//stash"
BobMnemonics = "//Bob"
AliceAddress = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"
AliceStashAddress = "5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY"
BobAddress = "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"
Accounts = map[AccountUser]struct {
Phrase string
Address string
}{
AccountAlice: {
Phrase: AliceMnemonics,
Address: AliceAddress,
},
AccountAliceStash: {
Phrase: AliceStashMnemonics,
Address: AliceStashAddress,
},
AccountBob: {
Phrase: BobMnemonics,
Address: BobAddress,
},
}
)
func startLocalConnection(t *testing.T) *Substrate {
var mgr Manager
if _, ok := os.LookupEnv("CI"); ok {
mgr = NewManager("ws://127.0.0.1:9944")
} else {
mgr = NewManager("wss://tfchain.dev.grid.tf")
}
cl, err := mgr.Substrate()
require.NoError(t, err)
return cl
}
func getUrlBasedOnEnv() string {
if _, ok := os.LookupEnv("CI"); ok {
return "ws://127.0.0.1:9944"
} else {
return "wss://tfchain.dev.grid.tf"
}
}
func assertCreateTwin(t *testing.T, cl *Substrate, user AccountUser) uint32 {
u := Accounts[user]
identity, err := NewIdentityFromSr25519Phrase(u.Phrase)
require.NoError(t, err)
account, err := FromAddress(u.Address)
require.NoError(t, err)
termsAndConditions, err := cl.SignedTermsAndConditions(account)
require.NoError(t, err)
if len(termsAndConditions) == 0 {
hash := md5.New()
hash.Write([]byte(someDocumentUrl))
h := hex.EncodeToString(hash.Sum(nil))
err = cl.AcceptTermsAndConditions(identity, someDocumentUrl, h)
require.NoError(t, err)
}
twnID, err := cl.GetTwinByPubKey(account.PublicKey())
if err != nil {
address := "relay.io"
pk := "pk"
twnID, err = cl.CreateTwin(identity, address, []byte(pk))
require.NoError(t, err)
}
return twnID
}
func assertCreateFarm(t *testing.T, cl *Substrate) (uint32, uint32) {
identity, err := NewIdentityFromSr25519Phrase(BobMnemonics)
require.NoError(t, err)
twnID := assertCreateTwin(t, cl, AccountBob)
id, err := cl.GetFarmByName(testName)
if err == nil {
return id, twnID
}
if errors.Is(err, ErrNotFound) {
err = cl.CreateFarm(identity, testName, []PublicIPInput{})
require.NoError(t, err)
}
id, err = cl.GetFarmByName(testName)
require.NoError(t, err)
return id, twnID
}
func assertCreateNode(t *testing.T, cl *Substrate, farmID uint32, twinID uint32, identity Identity) uint32 {
nodeID, err := cl.GetNodeByTwinID(twinID)
if err == nil {
return nodeID
} else if !errors.Is(err, ErrNotFound) {
require.NoError(t, err)
}
// if not found create a node.
nodeID, err = cl.CreateNode(identity,
Node{
FarmID: types.U32(farmID),
TwinID: types.U32(twinID),
Location: Location{
City: "SomeCity",
Country: "SomeCountry",
Latitude: "51.049999",
Longitude: "3.733333",
},
Resources: Resources{
SRU: types.U64(1024 * Gigabyte),
MRU: types.U64(16 * Gigabyte),
CRU: types.U64(8),
HRU: types.U64(1024 * Gigabyte),
},
BoardSerial: OptionBoardSerial{
HasValue: true,
AsValue: "some_serial",
},
},
)
require.NoError(t, err)
return nodeID
}