sync_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 http://mozilla.org/MPL/2.0/.
  6. // +build integration
  7. package integration
  8. import (
  9. "fmt"
  10. "io/ioutil"
  11. "log"
  12. "math/rand"
  13. "os"
  14. "testing"
  15. "time"
  16. "github.com/syncthing/syncthing/lib/config"
  17. "github.com/syncthing/syncthing/lib/protocol"
  18. "github.com/syncthing/syncthing/lib/rc"
  19. )
  20. const (
  21. longTimeLimit = 5 * time.Minute
  22. shortTimeLimit = 45 * time.Second
  23. )
  24. func TestSyncClusterWithoutVersioning(t *testing.T) {
  25. // Use no versioning
  26. id, _ := protocol.DeviceIDFromString(id2)
  27. cfg, _ := config.Load("h2/config.xml", id)
  28. fld := cfg.Folders()["default"]
  29. fld.Versioning = config.VersioningConfiguration{}
  30. cfg.SetFolder(fld)
  31. cfg.Save()
  32. testSyncCluster(t)
  33. }
  34. func TestSyncClusterSimpleVersioning(t *testing.T) {
  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. cfg.Save()
  45. testSyncCluster(t)
  46. }
  47. func TestSyncClusterTrashcanVersioning(t *testing.T) {
  48. // Use simple versioning
  49. id, _ := protocol.DeviceIDFromString(id2)
  50. cfg, _ := config.Load("h2/config.xml", id)
  51. fld := cfg.Folders()["default"]
  52. fld.Versioning = config.VersioningConfiguration{
  53. Type: "trashcan",
  54. Params: map[string]string{"cleanoutDays": "1"},
  55. }
  56. cfg.SetFolder(fld)
  57. cfg.Save()
  58. testSyncCluster(t)
  59. }
  60. func TestSyncClusterStaggeredVersioning(t *testing.T) {
  61. // Use staggered versioning
  62. id, _ := protocol.DeviceIDFromString(id2)
  63. cfg, _ := config.Load("h2/config.xml", id)
  64. fld := cfg.Folders()["default"]
  65. fld.Versioning = config.VersioningConfiguration{
  66. Type: "staggered",
  67. }
  68. cfg.SetFolder(fld)
  69. cfg.Save()
  70. testSyncCluster(t)
  71. }
  72. func TestSyncClusterForcedRescan(t *testing.T) {
  73. // Use no versioning
  74. id, _ := protocol.DeviceIDFromString(id2)
  75. cfg, _ := config.Load("h2/config.xml", id)
  76. fld := cfg.Folders()["default"]
  77. fld.Versioning = config.VersioningConfiguration{}
  78. cfg.SetFolder(fld)
  79. cfg.Save()
  80. testSyncClusterForcedRescan(t)
  81. }
  82. func testSyncCluster(t *testing.T) {
  83. // This tests syncing files back and forth between three cluster members.
  84. // Their configs are in h1, h2 and h3. The folder "default" is shared
  85. // between all and stored in s1, s2 and s3 respectively.
  86. //
  87. // Another folder is shared between 1 and 2 only, in s12-1 and s12-2. A
  88. // third folders is shared between 2 and 3, in s23-2 and s23-3.
  89. // When -short is passed, keep it more reasonable.
  90. timeLimit := longTimeLimit
  91. if testing.Short() {
  92. timeLimit = shortTimeLimit
  93. }
  94. const (
  95. numFiles = 100
  96. fileSizeExp = 20
  97. )
  98. rand.Seed(42)
  99. log.Printf("Testing with numFiles=%d, fileSizeExp=%d, timeLimit=%v", numFiles, fileSizeExp, timeLimit)
  100. log.Println("Cleaning...")
  101. err := removeAll("s1", "s12-1",
  102. "s2", "s12-2", "s23-2",
  103. "s3", "s23-3",
  104. "h1/index*", "h2/index*", "h3/index*")
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. // Create initial folder contents. All three devices have stuff in
  109. // "default", which should be merged. The other two folders are initially
  110. // empty on one side.
  111. log.Println("Generating files...")
  112. err = generateFiles("s1", numFiles, fileSizeExp, "../LICENSE")
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. err = generateFiles("s12-1", numFiles, fileSizeExp, "../LICENSE")
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. // We'll use this file for appending data without modifying the time stamp.
  121. fd, err := os.Create("s1/test-appendfile")
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. _, err = fd.WriteString("hello\n")
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. err = fd.Close()
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. err = generateFiles("s2", numFiles, fileSizeExp, "../LICENSE")
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. err = generateFiles("s23-2", numFiles, fileSizeExp, "../LICENSE")
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. err = generateFiles("s3", numFiles, fileSizeExp, "../LICENSE")
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. // Prepare the expected state of folders after the sync
  146. c1, err := directoryContents("s1")
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. c2, err := directoryContents("s2")
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. c3, err := directoryContents("s3")
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. e1 := mergeDirectoryContents(c1, c2, c3)
  159. e2, err := directoryContents("s12-1")
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. e3, err := directoryContents("s23-2")
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. expected := [][]fileInfo{e1, e2, e3}
  168. // Start the syncers
  169. log.Println("Starting Syncthing...")
  170. p0 := startInstance(t, 1)
  171. defer checkedStop(t, p0)
  172. p1 := startInstance(t, 2)
  173. defer checkedStop(t, p1)
  174. p2 := startInstance(t, 3)
  175. defer checkedStop(t, p2)
  176. p := []*rc.Process{p0, p1, p2}
  177. start := time.Now()
  178. iteration := 0
  179. for time.Since(start) < timeLimit {
  180. iteration++
  181. log.Println("Iteration", iteration)
  182. log.Println("Forcing rescan...")
  183. // Force rescan of folders
  184. for i, device := range p {
  185. if err := device.RescanDelay("default", 86400); err != nil {
  186. t.Fatal(err)
  187. }
  188. if i == 0 || i == 1 {
  189. if err := device.RescanDelay("s12", 86400); err != nil {
  190. t.Fatal(err)
  191. }
  192. }
  193. if i == 1 || i == 2 {
  194. if err := device.RescanDelay("s23", 86400); err != nil {
  195. t.Fatal(err)
  196. }
  197. }
  198. }
  199. // Sync stuff and verify it looks right
  200. err = scSyncAndCompare(p, expected)
  201. if err != nil {
  202. t.Error(err)
  203. break
  204. }
  205. log.Println("Altering...")
  206. // Alter the source files for another round
  207. err = alterFiles("s1")
  208. if err != nil {
  209. t.Error(err)
  210. break
  211. }
  212. err = alterFiles("s12-1")
  213. if err != nil {
  214. t.Error(err)
  215. break
  216. }
  217. err = alterFiles("s23-2")
  218. if err != nil {
  219. t.Error(err)
  220. break
  221. }
  222. // Alter the "test-appendfile" without changing it's modification time. Sneaky!
  223. fi, err := os.Stat("s1/test-appendfile")
  224. if err != nil {
  225. t.Fatal(err)
  226. }
  227. fd, err := os.OpenFile("s1/test-appendfile", os.O_APPEND|os.O_WRONLY, 0644)
  228. if err != nil {
  229. t.Fatal(err)
  230. }
  231. _, err = fd.Seek(0, os.SEEK_END)
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. _, err = fd.WriteString("more data\n")
  236. if err != nil {
  237. t.Fatal(err)
  238. }
  239. err = fd.Close()
  240. if err != nil {
  241. t.Fatal(err)
  242. }
  243. err = os.Chtimes("s1/test-appendfile", fi.ModTime(), fi.ModTime())
  244. if err != nil {
  245. t.Fatal(err)
  246. }
  247. // Prepare the expected state of folders after the sync
  248. e1, err = directoryContents("s1")
  249. if err != nil {
  250. t.Fatal(err)
  251. }
  252. e2, err = directoryContents("s12-1")
  253. if err != nil {
  254. t.Fatal(err)
  255. }
  256. e3, err = directoryContents("s23-2")
  257. if err != nil {
  258. t.Fatal(err)
  259. }
  260. expected = [][]fileInfo{e1, e2, e3}
  261. }
  262. }
  263. func testSyncClusterForcedRescan(t *testing.T) {
  264. // During this test, we create 1K files, remove and then create them
  265. // again. However, during these operations we will perform scan operations
  266. // such that other nodes will retrieve these options while data is
  267. // changing.
  268. // When -short is passed, keep it more reasonable.
  269. timeLimit := longTimeLimit
  270. if testing.Short() {
  271. timeLimit = shortTimeLimit
  272. }
  273. log.Println("Cleaning...")
  274. err := removeAll("s1", "s12-1",
  275. "s2", "s12-2", "s23-2",
  276. "s3", "s23-3",
  277. "h1/index*", "h2/index*", "h3/index*")
  278. if err != nil {
  279. t.Fatal(err)
  280. }
  281. // Create initial folder contents. All three devices have stuff in
  282. // "default", which should be merged. The other two folders are initially
  283. // empty on one side.
  284. log.Println("Generating files...")
  285. if err := os.MkdirAll("s1/test-stable-files", 0755); err != nil {
  286. t.Fatal(err)
  287. }
  288. for i := 0; i < 1000; i++ {
  289. name := fmt.Sprintf("s1/test-stable-files/%d", i)
  290. if err := ioutil.WriteFile(name, []byte(time.Now().Format(time.RFC3339Nano)), 0644); err != nil {
  291. t.Fatal(err)
  292. }
  293. }
  294. // Prepare the expected state of folders after the sync
  295. expected, err := directoryContents("s1")
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. // Start the syncers
  300. p0 := startInstance(t, 1)
  301. defer checkedStop(t, p0)
  302. p1 := startInstance(t, 2)
  303. defer checkedStop(t, p1)
  304. p2 := startInstance(t, 3)
  305. defer checkedStop(t, p2)
  306. p := []*rc.Process{p0, p1, p2}
  307. start := time.Now()
  308. for time.Since(start) < timeLimit {
  309. rescan := func() {
  310. for i := range p {
  311. if err := p[i].Rescan("default"); err != nil {
  312. t.Fatal(err)
  313. }
  314. }
  315. }
  316. log.Println("Forcing rescan...")
  317. rescan()
  318. // Sync stuff and verify it looks right
  319. err = scSyncAndCompare(p, [][]fileInfo{expected})
  320. if err != nil {
  321. t.Fatal(err)
  322. }
  323. log.Println("Altering...")
  324. // Delete and recreate stable files while scanners and pullers are active
  325. for i := 0; i < 1000; i++ {
  326. name := fmt.Sprintf("s1/test-stable-files/%d", i)
  327. if err := os.Remove(name); err != nil {
  328. t.Fatal(err)
  329. }
  330. if rand.Intn(10) == 0 {
  331. rescan()
  332. }
  333. }
  334. rescan()
  335. time.Sleep(50 * time.Millisecond)
  336. for i := 0; i < 1000; i++ {
  337. name := fmt.Sprintf("s1/test-stable-files/%d", i)
  338. if err := ioutil.WriteFile(name, []byte(time.Now().Format(time.RFC3339Nano)), 0644); err != nil {
  339. t.Fatal(err)
  340. }
  341. if rand.Intn(10) == 0 {
  342. rescan()
  343. }
  344. }
  345. rescan()
  346. // Prepare the expected state of folders after the sync
  347. expected, err = directoryContents("s1")
  348. if err != nil {
  349. t.Fatal(err)
  350. }
  351. if len(expected) != 1001 {
  352. t.Fatal("s1 does not have 1001 files;", len(expected))
  353. }
  354. }
  355. }
  356. func scSyncAndCompare(p []*rc.Process, expected [][]fileInfo) error {
  357. log.Println("Syncing...")
  358. for {
  359. time.Sleep(250 * time.Millisecond)
  360. if !rc.InSync("default", p...) {
  361. continue
  362. }
  363. if !rc.InSync("s12", p[0], p[1]) {
  364. continue
  365. }
  366. if !rc.InSync("s23", p[1], p[2]) {
  367. continue
  368. }
  369. break
  370. }
  371. log.Println("Checking...")
  372. for _, dir := range []string{"s1", "s2", "s3"} {
  373. actual, err := directoryContents(dir)
  374. if err != nil {
  375. return err
  376. }
  377. if err := compareDirectoryContents(actual, expected[0]); err != nil {
  378. return fmt.Errorf("%s: %v", dir, err)
  379. }
  380. }
  381. if len(expected) > 1 {
  382. for _, dir := range []string{"s12-1", "s12-2"} {
  383. actual, err := directoryContents(dir)
  384. if err != nil {
  385. return err
  386. }
  387. if err := compareDirectoryContents(actual, expected[1]); err != nil {
  388. return fmt.Errorf("%s: %v", dir, err)
  389. }
  390. }
  391. }
  392. if len(expected) > 2 {
  393. for _, dir := range []string{"s23-2", "s23-3"} {
  394. actual, err := directoryContents(dir)
  395. if err != nil {
  396. return err
  397. }
  398. if err := compareDirectoryContents(actual, expected[2]); err != nil {
  399. return fmt.Errorf("%s: %v", dir, err)
  400. }
  401. }
  402. }
  403. return nil
  404. }