-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreplace_blobber_test.go
199 lines (142 loc) · 8.09 KB
/
replace_blobber_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
package api_tests
import (
"crypto/rand"
"math/big"
"testing"
"time"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/system_test/internal/api/model"
"github.com/0chain/system_test/internal/api/util/client"
"github.com/0chain/system_test/internal/api/util/test"
"github.com/0chain/system_test/internal/api/util/wait"
"github.com/stretchr/testify/require"
)
func TestReplaceBlobber(testSetup *testing.T) {
t := test.NewSystemTest(testSetup)
t.SetSmokeTests("Replace blobber in allocation, should work")
t.RunSequentially("Replace blobber in allocation, should work", func(t *test.SystemTest) {
wallet := createWallet(t)
sdkClient.SetWallet(t, wallet)
blobberRequirements := model.DefaultBlobberRequirements(wallet.Id, wallet.PublicKey)
allocationBlobbers := apiClient.GetAllocationBlobbers(t, wallet, &blobberRequirements, client.HttpOkStatus)
allocationID := apiClient.CreateAllocation(t, wallet, allocationBlobbers, client.TxSuccessfulStatus)
allocation := apiClient.GetAllocation(t, allocationID, client.HttpOkStatus)
numberOfBlobbersBefore := len(allocation.Blobbers)
oldBlobberID := getFirstUsedStorageNodeID(allocationBlobbers.Blobbers, allocation.Blobbers)
require.NotZero(t, oldBlobberID, "Old blobber ID contains zero value")
newBlobberID := getNotUsedStorageNodeID(allocationBlobbers.Blobbers, allocation.Blobbers)
require.NotZero(t, newBlobberID, "New blobber ID contains zero value")
apiClient.UpdateAllocationBlobbers(t, wallet, newBlobberID, oldBlobberID, allocationID, client.TxSuccessfulStatus)
var numberOfBlobbersAfter int
wait.PoolImmediately(t, time.Second*30, func() bool {
allocation = apiClient.GetAllocation(t, allocationID, client.HttpOkStatus)
numberOfBlobbersAfter = len(allocation.Blobbers)
return numberOfBlobbersAfter == numberOfBlobbersBefore
})
require.Equal(t, numberOfBlobbersAfter, numberOfBlobbersBefore)
require.True(t, isBlobberExist(newBlobberID, allocation.Blobbers))
})
t.RunSequentially("Replace blobber with the same one in allocation, shouldn't work", func(t *test.SystemTest) {
wallet := createWallet(t)
sdkClient.SetWallet(t, wallet)
blobberRequirements := model.DefaultBlobberRequirements(wallet.Id, wallet.PublicKey)
allocationBlobbers := apiClient.GetAllocationBlobbers(t, wallet, &blobberRequirements, client.HttpOkStatus)
allocationID := apiClient.CreateAllocation(t, wallet, allocationBlobbers, client.TxSuccessfulStatus)
allocation := apiClient.GetAllocation(t, allocationID, client.HttpOkStatus)
numberOfBlobbersBefore := len(allocation.Blobbers)
oldBlobberID := getFirstUsedStorageNodeID(allocationBlobbers.Blobbers, allocation.Blobbers)
require.NotZero(t, oldBlobberID, "Old blobber ID contains zero value")
apiClient.UpdateAllocationBlobbers(t, wallet, oldBlobberID, oldBlobberID, allocationID, client.TxUnsuccessfulStatus)
var numberOfBlobbersAfter int
wait.PoolImmediately(t, time.Second*30, func() bool {
allocation = apiClient.GetAllocation(t, allocationID, client.HttpOkStatus)
numberOfBlobbersAfter = len(allocation.Blobbers)
return numberOfBlobbersAfter == numberOfBlobbersBefore
})
require.Equal(t, numberOfBlobbersAfter, numberOfBlobbersBefore)
})
t.RunSequentially("Replace blobber with incorrect blobber ID of an old blobber, shouldn't work", func(t *test.SystemTest) {
wallet := createWallet(t)
sdkClient.SetWallet(t, wallet)
blobberRequirements := model.DefaultBlobberRequirements(wallet.Id, wallet.PublicKey)
allocationBlobbers := apiClient.GetAllocationBlobbers(t, wallet, &blobberRequirements, client.HttpOkStatus)
allocationID := apiClient.CreateAllocation(t, wallet, allocationBlobbers, client.TxSuccessfulStatus)
allocation := apiClient.GetAllocation(t, allocationID, client.HttpOkStatus)
numberOfBlobbersBefore := len(allocation.Blobbers)
newBlobberID := getNotUsedStorageNodeID(allocationBlobbers.Blobbers, allocation.Blobbers)
require.NotZero(t, newBlobberID, "Old blobber ID contains zero value")
result, err := rand.Int(rand.Reader, big.NewInt(10))
require.Nil(t, err)
apiClient.UpdateAllocationBlobbers(t, wallet, newBlobberID, result.String(), allocationID, client.TxUnsuccessfulStatus)
var numberOfBlobbersAfter int
wait.PoolImmediately(t, time.Second*30, func() bool {
allocation = apiClient.GetAllocation(t, allocationID, client.HttpOkStatus)
numberOfBlobbersAfter = len(allocation.Blobbers)
return numberOfBlobbersAfter == numberOfBlobbersBefore
})
require.Equal(t, numberOfBlobbersAfter, numberOfBlobbersBefore)
})
t.RunSequentially("Check token accounting of a blobber replacing in allocation, should work", func(t *test.SystemTest) {
wallet := createWallet(t)
blobberRequirements := model.DefaultBlobberRequirements(wallet.Id, wallet.PublicKey)
allocationBlobbers := apiClient.GetAllocationBlobbers(t, wallet, &blobberRequirements, client.HttpOkStatus)
allocationID := apiClient.CreateAllocation(t, wallet, allocationBlobbers, client.TxSuccessfulStatus)
allocation := apiClient.GetAllocation(t, allocationID, client.HttpOkStatus)
numberOfBlobbersBefore := len(allocation.Blobbers)
oldBlobberID := getFirstUsedStorageNodeID(allocationBlobbers.Blobbers, allocation.Blobbers)
require.NotZero(t, oldBlobberID, "Old blobber ID contains zero value")
newBlobberID := getNotUsedStorageNodeID(allocationBlobbers.Blobbers, allocation.Blobbers)
require.NotZero(t, newBlobberID, "New blobber ID contains zero value")
walletBalance := apiClient.GetWalletBalance(t, wallet, client.HttpOkStatus)
balanceBeforeAllocationUpdate := walletBalance.Balance
apiClient.UpdateAllocationBlobbers(t, wallet, newBlobberID, oldBlobberID, allocationID, client.TxSuccessfulStatus)
var numberOfBlobbersAfter int
wait.PoolImmediately(t, time.Second*30, func() bool {
allocation = apiClient.GetAllocation(t, allocationID, client.HttpOkStatus)
numberOfBlobbersAfter = len(allocation.Blobbers)
return numberOfBlobbersAfter == numberOfBlobbersBefore
})
walletBalance = apiClient.GetWalletBalance(t, wallet, client.HttpOkStatus)
balanceAfterAllocationUpdate := walletBalance.Balance
require.Equal(t, numberOfBlobbersAfter, numberOfBlobbersBefore)
require.Greater(t, balanceBeforeAllocationUpdate, balanceAfterAllocationUpdate)
})
t.RunSequentiallyWithTimeout("Replace blobber in allocation with repair should work", 90*time.Second, func(t *test.SystemTest) {
wallet := createWallet(t)
sdkClient.SetWallet(t, wallet)
blobberRequirements := model.DefaultBlobberRequirements(wallet.Id, wallet.PublicKey)
allocationBlobbers := apiClient.GetAllocationBlobbers(t, wallet, &blobberRequirements, client.HttpOkStatus)
allocationID := apiClient.CreateAllocation(t, wallet, allocationBlobbers, client.TxSuccessfulStatus)
uploadOp := sdkClient.AddUploadOperation(t, "", "")
sdkClient.MultiOperation(t, allocationID, []sdk.OperationRequest{uploadOp})
allocation := apiClient.GetAllocation(t, allocationID, client.HttpOkStatus)
oldBlobberID := getFirstUsedStorageNodeID(allocationBlobbers.Blobbers, allocation.Blobbers)
require.NotZero(t, oldBlobberID, "Old blobber ID contains zero value")
newBlobberID := getNotUsedStorageNodeID(allocationBlobbers.Blobbers, allocation.Blobbers)
require.NotZero(t, newBlobberID, "New blobber ID contains zero value")
apiClient.UpdateAllocationBlobbers(t, wallet, newBlobberID, oldBlobberID, allocationID, client.TxSuccessfulStatus)
time.Sleep(10 * time.Second)
alloc, err := sdk.GetAllocation(allocationID)
require.Nil(t, err)
// Check for blobber replacement
notFound := true
require.True(t, len(alloc.Blobbers) > 0)
// Check if new blobber is in the same position as old blobber
require.True(t, alloc.Blobbers[0].ID == newBlobberID)
for _, blobber := range alloc.Blobbers {
if blobber.ID == oldBlobberID {
notFound = false
break
}
}
require.True(t, notFound, "old blobber should not be in the list")
// check for repair
_, _, req, _, err := alloc.RepairRequired("/")
require.Nil(t, err)
require.True(t, req)
// do repair
sdkClient.RepairAllocation(t, allocationID)
_, err = sdk.GetFileRefFromBlobber(allocationID, newBlobberID, uploadOp.RemotePath)
require.Nil(t, err)
})
}