symlink_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // +build integration
  16. package integration_test
  17. import (
  18. "log"
  19. "os"
  20. "strings"
  21. "testing"
  22. "time"
  23. "github.com/syncthing/syncthing/internal/symlinks"
  24. )
  25. func TestSymlinks(t *testing.T) {
  26. log.Println("Cleaning...")
  27. err := removeAll("s1", "s2", "h1/index", "h2/index")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. log.Println("Generating files...")
  32. err = generateFiles("s1", 100, 20, "../bin/syncthing")
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. // A file that we will replace with a symlink later
  37. fd, err := os.Create("s1/fileToReplace")
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. fd.Close()
  42. // A directory that we will replace with a symlink later
  43. err = os.Mkdir("s1/dirToReplace", 0755)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. // A file and a symlink to that file
  48. fd, err = os.Create("s1/file")
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. fd.Close()
  53. err = symlinks.Create("s1/fileLink", "file", 0)
  54. if err != nil {
  55. log.Fatal(err)
  56. }
  57. // A directory and a symlink to that directory
  58. err = os.Mkdir("s1/dir", 0755)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. err = symlinks.Create("s1/dirLink", "dir", 0)
  63. if err != nil {
  64. log.Fatal(err)
  65. }
  66. // A link to something in the repo that does not exist
  67. err = symlinks.Create("s1/noneLink", "does/not/exist", 0)
  68. if err != nil {
  69. log.Fatal(err)
  70. }
  71. // A link we will replace with a file later
  72. err = symlinks.Create("s1/repFileLink", "does/not/exist", 0)
  73. if err != nil {
  74. log.Fatal(err)
  75. }
  76. // A link we will replace with a directory later
  77. err = symlinks.Create("s1/repDirLink", "does/not/exist", 0)
  78. if err != nil {
  79. log.Fatal(err)
  80. }
  81. // Verify that the files and symlinks sync to the other side
  82. log.Println("Syncing...")
  83. sender := syncthingProcess{ // id1
  84. log: "1.out",
  85. argv: []string{"-home", "h1"},
  86. port: 8081,
  87. apiKey: apiKey,
  88. }
  89. err = sender.start()
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. receiver := syncthingProcess{ // id2
  94. log: "2.out",
  95. argv: []string{"-home", "h2"},
  96. port: 8082,
  97. apiKey: apiKey,
  98. }
  99. err = receiver.start()
  100. if err != nil {
  101. sender.stop()
  102. t.Fatal(err)
  103. }
  104. for {
  105. comp, err := sender.peerCompletion()
  106. if err != nil {
  107. if strings.Contains(err.Error(), "use of closed network connection") {
  108. time.Sleep(time.Second)
  109. continue
  110. }
  111. sender.stop()
  112. receiver.stop()
  113. t.Fatal(err)
  114. }
  115. curComp := comp[id2]
  116. if curComp == 100 {
  117. sender.stop()
  118. receiver.stop()
  119. break
  120. }
  121. time.Sleep(time.Second)
  122. }
  123. sender.stop()
  124. receiver.stop()
  125. log.Println("Comparing directories...")
  126. err = compareDirectories("s1", "s2")
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. log.Println("Making some changes...")
  131. // Remove one symlink
  132. err = os.Remove("s1/fileLink")
  133. if err != nil {
  134. log.Fatal(err)
  135. }
  136. // Change the target of another
  137. err = os.Remove("s1/dirLink")
  138. if err != nil {
  139. log.Fatal(err)
  140. }
  141. err = symlinks.Create("s1/dirLink", "file", 0)
  142. if err != nil {
  143. log.Fatal(err)
  144. }
  145. // Replace one with a file
  146. err = os.Remove("s1/repFileLink")
  147. if err != nil {
  148. log.Fatal(err)
  149. }
  150. fd, err = os.Create("s1/repFileLink")
  151. if err != nil {
  152. log.Fatal(err)
  153. }
  154. fd.Close()
  155. // Replace one with a directory
  156. err = os.Remove("s1/repDirLink")
  157. if err != nil {
  158. log.Fatal(err)
  159. }
  160. err = os.Mkdir("s1/repDirLink", 0755)
  161. if err != nil {
  162. log.Fatal(err)
  163. }
  164. // Replace a file with a symlink
  165. err = os.Remove("s1/fileToReplace")
  166. if err != nil {
  167. log.Fatal(err)
  168. }
  169. err = symlinks.Create("s1/fileToReplace", "somewhere/non/existent", 0)
  170. if err != nil {
  171. log.Fatal(err)
  172. }
  173. // Replace a directory with a symlink
  174. err = os.RemoveAll("s1/dirToReplace")
  175. if err != nil {
  176. log.Fatal(err)
  177. }
  178. err = symlinks.Create("s1/dirToReplace", "somewhere/non/existent", 0)
  179. if err != nil {
  180. log.Fatal(err)
  181. }
  182. // Sync these changes and recheck
  183. log.Println("Syncing...")
  184. err = sender.start()
  185. if err != nil {
  186. t.Fatal(err)
  187. }
  188. err = receiver.start()
  189. if err != nil {
  190. sender.stop()
  191. t.Fatal(err)
  192. }
  193. for {
  194. comp, err := sender.peerCompletion()
  195. if err != nil {
  196. if strings.Contains(err.Error(), "use of closed network connection") {
  197. time.Sleep(time.Second)
  198. continue
  199. }
  200. sender.stop()
  201. receiver.stop()
  202. t.Fatal(err)
  203. }
  204. curComp := comp[id2]
  205. if curComp == 100 {
  206. sender.stop()
  207. receiver.stop()
  208. break
  209. }
  210. time.Sleep(time.Second)
  211. }
  212. sender.stop()
  213. receiver.stop()
  214. log.Println("Comparing directories...")
  215. err = compareDirectories("s1", "s2")
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. }