testutils_test.go 4.5 KB

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