github.com/jstaf/onedriver@v0.14.2-0.20240420231225-f07678f9e6ef/fs/offline/offline_test.go (about) 1 // This package exists purely for the convenience of easily running tests which 2 // test the offline functionality of the graph package. 3 // `unshare -nr` is used to deny network access, and then the tests are run using 4 // cached data from the tests in the graph package. 5 package offline 6 7 import ( 8 "os" 9 "path/filepath" 10 "testing" 11 12 "github.com/jstaf/onedriver/fs" 13 "github.com/stretchr/testify/require" 14 ) 15 16 // We should see more than zero items when we run ls. 17 func TestOfflineReaddir(t *testing.T) { 18 t.Parallel() 19 files, err := os.ReadDir(TestDir) 20 if err != nil { 21 t.Fatal(err) 22 } 23 24 if len(files) == 0 { 25 t.Fatal("Expected more than 0 files in the test directory.") 26 } 27 } 28 29 // We should find the file named bagels (from TestEchoWritesToFile) 30 func TestOfflineBagelDetection(t *testing.T) { 31 t.Parallel() 32 files, err := os.ReadDir(TestDir) 33 if err != nil { 34 t.Fatal(err) 35 } 36 37 found := false 38 allFiles := make([]string, 0) 39 for _, f := range files { 40 allFiles = append(allFiles, f.Name()) 41 42 if f.Name() == "bagels" { 43 found = true 44 if f.IsDir() { 45 t.Fatal("\"bagels\" should be an ordinary file, not a directory") 46 } 47 info, _ := f.Info() 48 octal := fs.Octal(uint32(info.Mode().Perm())) 49 if octal[0] != '6' || int(octal[1])-4 < 0 || octal[2] != '4' { 50 // middle bit just needs to be higher than 4 51 // for compatibility with 022 / 002 umasks on different distros 52 t.Fatalf("\"bagels\" permissions bits wrong, got %s, expected 644", octal) 53 } 54 break 55 } 56 } 57 if !found { 58 t.Error("\"bagels\" not found! Expected file not present.") 59 t.Errorf("Got: %+v", allFiles) 60 } 61 } 62 63 // Does the contents of the bagels file match what it should? 64 // (File contents generated by TestEchoWritesToFile in previous tests.) 65 func TestOfflineBagelContents(t *testing.T) { 66 t.Parallel() 67 contents, err := os.ReadFile(filepath.Join(TestDir, "bagels")) 68 require.NoError(t, err) 69 require.Equal(t, []byte("bagels\n"), contents, "Offline file contents did not match.") 70 } 71 72 // Creating a file should fail 73 func TestOfflineFileCreation(t *testing.T) { 74 t.Parallel() 75 require.Error(t, 76 os.WriteFile(filepath.Join(TestDir, "donuts"), []byte("fail me"), 0644), 77 "Writing a file while offline should fail.", 78 ) 79 } 80 81 // Modifying a file offline should fail. 82 func TestOfflineFileModification(t *testing.T) { 83 t.Parallel() 84 require.Error(t, 85 os.WriteFile(filepath.Join(TestDir, "bagels"), []byte("fail me too"), 0644), 86 "Modifying a file while offline should fail.", 87 ) 88 } 89 90 // Deleting a file offline should fail. 91 func TestOfflineFileDeletion(t *testing.T) { 92 t.Parallel() 93 if os.Remove(filepath.Join(TestDir, "write.txt")) == nil { 94 t.Error("Deleting a file while offline should fail.") 95 } 96 if os.Remove(filepath.Join(TestDir, "empty")) == nil { 97 t.Error("Deleting an empty file while offline should fail.") 98 } 99 } 100 101 // Creating a directory offline should fail. 102 func TestOfflineMkdir(t *testing.T) { 103 t.Parallel() 104 if os.Mkdir(filepath.Join(TestDir, "offline_dir"), 0755) == nil { 105 t.Fatal("Creating a directory should have failed offline.") 106 } 107 } 108 109 // Deleting a directory offline should fail. 110 func TestOfflineRmdir(t *testing.T) { 111 t.Parallel() 112 if os.Remove(filepath.Join(TestDir, "folder1")) == nil { 113 t.Fatal("Removing a directory should have failed offline.") 114 } 115 }