testutils_test.go 4.4 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/fs"
  14. "github.com/syncthing/syncthing/lib/protocol"
  15. )
  16. var (
  17. myID, device1, device2 protocol.DeviceID
  18. defaultCfgWrapper config.Wrapper
  19. defaultFolderConfig config.FolderConfiguration
  20. defaultFs fs.Filesystem
  21. defaultCfg config.Configuration
  22. defaultAutoAcceptCfg config.Configuration
  23. )
  24. func init() {
  25. myID, _ = protocol.DeviceIDFromString("ZNWFSWE-RWRV2BD-45BLMCV-LTDE2UR-4LJDW6J-R5BPWEB-TXD27XJ-IZF5RA4")
  26. device1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  27. device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
  28. defaultFs = fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata")
  29. defaultFolderConfig = testFolderConfig("testdata")
  30. defaultCfgWrapper = createTmpWrapper(config.New(myID))
  31. _, _ = defaultCfgWrapper.SetDevice(config.NewDeviceConfiguration(device1, "device1"))
  32. _, _ = defaultCfgWrapper.SetFolder(defaultFolderConfig)
  33. opts := defaultCfgWrapper.Options()
  34. opts.KeepTemporariesH = 1
  35. _, _ = defaultCfgWrapper.SetOptions(opts)
  36. defaultCfg = defaultCfgWrapper.RawCopy()
  37. defaultAutoAcceptCfg = config.Configuration{
  38. Devices: []config.DeviceConfiguration{
  39. {
  40. DeviceID: myID, // self
  41. },
  42. {
  43. DeviceID: device1,
  44. AutoAcceptFolders: true,
  45. },
  46. {
  47. DeviceID: device2,
  48. AutoAcceptFolders: true,
  49. },
  50. },
  51. Options: config.OptionsConfiguration{
  52. DefaultFolderPath: ".",
  53. },
  54. }
  55. }
  56. func tmpDefaultWrapper() (config.Wrapper, config.FolderConfiguration) {
  57. w := createTmpWrapper(defaultCfgWrapper.RawCopy())
  58. fcfg := testFolderConfigTmp()
  59. _, _ = w.SetFolder(fcfg)
  60. return w, fcfg
  61. }
  62. func testFolderConfigTmp() config.FolderConfiguration {
  63. tmpDir := createTmpDir()
  64. return testFolderConfig(tmpDir)
  65. }
  66. func testFolderConfig(path string) config.FolderConfiguration {
  67. cfg := config.NewFolderConfiguration(myID, "default", "default", fs.FilesystemTypeBasic, path)
  68. cfg.FSWatcherEnabled = false
  69. cfg.Devices = append(cfg.Devices, config.FolderDeviceConfiguration{DeviceID: device1})
  70. return cfg
  71. }
  72. func setupModelWithConnection() (*model, *fakeConnection, config.FolderConfiguration) {
  73. w, fcfg := tmpDefaultWrapper()
  74. m, fc := setupModelWithConnectionFromWrapper(w)
  75. return m, fc, fcfg
  76. }
  77. func setupModelWithConnectionFromWrapper(w config.Wrapper) (*model, *fakeConnection) {
  78. m := setupModel(w)
  79. fc := addFakeConn(m, device1)
  80. fc.folder = "default"
  81. _ = m.ScanFolder("default")
  82. return m, fc
  83. }
  84. func setupModel(w config.Wrapper) *model {
  85. db := db.OpenMemory()
  86. m := newModel(w, myID, "syncthing", "dev", db, nil)
  87. m.ServeBackground()
  88. for id, cfg := range w.Folders() {
  89. if !cfg.Paused {
  90. m.AddFolder(cfg)
  91. m.StartFolder(id)
  92. }
  93. }
  94. m.ScanFolders()
  95. return m
  96. }
  97. func newModel(cfg config.Wrapper, id protocol.DeviceID, clientName, clientVersion string, ldb *db.Lowlevel, protectedFiles []string) *model {
  98. return NewModel(cfg, id, clientName, clientVersion, ldb, protectedFiles).(*model)
  99. }
  100. func cleanupModel(m *model) {
  101. m.Stop()
  102. m.db.Close()
  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. }