From 5fa2a66d8c9427e8b259e92b53edbcfef49844ca Mon Sep 17 00:00:00 2001 From: Manohar Reddy Date: Mon, 11 Sep 2023 14:55:01 +0200 Subject: [PATCH] Deprecate non multi-operation functions (#1214) * remove CreateDir, Rename, Copy, Move * use docker build wasm * use @v4 version of git action * Revert "use @v4 version of git action" This reverts commit fbb93f25a6ca63d6e205eac418d031bba428eb29. --- .github/workflows/sdk-release.yml | 2 +- mobilesdk/zbox/allocation.go | 32 +++- mobilesdk/zbox/storage.go | 35 +++- wasmsdk/README.md | 14 +- wasmsdk/blobber.go | 147 ++++------------- wasmsdk/demo/index.html | 2 +- wasmsdk/proxy.go | 5 - zboxcore/sdk/allocation.go | 171 -------------------- zboxcore/sdk/allocation_file_copy_test.go | 142 ---------------- zboxcore/sdk/allocation_file_move_test.go | 129 --------------- zboxcore/sdk/allocation_file_rename_test.go | 139 ---------------- 11 files changed, 92 insertions(+), 726 deletions(-) delete mode 100644 zboxcore/sdk/allocation_file_copy_test.go delete mode 100644 zboxcore/sdk/allocation_file_move_test.go delete mode 100644 zboxcore/sdk/allocation_file_rename_test.go diff --git a/.github/workflows/sdk-release.yml b/.github/workflows/sdk-release.yml index a8b5ee337..a52bad2ed 100644 --- a/.github/workflows/sdk-release.yml +++ b/.github/workflows/sdk-release.yml @@ -71,7 +71,7 @@ jobs: fi - name: Build - run: make wasm-build + run: docker run --rm -v $PWD:/gosdk -w /gosdk golang:1.18 make wasm-build - name: Upload binaries to release uses: svenstaro/upload-release-action@v2 diff --git a/mobilesdk/zbox/allocation.go b/mobilesdk/zbox/allocation.go index 51e1c6153..4114915e5 100644 --- a/mobilesdk/zbox/allocation.go +++ b/mobilesdk/zbox/allocation.go @@ -6,6 +6,7 @@ import ( "fmt" "time" + "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/zboxcore/blockchain" "github.com/0chain/gosdk/zboxcore/sdk" @@ -200,7 +201,13 @@ func (a *Allocation) RenameObject(remotePath string, destName string) error { if a == nil || a.sdkAllocation == nil { return ErrInvalidAllocation } - return a.sdkAllocation.RenameObject(remotePath, destName) + return a.sdkAllocation.DoMultiOperation([]sdk.OperationRequest{ + { + OperationType: constants.FileOperationRename, + RemotePath: remotePath, + DestName: destName, + }, + }) } // GetStatistics - get allocation stats @@ -374,7 +381,13 @@ func (a *Allocation) CopyObject(path string, destPath string) error { if a == nil || a.sdkAllocation == nil { return ErrInvalidAllocation } - return a.sdkAllocation.CopyObject(path, destPath) + return a.sdkAllocation.DoMultiOperation([]sdk.OperationRequest{ + { + OperationType: constants.FileOperationRename, + RemotePath: path, + DestPath: destPath, + }, + }) } // MoveObject - move object from path to dest @@ -382,7 +395,13 @@ func (a *Allocation) MoveObject(path string, destPath string) error { if a == nil || a.sdkAllocation == nil { return ErrInvalidAllocation } - return a.sdkAllocation.MoveObject(path, destPath) + return a.sdkAllocation.DoMultiOperation([]sdk.OperationRequest{ + { + OperationType: constants.FileOperationRename, + RemotePath: path, + DestPath: destPath, + }, + }) } // GetMinWriteRead - getting back cost for allocation @@ -455,7 +474,12 @@ func (a *Allocation) GetFirstSegment(localPath, remotePath, tmpPath string, dela } func (a *Allocation) CreateDir(dirName string) error { - return a.sdkAllocation.CreateDir(dirName) + return a.sdkAllocation.DoMultiOperation([]sdk.OperationRequest{ + { + OperationType: constants.FileOperationCreateDir, + RemotePath: dirName, + }, + }) } var currentPlayback StreamingImpl diff --git a/mobilesdk/zbox/storage.go b/mobilesdk/zbox/storage.go index 3e88bb6ef..da702d1da 100644 --- a/mobilesdk/zbox/storage.go +++ b/mobilesdk/zbox/storage.go @@ -6,12 +6,11 @@ import ( "strings" "time" + "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/sdk" ) -const SPACE string = " " - type fileResp struct { sdk.FileInfo Name string `json:"name"` @@ -468,7 +467,13 @@ func RenameObject(allocationID, remotePath string, destName string) error { if err != nil { return err } - return a.RenameObject(remotePath, destName) + return a.DoMultiOperation([]sdk.OperationRequest{ + { + OperationType: constants.FileOperationRename, + RemotePath: remotePath, + DestName: destName, + }, + }) } // GetStatistics - get allocation stats @@ -747,7 +752,13 @@ func CopyObject(allocationID, path string, destPath string) error { if err != nil { return err } - return a.CopyObject(path, destPath) + return a.DoMultiOperation([]sdk.OperationRequest{ + { + OperationType: constants.FileOperationCopy, + RemotePath: path, + DestPath: destPath, + }, + }) } // MoveObject - move object from path to dest @@ -760,7 +771,14 @@ func MoveObject(allocationID, path string, destPath string) error { if err != nil { return err } - return a.MoveObject(path, destPath) + return a.DoMultiOperation([]sdk.OperationRequest{ + { + OperationType: constants.FileOperationMove, + RemotePath: path, + DestPath: destPath, + }, + }) + } // CreateDir create empty directoy on remote blobbers @@ -773,7 +791,12 @@ func CreateDir(allocationID, dirName string) error { if err != nil { return err } - return a.CreateDir(dirName) + return a.DoMultiOperation([]sdk.OperationRequest{ + { + OperationType: constants.FileOperationCreateDir, + RemotePath: dirName, + }, + }) } // RevokeShare revoke authTicket diff --git a/wasmsdk/README.md b/wasmsdk/README.md index d94a186b5..a1dc459c2 100644 --- a/wasmsdk/README.md +++ b/wasmsdk/README.md @@ -18,18 +18,6 @@ set bls.SecretKey on runtime env(browser,nodejs...etc), and call `zcn.sdk.setWal > N/A -### zcn.bulkUpload - -bulk upload files. it will wrap options, and call `zcn.sdk.bulkUpload` to process upload - -**Input**: - -> bulkOptions: [ { allocationId:string,remotePath:string,file:FileReader, thumbnailBytes:[]byte, encrypt:bool,isUpdate:bool,isRepair:bool,numBlocks:int,callback:function(totalBytes, completedBytes, error) } ] - -**Output**: - -> [ {remotePath:"/d.png", success:true,error:""} ] - ## ZCN methods ### zcn.sdk.init @@ -512,7 +500,7 @@ upload file > {commandSuccess:bool, error:string} -### zcn.sdk.bulkUpload +### zcn.sdk.multiUpload bulk upload files with json options diff --git a/wasmsdk/blobber.go b/wasmsdk/blobber.go index 579d946ae..bc951826c 100644 --- a/wasmsdk/blobber.go +++ b/wasmsdk/blobber.go @@ -14,9 +14,11 @@ import ( "syscall/js" "time" + "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/pathutil" "github.com/0chain/gosdk/core/sys" + "github.com/0chain/gosdk/core/transaction" "github.com/0chain/gosdk/wasmsdk/jsbridge" "github.com/0chain/gosdk/zboxcore/fileref" @@ -52,7 +54,12 @@ func createDir(allocationID, remotePath string) error { return err } - return allocationObj.CreateDir(remotePath) + return allocationObj.DoMultiOperation([]sdk.OperationRequest{ + { + OperationType: constants.FileOperationCreateDir, + RemotePath: remotePath, + }, + }) } // getFileStats get file stats from blobbers @@ -152,7 +159,14 @@ func Rename(allocationID, remotePath, destName string) (*FileCommandResponse, er return nil, err } - err = allocationObj.RenameObject(remotePath, destName) + err = allocationObj.DoMultiOperation([]sdk.OperationRequest{ + { + OperationType: constants.FileOperationRename, + RemotePath: remotePath, + DestName: destName, + }, + }) + if err != nil { PrintError(err.Error()) return nil, err @@ -187,7 +201,14 @@ func Copy(allocationID, remotePath, destPath string) (*FileCommandResponse, erro return nil, err } - err = allocationObj.CopyObject(remotePath, destPath) + err = allocationObj.DoMultiOperation([]sdk.OperationRequest{ + { + OperationType: constants.FileOperationCopy, + RemotePath: remotePath, + DestPath: destPath, + }, + }) + if err != nil { PrintError(err.Error()) return nil, err @@ -222,7 +243,14 @@ func Move(allocationID, remotePath, destPath string) (*FileCommandResponse, erro return nil, err } - err = allocationObj.MoveObject(remotePath, destPath) + err = allocationObj.DoMultiOperation([]sdk.OperationRequest{ + { + OperationType: constants.FileOperationMove, + RemotePath: remotePath, + DestPath: destPath, + }, + }) + if err != nil { PrintError(err.Error()) return nil, err @@ -305,71 +333,6 @@ func Share(allocationID, remotePath, clientID, encryptionPublicKey string, expir } -// download file -func download( - allocationID, remotePath, authTicket, lookupHash string, - downloadThumbnailOnly bool, numBlocks int, callbackFuncName string, isFinal bool) (*DownloadCommandResponse, error) { - - wg := &sync.WaitGroup{} - statusBar := &StatusBar{wg: wg} - if callbackFuncName != "" { - callback := js.Global().Get(callbackFuncName) - statusBar.callback = func(totalBytes, completedBytes int, filename, objURL, err string) { - callback.Invoke(totalBytes, completedBytes, filename, objURL, err) - } - } - wg.Add(1) - - if len(remotePath) == 0 && len(authTicket) == 0 { - return nil, RequiredArg("remotePath/authTicket") - } - - fileName := strings.Replace(path.Base(remotePath), "/", "-", -1) - localPath := allocationID + "_" + fileName - var ( - err error - downloader sdk.Downloader - ) - - fs, _ := sys.Files.Open(localPath) - mf, _ := fs.(*sys.MemFile) - - downloader, err = sdk.CreateDownloader(allocationID, localPath, remotePath, - sdk.WithAuthticket(authTicket, lookupHash), - sdk.WithOnlyThumbnail(downloadThumbnailOnly), - sdk.WithBlocks(0, 0, numBlocks), - sdk.WithFileHandler(mf)) - - if err != nil { - PrintError(err.Error()) - return nil, err - } - - defer sys.Files.Remove(localPath) //nolint - - err = downloader.Start(statusBar, isFinal) - - if err == nil { - wg.Wait() - } else { - PrintError("Download failed.", err.Error()) - return nil, err - } - if !statusBar.success { - return nil, errors.New("Download failed: unknown error") - } - - resp := &DownloadCommandResponse{ - CommandSuccess: true, - FileName: downloader.GetFileName(), - } - - resp.Url = CreateObjectURL(mf.Buffer.Bytes(), "application/octet-stream") - - return resp, nil - -} - // MultiOperation - do copy, move, delete and createdir operation together // ## Inputs // - allocationID @@ -548,52 +511,6 @@ func MultiOperation(allocationID string, jsonMultiUploadOptions string) error { return allocationObj.DoMultiOperation(operations) } -func bulkUpload(jsonBulkUploadOptions string) ([]BulkUploadResult, error) { - var options []BulkUploadOption - err := json.Unmarshal([]byte(jsonBulkUploadOptions), &options) - if err != nil { - return nil, err - } - - n := len(options) - wait := make(chan BulkUploadResult, 1) - - for _, option := range options { - go func(o BulkUploadOption) { - result := BulkUploadResult{ - RemotePath: o.RemotePath, - } - defer func() { wait <- result }() - - ok, err := uploadWithJsFuncs(o.AllocationID, o.RemotePath, - o.ReadChunkFuncName, - o.FileSize, - o.ThumbnailBytes.Buffer, - o.Webstreaming, - o.Encrypt, - o.IsUpdate, - o.IsRepair, - o.NumBlocks, - o.CallbackFuncName) - result.Success = ok - if err != nil { - result.Error = err.Error() - result.Success = false - } - - }(option) - - } - - results := make([]BulkUploadResult, 0, n) - for i := 0; i < n; i++ { - result := <-wait - results = append(results, result) - } - - return results, nil -} - func multiUpload(jsonBulkUploadOptions string) (MultiUploadResult, error) { var options []BulkUploadOption result := MultiUploadResult{} diff --git a/wasmsdk/demo/index.html b/wasmsdk/demo/index.html index 6b5867813..58184593a 100644 --- a/wasmsdk/demo/index.html +++ b/wasmsdk/demo/index.html @@ -443,7 +443,7 @@

please download zcn.wasm from https://github.com/0chain/gosdk/releases/lates } }) } - const results = await goWasm.bulkUpload(objects) + const results = await goWasm.multiUpload(objects) console.log(JSON.stringify(results)) } }) diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index acd6cb950..76648d4b3 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -163,14 +163,9 @@ func main() { //blobber "delete": Delete, - "rename": Rename, - "copy": Copy, - "move": Move, "share": Share, - "download": download, "multiDownload": multiDownload, "upload": upload, - "bulkUpload": bulkUpload, "multiUpload": multiUpload, "multiOperation": MultiOperation, "listObjects": listObjects, diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 0328bbe9a..7696357c5 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -13,7 +13,6 @@ import ( "net/http" "net/url" "os" - "path" "path/filepath" "strconv" "strings" @@ -362,45 +361,6 @@ func (a *Allocation) UploadFile(workdir, localpath string, remotepath string, return a.StartChunkedUpload(workdir, localpath, remotepath, status, false, false, "", false, false) } -func (a *Allocation) CreateDir(remotePath string) error { - if !a.isInitialized() { - return notInitialized - } - - if remotePath == "" { - return errors.New("invalid_name", "Invalid name for dir") - } - - if !path.IsAbs(remotePath) { - return errors.New("invalid_path", "Path is not absolute") - } - - remotePath = zboxutil.RemoteClean(remotePath) - timestamp := int64(common.Now()) - req := DirRequest{ - allocationObj: a, - allocationID: a.ID, - allocationTx: a.Tx, - blobbers: a.Blobbers, - mu: &sync.Mutex{}, - dirMask: zboxutil.NewUint128(1).Lsh(uint64(len(a.Blobbers))).Sub64(1), - connectionID: zboxutil.NewConnectionId(), - remotePath: remotePath, - wg: &sync.WaitGroup{}, - timestamp: timestamp, - alreadyExists: map[uint64]bool{}, - Consensus: Consensus{ - RWMutex: &sync.RWMutex{}, - consensusThresh: a.consensusThreshold, - fullconsensus: a.fullconsensus, - }, - } - req.ctx, req.ctxCncl = context.WithCancel(a.ctx) - - err := req.ProcessDir(a) - return err -} - func (a *Allocation) RepairFile(file sys.File, remotepath string, status StatusCallback, mask zboxutil.Uint128, ref *fileref.FileRef) error { @@ -1634,137 +1594,6 @@ func (a *Allocation) deleteFile(path string, threshConsensus, fullConsensus int, return err } -func (a *Allocation) RenameObject(path string, destName string) error { - if !a.isInitialized() { - return notInitialized - } - - if !a.CanRename() { - return constants.ErrFileOptionNotPermitted - } - - if path == "" { - return errors.New("invalid_path", "Invalid path for the list") - } - - if path == "/" { - return errors.New("invalid_operation", "cannot rename root path") - } - - path = zboxutil.RemoteClean(path) - isabs := zboxutil.IsRemoteAbs(path) - if !isabs { - return errors.New("invalid_path", "Path should be valid and absolute") - } - - err := ValidateRemoteFileName(destName) - if err != nil { - return err - } - - req := &RenameRequest{consensus: Consensus{RWMutex: &sync.RWMutex{}}} - req.allocationObj = a - req.blobbers = a.Blobbers - req.allocationID = a.ID - req.allocationTx = a.Tx - req.newName = destName - req.consensus.fullconsensus = a.fullconsensus - req.consensus.consensusThresh = a.consensusThreshold - req.ctx, req.ctxCncl = context.WithCancel(a.ctx) - req.remotefilepath = path - req.renameMask = zboxutil.NewUint128(1).Lsh(uint64(len(a.Blobbers))).Sub64(1) - req.maskMU = &sync.Mutex{} - req.connectionID = zboxutil.NewConnectionId() - req.timestamp = int64(common.Now()) - return req.ProcessRename() -} - -func (a *Allocation) MoveObject(srcPath string, destPath string) error { - if !a.isInitialized() { - return notInitialized - } - - if !a.CanMove() { - return constants.ErrFileOptionNotPermitted - } - - if len(srcPath) == 0 || len(destPath) == 0 { - return errors.New("invalid_path", "Invalid path for copy") - } - srcPath = zboxutil.RemoteClean(srcPath) - isabs := zboxutil.IsRemoteAbs(srcPath) - if !isabs { - return errors.New("invalid_path", "Path should be valid and absolute") - } - - err := ValidateRemoteFileName(destPath) - if err != nil { - return err - } - - req := &MoveRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} - req.allocationObj = a - req.blobbers = a.Blobbers - req.allocationID = a.ID - req.allocationTx = a.Tx - if destPath != "/" { - destPath = strings.TrimSuffix(destPath, "/") - } - req.destPath = destPath - req.fullconsensus = a.fullconsensus - req.consensusThresh = a.consensusThreshold - req.ctx, req.ctxCncl = context.WithCancel(a.ctx) - req.remotefilepath = srcPath - req.moveMask = zboxutil.NewUint128(1).Lsh(uint64(len(a.Blobbers))).Sub64(1) - req.maskMU = &sync.Mutex{} - req.connectionID = zboxutil.NewConnectionId() - req.timestamp = int64(common.Now()) - return req.ProcessMove() -} - -func (a *Allocation) CopyObject(path string, destPath string) error { - if !a.isInitialized() { - return notInitialized - } - - if !a.CanCopy() { - return constants.ErrFileOptionNotPermitted - } - - if len(path) == 0 || len(destPath) == 0 { - return errors.New("invalid_path", "Invalid path for copy") - } - path = zboxutil.RemoteClean(path) - isabs := zboxutil.IsRemoteAbs(path) - if !isabs { - return errors.New("invalid_path", "Path should be valid and absolute") - } - - err := ValidateRemoteFileName(destPath) - if err != nil { - return err - } - - req := &CopyRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} - req.allocationObj = a - req.blobbers = a.Blobbers - req.allocationID = a.ID - req.allocationTx = a.Tx - if destPath != "/" { - destPath = strings.TrimSuffix(destPath, "/") - } - req.destPath = destPath - req.fullconsensus = a.fullconsensus - req.consensusThresh = a.consensusThreshold - req.ctx, req.ctxCncl = context.WithCancel(a.ctx) - req.remotefilepath = path - req.copyMask = zboxutil.NewUint128(1).Lsh(uint64(len(a.Blobbers))).Sub64(1) - req.maskMU = &sync.Mutex{} - req.connectionID = zboxutil.NewConnectionId() - req.timestamp = int64(common.Now()) - return req.ProcessCopy() -} - func (a *Allocation) GetAuthTicketForShare( path, filename, referenceType, refereeClientID string) (string, error) { diff --git a/zboxcore/sdk/allocation_file_copy_test.go b/zboxcore/sdk/allocation_file_copy_test.go deleted file mode 100644 index 317e4e580..000000000 --- a/zboxcore/sdk/allocation_file_copy_test.go +++ /dev/null @@ -1,142 +0,0 @@ -package sdk - -import ( - "encoding/json" - "net/http" - "strconv" - "testing" - "time" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/resty" - "github.com/0chain/gosdk/core/zcncrypto" - "github.com/0chain/gosdk/zboxcore/blockchain" - zclient "github.com/0chain/gosdk/zboxcore/client" - "github.com/0chain/gosdk/zboxcore/fileref" - "github.com/0chain/gosdk/zboxcore/mocks" - "github.com/0chain/gosdk/zboxcore/zboxutil" - "github.com/stretchr/testify/require" -) - -func TestAllocation_CopyObject(t *testing.T) { - const ( - mockType = "f" - ) - - rawClient := zboxutil.Client - createClient := resty.CreateClient - - var mockClient = mocks.HttpClient{} - zboxutil.Client = &mockClient - - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ - ClientID: mockClientId, - ClientKey: mockClientKey, - } - - zboxutil.Client = &mockClient - resty.CreateClient = func(t *http.Transport, timeout time.Duration) resty.Client { - return &mockClient - } - - defer func() { - zboxutil.Client = rawClient - resty.CreateClient = createClient - }() - - type parameters struct { - path string - destPath string - } - tests := []struct { - name string - parameters parameters - setup func(*testing.T, string, *Allocation) (teardown func(*testing.T)) - wantErr bool - errMsg string - }{ - { - name: "Test_Uninitialized_Failed", - setup: func(t *testing.T, testCaseName string, a *Allocation) (teardown func(t *testing.T)) { - a.initialized = false - return func(t *testing.T) { - a.initialized = true - } - }, - wantErr: true, - errMsg: "sdk_not_initialized: Please call InitStorageSDK Init and use GetAllocation to get the allocation object", - }, - { - name: "Test_Wrong_Path_Or_Destination_Path_Failed", - parameters: parameters{ - path: "", - destPath: "", - }, - wantErr: true, - errMsg: "invalid_path: Invalid path for copy", - }, - { - name: "Test_Invalid_Remote_Absolute_Path", - parameters: parameters{ - path: "abc", - destPath: "/d", - }, - wantErr: true, - errMsg: "invalid_path: Path should be valid and absolute", - }, - { - name: "Test_Success", - parameters: parameters{ - path: "/1.txt", - destPath: "/d", - }, - setup: func(t *testing.T, testCaseName string, a *Allocation) (teardown func(t *testing.T)) { - body, err := json.Marshal(&fileref.ReferencePath{ - Meta: map[string]interface{}{ - "type": mockType, - }, - }) - require.NoError(t, err) - setupMockHttpResponse(t, &mockClient, "TestAllocation_CopyObject", testCaseName, a, http.MethodGet, http.StatusOK, body) - setupMockHttpResponse(t, &mockClient, "TestAllocation_CopyObject", testCaseName, a, http.MethodPost, http.StatusOK, []byte("")) - setupMockRollback(a, &mockClient) - setupMockCommitRequest(a) - setupMockWriteLockRequest(a, &mockClient) - return nil - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - require := require.New(t) - a := &Allocation{ - DataShards: numBlobbers, - FileOptions: 63, - } - a.InitAllocation() - - setupMockAllocation(t, a) - - sdkInitialized = true - for i := 0; i < numBlobbers; i++ { - a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ - ID: tt.name + mockBlobberId + strconv.Itoa(i), - Baseurl: "http://TestAllocation_CopyObject" + tt.name + mockBlobberUrl + strconv.Itoa(i), - }) - } - if tt.setup != nil { - if teardown := tt.setup(t, tt.name, a); teardown != nil { - defer teardown(t) - } - } - err := a.CopyObject(tt.parameters.path, tt.parameters.destPath) - require.EqualValues(tt.wantErr, err != nil) - if err != nil { - require.EqualValues(tt.errMsg, errors.Top(err)) - return - } - require.NoErrorf(err, "unexpected error: %v", err) - }) - } -} diff --git a/zboxcore/sdk/allocation_file_move_test.go b/zboxcore/sdk/allocation_file_move_test.go deleted file mode 100644 index e1f437992..000000000 --- a/zboxcore/sdk/allocation_file_move_test.go +++ /dev/null @@ -1,129 +0,0 @@ -package sdk - -import ( - "encoding/json" - "net/http" - "strconv" - "testing" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/zcncrypto" - "github.com/0chain/gosdk/zboxcore/blockchain" - zclient "github.com/0chain/gosdk/zboxcore/client" - "github.com/0chain/gosdk/zboxcore/fileref" - "github.com/0chain/gosdk/zboxcore/mocks" - "github.com/0chain/gosdk/zboxcore/zboxutil" - "github.com/stretchr/testify/require" -) - -func TestAllocation_MoveObject(t *testing.T) { - - const ( - mockType = "f" - ) - - rawClient := zboxutil.Client - - var mockClient = mocks.HttpClient{} - zboxutil.Client = &mockClient - - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ - ClientID: mockClientId, - ClientKey: mockClientKey, - } - - zboxutil.Client = &mockClient - - defer func() { - zboxutil.Client = rawClient - }() - - type parameters struct { - path string - destPath string - } - tests := []struct { - name string - parameters parameters - setup func(*testing.T, string, *Allocation) (teardown func(*testing.T)) - wantErr bool - errMsg string - }{ - { - name: "Test_Cover_Copy_Object", - parameters: parameters{ - path: "/1.txt", - destPath: "/d", - }, - setup: func(t *testing.T, testCaseName string, a *Allocation) (teardown func(t *testing.T)) { - a.initialized = false - return func(t *testing.T) { - a.initialized = true - } - }, - wantErr: true, - errMsg: "sdk_not_initialized: Please call InitStorageSDK Init and use GetAllocation to get the allocation object", - }, - { - name: "Test_Cover_Delete_Object", - parameters: parameters{ - path: "/1.txt", - destPath: "/d", - }, - setup: func(t *testing.T, testCaseName string, a *Allocation) (teardown func(t *testing.T)) { - body, err := json.Marshal(&fileref.ReferencePath{ - Meta: map[string]interface{}{ - "type": mockType, - }, - }) - require.NoError(t, err) - setupMockHttpResponse(t, &mockClient, "TestAllocation_MoveObject", testCaseName, a, http.MethodGet, http.StatusOK, body) - setupMockHttpResponse(t, &mockClient, "TestAllocation_MoveObject", testCaseName, a, http.MethodPost, http.StatusOK, []byte("")) - setupMockHttpResponse(t, &mockClient, "TestAllocation_MoveObject", testCaseName, a, http.MethodPost, http.StatusOK, []byte(`{"status":2}`)) - setupMockHttpResponse(t, &mockClient, "TestAllocation_MoveObject", testCaseName, a, http.MethodPost, http.StatusOK, []byte(`{"status":2}`)) - setupMockHttpResponse(t, &mockClient, "TestAllocation_MoveObject", testCaseName, a, http.MethodGet, http.StatusOK, body) - setupMockHttpResponse(t, &mockClient, "TestAllocation_MoveObject", testCaseName, a, http.MethodDelete, http.StatusOK, []byte("")) - setupMockHttpResponse(t, &mockClient, "TestAllocation_MoveObject", testCaseName, a, http.MethodDelete, http.StatusOK, []byte("")) - setupMockHttpResponse(t, &mockClient, "TestAllocation_MoveObject", testCaseName, a, http.MethodDelete, http.StatusOK, []byte("")) - setupMockCommitRequest(a) - setupMockRollback(a, &mockClient) - return nil - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - require := require.New(t) - a := &Allocation{ - Tx: "TestAllocation_MoveObject", - DataShards: 2, - ParityShards: 2, - } - - setupMockAllocation(t, a) - - for i := 0; i < numBlobbers; i++ { - - path := "/TestAllocation_MoveObject" + tt.name + mockBlobberUrl + strconv.Itoa(i) - - a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ - ID: tt.name + mockBlobberId + strconv.Itoa(i), - Baseurl: path, - }) - } - if tt.setup != nil { - if teardown := tt.setup(t, tt.name, a); teardown != nil { - defer teardown(t) - } - } - err := a.MoveObject(tt.parameters.path, tt.parameters.destPath) - require.EqualValues(tt.wantErr, err != nil) - if err != nil { - require.EqualValues(tt.errMsg, errors.Top(err)) - return - } - require.NoErrorf(err, "unexpected error: %v", err) - }) - } -} diff --git a/zboxcore/sdk/allocation_file_rename_test.go b/zboxcore/sdk/allocation_file_rename_test.go deleted file mode 100644 index 32a9ec05b..000000000 --- a/zboxcore/sdk/allocation_file_rename_test.go +++ /dev/null @@ -1,139 +0,0 @@ -package sdk - -import ( - "encoding/json" - "net/http" - "strconv" - "testing" - "time" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/resty" - "github.com/0chain/gosdk/core/zcncrypto" - "github.com/0chain/gosdk/zboxcore/blockchain" - zclient "github.com/0chain/gosdk/zboxcore/client" - "github.com/0chain/gosdk/zboxcore/fileref" - "github.com/0chain/gosdk/zboxcore/mocks" - "github.com/0chain/gosdk/zboxcore/zboxutil" - "github.com/stretchr/testify/require" -) - -func TestAllocation_RenameObject(t *testing.T) { - const ( - mockType = "f" - ) - - rawClient := zboxutil.Client - createClient := resty.CreateClient - - var mockClient = mocks.HttpClient{} - zboxutil.Client = &mockClient - - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ - ClientID: mockClientId, - ClientKey: mockClientKey, - } - - zboxutil.Client = &mockClient - resty.CreateClient = func(t *http.Transport, timeout time.Duration) resty.Client { - return &mockClient - } - - defer func() { - zboxutil.Client = rawClient - resty.CreateClient = createClient - }() - type parameters struct { - path string - destName string - } - tests := []struct { - name string - parameters parameters - setup func(*testing.T, string, *Allocation) (teardown func(*testing.T)) - wantErr bool - errMsg string - }{ - { - name: "Test_Uninitialized_Failed", - setup: func(t *testing.T, testCaseName string, a *Allocation) (teardown func(t *testing.T)) { - a.initialized = false - return func(t *testing.T) { - a.initialized = true - } - }, - wantErr: true, - errMsg: "sdk_not_initialized: Please call InitStorageSDK Init and use GetAllocation to get the allocation object", - }, - { - name: "Test_Wrong_Path_Or_Destination_Path_Failed", - parameters: parameters{ - path: "", - destName: "", - }, - wantErr: true, - errMsg: "invalid_path: Invalid path for the list", - }, - { - name: "Test_Invalid_Remote_Absolute_Path", - parameters: parameters{ - path: "abc", - destName: "/2.txt", - }, - wantErr: true, - errMsg: "invalid_path: Path should be valid and absolute", - }, - { - name: "Test_Success", - parameters: parameters{ - path: "/1.txt", - destName: "/2.txt", - }, - setup: func(t *testing.T, testCaseName string, a *Allocation) (teardown func(t *testing.T)) { - body, err := json.Marshal(&fileref.ReferencePath{ - Meta: map[string]interface{}{ - "type": mockType, - }, - }) - require.NoError(t, err) - setupMockHttpResponse(t, &mockClient, "TestAllocation_RenameObject", testCaseName, a, http.MethodGet, http.StatusOK, body) - setupMockHttpResponse(t, &mockClient, "TestAllocation_RenameObject", testCaseName, a, http.MethodPost, http.StatusOK, []byte("")) - setupMockCommitRequest(a) - setupMockRollback(a, &mockClient) - setupMockWriteLockRequest(a, &mockClient) - return nil - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - require := require.New(t) - a := &Allocation{ - DataShards: 2, - ParityShards: 2, - FileOptions: 63, - } - a.InitAllocation() - sdkInitialized = true - for i := 0; i < numBlobbers; i++ { - a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ - ID: tt.name + mockBlobberId + strconv.Itoa(i), - Baseurl: "http://TestAllocation_RenameObject" + tt.name + mockBlobberUrl + strconv.Itoa(i), - }) - } - if tt.setup != nil { - if teardown := tt.setup(t, tt.name, a); teardown != nil { - defer teardown(t) - } - } - err := a.RenameObject(tt.parameters.path, tt.parameters.destName) - require.EqualValues(tt.wantErr, err != nil) - if err != nil { - require.EqualValues(tt.errMsg, errors.Top(err)) - return - } - require.NoErrorf(err, "unexpected error: %v", err) - }) - } -}