github.com/jstaf/onedriver@v0.14.2-0.20240420231225-f07678f9e6ef/fs/upload_session_test.go (about) 1 package fs 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "os" 7 "os/exec" 8 "path/filepath" 9 "testing" 10 "time" 11 12 "github.com/jstaf/onedriver/fs/graph" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 // TestUploadSession verifies that the basic functionality of uploads works correctly. 18 func TestUploadSession(t *testing.T) { 19 t.Parallel() 20 testDir, err := fs.GetPath("/onedriver_tests", auth) 21 require.NoError(t, err) 22 23 inode := NewInode("uploadSessionSmall.txt", 0644, testDir) 24 data := []byte("our super special data") 25 inode.setContent(fs, data) 26 mtime := inode.ModTime() 27 28 session, err := NewUploadSession(inode, &data) 29 require.NoError(t, err) 30 err = session.Upload(auth) 31 require.NoError(t, err) 32 if isLocalID(session.ID) { 33 t.Fatalf("The session's ID was somehow still local following an upload: %s\n", 34 session.ID) 35 } 36 if sessionMtime := uint64(session.ModTime.Unix()); sessionMtime != mtime { 37 t.Errorf("session modtime changed - before: %d - after: %d", mtime, sessionMtime) 38 } 39 40 resp, _, err := graph.GetItemContent(session.ID, auth) 41 require.NoError(t, err) 42 if !bytes.Equal(data, resp) { 43 t.Fatalf("Data mismatch. Original content: %s\nRemote content: %s\n", data, resp) 44 } 45 46 // item now has a new id following the upload. We just change the ID here 47 // because thats part of the UploadManager functionality and gets tested elsewhere. 48 inode.DriveItem.ID = session.ID 49 50 // we overwrite and upload again to test uploading with the new remote id 51 newData := []byte("new data is extra long so it covers the old one completely") 52 inode.setContent(fs, newData) 53 54 session2, err := NewUploadSession(inode, &newData) 55 require.NoError(t, err) 56 err = session2.Upload(auth) 57 require.NoError(t, err) 58 59 resp, _, err = graph.GetItemContent(session.ID, auth) 60 require.NoError(t, err) 61 if !bytes.Equal(newData, resp) { 62 t.Fatalf("Data mismatch. Original content: %s\nRemote content: %s\n", newData, resp) 63 } 64 } 65 66 // TestUploadSessionSmallFS verifies is the same test as TestUploadSessionSmall, but uses 67 // the filesystem itself to perform the uploads instead of testing the internal upload 68 // functions directly 69 func TestUploadSessionSmallFS(t *testing.T) { 70 t.Parallel() 71 data := []byte("super special data for upload test 2") 72 err := ioutil.WriteFile(filepath.Join(TestDir, "uploadSessionSmallFS.txt"), data, 0644) 73 require.NoError(t, err) 74 75 time.Sleep(10 * time.Second) 76 item, err := graph.GetItemPath("/onedriver_tests/uploadSessionSmallFS.txt", auth) 77 if err != nil || item == nil { 78 t.Fatal(err) 79 } 80 81 content, _, err := graph.GetItemContent(item.ID, auth) 82 require.NoError(t, err) 83 if !bytes.Equal(content, data) { 84 t.Fatalf("Data mismatch. Original content: %s\nRemote content: %s\n", data, content) 85 } 86 87 // upload it again to ensure uploads with an existing remote id succeed 88 data = []byte("more super special data") 89 err = ioutil.WriteFile(filepath.Join(TestDir, "uploadSessionSmallFS.txt"), data, 0644) 90 require.NoError(t, err) 91 92 time.Sleep(15 * time.Second) 93 item2, err := graph.GetItemPath("/onedriver_tests/uploadSessionSmallFS.txt", auth) 94 if err != nil || item == nil { 95 t.Fatal(err) 96 } 97 98 content, _, err = graph.GetItemContent(item2.ID, auth) 99 require.NoError(t, err) 100 if !bytes.Equal(content, data) { 101 t.Fatalf("Data mismatch. Original content: %s\nRemote content: %s\n", data, content) 102 } 103 } 104 105 // copy large file inside onedrive mount, then verify that we can still 106 // access selected lines 107 func TestUploadSessionLargeFS(t *testing.T) { 108 t.Parallel() 109 fname := filepath.Join(TestDir, "dmel.fa") 110 require.NoError(t, exec.Command("cp", "dmel.fa", fname).Run()) 111 112 contents, err := ioutil.ReadFile(fname) 113 require.NoError(t, err) 114 115 header := ">X dna:chromosome chromosome:BDGP6.22:X:1:23542271:1 REF" 116 if string(contents[:len(header)]) != header { 117 t.Fatalf("Could not read FASTA header. Wanted \"%s\", got \"%s\"\n", 118 header, string(contents[:len(header)])) 119 } 120 121 final := "AAATAAAATAC\n" // makes yucky test output, but is the final line 122 match := string(contents[len(contents)-len(final):]) 123 if match != final { 124 t.Fatalf("Could not read final line of FASTA. Wanted \"%s\", got \"%s\"\n", 125 final, match) 126 } 127 128 st, _ := os.Stat(fname) 129 if st.Size() == 0 { 130 t.Fatal("File size cannot be 0.") 131 } 132 133 // poll endpoint to make sure it has a size greater than 0 134 size := uint64(len(contents)) 135 var item *graph.DriveItem 136 assert.Eventually(t, func() bool { 137 item, _ = graph.GetItemPath("/onedriver_tests/dmel.fa", auth) 138 inode := NewInodeDriveItem(item) 139 return item != nil && inode.Size() == size 140 }, 120*time.Second, time.Second, "Upload session did not complete successfully!") 141 142 // test multipart downloads as a bonus part of the test 143 downloaded, _, err := graph.GetItemContent(item.ID, auth) 144 assert.NoError(t, err) 145 assert.Equal(t, graph.QuickXORHash(&contents), graph.QuickXORHash(&downloaded), 146 "Downloaded content did not match original content.") 147 }