lib/model: Small fixes to test convenience functions (#5128)

This commit is contained in:
Simon Frei 2018-08-16 12:11:48 +02:00 committed by GitHub
parent f6da436f4b
commit 9028969617
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 115 additions and 71 deletions

View File

@ -129,12 +129,11 @@ func TestMain(m *testing.M) {
panic(err) panic(err)
} }
var wrapperPath string defaultCfgWrapper = createTmpWrapper(defaultCfg)
defaultCfgWrapper, wrapperPath = createTmpWrapper(defaultCfg)
exitCode := m.Run() exitCode := m.Run()
os.Remove(wrapperPath) os.Remove(defaultCfgWrapper.ConfigPath())
defaultFs.Remove(tmpName) defaultFs.Remove(tmpName)
defaultFs.RemoveAll(config.DefaultMarkerName) defaultFs.RemoveAll(config.DefaultMarkerName)
defaultFs.RemoveAll(tmpLocation) defaultFs.RemoveAll(tmpLocation)
@ -142,26 +141,26 @@ func TestMain(m *testing.M) {
os.Exit(exitCode) os.Exit(exitCode)
} }
func createTmpWrapper(cfg config.Configuration) (*config.Wrapper, string) { func createTmpWrapper(cfg config.Configuration) *config.Wrapper {
tmpFile, err := ioutil.TempFile(tmpLocation, "syncthing-testConfig-") tmpFile, err := ioutil.TempFile(tmpLocation, "syncthing-testConfig-")
if err != nil { if err != nil {
panic(err) panic(err)
} }
wrapper := config.Wrap(tmpFile.Name(), cfg) wrapper := config.Wrap(tmpFile.Name(), cfg)
tmpFile.Close() tmpFile.Close()
return wrapper, tmpFile.Name() return wrapper
} }
func newState(cfg config.Configuration) (*config.Wrapper, *Model) { func newState(cfg config.Configuration) (*config.Wrapper, *Model) {
db := db.OpenMemory() db := db.OpenMemory()
wcfg, path := createTmpWrapper(cfg) wcfg := createTmpWrapper(cfg)
defer os.Remove(path)
m := NewModel(wcfg, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m := NewModel(wcfg, protocol.LocalDeviceID, "syncthing", "dev", db, nil)
for _, folder := range cfg.Folders { for _, folder := range cfg.Folders {
if !folder.Paused { if !folder.Paused {
m.AddFolder(folder) m.AddFolder(folder)
m.StartFolder(folder.ID)
} }
} }
m.ServeBackground() m.ServeBackground()
@ -657,8 +656,8 @@ func TestClusterConfig(t *testing.T) {
db := db.OpenMemory() db := db.OpenMemory()
wrapper, path := createTmpWrapper(cfg) wrapper := createTmpWrapper(cfg)
defer os.Remove(path) defer os.Remove(wrapper.ConfigPath())
m := NewModel(wrapper, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m := NewModel(wrapper, protocol.LocalDeviceID, "syncthing", "dev", db, nil)
m.AddFolder(cfg.Folders[0]) m.AddFolder(cfg.Folders[0])
m.AddFolder(cfg.Folders[1]) m.AddFolder(cfg.Folders[1])
@ -752,6 +751,7 @@ func TestIntroducer(t *testing.T) {
}, },
}, },
}) })
defer os.Remove(wcfg.ConfigPath())
m.ClusterConfig(device1, protocol.ClusterConfig{ m.ClusterConfig(device1, protocol.ClusterConfig{
Folders: []protocol.Folder{ Folders: []protocol.Folder{
{ {
@ -804,6 +804,7 @@ func TestIntroducer(t *testing.T) {
}, },
}, },
}) })
defer os.Remove(wcfg.ConfigPath())
m.ClusterConfig(device1, protocol.ClusterConfig{ m.ClusterConfig(device1, protocol.ClusterConfig{
Folders: []protocol.Folder{ Folders: []protocol.Folder{
{ {
@ -862,6 +863,7 @@ func TestIntroducer(t *testing.T) {
}, },
}, },
}) })
defer os.Remove(wcfg.ConfigPath())
m.ClusterConfig(device1, protocol.ClusterConfig{}) m.ClusterConfig(device1, protocol.ClusterConfig{})
if _, ok := wcfg.Device(device2); ok { if _, ok := wcfg.Device(device2); ok {
@ -909,6 +911,7 @@ func TestIntroducer(t *testing.T) {
}, },
}, },
}) })
defer os.Remove(wcfg.ConfigPath())
m.ClusterConfig(device1, protocol.ClusterConfig{}) m.ClusterConfig(device1, protocol.ClusterConfig{})
if _, ok := wcfg.Device(device2); !ok { if _, ok := wcfg.Device(device2); !ok {
@ -955,6 +958,7 @@ func TestIntroducer(t *testing.T) {
}, },
}, },
}) })
defer os.Remove(wcfg.ConfigPath())
m.ClusterConfig(device1, protocol.ClusterConfig{ m.ClusterConfig(device1, protocol.ClusterConfig{
Folders: []protocol.Folder{ Folders: []protocol.Folder{
{ {
@ -1014,6 +1018,7 @@ func TestIntroducer(t *testing.T) {
}, },
}, },
}) })
defer os.Remove(wcfg.ConfigPath())
m.ClusterConfig(device1, protocol.ClusterConfig{}) m.ClusterConfig(device1, protocol.ClusterConfig{})
if _, ok := wcfg.Device(device2); !ok { if _, ok := wcfg.Device(device2); !ok {
@ -1060,6 +1065,7 @@ func TestIntroducer(t *testing.T) {
}, },
}, },
}) })
defer os.Remove(wcfg.ConfigPath())
m.ClusterConfig(device1, protocol.ClusterConfig{}) m.ClusterConfig(device1, protocol.ClusterConfig{})
if _, ok := wcfg.Device(device2); !ok { if _, ok := wcfg.Device(device2); !ok {
@ -1076,7 +1082,7 @@ func TestIntroducer(t *testing.T) {
} }
func TestIssue4897(t *testing.T) { func TestIssue4897(t *testing.T) {
_, m := newState(config.Configuration{ wcfg, m := newState(config.Configuration{
Devices: []config.DeviceConfiguration{ Devices: []config.DeviceConfiguration{
{ {
DeviceID: device1, DeviceID: device1,
@ -1094,6 +1100,7 @@ func TestIssue4897(t *testing.T) {
}, },
}, },
}) })
defer os.Remove(wcfg.ConfigPath())
cm := m.generateClusterConfig(device1) cm := m.generateClusterConfig(device1)
if l := len(cm.Folders); l != 1 { if l := len(cm.Folders); l != 1 {
@ -1103,6 +1110,7 @@ func TestIssue4897(t *testing.T) {
func TestIssue5063(t *testing.T) { func TestIssue5063(t *testing.T) {
wcfg, m := newState(defaultAutoAcceptCfg) wcfg, m := newState(defaultAutoAcceptCfg)
defer os.Remove(wcfg.ConfigPath())
addAndVerify := func(wg *sync.WaitGroup) { addAndVerify := func(wg *sync.WaitGroup) {
id := srand.String(8) id := srand.String(8)
@ -1136,7 +1144,8 @@ func TestAutoAcceptRejected(t *testing.T) {
for i := range tcfg.Devices { for i := range tcfg.Devices {
tcfg.Devices[i].AutoAcceptFolders = false tcfg.Devices[i].AutoAcceptFolders = false
} }
_, m := newState(tcfg) wcfg, m := newState(tcfg)
defer os.Remove(wcfg.ConfigPath())
id := srand.String(8) id := srand.String(8)
defer os.RemoveAll(filepath.Join("testdata", id)) defer os.RemoveAll(filepath.Join("testdata", id))
m.ClusterConfig(device1, protocol.ClusterConfig{ m.ClusterConfig(device1, protocol.ClusterConfig{
@ -1156,6 +1165,7 @@ func TestAutoAcceptRejected(t *testing.T) {
func TestAutoAcceptNewFolder(t *testing.T) { func TestAutoAcceptNewFolder(t *testing.T) {
// New folder // New folder
wcfg, m := newState(defaultAutoAcceptCfg) wcfg, m := newState(defaultAutoAcceptCfg)
defer os.Remove(wcfg.ConfigPath())
id := srand.String(8) id := srand.String(8)
defer os.RemoveAll(filepath.Join("testdata", id)) defer os.RemoveAll(filepath.Join("testdata", id))
m.ClusterConfig(device1, protocol.ClusterConfig{ m.ClusterConfig(device1, protocol.ClusterConfig{
@ -1173,6 +1183,7 @@ func TestAutoAcceptNewFolder(t *testing.T) {
func TestAutoAcceptNewFolderFromTwoDevices(t *testing.T) { func TestAutoAcceptNewFolderFromTwoDevices(t *testing.T) {
wcfg, m := newState(defaultAutoAcceptCfg) wcfg, m := newState(defaultAutoAcceptCfg)
defer os.Remove(wcfg.ConfigPath())
id := srand.String(8) id := srand.String(8)
defer os.RemoveAll(filepath.Join("testdata", id)) defer os.RemoveAll(filepath.Join("testdata", id))
m.ClusterConfig(device1, protocol.ClusterConfig{ m.ClusterConfig(device1, protocol.ClusterConfig{
@ -1207,6 +1218,7 @@ func TestAutoAcceptNewFolderFromOnlyOneDevice(t *testing.T) {
modifiedCfg := defaultAutoAcceptCfg.Copy() modifiedCfg := defaultAutoAcceptCfg.Copy()
modifiedCfg.Devices[2].AutoAcceptFolders = false modifiedCfg.Devices[2].AutoAcceptFolders = false
wcfg, m := newState(modifiedCfg) wcfg, m := newState(modifiedCfg)
defer os.Remove(wcfg.ConfigPath())
id := srand.String(8) id := srand.String(8)
defer os.RemoveAll(filepath.Join("testdata", id)) defer os.RemoveAll(filepath.Join("testdata", id))
m.ClusterConfig(device1, protocol.ClusterConfig{ m.ClusterConfig(device1, protocol.ClusterConfig{
@ -1260,7 +1272,8 @@ func TestAutoAcceptNewFolderPremutationsNoPanic(t *testing.T) {
fcfg.Paused = localFolderPaused fcfg.Paused = localFolderPaused
cfg.Folders = append(cfg.Folders, fcfg) cfg.Folders = append(cfg.Folders, fcfg)
} }
_, m := newState(cfg) wcfg, m := newState(cfg)
defer os.Remove(wcfg.ConfigPath())
m.ClusterConfig(device1, protocol.ClusterConfig{ m.ClusterConfig(device1, protocol.ClusterConfig{
Folders: []protocol.Folder{dev1folder}, Folders: []protocol.Folder{dev1folder},
}) })
@ -1279,6 +1292,7 @@ func TestAutoAcceptNewFolderPremutationsNoPanic(t *testing.T) {
func TestAutoAcceptMultipleFolders(t *testing.T) { func TestAutoAcceptMultipleFolders(t *testing.T) {
// Multiple new folders // Multiple new folders
wcfg, m := newState(defaultAutoAcceptCfg) wcfg, m := newState(defaultAutoAcceptCfg)
defer os.Remove(wcfg.ConfigPath())
id1 := srand.String(8) id1 := srand.String(8)
defer os.RemoveAll(filepath.Join("testdata", id1)) defer os.RemoveAll(filepath.Join("testdata", id1))
id2 := srand.String(8) id2 := srand.String(8)
@ -1318,6 +1332,7 @@ func TestAutoAcceptExistingFolder(t *testing.T) {
}, },
} }
wcfg, m := newState(tcfg) wcfg, m := newState(tcfg)
defer os.Remove(wcfg.ConfigPath())
if fcfg, ok := wcfg.Folder(id); !ok || fcfg.SharedWith(device1) { if fcfg, ok := wcfg.Folder(id); !ok || fcfg.SharedWith(device1) {
t.Error("missing folder, or shared", id) t.Error("missing folder, or shared", id)
} }
@ -1350,6 +1365,7 @@ func TestAutoAcceptNewAndExistingFolder(t *testing.T) {
}, },
} }
wcfg, m := newState(tcfg) wcfg, m := newState(tcfg)
defer os.Remove(wcfg.ConfigPath())
if fcfg, ok := wcfg.Folder(id1); !ok || fcfg.SharedWith(device1) { if fcfg, ok := wcfg.Folder(id1); !ok || fcfg.SharedWith(device1) {
t.Error("missing folder, or shared", id1) t.Error("missing folder, or shared", id1)
} }
@ -1390,6 +1406,7 @@ func TestAutoAcceptAlreadyShared(t *testing.T) {
}, },
} }
wcfg, m := newState(tcfg) wcfg, m := newState(tcfg)
defer os.Remove(wcfg.ConfigPath())
if fcfg, ok := wcfg.Folder(id); !ok || !fcfg.SharedWith(device1) { if fcfg, ok := wcfg.Folder(id); !ok || !fcfg.SharedWith(device1) {
t.Error("missing folder, or not shared", id) t.Error("missing folder, or not shared", id)
} }
@ -1415,6 +1432,7 @@ func TestAutoAcceptNameConflict(t *testing.T) {
defer os.RemoveAll(filepath.Join("testdata", id)) defer os.RemoveAll(filepath.Join("testdata", id))
defer os.RemoveAll(filepath.Join("testdata", label)) defer os.RemoveAll(filepath.Join("testdata", label))
wcfg, m := newState(defaultAutoAcceptCfg) wcfg, m := newState(defaultAutoAcceptCfg)
defer os.Remove(wcfg.ConfigPath())
m.ClusterConfig(device1, protocol.ClusterConfig{ m.ClusterConfig(device1, protocol.ClusterConfig{
Folders: []protocol.Folder{ Folders: []protocol.Folder{
{ {
@ -1431,6 +1449,7 @@ func TestAutoAcceptNameConflict(t *testing.T) {
func TestAutoAcceptPrefersLabel(t *testing.T) { func TestAutoAcceptPrefersLabel(t *testing.T) {
// Prefers label, falls back to ID. // Prefers label, falls back to ID.
wcfg, m := newState(defaultAutoAcceptCfg) wcfg, m := newState(defaultAutoAcceptCfg)
defer os.Remove(wcfg.ConfigPath())
id := srand.String(8) id := srand.String(8)
label := srand.String(8) label := srand.String(8)
defer os.RemoveAll(filepath.Join("testdata", id)) defer os.RemoveAll(filepath.Join("testdata", id))
@ -1451,6 +1470,7 @@ func TestAutoAcceptPrefersLabel(t *testing.T) {
func TestAutoAcceptFallsBackToID(t *testing.T) { func TestAutoAcceptFallsBackToID(t *testing.T) {
// Prefers label, falls back to ID. // Prefers label, falls back to ID.
wcfg, m := newState(defaultAutoAcceptCfg) wcfg, m := newState(defaultAutoAcceptCfg)
defer os.Remove(wcfg.ConfigPath())
id := srand.String(8) id := srand.String(8)
label := srand.String(8) label := srand.String(8)
os.MkdirAll(filepath.Join("testdata", label), 0777) os.MkdirAll(filepath.Join("testdata", label), 0777)
@ -1487,6 +1507,7 @@ func TestAutoAcceptPausedWhenFolderConfigChanged(t *testing.T) {
}) })
tcfg.Folders = []config.FolderConfiguration{fcfg} tcfg.Folders = []config.FolderConfiguration{fcfg}
wcfg, m := newState(tcfg) wcfg, m := newState(tcfg)
defer os.Remove(wcfg.ConfigPath())
if fcfg, ok := wcfg.Folder(id); !ok || !fcfg.SharedWith(device1) { if fcfg, ok := wcfg.Folder(id); !ok || !fcfg.SharedWith(device1) {
t.Error("missing folder, or not shared", id) t.Error("missing folder, or not shared", id)
} }
@ -1543,6 +1564,7 @@ func TestAutoAcceptPausedWhenFolderConfigNotChanged(t *testing.T) {
}, fcfg.Devices...) // Need to ensure this device order to avoid folder restart. }, fcfg.Devices...) // Need to ensure this device order to avoid folder restart.
tcfg.Folders = []config.FolderConfiguration{fcfg} tcfg.Folders = []config.FolderConfiguration{fcfg}
wcfg, m := newState(tcfg) wcfg, m := newState(tcfg)
defer os.Remove(wcfg.ConfigPath())
if fcfg, ok := wcfg.Folder(id); !ok || !fcfg.SharedWith(device1) { if fcfg, ok := wcfg.Folder(id); !ok || !fcfg.SharedWith(device1) {
t.Error("missing folder, or not shared", id) t.Error("missing folder, or not shared", id)
} }
@ -1719,7 +1741,7 @@ func TestROScanRecovery(t *testing.T) {
RescanIntervalS: 1, RescanIntervalS: 1,
MarkerName: config.DefaultMarkerName, MarkerName: config.DefaultMarkerName,
} }
cfg, path := createTmpWrapper(config.Configuration{ cfg := createTmpWrapper(config.Configuration{
Folders: []config.FolderConfiguration{fcfg}, Folders: []config.FolderConfiguration{fcfg},
Devices: []config.DeviceConfiguration{ Devices: []config.DeviceConfiguration{
{ {
@ -1727,7 +1749,7 @@ func TestROScanRecovery(t *testing.T) {
}, },
}, },
}) })
defer os.Remove(path) defer os.Remove(cfg.ConfigPath())
os.RemoveAll(fcfg.Path) os.RemoveAll(fcfg.Path)
@ -1808,7 +1830,7 @@ func TestRWScanRecovery(t *testing.T) {
RescanIntervalS: 1, RescanIntervalS: 1,
MarkerName: config.DefaultMarkerName, MarkerName: config.DefaultMarkerName,
} }
cfg, path := createTmpWrapper(config.Configuration{ cfg := createTmpWrapper(config.Configuration{
Folders: []config.FolderConfiguration{fcfg}, Folders: []config.FolderConfiguration{fcfg},
Devices: []config.DeviceConfiguration{ Devices: []config.DeviceConfiguration{
{ {
@ -1816,7 +1838,7 @@ func TestRWScanRecovery(t *testing.T) {
}, },
}, },
}) })
defer os.Remove(path) defer os.Remove(cfg.ConfigPath())
os.RemoveAll(fcfg.Path) os.RemoveAll(fcfg.Path)
@ -2521,8 +2543,8 @@ func TestIssue4357(t *testing.T) {
db := db.OpenMemory() db := db.OpenMemory()
cfg := defaultCfgWrapper.RawCopy() cfg := defaultCfgWrapper.RawCopy()
// Create a separate wrapper not to pollute other tests. // Create a separate wrapper not to pollute other tests.
wrapper, path := createTmpWrapper(config.Configuration{}) wrapper := createTmpWrapper(config.Configuration{})
defer os.Remove(path) defer os.Remove(wrapper.ConfigPath())
m := NewModel(wrapper, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m := NewModel(wrapper, protocol.LocalDeviceID, "syncthing", "dev", db, nil)
m.ServeBackground() m.ServeBackground()
defer m.Stop() defer m.Stop()
@ -2744,8 +2766,8 @@ func TestSharedWithClearedOnDisconnect(t *testing.T) {
}, },
} }
wcfg, path := createTmpWrapper(cfg) wcfg := createTmpWrapper(cfg)
defer os.Remove(path) defer os.Remove(wcfg.ConfigPath())
m := NewModel(wcfg, protocol.LocalDeviceID, "syncthing", "dev", dbi, nil) m := NewModel(wcfg, protocol.LocalDeviceID, "syncthing", "dev", dbi, nil)
m.AddFolder(fcfg) m.AddFolder(fcfg)
@ -2982,8 +3004,8 @@ func TestNoRequestsFromPausedDevices(t *testing.T) {
}, },
} }
wcfg, path := createTmpWrapper(cfg) wcfg := createTmpWrapper(cfg)
defer os.Remove(path) defer os.Remove(wcfg.ConfigPath())
m := NewModel(wcfg, protocol.LocalDeviceID, "syncthing", "dev", dbi, nil) m := NewModel(wcfg, protocol.LocalDeviceID, "syncthing", "dev", dbi, nil)
m.AddFolder(fcfg) m.AddFolder(fcfg)
@ -3248,7 +3270,7 @@ func TestCustomMarkerName(t *testing.T) {
RescanIntervalS: 1, RescanIntervalS: 1,
MarkerName: "myfile", MarkerName: "myfile",
} }
cfg, path := createTmpWrapper(config.Configuration{ cfg := createTmpWrapper(config.Configuration{
Folders: []config.FolderConfiguration{fcfg}, Folders: []config.FolderConfiguration{fcfg},
Devices: []config.DeviceConfiguration{ Devices: []config.DeviceConfiguration{
{ {
@ -3256,7 +3278,7 @@ func TestCustomMarkerName(t *testing.T) {
}, },
}, },
}) })
defer os.Remove(path) defer os.Remove(cfg.ConfigPath())
os.RemoveAll(fcfg.Path) os.RemoveAll(fcfg.Path)
defer os.RemoveAll(fcfg.Path) defer os.RemoveAll(fcfg.Path)
@ -3466,8 +3488,8 @@ func TestVersionRestore(t *testing.T) {
rawConfig := config.Configuration{ rawConfig := config.Configuration{
Folders: []config.FolderConfiguration{fcfg}, Folders: []config.FolderConfiguration{fcfg},
} }
cfg, path := createTmpWrapper(rawConfig) cfg := createTmpWrapper(rawConfig)
defer os.Remove(path) defer os.Remove(cfg.ConfigPath())
m := NewModel(cfg, protocol.LocalDeviceID, "syncthing", "dev", dbi, nil) m := NewModel(cfg, protocol.LocalDeviceID, "syncthing", "dev", dbi, nil)
m.AddFolder(fcfg) m.AddFolder(fcfg)
@ -3660,8 +3682,8 @@ func TestVersionRestore(t *testing.T) {
func TestPausedFolders(t *testing.T) { func TestPausedFolders(t *testing.T) {
// Create a separate wrapper not to pollute other tests. // Create a separate wrapper not to pollute other tests.
cfg := defaultCfgWrapper.RawCopy() cfg := defaultCfgWrapper.RawCopy()
wrapper, path := createTmpWrapper(cfg) wrapper := createTmpWrapper(cfg)
defer os.Remove(path) defer os.Remove(wrapper.ConfigPath())
db := db.OpenMemory() db := db.OpenMemory()
m := NewModel(wrapper, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m := NewModel(wrapper, protocol.LocalDeviceID, "syncthing", "dev", db, nil)
@ -3694,8 +3716,8 @@ func TestPausedFolders(t *testing.T) {
func TestIssue4094(t *testing.T) { func TestIssue4094(t *testing.T) {
db := db.OpenMemory() db := db.OpenMemory()
// Create a separate wrapper not to pollute other tests. // Create a separate wrapper not to pollute other tests.
wrapper, path := createTmpWrapper(config.Configuration{}) wrapper := createTmpWrapper(config.Configuration{})
defer os.Remove(path) defer os.Remove(wrapper.ConfigPath())
m := NewModel(wrapper, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m := NewModel(wrapper, protocol.LocalDeviceID, "syncthing", "dev", db, nil)
m.ServeBackground() m.ServeBackground()
defer m.Stop() defer m.Stop()
@ -3731,8 +3753,8 @@ func TestIssue4094(t *testing.T) {
func TestIssue4903(t *testing.T) { func TestIssue4903(t *testing.T) {
db := db.OpenMemory() db := db.OpenMemory()
// Create a separate wrapper not to pollute other tests. // Create a separate wrapper not to pollute other tests.
wrapper, path := createTmpWrapper(config.Configuration{}) wrapper := createTmpWrapper(config.Configuration{})
defer os.Remove(path) defer os.Remove(wrapper.ConfigPath())
m := NewModel(wrapper, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m := NewModel(wrapper, protocol.LocalDeviceID, "syncthing", "dev", db, nil)
m.ServeBackground() m.ServeBackground()
defer m.Stop() defer m.Stop()

View File

@ -54,11 +54,11 @@ func expectTimeout(w *events.Subscription, t *testing.T) {
func TestProgressEmitter(t *testing.T) { func TestProgressEmitter(t *testing.T) {
w := events.Default.Subscribe(events.DownloadProgress) w := events.Default.Subscribe(events.DownloadProgress)
c, path := createTmpWrapper(config.Configuration{}) c := createTmpWrapper(config.Configuration{})
defer os.Remove(c.ConfigPath())
c.SetOptions(config.OptionsConfiguration{ c.SetOptions(config.OptionsConfiguration{
ProgressUpdateIntervalS: 0, ProgressUpdateIntervalS: 0,
}) })
defer os.Remove(path)
p := NewProgressEmitter(c) p := NewProgressEmitter(c)
go p.Serve() go p.Serve()
@ -103,12 +103,12 @@ func TestProgressEmitter(t *testing.T) {
} }
func TestSendDownloadProgressMessages(t *testing.T) { func TestSendDownloadProgressMessages(t *testing.T) {
c, path := createTmpWrapper(config.Configuration{}) c := createTmpWrapper(config.Configuration{})
defer os.Remove(c.ConfigPath())
c.SetOptions(config.OptionsConfiguration{ c.SetOptions(config.OptionsConfiguration{
ProgressUpdateIntervalS: 0, ProgressUpdateIntervalS: 0,
TempIndexMinBlocks: 10, TempIndexMinBlocks: 10,
}) })
defer os.Remove(path)
fc := &fakeConnection{} fc := &fakeConnection{}

View File

@ -29,9 +29,12 @@ func TestRequestSimple(t *testing.T) {
// Verify that the model performs a request and creates a file based on // Verify that the model performs a request and creates a file based on
// an incoming index update. // an incoming index update.
m, fc, tmpDir := setupModelWithConnection() m, fc, tmpDir, w := setupModelWithConnection()
defer m.Stop() defer func() {
defer os.RemoveAll(tmpDir) m.Stop()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
// We listen for incoming index updates and trigger when we see one for // We listen for incoming index updates and trigger when we see one for
// the expected test file. // the expected test file.
@ -67,9 +70,12 @@ func TestSymlinkTraversalRead(t *testing.T) {
return return
} }
m, fc, tmpDir := setupModelWithConnection() m, fc, tmpDir, w := setupModelWithConnection()
defer m.Stop() defer func() {
defer os.RemoveAll(tmpDir) m.Stop()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
// We listen for incoming index updates and trigger when we see one for // We listen for incoming index updates and trigger when we see one for
// the expected test file. // the expected test file.
@ -107,9 +113,12 @@ func TestSymlinkTraversalWrite(t *testing.T) {
return return
} }
m, fc, tmpDir := setupModelWithConnection() m, fc, tmpDir, w := setupModelWithConnection()
defer m.Stop() defer func() {
defer os.RemoveAll(tmpDir) m.Stop()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
// We listen for incoming index updates and trigger when we see one for // We listen for incoming index updates and trigger when we see one for
// the expected names. // the expected names.
@ -167,9 +176,12 @@ func TestSymlinkTraversalWrite(t *testing.T) {
func TestRequestCreateTmpSymlink(t *testing.T) { func TestRequestCreateTmpSymlink(t *testing.T) {
// Test that an update for a temporary file is invalidated // Test that an update for a temporary file is invalidated
m, fc, tmpDir := setupModelWithConnection() m, fc, tmpDir, w := setupModelWithConnection()
defer m.Stop() defer func() {
defer os.RemoveAll(tmpDir) m.Stop()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
// We listen for incoming index updates and trigger when we see one for // We listen for incoming index updates and trigger when we see one for
// the expected test file. // the expected test file.
@ -221,8 +233,8 @@ func TestRequestVersioningSymlinkAttack(t *testing.T) {
cfg.Folders[0].Versioning = config.VersioningConfiguration{ cfg.Folders[0].Versioning = config.VersioningConfiguration{
Type: "trashcan", Type: "trashcan",
} }
w, path := createTmpWrapper(cfg) w := createTmpWrapper(cfg)
defer os.Remove(path) defer os.Remove(w.ConfigPath())
db := db.OpenMemory() db := db.OpenMemory()
m := NewModel(w, device1, "syncthing", "dev", db, nil) m := NewModel(w, device1, "syncthing", "dev", db, nil)
@ -278,7 +290,7 @@ func TestRequestVersioningSymlinkAttack(t *testing.T) {
for updates := 0; updates < 1; updates += <-idx { for updates := 0; updates < 1; updates += <-idx {
} }
path = filepath.Join(tmpdir, "test") path := filepath.Join(tmpdir, "test")
if _, err := os.Lstat(path); !os.IsNotExist(err) { if _, err := os.Lstat(path); !os.IsNotExist(err) {
t.Fatal("File escaped to", path) t.Fatal("File escaped to", path)
} }
@ -298,8 +310,6 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
t.Helper() t.Helper()
tmpDir := createTmpDir() tmpDir := createTmpDir()
defer os.RemoveAll(tmpDir)
cfg := defaultCfgWrapper.RawCopy() cfg := defaultCfgWrapper.RawCopy()
cfg.Devices = append(cfg.Devices, config.NewDeviceConfiguration(device2, "device2")) cfg.Devices = append(cfg.Devices, config.NewDeviceConfiguration(device2, "device2"))
cfg.Folders[0] = config.NewFolderConfiguration(protocol.LocalDeviceID, "default", "default", fs.FilesystemTypeBasic, tmpDir) cfg.Folders[0] = config.NewFolderConfiguration(protocol.LocalDeviceID, "default", "default", fs.FilesystemTypeBasic, tmpDir)
@ -308,8 +318,12 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
{DeviceID: device2}, {DeviceID: device2},
} }
cfg.Folders[0].Type = ft cfg.Folders[0].Type = ft
m, fc := setupModelWithConnectionManual(cfg) m, fc, w := setupModelWithConnectionManual(cfg)
defer m.Stop() defer func() {
m.Stop()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
// Reach in and update the ignore matcher to one that always does // Reach in and update the ignore matcher to one that always does
// reloads when asked to, instead of checking file mtimes. This is // reloads when asked to, instead of checking file mtimes. This is
@ -426,9 +440,12 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
} }
func TestIssue4841(t *testing.T) { func TestIssue4841(t *testing.T) {
m, fc, tmpDir := setupModelWithConnection() m, fc, tmpDir, w := setupModelWithConnection()
defer m.Stop() defer func() {
defer os.RemoveAll(tmpDir) m.Stop()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
received := make(chan protocol.FileInfo) received := make(chan protocol.FileInfo)
fc.mut.Lock() fc.mut.Lock()
@ -465,9 +482,12 @@ func TestIssue4841(t *testing.T) {
} }
func TestRescanIfHaveInvalidContent(t *testing.T) { func TestRescanIfHaveInvalidContent(t *testing.T) {
m, fc, tmpDir := setupModelWithConnection() m, fc, tmpDir, w := setupModelWithConnection()
defer m.Stop() defer func() {
defer os.RemoveAll(tmpDir) m.Stop()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
payload := []byte("hello") payload := []byte("hello")
@ -532,9 +552,12 @@ func TestRescanIfHaveInvalidContent(t *testing.T) {
} }
func TestParentDeletion(t *testing.T) { func TestParentDeletion(t *testing.T) {
m, fc, tmpDir := setupModelWithConnection() m, fc, tmpDir, w := setupModelWithConnection()
defer m.Stop() defer func() {
defer os.RemoveAll(tmpDir) m.Stop()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
parent := "foo" parent := "foo"
child := filepath.Join(parent, "bar") child := filepath.Join(parent, "bar")
@ -608,7 +631,7 @@ func TestParentDeletion(t *testing.T) {
} }
} }
func setupModelWithConnection() (*Model, *fakeConnection, string) { func setupModelWithConnection() (*Model, *fakeConnection, string, *config.Wrapper) {
tmpDir := createTmpDir() tmpDir := createTmpDir()
cfg := defaultCfgWrapper.RawCopy() cfg := defaultCfgWrapper.RawCopy()
cfg.Devices = append(cfg.Devices, config.NewDeviceConfiguration(device2, "device2")) cfg.Devices = append(cfg.Devices, config.NewDeviceConfiguration(device2, "device2"))
@ -617,13 +640,12 @@ func setupModelWithConnection() (*Model, *fakeConnection, string) {
{DeviceID: device1}, {DeviceID: device1},
{DeviceID: device2}, {DeviceID: device2},
} }
m, fc := setupModelWithConnectionManual(cfg) m, fc, w := setupModelWithConnectionManual(cfg)
return m, fc, tmpDir return m, fc, tmpDir, w
} }
func setupModelWithConnectionManual(cfg config.Configuration) (*Model, *fakeConnection) { func setupModelWithConnectionManual(cfg config.Configuration) (*Model, *fakeConnection, *config.Wrapper) {
w, path := createTmpWrapper(cfg) w := createTmpWrapper(cfg)
defer os.Remove(path)
db := db.OpenMemory() db := db.OpenMemory()
m := NewModel(w, device1, "syncthing", "dev", db, nil) m := NewModel(w, device1, "syncthing", "dev", db, nil)
@ -636,7 +658,7 @@ func setupModelWithConnectionManual(cfg config.Configuration) (*Model, *fakeConn
m.ScanFolder("default") m.ScanFolder("default")
return m, fc return m, fc, w
} }
func createTmpDir() string { func createTmpDir() string {