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

Draft
wants to merge 7 commits into
base: sprint-1.14
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
return localfile
}

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

Check failure on line 177 in tests/cli_tests/zboxcli_common_user_functions_test.go

View workflow job for this annotation

GitHub Actions / lint

paramTypeCombine: func(t *test.SystemTest, allocationID, remotepath string, filename string, size int64) string could be replaced with func(t *test.SystemTest, allocationID, remotepath, filename string, size int64) string (gocritic)
return updateFileContentWithRandomlyGeneratedDataWithWallet(t, escapedTestName(t), allocationID, remotepath, filename, size)
}

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

Check failure on line 181 in tests/cli_tests/zboxcli_common_user_functions_test.go

View workflow job for this annotation

GitHub Actions / lint

paramTypeCombine: func(t *test.SystemTest, walletName, allocationID, remotepath string, filename string, size int64) string could be replaced with func(t *test.SystemTest, walletName, allocationID, remotepath, filename string, size int64) string (gocritic)

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 @@
return filename
}

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

Check failure on line 1037 in tests/cli_tests/zboxcli_file_upload_test.go

View workflow job for this annotation

GitHub Actions / lint

paramTypeCombine: func(t *test.SystemTest, allocationID, remotepath string, filename string, size int64) string could be replaced with func(t *test.SystemTest, allocationID, remotepath, filename string, size int64) string (gocritic)
return generateFileContentAndUploadForWallet(t, escapedTestName(t), allocationID, remotepath, filename, size)
}

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

Check failure on line 1041 in tests/cli_tests/zboxcli_file_upload_test.go

View workflow job for this annotation

GitHub Actions / lint

paramTypeCombine: func(t *test.SystemTest, wallet, allocationID, remotepath string, filename string, size int64) string could be replaced with func(t *test.SystemTest, wallet, allocationID, remotepath, filename string, size int64) string (gocritic)

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 @@
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,
})

Check failure on line 361 in tests/cli_tests/zboxcli_rollback_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
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))

Check failure on line 387 in tests/cli_tests/zboxcli_rollback_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/golangci/golangci-lint (goimports)

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

Check failure on line 443 in tests/cli_tests/zboxcli_rollback_test.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
for filepath, fileSize := range files {
//wg.Add(1)

Check failure on line 445 in tests/cli_tests/zboxcli_rollback_test.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
go func(path string, size int64) {
//defer wg.Done()

Check failure on line 447 in tests/cli_tests/zboxcli_rollback_test.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
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