-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathzboxcli_file_stats_test.go
505 lines (423 loc) · 17.7 KB
/
zboxcli_file_stats_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
package cli_tests
import (
"encoding/json"
"fmt"
"math"
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"
"github.com/0chain/system_test/internal/api/util/test"
climodel "github.com/0chain/system_test/internal/cli/model"
cliutils "github.com/0chain/system_test/internal/cli/util"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/sha3"
)
func TestFileStats(testSetup *testing.T) {
t := test.NewSystemTest(testSetup)
t.SetSmokeTests("get file stats in root directory should work")
t.Parallel()
t.TestSetup("Create tmp dir", func() {
// Create a folder to keep all the generated files to be uploaded
err := os.MkdirAll("tmp", os.ModePerm)
require.Nil(t, err)
})
const chunksize = 64 * 1024
t.Run("get file stats in root directory should work", func(t *test.SystemTest) {
allocationID := setupAllocation(t, configPath)
remotepath := "/"
filesize := int64(555)
filename := generateFileAndUpload(t, allocationID, remotepath, filesize)
fname := filepath.Base(filename)
remoteFilePath := path.Join(remotepath, fname)
output, err := getFileStats(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remoteFilePath,
"json": "",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
var stats map[string]climodel.FileStats
err = json.Unmarshal([]byte(output[0]), &stats)
require.Nil(t, err)
for _, data := range stats {
require.Equal(t, fname, data.Name)
require.Equal(t, remoteFilePath, data.Path)
require.Equal(t, fmt.Sprintf("%x", sha3.Sum256([]byte(allocationID+":"+remoteFilePath))), data.PathHash)
require.Equal(t, int64(0), data.NumOfBlockDownloads)
require.Equal(t, int64(1), data.NumOfUpdates)
require.Equal(t, float64(data.NumOfBlocks), math.Ceil(float64(data.Size)/float64(chunksize)))
if data.WriteMarkerTxn == "" {
require.Equal(t, false, data.BlockchainAware)
} else {
require.Equal(t, true, data.BlockchainAware)
}
// FIXME: POSSIBLE BUG: key name and blobberID in value should be same but this is not consistent for every run and happening randomly
// require.Equal(t, blobberID, data.BlobberID, "key name and blobberID in value should be same")
}
})
t.Run("get file stats in sub directory should work", func(t *test.SystemTest) {
allocationID := setupAllocation(t, configPath)
remotepath := "/dir/"
filesize := int64(533)
filename := generateFileAndUpload(t, allocationID, remotepath, filesize)
fname := filepath.Base(filename)
remoteFilePath := path.Join(remotepath, fname)
output, err := getFileStats(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remoteFilePath,
"json": "",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
var stats map[string]climodel.FileStats
err = json.Unmarshal([]byte(output[0]), &stats)
require.Nil(t, err)
for _, data := range stats {
require.Equal(t, fname, data.Name)
require.Equal(t, remoteFilePath, data.Path)
require.Equal(t, fmt.Sprintf("%x", sha3.Sum256([]byte(allocationID+":"+remoteFilePath))), data.PathHash)
require.Equal(t, int64(0), data.NumOfBlockDownloads)
require.Equal(t, int64(1), data.NumOfUpdates)
require.Equal(t, float64(data.NumOfBlocks), math.Ceil(float64(data.Size)/float64(chunksize)))
if data.WriteMarkerTxn == "" {
require.Equal(t, false, data.BlockchainAware)
} else {
require.Equal(t, true, data.BlockchainAware)
}
// FIXME: POSSIBLE BUG: key name and blobberID in value should be same but this is not consistent for every run and happening randomly
// require.Equal(t, blobberID, data.BlobberID, "key name and blobberID in value should be same")
}
})
t.Run("get file stats in nested sub directory should work", func(t *test.SystemTest) {
allocationID := setupAllocation(t, configPath)
remotepath := "/nested/dir/"
filesize := int64(523)
filename := generateFileAndUpload(t, allocationID, remotepath, filesize)
fname := filepath.Base(filename)
remoteFilePath := path.Join(remotepath, fname)
output, err := getFileStats(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remoteFilePath,
"json": "",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
var stats map[string]climodel.FileStats
err = json.Unmarshal([]byte(output[0]), &stats)
require.Nil(t, err)
for _, data := range stats {
require.Equal(t, fname, data.Name)
require.Equal(t, remoteFilePath, data.Path)
require.Equal(t, fmt.Sprintf("%x", sha3.Sum256([]byte(allocationID+":"+remoteFilePath))), data.PathHash)
require.Equal(t, int64(0), data.NumOfBlockDownloads)
require.Equal(t, int64(1), data.NumOfUpdates)
require.Equal(t, float64(data.NumOfBlocks), math.Ceil(float64(data.Size)/float64(chunksize)))
if data.WriteMarkerTxn == "" {
require.Equal(t, false, data.BlockchainAware)
} else {
require.Equal(t, true, data.BlockchainAware)
}
// FIXME: POSSIBLE BUG: key name and blobberID in value should be same but this is not consistent for every run and happening randomly
// require.Equal(t, blobberID, data.BlobberID, "key name and blobberID in value should be same")
}
})
t.Run("get file stats on an empty allocation", func(t *test.SystemTest) {
allocationID := setupAllocation(t, configPath)
remotepath := "/"
output, err := getFileStats(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remotepath,
"json": "",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
var stats map[string]climodel.FileStats
err = json.Unmarshal([]byte(output[0]), &stats)
require.Nil(t, err)
for _, data := range stats {
require.Equal(t, "", data.Name)
require.Equal(t, "", data.Path)
require.Equal(t, "", data.PathHash)
require.Equal(t, int64(0), data.Size)
require.Equal(t, int64(0), data.NumOfBlocks)
require.Equal(t, int64(0), data.NumOfBlockDownloads)
require.Equal(t, int64(0), data.NumOfChallenges)
require.Equal(t, int64(0), data.NumOfUpdates)
require.Equal(t, "", data.WriteMarkerTxn)
require.Equal(t, "", data.LastChallengeTxn)
require.Equal(t, "", data.BlobberID)
require.Equal(t, "", data.BlobberURL)
require.Equal(t, false, data.BlockchainAware)
}
})
t.Run("get file stats for a file that does not exists", func(t *test.SystemTest) {
allocationID := setupAllocation(t, configPath)
remotepath := "/"
absentFileName := "randomFileName.txt"
output, err := getFileStats(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": path.Join(remotepath, absentFileName),
"json": "",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
var stats map[string]climodel.FileStats
err = json.Unmarshal([]byte(output[0]), &stats)
require.Nil(t, err)
for _, data := range stats {
require.Equal(t, "", data.Name)
require.Equal(t, "", data.Path)
require.Equal(t, "", data.PathHash)
require.Equal(t, int64(0), data.Size)
require.Equal(t, int64(0), data.NumOfBlocks)
require.Equal(t, int64(0), data.NumOfBlockDownloads)
require.Equal(t, int64(0), data.NumOfChallenges)
require.Equal(t, int64(0), data.NumOfUpdates)
require.Equal(t, "", data.WriteMarkerTxn)
require.Equal(t, "", data.LastChallengeTxn)
require.Equal(t, "", data.BlobberID)
require.Equal(t, "", data.BlobberURL)
require.Equal(t, false, data.BlockchainAware)
}
})
t.Run("get file stats for an allocation you dont own", func(t *test.SystemTest) {
otherAllocationID := ""
remotepath := "/"
filesize := int64(533)
remoteFilePath := ""
t.Run("Get Other Allocation ID", func(t *test.SystemTest) {
otherAllocationID = setupAllocation(t, configPath)
filename := generateFileAndUpload(t, otherAllocationID, remotepath, filesize)
fname := filepath.Base(filename)
remoteFilePath = path.Join(remotepath, fname)
// allocation should work for other wallet
output, err := getFileStats(t, configPath, createParams(map[string]interface{}{
"allocation": otherAllocationID,
"remotepath": remoteFilePath,
"json": "",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
var stats map[string]climodel.FileStats
err = json.Unmarshal([]byte(output[0]), &stats)
require.Nil(t, err)
for _, data := range stats {
require.Equal(t, fname, data.Name)
require.Equal(t, remoteFilePath, data.Path)
require.Equal(t, fmt.Sprintf("%x", sha3.Sum256([]byte(otherAllocationID+":"+remoteFilePath))), data.PathHash)
require.Equal(t, int64(0), data.NumOfBlockDownloads)
require.Equal(t, int64(1), data.NumOfUpdates)
require.Equal(t, float64(data.NumOfBlocks), math.Ceil(float64(data.Size)/float64(chunksize)))
if data.WriteMarkerTxn == "" {
require.Equal(t, false, data.BlockchainAware)
} else {
require.Equal(t, true, data.BlockchainAware)
}
// FIXME: POSSIBLE BUG: key name and blobberID in value should be same but this is not consistent for every run and happening randomly
// require.Equal(t, blobberID, data.BlobberID, "key name and blobberID in value should be same")
}
})
createWallet(t)
output, err := getFileStats(t, configPath, createParams(map[string]interface{}{
"allocation": otherAllocationID,
"remotepath": remoteFilePath,
"json": "",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
var stats map[string]climodel.FileStats
err = json.Unmarshal([]byte(output[0]), &stats)
require.Nil(t, err)
require.Len(t, stats, 0)
})
t.Run("get file stats with no params supplied", func(t *test.SystemTest) {
setupAllocation(t, configPath)
output, err := getFileStats(t, configPath, createParams(map[string]interface{}{}), false)
require.NotNil(t, err, "getting stats without params should fail", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, output[0], "Error: allocation flag is missing", strings.Join(output, "\n"))
})
t.Run("get file stats with no allocation param supplied", func(t *test.SystemTest) {
allocationID := setupAllocation(t, configPath)
remotepath := "/"
filesize := int64(533)
filename := generateFileAndUpload(t, allocationID, remotepath, filesize)
fname := filepath.Base(filename)
remoteFilePath := path.Join(remotepath, fname)
output, err := getFileStats(t, configPath, createParams(map[string]interface{}{
"remotepath": remoteFilePath,
"json": "",
}), false)
require.NotNil(t, err, "getting stats without params should fail", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, output[0], "Error: allocation flag is missing", strings.Join(output, "\n"))
})
t.Run("get file stats with no remotepath param supplied", func(t *test.SystemTest) {
allocationID := setupAllocation(t, configPath)
output, err := getFileStats(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"json": "",
}), false)
require.NotNil(t, err, "getting stats without params should fail", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, output[0], "Error: remotepath flag is missing", strings.Join(output, "\n"))
})
t.RunWithTimeout("get file stats before and after update", 3*time.Minute, func(t *test.SystemTest) { //todo: too slow
allocationID := setupAllocation(t, configPath, map[string]interface{}{"size": 10 * MB})
remotepath := "/"
filesize := int64(0.5 * MB)
filename := generateFileAndUpload(t, allocationID, remotepath, filesize)
fname := filepath.Base(filename)
remoteFilePath := path.Join(remotepath, fname)
output, err := getFileStats(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remoteFilePath,
"json": "",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
var stats map[string]climodel.FileStats
err = json.Unmarshal([]byte(output[0]), &stats)
require.Nil(t, err)
for _, data := range stats {
require.Equal(t, fname, data.Name)
require.Equal(t, remoteFilePath, data.Path)
require.Equal(t, int64(0), data.NumOfBlockDownloads)
require.Equal(t, fmt.Sprintf("%x", sha3.Sum256([]byte(allocationID+":"+remoteFilePath))), data.PathHash)
require.Equal(t, float64(data.NumOfBlocks), math.Ceil(float64(data.Size)/float64(chunksize)))
require.Equal(t, int64(1), data.NumOfUpdates)
require.Equal(t, int64(0.5*MB/2), data.Size)
if data.WriteMarkerTxn == "" {
require.Equal(t, false, data.BlockchainAware)
} else {
require.Equal(t, true, data.BlockchainAware)
}
// FIXME: POSSIBLE BUG: key name and blobberID in value should be same but this is not consistent for every run and happening randomly
// require.Equal(t, blobberID, data.BlobberID, "key name and blobberID in value should be same")
}
// update size for the file
updateFileWithRandomlyGeneratedData(t, allocationID, "/"+fname, int64(1*MB))
cliutils.Wait(t, 2*time.Minute)
// fetch file stats after update
output, err = getFileStats(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remoteFilePath,
"json": "",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
err = json.Unmarshal([]byte(output[0]), &stats)
require.Nil(t, err)
for _, data := range stats {
require.Equal(t, fname, data.Name)
require.Equal(t, remoteFilePath, data.Path)
require.Equal(t, fmt.Sprintf("%x", sha3.Sum256([]byte(allocationID+":"+remoteFilePath))), data.PathHash)
require.Equal(t, int64(0), data.NumOfBlockDownloads)
require.Equal(t, float64(data.NumOfBlocks), math.Ceil(float64(data.Size)/float64(chunksize)))
require.Equal(t, int64(2), data.NumOfUpdates, "the number of updates count should increment")
require.Equal(t, int64(1*MB/2), data.Size)
if data.WriteMarkerTxn == "" {
require.Equal(t, false, data.BlockchainAware)
} else {
require.Equal(t, true, data.BlockchainAware)
}
// FIXME: POSSIBLE BUG: key name and blobberID in value should be same but this is not consistent for every run and happening randomly
// require.Equal(t, blobberID, data.BlobberID, "key name and blobberID in value should be same")
}
})
t.RunWithTimeout("get file stats before and after download", 7*time.Minute, func(t *test.SystemTest) { //todo: too slow
t.Skip()
allocSize := int64(2048)
allocationID := setupAllocation(t, configPath, map[string]interface{}{
"size": allocSize,
"tokens": 9,
})
remotepath := "/"
filesize := int64(256)
filename := generateFileAndUpload(t, allocationID, remotepath, filesize)
fname := filepath.Base(filename)
remoteFilePath := path.Join(remotepath, fname)
output, err := getFileStats(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remoteFilePath,
"json": "",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
var stats map[string]climodel.FileStats
err = json.Unmarshal([]byte(output[0]), &stats)
require.Nil(t, err)
for _, data := range stats {
require.Equal(t, fname, data.Name)
require.Equal(t, remoteFilePath, data.Path)
require.Equal(t, int64(0), data.NumOfBlockDownloads)
require.Equal(t, fmt.Sprintf("%x", sha3.Sum256([]byte(allocationID+":"+remoteFilePath))), data.PathHash)
require.Equal(t, float64(data.NumOfBlocks), math.Ceil(float64(data.Size)/float64(chunksize)))
require.Equal(t, int64(1), data.NumOfUpdates)
if data.WriteMarkerTxn == "" {
require.Equal(t, false, data.BlockchainAware)
} else {
require.Equal(t, true, data.BlockchainAware)
}
}
// Delete the uploaded file, since we will be downloading it now
err = os.Remove(filename)
require.Nil(t, err)
output, err = downloadFile(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remoteFilePath,
"localpath": "tmp/",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
aggregatedOutput := strings.Join(output, " ")
require.Contains(t, aggregatedOutput, StatusCompletedCB)
require.Contains(t, aggregatedOutput, filepath.Base(filename))
cliutils.Wait(t, 2*time.Minute)
// get file stats after download
output, err = getFileStats(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remoteFilePath,
"json": "",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
err = json.Unmarshal([]byte(output[0]), &stats)
require.Nil(t, err)
var skippedBlobber int
for _, data := range stats {
require.Equal(t, fname, data.Name)
require.Equal(t, remoteFilePath, data.Path)
if data.NumOfBlockDownloads == 0 {
skippedBlobber++
} else {
require.Equal(t, int64(1), data.NumOfBlockDownloads)
}
require.Equal(t, fmt.Sprintf("%x", sha3.Sum256([]byte(allocationID+":"+remoteFilePath))), data.PathHash)
require.Equal(t, int64(1), data.NumOfUpdates)
require.Equal(t, float64(data.NumOfBlocks), math.Ceil(float64(data.Size)/float64(chunksize)))
if data.WriteMarkerTxn == "" {
require.Equal(t, false, data.BlockchainAware)
} else {
require.Equal(t, true, data.BlockchainAware)
}
}
require.Equal(t, 1, skippedBlobber)
})
}
func getFileStats(t *test.SystemTest, cliConfigFilename, param string, retry bool) ([]string, error) {
t.Logf("Getting file stats...")
cmd := fmt.Sprintf(
"./zbox stats %s --silent --wallet %s --configDir ./config --config %s",
param,
escapedTestName(t)+"_wallet.json",
cliConfigFilename,
)
if retry {
return cliutils.RunCommand(t, cmd, 3, time.Second*2)
} else {
return cliutils.RunCommandWithoutRetry(cmd)
}
}