testutils_test.go 4.0 KB

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