override_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // Copyright (C) 2015 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. //go:build integration
  7. // +build integration
  8. package integration
  9. import (
  10. "io"
  11. "log"
  12. "os"
  13. "strings"
  14. "testing"
  15. "time"
  16. "github.com/syncthing/syncthing/lib/config"
  17. "github.com/syncthing/syncthing/lib/events"
  18. "github.com/syncthing/syncthing/lib/protocol"
  19. "github.com/syncthing/syncthing/lib/rc"
  20. )
  21. func TestOverride(t *testing.T) {
  22. // Enable "send-only" on s1/default
  23. id, _ := protocol.DeviceIDFromString(id1)
  24. cfg, _, _ := config.Load("h1/config.xml", id, events.NoopLogger)
  25. fld := cfg.Folders()["default"]
  26. fld.Type = config.FolderTypeSendOnly
  27. cfg.SetFolder(fld)
  28. os.Rename("h1/config.xml", "h1/config.xml.orig")
  29. defer os.Rename("h1/config.xml.orig", "h1/config.xml")
  30. cfg.Save()
  31. log.Println("Cleaning...")
  32. err := removeAll("s1", "s2", "h1/index*", "h2/index*")
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. log.Println("Generating files...")
  37. err = generateFiles("s1", 100, 20, "../LICENSE")
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. fd, err := os.Create("s1/testfile.txt")
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. _, err = fd.WriteString("hello\n")
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. err = fd.Close()
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. expected, err := directoryContents("s1")
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. sendOnly := startInstance(t, 1)
  58. defer checkedStop(t, sendOnly)
  59. sendRecv := startInstance(t, 2)
  60. defer checkedStop(t, sendRecv)
  61. sendOnly.ResumeAll()
  62. sendRecv.ResumeAll()
  63. log.Println("Syncing...")
  64. rc.AwaitSync("default", sendOnly, sendRecv)
  65. log.Println("Verifying...")
  66. actual, err := directoryContents("s2")
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. err = compareDirectoryContents(actual, expected)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. log.Println("Changing file on sendRecv side...")
  75. fd, err = os.OpenFile("s2/testfile.txt", os.O_WRONLY|os.O_APPEND, 0644)
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. _, err = fd.WriteString("text added to s2\n")
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. err = fd.Close()
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. if err := sendRecv.Rescan("default"); err != nil {
  88. t.Fatal(err)
  89. }
  90. log.Println("Waiting for index to send...")
  91. time.Sleep(10 * time.Second)
  92. log.Println("Hitting Override on sendOnly...")
  93. if _, err := sendOnly.Post("/rest/db/override?folder=default", nil); err != nil {
  94. t.Fatal(err)
  95. }
  96. log.Println("Syncing...")
  97. rc.AwaitSync("default", sendOnly, sendRecv)
  98. // Verify that the override worked
  99. fd, err = os.Open("s1/testfile.txt")
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. bs, err := io.ReadAll(fd)
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. fd.Close()
  108. if strings.Contains(string(bs), "added to s2") {
  109. t.Error("Change should not have been synced to sendOnly")
  110. }
  111. fd, err = os.Open("s2/testfile.txt")
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. bs, err = io.ReadAll(fd)
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. fd.Close()
  120. if strings.Contains(string(bs), "added to s2") {
  121. t.Error("Change should have been overridden on sendRecv")
  122. }
  123. }
  124. /* This doesn't currently work with detection completion, as we don't actually
  125. get to completion when in sendOnly/sendRecv mode. Needs fixing.
  126. func TestOverrideIgnores(t *testing.T) {
  127. // Enable "sendOnly" on s1/default
  128. id, _ := protocol.DeviceIDFromString(id1)
  129. cfg, _, _ := config.Load("h1/config.xml", id, events.NoopLogger)
  130. fld := cfg.Folders()["default"]
  131. fld.ReadOnly = true
  132. cfg.SetFolder(fld)
  133. os.Rename("h1/config.xml", "h1/config.xml.orig")
  134. defer os.Rename("h1/config.xml.orig", "h1/config.xml")
  135. cfg.Save()
  136. log.Println("Cleaning...")
  137. err := removeAll("s1", "s2", "h1/index*", "h2/index*")
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. log.Println("Generating files...")
  142. err = generateFiles("s1", 10, 2, "../LICENSE")
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. fd, err := os.Create("s1/testfile.txt")
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. _, err = fd.WriteString("original text\n")
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. err = fd.Close()
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. expected, err := directoryContents("s1")
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. log.Println("Starting sendOnly...")
  163. sendOnly := syncthingProcess{ // id1
  164. instance: "1",
  165. argv: []string{"--home", "h1"},
  166. port: 8081,
  167. apiKey: apiKey,
  168. }
  169. err = sendOnly.start()
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. defer sendOnly.stop()
  174. log.Println("Starting sendRecv...")
  175. sendRecv := syncthingProcess{ // id2
  176. instance: "2",
  177. argv: []string{"--home", "h2"},
  178. port: 8082,
  179. apiKey: apiKey,
  180. }
  181. err = sendRecv.start()
  182. if err != nil {
  183. sendOnly.stop()
  184. t.Fatal(err)
  185. }
  186. defer sendRecv.stop()
  187. log.Println("Syncing...")
  188. err = awaitCompletion("default", sendOnly, sendRecv)
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. log.Println("Verifying...")
  193. actual, err := directoryContents("s2")
  194. if err != nil {
  195. t.Fatal(err)
  196. }
  197. err = compareDirectoryContents(actual, expected)
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. log.Println("Ignoring testfile.txt on sendOnly...")
  202. fd, err = os.Create("s1/.stignore")
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. _, err = fd.WriteString("testfile.txt\n")
  207. if err != nil {
  208. t.Fatal(err)
  209. }
  210. err = fd.Close()
  211. if err != nil {
  212. t.Fatal(err)
  213. }
  214. log.Println("Modify testfile.txt on sendOnly...")
  215. fd, err = os.Create("s1/testfile.txt")
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. _, err = fd.WriteString("updated on sendOnly but ignored\n")
  220. if err != nil {
  221. t.Fatal(err)
  222. }
  223. err = fd.Close()
  224. if err != nil {
  225. t.Fatal(err)
  226. }
  227. fd, err = os.Create("s1/testfile2.txt")
  228. if err != nil {
  229. t.Fatal(err)
  230. }
  231. _, err = fd.WriteString("sync me\n")
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. err = fd.Close()
  236. if err != nil {
  237. t.Fatal(err)
  238. }
  239. err = sendOnly.rescan("default")
  240. log.Println("Waiting for sync...")
  241. time.Sleep(10 * time.Second)
  242. // Verify that sync worked
  243. fd, err = os.Open("s2/testfile.txt")
  244. if err != nil {
  245. t.Fatal(err)
  246. }
  247. bs, err := io.ReadAll(fd)
  248. if err != nil {
  249. t.Fatal(err)
  250. }
  251. fd.Close()
  252. if !strings.Contains(string(bs), "original text") {
  253. t.Error("Changes should not have been synced to sendRecv")
  254. }
  255. fd, err = os.Open("s2/testfile2.txt")
  256. if err != nil {
  257. t.Fatal(err)
  258. }
  259. bs, err = io.ReadAll(fd)
  260. if err != nil {
  261. t.Fatal(err)
  262. }
  263. fd.Close()
  264. if !strings.Contains(string(bs), "sync me") {
  265. t.Error("Changes should have been synced to sendRecv")
  266. }
  267. log.Println("Removing file on sendRecv side...")
  268. os.Remove("s2/testfile.txt")
  269. err = sendRecv.rescan("default")
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. log.Println("Waiting for sync...")
  274. time.Sleep(10 * time.Second)
  275. // Verify that nothing changed
  276. fd, err = os.Open("s1/testfile.txt")
  277. if err != nil {
  278. t.Fatal(err)
  279. }
  280. bs, err = io.ReadAll(fd)
  281. if err != nil {
  282. t.Fatal(err)
  283. }
  284. fd.Close()
  285. if !strings.Contains(string(bs), "updated on sendOnly but ignored") {
  286. t.Error("Changes should not have been synced to sendOnly")
  287. }
  288. fd, err = os.Open("s2/testfile.txt")
  289. if err == nil {
  290. t.Error("File should not exist on the sendRecv")
  291. }
  292. log.Println("Creating file on sendRecv...")
  293. fd, err = os.Create("s2/testfile3.txt")
  294. if err != nil {
  295. t.Fatal(err)
  296. }
  297. _, err = fd.WriteString("created on sendRecv, should be removed on override\n")
  298. if err != nil {
  299. t.Fatal(err)
  300. }
  301. err = fd.Close()
  302. if err != nil {
  303. t.Fatal(err)
  304. }
  305. log.Println("Hitting Override on sendOnly...")
  306. resp, err := sendOnly.post("/rest/db/override?folder=default", nil)
  307. if err != nil {
  308. t.Fatal(err)
  309. }
  310. if resp.StatusCode != 200 {
  311. t.Fatal(resp.Status)
  312. }
  313. log.Println("Waiting for sync...")
  314. time.Sleep(10 * time.Second)
  315. fd, err = os.Open("s2/testfile.txt")
  316. if err == nil {
  317. t.Error("File should not exist on the sendRecv")
  318. }
  319. fd, err = os.Open("s2/testfile2.txt")
  320. if err != nil {
  321. t.Fatal(err)
  322. }
  323. bs, err = io.ReadAll(fd)
  324. if err != nil {
  325. t.Fatal(err)
  326. }
  327. fd.Close()
  328. if !strings.Contains(string(bs), "sync me") {
  329. t.Error("Changes should have been synced to sendRecv")
  330. }
  331. fd, err = os.Open("s2/testfile3.txt")
  332. if err != nil {
  333. t.Error("File should still exist on the sendRecv")
  334. }
  335. fd.Close()
  336. log.Println("Hitting Override on sendOnly (again)...")
  337. resp, err = sendOnly.post("/rest/db/override?folder=default", nil)
  338. if err != nil {
  339. t.Fatal(err)
  340. }
  341. if resp.StatusCode != 200 {
  342. t.Fatal(resp.Status)
  343. }
  344. log.Println("Waiting for sync...")
  345. time.Sleep(10 * time.Second)
  346. fd, err = os.Open("s2/testfile.txt")
  347. if err == nil {
  348. t.Error("File should not exist on the sendRecv")
  349. }
  350. fd, err = os.Open("s2/testfile2.txt")
  351. if err != nil {
  352. t.Fatal(err)
  353. }
  354. bs, err = io.ReadAll(fd)
  355. if err != nil {
  356. t.Fatal(err)
  357. }
  358. fd.Close()
  359. if !strings.Contains(string(bs), "sync me") {
  360. t.Error("Changes should have been synced to sendRecv")
  361. }
  362. fd, err = os.Open("s2/testfile3.txt")
  363. if err != nil {
  364. t.Error("File should still exist on the sendRecv")
  365. }
  366. fd.Close()
  367. }
  368. */