Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollback allocation tc #1043

Closed
wants to merge 8 commits into from
18 changes: 18 additions & 0 deletions tests/cli_tests/zboxcli_common_user_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ func updateFileWithRandomlyGeneratedDataWithWallet(t *test.SystemTest, walletNam
return localfile
}

func updateFileContentWithRandomlyGeneratedData(t *test.SystemTest, allocationID, remotepath string, filename string, size int64) string {
return updateFileContentWithRandomlyGeneratedDataWithWallet(t, escapedTestName(t), allocationID, remotepath, filename, size)
}

func updateFileContentWithRandomlyGeneratedDataWithWallet(t *test.SystemTest, walletName, allocationID, remotepath string, filename string, size int64) string {

err := createFileWithSize(filename, size)
require.Nil(t, err)

output, err := updateFileWithWallet(t, walletName, configPath, map[string]interface{}{
"allocation": allocationID,
"remotepath": remotepath,
"localpath": filename,
}, true)
require.Nil(t, err, strings.Join(output, "\n"))
return filename
}

func renameFile(t *test.SystemTest, cliConfigFilename string, param map[string]interface{}, retry bool) ([]string, error) {
t.Logf("Renaming file...")
p := createParams(param)
Expand Down
21 changes: 21 additions & 0 deletions tests/cli_tests/zboxcli_file_upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,27 @@ func generateFileAndUploadForWallet(t *test.SystemTest, wallet, allocationID, re
return filename
}

func generateFileContentAndUpload(t *test.SystemTest, allocationID, remotepath string, filename string, size int64) string {
return generateFileContentAndUploadForWallet(t, escapedTestName(t), allocationID, remotepath, filename, size)
}

func generateFileContentAndUploadForWallet(t *test.SystemTest, wallet, allocationID, remotepath string, filename string, size int64) string {

err := createFileWithSize(filename, size)
require.Nil(t, err)

// Upload parameters
// log command with allocation id, filename and remotepath
t.Logf("Uploading file %s to allocation %s with remotepath %s", filename, allocationID, remotepath+filepath.Base(filename))
uploadWithParamForWallet(t, wallet, configPath, map[string]interface{}{
"allocation": allocationID,
"localpath": filename,
"remotepath": remotepath + filepath.Base(filename),
})

return filename
}

func generateFileAndUploadWithParam(t *test.SystemTest, allocationID, remotepath string, size int64, params map[string]interface{}) string {
filename := generateRandomTestFileName(t)

Expand Down
287 changes: 287 additions & 0 deletions tests/cli_tests/zboxcli_rollback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,293 @@ func TestRollbackAllocation(testSetup *testing.T) {
require.True(t, foundAtSource, "file is found at source: ", strings.Join(output, "\n"))
require.False(t, foundAtDest, "file not found at destination: ", strings.Join(output, "\n"))
})

t.RunSequentially("rollback allocation after duplicating a file should work", func(t *test.SystemTest) {
allocationID := setupAllocationAndReadLock(t, configPath, map[string]interface{}{
"size": 2 * MB,
"tokens": 10,
})

remotePath := "/"
file := "original.txt"

fileSize := int64(1 * MB)

localFilePath := generateFileContentAndUpload(t, allocationID, remotePath, file, fileSize)
localFileChecksum := generateChecksum(t, localFilePath)

err := os.Remove(localFilePath)
require.Nil(t, err)

output, err := getFileMeta(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"json": "",
"remotepath": remotePath + filepath.Base(localFilePath),
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)

var meta climodel.FileMetaResult
err = json.NewDecoder(strings.NewReader(output[0])).Decode(&meta)
require.Nil(t, err, strings.Join(output, "\n"))
require.Equal(t, fileSize, meta.ActualFileSize, "file size should be same as uploaded")

newFileSize := int64(1.5 * MB)
updateFileContentWithRandomlyGeneratedData(t, allocationID, "/"+filepath.Base(localFilePath),localFilePath, int64(newFileSize))

output, err = getFileMeta(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"json": "",
"remotepath": remotePath + filepath.Base(localFilePath),
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)

err = json.NewDecoder(strings.NewReader(output[0])).Decode(&meta)
require.Nil(t, err, strings.Join(output, "\n"))
require.Equal(t, newFileSize, meta.ActualFileSize)

// rollback allocation

output, err = rollbackAllocation(t, escapedTestName(t), configPath, createParams(map[string]interface{}{
"allocation": allocationID,
}))
t.Log(strings.Join(output, "\n"))
require.NoError(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)

output, err = downloadFile(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remotePath + filepath.Base(localFilePath),
"localpath": "tmp/",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 2)

require.Contains(t, output[1], StatusCompletedCB)
require.Contains(t, output[1], filepath.Base(localFilePath))

downloadedFileChecksum := generateChecksum(t, "tmp/"+filepath.Base(localFilePath))

require.Equal(t, localFileChecksum, downloadedFileChecksum)

createAllocationTestTeardown(t, allocationID)
})

t.RunSequentially("rollback allocation after multiple files upload and single file update should work", func(t *test.SystemTest) {
allocationID := setupAllocationAndReadLock(t, configPath, map[string]interface{}{
"size": 4 * MB,
"tokens": 9,
})

remotepath := "/"
fileSize := int64(1 * MB)
localFilePath := "file2.txt"
files := map[string]int64{
"file1.txt": 1 * MB,
"file2.txt": 1 * MB,
"file3.txt": 1 * MB,
}

//var wg sync.WaitGroup
for filepath, fileSize := range files {
//wg.Add(1)
go func(path string, size int64) {
//defer wg.Done()
generateFileContentAndUpload(t, allocationID, remotepath, path, size)

}(filepath, fileSize)
}
//wg.Wait()

localFileChecksum := generateChecksum(t, filepath.Base(localFilePath))

err := os.Remove(localFilePath)
require.Nil(t, err)

updateFileContentWithRandomlyGeneratedData(t, allocationID, remotepath, filepath.Base(localFilePath), int64(fileSize/2))

output, err := getFileMeta(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"json": "",
"remotepath": remotepath + filepath.Base(localFilePath),
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)

var meta climodel.FileMetaResult
err = json.NewDecoder(strings.NewReader(output[0])).Decode(&meta)
require.Nil(t, err, strings.Join(output, "\n"))
require.Equal(t, fileSize, meta.ActualFileSize, "file size should be same as uploaded")

newFileSize := int64(1.5 * MB)
updateFileContentWithRandomlyGeneratedData(t, allocationID, remotepath,filepath.Base(localFilePath), int64(newFileSize))

// rollback allocation

output, err = rollbackAllocation(t, escapedTestName(t), configPath, createParams(map[string]interface{}{
"allocation": allocationID,
}))
t.Log(strings.Join(output, "\n"))
require.NoError(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)

output, err = downloadFile(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remotepath + filepath.Base(localFilePath),
"localpath": "tmp/",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 2)

require.Contains(t, output[1], StatusCompletedCB)
require.Contains(t, output[1], filepath.Base(localFilePath))

downloadedFileChecksum := generateChecksum(t, "tmp/"+filepath.Base(localFilePath))

require.Equal(t, localFileChecksum, downloadedFileChecksum)

createAllocationTestTeardown(t, allocationID)
})





t.RunSequentially("rollback allocation after multiple files upload and single file delete should work", func(t *test.SystemTest) {
allocationID := setupAllocationAndReadLock(t, configPath, map[string]interface{}{
"size": 4 * MB,
"tokens": 9,
})

remotepath := "/"
filesize := int64(1 * MB)
localfilepath := "file2.txt"

files := map[string]int64{
"file1.txt": 1 * MB,
"file2.txt": 1 * MB,
"file3.txt": 1 * MB,
}

//var wg sync.WaitGroup
for filepath, fileSize := range files {
//wg.Add(1)
go func(path string, size int64) {
//defer wg.Done()
generateFileAndUpload(t, allocationID, path, size)

}(filepath, fileSize)
}
//wg.Wait()

localFileChecksum := generateChecksum(t, filepath.Base(localfilepath))

for filepath:= range files {
err := os.Remove(filepath)
require.Nil(t, err)
}

output, err := getFileMeta(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"json": "",
"remotepath": remotepath + filepath.Base(localfilepath),
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)

var meta climodel.FileMetaResult
err = json.NewDecoder(strings.NewReader(output[0])).Decode(&meta)
require.Nil(t, err, strings.Join(output, "\n"))
require.Equal(t, filesize, meta.ActualFileSize, "file size should be same as uploaded")

output, err = deleteFile(t, escapedTestName(t), createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remotepath + filepath.Base(localfilepath),
}), true)


require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, fmt.Sprintf("%s deleted", "file2.txt"), output[0])

// rollback allocation

output, err = rollbackAllocation(t, escapedTestName(t), configPath, createParams(map[string]interface{}{
"allocation": allocationID,
}))
t.Log(strings.Join(output, "\n"))
require.NoError(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)

output, err = downloadFile(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remotepath + filepath.Base(localfilepath),
"localpath": "tmp/",
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 2)

require.Contains(t, output[1], StatusCompletedCB)
require.Contains(t, output[1], filepath.Base(localfilepath))

downloadedFileChecksum := generateChecksum(t, "tmp/"+filepath.Base(localfilepath))

require.Equal(t, localFileChecksum, downloadedFileChecksum)

createAllocationTestTeardown(t, allocationID)
})


t.RunSequentially("rollback allocation in the middle of updating a large file should work", func(t *test.SystemTest) {
allocationID := setupAllocationAndReadLock(t, configPath, map[string]interface{}{
"size": 1 * GB,
"tokens": 10,
})

filesize := int64(0.5 * GB)
remotepath := "/"
localFilePath := ""
doneUploading := make(chan bool)
go func() {
localFilePath = generateFileAndUpload(t, allocationID, remotepath, filesize)
doneUploading <- true
}()

time.Sleep(5 * time.Second)

// Ensure the upload was interrupted
select {
case <-doneUploading:
t.Error("Upload completed unexpectedly")
case <-time.After(10 * time.Second):

// rollback allocation

output, err := rollbackAllocation(t, escapedTestName(t), configPath, createParams(map[string]interface{}{
"allocation": allocationID,
}))
t.Log(strings.Join(output, "\n"))
require.NoError(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)
}

output, err := listFilesInAllocation(t, configPath, createParams(map[string]interface{}{
"allocation": allocationID,
"remotepath": remotepath,
"json": "",
}), true)
require.Nil(t, err, "List files failed", err, strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, "null", output[0], strings.Join(output, "\n"))


err = os.Remove(localFilePath)
require.Nil(t, err)

createAllocationTestTeardown(t, allocationID)
})

}

func rollbackAllocation(t *test.SystemTest, wallet, cliConfigFilename, params string) ([]string, error) {
Expand Down
Loading
Loading