testutils_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. m.ScanFolders()
  90. return m
  91. }
  92. func newModel(cfg config.Wrapper, id protocol.DeviceID, clientName, clientVersion string, ldb *db.Lowlevel, protectedFiles []string) *model {
  93. evLogger := events.NewLogger()
  94. m := NewModel(cfg, id, clientName, clientVersion, ldb, protectedFiles, evLogger).(*model)
  95. go evLogger.Serve()
  96. return m
  97. }
  98. func cleanupModel(m *model) {
  99. m.Stop()
  100. m.db.Close()
  101. m.evLogger.Stop()
  102. os.Remove(m.cfg.ConfigPath())
  103. }
  104. func cleanupModelAndRemoveDir(m *model, dir string) {
  105. cleanupModel(m)
  106. os.RemoveAll(dir)
  107. }
  108. func createTmpDir() string {
  109. tmpDir, err := ioutil.TempDir("", "syncthing_testFolder-")
  110. if err != nil {
  111. panic("Failed to create temporary testing dir")
  112. }
  113. return tmpDir
  114. }
  115. type alwaysChangedKey struct {
  116. fs fs.Filesystem
  117. name string
  118. }
  119. // alwaysChanges is an ignore.ChangeDetector that always returns true on Changed()
  120. type alwaysChanged struct {
  121. seen map[alwaysChangedKey]struct{}
  122. }
  123. func newAlwaysChanged() *alwaysChanged {
  124. return &alwaysChanged{
  125. seen: make(map[alwaysChangedKey]struct{}),
  126. }
  127. }
  128. func (c *alwaysChanged) Remember(fs fs.Filesystem, name string, _ time.Time) {
  129. c.seen[alwaysChangedKey{fs, name}] = struct{}{}
  130. }
  131. func (c *alwaysChanged) Reset() {
  132. c.seen = make(map[alwaysChangedKey]struct{})
  133. }
  134. func (c *alwaysChanged) Seen(fs fs.Filesystem, name string) bool {
  135. _, ok := c.seen[alwaysChangedKey{fs, name}]
  136. return ok
  137. }
  138. func (c *alwaysChanged) Changed() bool {
  139. return true
  140. }