testutils_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright (C) 2016 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. package model
  7. import (
  8. "io/ioutil"
  9. "os"
  10. "time"
  11. "github.com/syncthing/syncthing/lib/config"
  12. "github.com/syncthing/syncthing/lib/db"
  13. "github.com/syncthing/syncthing/lib/events"
  14. "github.com/syncthing/syncthing/lib/fs"
  15. "github.com/syncthing/syncthing/lib/protocol"
  16. )
  17. var (
  18. myID, device1, device2 protocol.DeviceID
  19. defaultCfgWrapper config.Wrapper
  20. defaultFolderConfig config.FolderConfiguration
  21. defaultFs fs.Filesystem
  22. defaultCfg config.Configuration
  23. defaultAutoAcceptCfg config.Configuration
  24. )
  25. func init() {
  26. myID, _ = protocol.DeviceIDFromString("ZNWFSWE-RWRV2BD-45BLMCV-LTDE2UR-4LJDW6J-R5BPWEB-TXD27XJ-IZF5RA4")
  27. device1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  28. device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
  29. defaultFs = fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata")
  30. defaultFolderConfig = testFolderConfig("testdata")
  31. defaultCfgWrapper = createTmpWrapper(config.New(myID))
  32. _, _ = defaultCfgWrapper.SetDevice(config.NewDeviceConfiguration(device1, "device1"))
  33. _, _ = defaultCfgWrapper.SetFolder(defaultFolderConfig)
  34. opts := defaultCfgWrapper.Options()
  35. opts.KeepTemporariesH = 1
  36. _, _ = defaultCfgWrapper.SetOptions(opts)
  37. defaultCfg = defaultCfgWrapper.RawCopy()
  38. defaultAutoAcceptCfg = config.Configuration{
  39. Devices: []config.DeviceConfiguration{
  40. {
  41. DeviceID: myID, // self
  42. },
  43. {
  44. DeviceID: device1,
  45. AutoAcceptFolders: true,
  46. },
  47. {
  48. DeviceID: device2,
  49. AutoAcceptFolders: true,
  50. },
  51. },
  52. Options: config.OptionsConfiguration{
  53. DefaultFolderPath: ".",
  54. },
  55. }
  56. }
  57. func tmpDefaultWrapper() (config.Wrapper, config.FolderConfiguration) {
  58. w := createTmpWrapper(defaultCfgWrapper.RawCopy())
  59. fcfg := testFolderConfigTmp()
  60. _, _ = w.SetFolder(fcfg)
  61. return w, fcfg
  62. }
  63. func testFolderConfigTmp() config.FolderConfiguration {
  64. tmpDir := createTmpDir()
  65. return testFolderConfig(tmpDir)
  66. }
  67. func testFolderConfig(path string) config.FolderConfiguration {
  68. cfg := config.NewFolderConfiguration(myID, "default", "default", fs.FilesystemTypeBasic, path)
  69. cfg.FSWatcherEnabled = false
  70. cfg.Devices = append(cfg.Devices, config.FolderDeviceConfiguration{DeviceID: device1})
  71. return cfg
  72. }
  73. func setupModelWithConnection() (*model, *fakeConnection, config.FolderConfiguration) {
  74. w, fcfg := tmpDefaultWrapper()
  75. m, fc := setupModelWithConnectionFromWrapper(w)
  76. return m, fc, fcfg
  77. }
  78. func setupModelWithConnectionFromWrapper(w config.Wrapper) (*model, *fakeConnection) {
  79. m := setupModel(w)
  80. fc := addFakeConn(m, device1)
  81. fc.folder = "default"
  82. _ = m.ScanFolder("default")
  83. return m, fc
  84. }
  85. func setupModel(w config.Wrapper) *model {
  86. db := db.OpenMemory()
  87. m := newModel(w, myID, "syncthing", "dev", db, nil)
  88. m.ServeBackground()
  89. for id, cfg := range w.Folders() {
  90. if !cfg.Paused {
  91. m.AddFolder(cfg)
  92. m.StartFolder(id)
  93. }
  94. }
  95. m.ScanFolders()
  96. return m
  97. }
  98. func newModel(cfg config.Wrapper, id protocol.DeviceID, clientName, clientVersion string, ldb *db.Lowlevel, protectedFiles []string) *model {
  99. evLogger := events.NewLogger()
  100. m := NewModel(cfg, id, clientName, clientVersion, ldb, protectedFiles, evLogger).(*model)
  101. go evLogger.Serve()
  102. return m
  103. }
  104. func cleanupModel(m *model) {
  105. m.Stop()
  106. m.db.Close()
  107. m.evLogger.Stop()
  108. os.Remove(m.cfg.ConfigPath())
  109. }
  110. func cleanupModelAndRemoveDir(m *model, dir string) {
  111. cleanupModel(m)
  112. os.RemoveAll(dir)
  113. }
  114. func createTmpDir() string {
  115. tmpDir, err := ioutil.TempDir("", "syncthing_testFolder-")
  116. if err != nil {
  117. panic("Failed to create temporary testing dir")
  118. }
  119. return tmpDir
  120. }
  121. type alwaysChangedKey struct {
  122. fs fs.Filesystem
  123. name string
  124. }
  125. // alwaysChanges is an ignore.ChangeDetector that always returns true on Changed()
  126. type alwaysChanged struct {
  127. seen map[alwaysChangedKey]struct{}
  128. }
  129. func newAlwaysChanged() *alwaysChanged {
  130. return &alwaysChanged{
  131. seen: make(map[alwaysChangedKey]struct{}),
  132. }
  133. }
  134. func (c *alwaysChanged) Remember(fs fs.Filesystem, name string, _ time.Time) {
  135. c.seen[alwaysChangedKey{fs, name}] = struct{}{}
  136. }
  137. func (c *alwaysChanged) Reset() {
  138. c.seen = make(map[alwaysChangedKey]struct{})
  139. }
  140. func (c *alwaysChanged) Seen(fs fs.Filesystem, name string) bool {
  141. _, ok := c.seen[alwaysChangedKey{fs, name}]
  142. return ok
  143. }
  144. func (c *alwaysChanged) Changed() bool {
  145. return true
  146. }