norestart_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. "log"
  10. "os"
  11. "testing"
  12. "github.com/syncthing/syncthing/lib/config"
  13. "github.com/syncthing/syncthing/lib/osutil"
  14. "github.com/syncthing/syncthing/lib/protocol"
  15. "github.com/syncthing/syncthing/lib/rc"
  16. )
  17. func TestAddDeviceWithoutRestart(t *testing.T) {
  18. log.Println("Cleaning...")
  19. err := removeAll("s1", "h1/index*", "s4", "h4/index*")
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. log.Println("Generating files...")
  24. err = generateFiles("s1", 100, 18, "../LICENSE")
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. p1 := startInstance(t, 1)
  29. defer checkedStop(t, p1)
  30. p4 := startInstance(t, 4)
  31. defer checkedStop(t, p4)
  32. if ok, err := p1.ConfigInSync(); err != nil || !ok {
  33. t.Fatal("p1 should be in sync;", ok, err)
  34. }
  35. if ok, err := p4.ConfigInSync(); err != nil || !ok {
  36. t.Fatal("p4 should be in sync;", ok, err)
  37. }
  38. // Add the p1 device to p4. Back up and restore p4's config first.
  39. log.Println("Adding p1 to p4...")
  40. os.Remove("h4/config.xml.orig")
  41. os.Rename("h4/config.xml", "h4/config.xml.orig")
  42. defer osutil.Rename("h4/config.xml.orig", "h4/config.xml")
  43. cfg, err := p4.GetConfig()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. devCfg := config.DeviceConfiguration{
  48. DeviceID: p1.ID(),
  49. Name: "s1",
  50. Addresses: []string{"127.0.0.1:22001"},
  51. Compression: protocol.CompressMetadata,
  52. }
  53. cfg.Devices = append(cfg.Devices, devCfg)
  54. cfg.Folders[0].Devices = append(cfg.Folders[0].Devices, config.FolderDeviceConfiguration{DeviceID: p1.ID()})
  55. if err = p4.PostConfig(cfg); err != nil {
  56. t.Fatal(err)
  57. }
  58. // The change should not require a restart, so the config should be "in sync"
  59. if ok, err := p4.ConfigInSync(); err != nil || !ok {
  60. t.Fatal("p4 should be in sync;", ok, err)
  61. }
  62. // Wait for the devices to connect and sync.
  63. log.Println("Waiting for p1 and p4 to connect and sync...")
  64. rc.AwaitSync("default", p1, p4)
  65. }
  66. func TestFolderWithoutRestart(t *testing.T) {
  67. log.Println("Cleaning...")
  68. err := removeAll("testfolder-p1", "testfolder-p4", "h1/index*", "h4/index*")
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. defer removeAll("testfolder-p1", "testfolder-p4")
  73. if err := generateFiles("testfolder-p1", 50, 18, "../LICENSE"); err != nil {
  74. t.Fatal(err)
  75. }
  76. p1 := startInstance(t, 1)
  77. defer checkedStop(t, p1)
  78. p4 := startInstance(t, 4)
  79. defer checkedStop(t, p4)
  80. if ok, err := p1.ConfigInSync(); err != nil || !ok {
  81. t.Fatal("p1 should be in sync;", ok, err)
  82. }
  83. if ok, err := p4.ConfigInSync(); err != nil || !ok {
  84. t.Fatal("p4 should be in sync;", ok, err)
  85. }
  86. // Add a new folder to p1, shared with p4. Back up and restore the config
  87. // first.
  88. log.Println("Adding testfolder to p1...")
  89. os.Remove("h1/config.xml.orig")
  90. os.Rename("h1/config.xml", "h1/config.xml.orig")
  91. defer osutil.Rename("h1/config.xml.orig", "h1/config.xml")
  92. cfg, err := p1.GetConfig()
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. newFolder := config.FolderConfiguration{
  97. ID: "testfolder",
  98. RawPath: "testfolder-p1",
  99. RescanIntervalS: 86400,
  100. Copiers: 1,
  101. Hashers: 1,
  102. Pullers: 1,
  103. Devices: []config.FolderDeviceConfiguration{{DeviceID: p4.ID()}},
  104. }
  105. newDevice := config.DeviceConfiguration{
  106. DeviceID: p4.ID(),
  107. Name: "p4",
  108. Addresses: []string{"dynamic"},
  109. Compression: protocol.CompressMetadata,
  110. }
  111. cfg.Folders = append(cfg.Folders, newFolder)
  112. cfg.Devices = append(cfg.Devices, newDevice)
  113. if err = p1.PostConfig(cfg); err != nil {
  114. t.Fatal(err)
  115. }
  116. // Add a new folder to p4, shared with p1. Back up and restore the config
  117. // first.
  118. log.Println("Adding testfolder to p4...")
  119. os.Remove("h4/config.xml.orig")
  120. os.Rename("h4/config.xml", "h4/config.xml.orig")
  121. defer osutil.Rename("h4/config.xml.orig", "h4/config.xml")
  122. cfg, err = p4.GetConfig()
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. newFolder.RawPath = "testfolder-p4"
  127. newFolder.Devices = []config.FolderDeviceConfiguration{{DeviceID: p1.ID()}}
  128. newDevice.DeviceID = p1.ID()
  129. newDevice.Name = "p1"
  130. newDevice.Addresses = []string{"127.0.0.1:22001"}
  131. cfg.Folders = append(cfg.Folders, newFolder)
  132. cfg.Devices = append(cfg.Devices, newDevice)
  133. if err = p4.PostConfig(cfg); err != nil {
  134. t.Fatal(err)
  135. }
  136. // The change should not require a restart, so the config should be "in sync"
  137. if ok, err := p1.ConfigInSync(); err != nil || !ok {
  138. t.Fatal("p1 should be in sync;", ok, err)
  139. }
  140. if ok, err := p4.ConfigInSync(); err != nil || !ok {
  141. t.Fatal("p4 should be in sync;", ok, err)
  142. }
  143. // The folder should start and scan - wait for the event that signals this
  144. // has happened.
  145. log.Println("Waiting for testfolder to scan...")
  146. since := 0
  147. outer:
  148. for {
  149. events, err := p4.Events(since)
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. for _, event := range events {
  154. if event.Type == "StateChanged" {
  155. data := event.Data.(map[string]interface{})
  156. folder := data["folder"].(string)
  157. from := data["from"].(string)
  158. to := data["to"].(string)
  159. if folder == "testfolder" && from == "scanning" && to == "idle" {
  160. break outer
  161. }
  162. }
  163. since = event.ID
  164. }
  165. }
  166. // It should sync to the other side successfully
  167. log.Println("Waiting for p1 and p4 to connect and sync...")
  168. rc.AwaitSync("testfolder", p1, p4)
  169. }