You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

write.go 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package vfstest
  2. import (
  3. "os"
  4. "runtime"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. // TestWriteFileNoWrite tests writing a file with no write()'s to it
  11. func TestWriteFileNoWrite(t *testing.T) {
  12. run.skipIfNoFUSE(t)
  13. fd, err := osCreate(run.path("testnowrite"))
  14. require.NoError(t, err)
  15. err = fd.Close()
  16. assert.NoError(t, err)
  17. run.waitForWriters()
  18. run.checkDir(t, "testnowrite 0")
  19. run.rm(t, "testnowrite")
  20. }
  21. // FIXMETestWriteOpenFileInDirListing tests open file in directory listing
  22. func FIXMETestWriteOpenFileInDirListing(t *testing.T) {
  23. run.skipIfNoFUSE(t)
  24. fd, err := osCreate(run.path("testnowrite"))
  25. assert.NoError(t, err)
  26. run.checkDir(t, "testnowrite 0")
  27. err = fd.Close()
  28. assert.NoError(t, err)
  29. run.waitForWriters()
  30. run.rm(t, "testnowrite")
  31. }
  32. // TestWriteFileWrite tests writing a file and reading it back
  33. func TestWriteFileWrite(t *testing.T) {
  34. run.skipIfNoFUSE(t)
  35. run.createFile(t, "testwrite", "data")
  36. run.checkDir(t, "testwrite 4")
  37. contents := run.readFile(t, "testwrite")
  38. assert.Equal(t, "data", contents)
  39. run.rm(t, "testwrite")
  40. }
  41. // TestWriteFileOverwrite tests overwriting a file
  42. func TestWriteFileOverwrite(t *testing.T) {
  43. run.skipIfNoFUSE(t)
  44. run.createFile(t, "testwrite", "data")
  45. run.checkDir(t, "testwrite 4")
  46. run.createFile(t, "testwrite", "potato")
  47. contents := run.readFile(t, "testwrite")
  48. assert.Equal(t, "potato", contents)
  49. run.rm(t, "testwrite")
  50. }
  51. // TestWriteFileFsync tests Fsync
  52. //
  53. // NB the code for this is in file.go rather than write.go
  54. func TestWriteFileFsync(t *testing.T) {
  55. run.skipIfNoFUSE(t)
  56. filepath := run.path("to be synced")
  57. fd, err := osCreate(filepath)
  58. require.NoError(t, err)
  59. _, err = fd.Write([]byte("hello"))
  60. require.NoError(t, err)
  61. err = fd.Sync()
  62. require.NoError(t, err)
  63. err = fd.Close()
  64. require.NoError(t, err)
  65. run.waitForWriters()
  66. run.rm(t, "to be synced")
  67. }
  68. // TestWriteFileDup tests behavior of mmap() in Python by using dup() on a file handle
  69. func TestWriteFileDup(t *testing.T) {
  70. run.skipIfVFS(t)
  71. run.skipIfNoFUSE(t)
  72. filepath := run.path("to be synced")
  73. fh, err := osCreate(filepath)
  74. require.NoError(t, err)
  75. testData := []byte("0123456789")
  76. err = fh.Truncate(int64(len(testData) + 2))
  77. require.NoError(t, err)
  78. err = fh.Sync()
  79. require.NoError(t, err)
  80. var dupFd uintptr
  81. dupFd, err = writeTestDup(fh.Fd())
  82. require.NoError(t, err)
  83. dupFile := os.NewFile(dupFd, fh.Name())
  84. _, err = dupFile.Write(testData)
  85. require.NoError(t, err)
  86. err = dupFile.Close()
  87. require.NoError(t, err)
  88. _, err = fh.Seek(int64(len(testData)), 0)
  89. require.NoError(t, err)
  90. _, err = fh.Write([]byte("10"))
  91. require.NoError(t, err)
  92. err = fh.Close()
  93. require.NoError(t, err)
  94. run.waitForWriters()
  95. run.rm(t, "to be synced")
  96. }
  97. // TestWriteFileAppend tests that O_APPEND works on cache backends >= writes
  98. func TestWriteFileAppend(t *testing.T) {
  99. run.skipIfNoFUSE(t)
  100. // TODO: Windows needs the v1.5 release of WinFsp to handle O_APPEND properly.
  101. // Until it gets released, skip this test on Windows.
  102. if runtime.GOOS == "windows" {
  103. t.Skip("currently unsupported on Windows")
  104. }
  105. filepath := run.path("to be synced")
  106. fh, err := osCreate(filepath)
  107. require.NoError(t, err)
  108. testData := []byte("0123456789")
  109. appendData := []byte("10")
  110. _, err = fh.Write(testData)
  111. require.NoError(t, err)
  112. err = fh.Close()
  113. require.NoError(t, err)
  114. fh, err = osAppend(filepath)
  115. require.NoError(t, err)
  116. _, err = fh.Write(appendData)
  117. require.NoError(t, err)
  118. err = fh.Close()
  119. require.NoError(t, err)
  120. <-time.After(time.Second * 10)
  121. info, err := run.os.Stat(filepath)
  122. require.NoError(t, err)
  123. require.EqualValues(t, len(testData)+len(appendData), info.Size())
  124. run.waitForWriters()
  125. run.rm(t, "to be synced")
  126. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。