symlink_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. // +build integration
  7. package integration
  8. import (
  9. "log"
  10. "os"
  11. "testing"
  12. "github.com/syncthing/syncthing/lib/config"
  13. "github.com/syncthing/syncthing/lib/protocol"
  14. "github.com/syncthing/syncthing/lib/rc"
  15. )
  16. func TestSymlinks(t *testing.T) {
  17. if !symlinksSupported() {
  18. t.Skip("symlinks unsupported")
  19. }
  20. // Use no versioning
  21. id, _ := protocol.DeviceIDFromString(id2)
  22. cfg, _ := config.Load("h2/config.xml", id)
  23. fld := cfg.Folders()["default"]
  24. fld.Versioning = config.VersioningConfiguration{}
  25. cfg.SetFolder(fld)
  26. os.Rename("h2/config.xml", "h2/config.xml.orig")
  27. defer os.Rename("h2/config.xml.orig", "h2/config.xml")
  28. cfg.Save()
  29. testSymlinks(t)
  30. }
  31. func TestSymlinksSimpleVersioning(t *testing.T) {
  32. if !symlinksSupported() {
  33. t.Skip("symlinks unsupported")
  34. }
  35. // Use simple versioning
  36. id, _ := protocol.DeviceIDFromString(id2)
  37. cfg, _ := config.Load("h2/config.xml", id)
  38. fld := cfg.Folders()["default"]
  39. fld.Versioning = config.VersioningConfiguration{
  40. Type: "simple",
  41. Params: map[string]string{"keep": "5"},
  42. }
  43. cfg.SetFolder(fld)
  44. os.Rename("h2/config.xml", "h2/config.xml.orig")
  45. defer os.Rename("h2/config.xml.orig", "h2/config.xml")
  46. cfg.Save()
  47. testSymlinks(t)
  48. }
  49. func TestSymlinksStaggeredVersioning(t *testing.T) {
  50. if !symlinksSupported() {
  51. t.Skip("symlinks unsupported")
  52. }
  53. // Use staggered versioning
  54. id, _ := protocol.DeviceIDFromString(id2)
  55. cfg, _ := config.Load("h2/config.xml", id)
  56. fld := cfg.Folders()["default"]
  57. fld.Versioning = config.VersioningConfiguration{
  58. Type: "staggered",
  59. }
  60. cfg.SetFolder(fld)
  61. os.Rename("h2/config.xml", "h2/config.xml.orig")
  62. defer os.Rename("h2/config.xml.orig", "h2/config.xml")
  63. cfg.Save()
  64. testSymlinks(t)
  65. }
  66. func testSymlinks(t *testing.T) {
  67. log.Println("Cleaning...")
  68. err := removeAll("s1", "s2", "h1/index*", "h2/index*")
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. log.Println("Generating files...")
  73. err = generateFiles("s1", 100, 20, "../LICENSE")
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. // A file that we will replace with a symlink later
  78. fd, err := os.Create("s1/fileToReplace")
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. fd.Close()
  83. // A directory that we will replace with a symlink later
  84. err = os.Mkdir("s1/dirToReplace", 0755)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. // A file and a symlink to that file
  89. fd, err = os.Create("s1/file")
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. fd.Close()
  94. err = os.Symlink("file", "s1/fileLink")
  95. if err != nil {
  96. log.Fatal(err)
  97. }
  98. // A directory and a symlink to that directory
  99. err = os.Mkdir("s1/dir", 0755)
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. err = os.Symlink("dir", "s1/dirLink")
  104. if err != nil {
  105. log.Fatal(err)
  106. }
  107. // A link to something in the repo that does not exist
  108. err = os.Symlink("does/not/exist", "s1/noneLink")
  109. if err != nil {
  110. log.Fatal(err)
  111. }
  112. // A link we will replace with a file later
  113. err = os.Symlink("does/not/exist", "s1/repFileLink")
  114. if err != nil {
  115. log.Fatal(err)
  116. }
  117. // A link we will replace with a directory later
  118. err = os.Symlink("does/not/exist", "s1/repDirLink")
  119. if err != nil {
  120. log.Fatal(err)
  121. }
  122. // A link we will remove later
  123. err = os.Symlink("does/not/exist", "s1/removeLink")
  124. if err != nil {
  125. log.Fatal(err)
  126. }
  127. // Verify that the files and symlinks sync to the other side
  128. sender := startInstance(t, 1)
  129. defer checkedStop(t, sender)
  130. receiver := startInstance(t, 2)
  131. defer checkedStop(t, receiver)
  132. sender.ResumeAll()
  133. receiver.ResumeAll()
  134. log.Println("Syncing...")
  135. rc.AwaitSync("default", sender, receiver)
  136. log.Println("Comparing directories...")
  137. err = compareDirectories("s1", "s2")
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. log.Println("Making some changes...")
  142. // Remove one symlink
  143. err = os.Remove("s1/fileLink")
  144. if err != nil {
  145. log.Fatal(err)
  146. }
  147. // Change the target of another
  148. err = os.Remove("s1/dirLink")
  149. if err != nil {
  150. log.Fatal(err)
  151. }
  152. err = os.Symlink("file", "s1/dirLink")
  153. if err != nil {
  154. log.Fatal(err)
  155. }
  156. // Replace one with a file
  157. err = os.Remove("s1/repFileLink")
  158. if err != nil {
  159. log.Fatal(err)
  160. }
  161. fd, err = os.Create("s1/repFileLink")
  162. if err != nil {
  163. log.Fatal(err)
  164. }
  165. fd.Close()
  166. // Replace one with a directory
  167. err = os.Remove("s1/repDirLink")
  168. if err != nil {
  169. log.Fatal(err)
  170. }
  171. err = os.Mkdir("s1/repDirLink", 0755)
  172. if err != nil {
  173. log.Fatal(err)
  174. }
  175. // Replace a file with a symlink
  176. err = os.Remove("s1/fileToReplace")
  177. if err != nil {
  178. log.Fatal(err)
  179. }
  180. err = os.Symlink("somewhere/non/existent", "s1/fileToReplace")
  181. if err != nil {
  182. log.Fatal(err)
  183. }
  184. // Replace a directory with a symlink
  185. err = os.RemoveAll("s1/dirToReplace")
  186. if err != nil {
  187. log.Fatal(err)
  188. }
  189. err = os.Symlink("somewhere/non/existent", "s1/dirToReplace")
  190. if err != nil {
  191. log.Fatal(err)
  192. }
  193. // Remove a broken symlink
  194. err = os.Remove("s1/removeLink")
  195. if err != nil {
  196. log.Fatal(err)
  197. }
  198. // Sync these changes and recheck
  199. log.Println("Syncing...")
  200. if err := sender.Rescan("default"); err != nil {
  201. t.Fatal(err)
  202. }
  203. rc.AwaitSync("default", sender, receiver)
  204. log.Println("Comparing directories...")
  205. err = compareDirectories("s1", "s2")
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. }