testutils_test.go 3.3 KB

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