lib/model: Introduce setupModel test utility (#5524)

This commit is contained in:
Simon Frei 2019-02-12 13:18:13 +01:00 committed by Audrius Butkevicius
parent dc929946fe
commit d85ef949be
2 changed files with 43 additions and 154 deletions

View File

@ -153,36 +153,36 @@ func createTmpWrapper(cfg config.Configuration) *config.Wrapper {
}
func newState(cfg config.Configuration) (*config.Wrapper, *Model) {
db := db.OpenMemory()
wcfg := createTmpWrapper(cfg)
m := NewModel(wcfg, myID, "syncthing", "dev", db, nil)
for _, folder := range cfg.Folders {
if !folder.Paused {
m.AddFolder(folder)
m.StartFolder(folder.ID)
}
}
m.ServeBackground()
m := setupModel(wcfg)
for _, dev := range cfg.Devices {
m.AddConnection(&fakeConnection{id: dev.DeviceID}, protocol.HelloResult{})
}
return wcfg, m
}
func TestRequest(t *testing.T) {
func setupModel(w *config.Wrapper) *Model {
db := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", db, nil)
// device1 shares default, but device2 doesn't
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
m := NewModel(w, myID, "syncthing", "dev", db, nil)
m.ServeBackground()
for id, cfg := range w.Folders() {
if !cfg.Paused {
m.AddFolder(cfg)
m.StartFolder(id)
}
}
m.ScanFolders()
return m
}
func TestRequest(t *testing.T) {
m := setupModel(defaultCfgWrapper)
defer m.Stop()
m.ScanFolder("default")
// Existing, shared file
res, err := m.Request(device1, "default", "foo", 6, 0, nil, 0, false)
@ -250,11 +250,7 @@ func BenchmarkIndex_100(b *testing.B) {
}
func benchmarkIndex(b *testing.B, nfiles int) {
db := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", db, nil)
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
m.ServeBackground()
m := setupModel(defaultCfgWrapper)
defer m.Stop()
files := genFiles(nfiles)
@ -280,11 +276,7 @@ func BenchmarkIndexUpdate_10000_1(b *testing.B) {
}
func benchmarkIndexUpdate(b *testing.B, nfiles, nufiles int) {
db := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", db, nil)
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
m.ServeBackground()
m := setupModel(defaultCfgWrapper)
defer m.Stop()
files := genFiles(nfiles)
@ -486,12 +478,8 @@ func (f *fakeConnection) sendIndexUpdate() {
}
func BenchmarkRequestOut(b *testing.B) {
db := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", db, nil)
m.AddFolder(defaultFolderConfig)
m.ServeBackground()
m := setupModel(defaultCfgWrapper)
defer m.Stop()
m.ScanFolder("default")
const n = 1000
files := genFiles(n)
@ -518,12 +506,7 @@ func BenchmarkRequestOut(b *testing.B) {
func BenchmarkRequestInSingleFile(b *testing.B) {
testOs := &fatalOs{b}
db := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", db, nil)
m.AddFolder(defaultFolderConfig)
m.ServeBackground()
defer m.Stop()
m.ScanFolder("default")
m := setupModel(defaultCfgWrapper)
buf := make([]byte, 128<<10)
rand.Read(buf)
@ -1731,16 +1714,9 @@ func TestIgnores(t *testing.T) {
testOs.MkdirAll(filepath.Join("testdata", config.DefaultMarkerName), 0644)
ioutil.WriteFile("testdata/.stignore", []byte(".*\nquux\n"), 0644)
db := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", db, nil)
m.ServeBackground()
m := setupModel(defaultCfgWrapper)
defer m.Stop()
// m.cfg.SetFolder is not usable as it is non-blocking, and there is no
// way to know when the folder is actually added.
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
// Reach in and update the ignore matcher to one that always does
// reloads when asked to, instead of checking file mtimes. This is
// because we will be changing the files on disk often enough that the
@ -2426,16 +2402,7 @@ func TestIssue3028(t *testing.T) {
// Create a model and default folder
db := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", db, nil)
defCfg := defaultFolderConfig.Copy()
defCfg.RescanIntervalS = 86400
m.AddFolder(defCfg)
m.StartFolder("default")
m.ServeBackground()
// Make sure the initial scan has finished (ScanFolders is blocking)
m.ScanFolders()
m := setupModel(defaultCfgWrapper)
// Get a count of how many files are there now
@ -2580,12 +2547,7 @@ func TestIssue2782(t *testing.T) {
}
defer testOs.RemoveAll(testDir)
db := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", db, nil)
m.AddFolder(testFolderConfig("~/" + testName + "/synclink/"))
m.StartFolder("default")
m.ServeBackground()
defer m.Stop()
m := setupModel(defaultCfgWrapper)
if err := m.ScanFolder("default"); err != nil {
t.Error("scan error:", err)
@ -2627,8 +2589,6 @@ func TestIndexesForUnknownDevicesDropped(t *testing.T) {
func TestSharedWithClearedOnDisconnect(t *testing.T) {
testOs := &fatalOs{t}
dbi := db.OpenMemory()
wcfg := createTmpWrapper(defaultCfg)
wcfg.SetDevice(config.NewDeviceConfiguration(device2, "device2"))
fcfg := wcfg.FolderList()[0]
@ -2636,10 +2596,8 @@ func TestSharedWithClearedOnDisconnect(t *testing.T) {
wcfg.SetFolder(fcfg)
defer testOs.Remove(wcfg.ConfigPath())
m := NewModel(wcfg, myID, "syncthing", "dev", dbi, nil)
m.AddFolder(fcfg)
m.StartFolder(fcfg.ID)
m.ServeBackground()
m := setupModel(wcfg)
defer m.Stop()
conn1 := &fakeConnection{id: device1}
m.AddConnection(conn1, protocol.HelloResult{})
@ -2746,11 +2704,7 @@ func TestIssue3496(t *testing.T) {
// percentages. Lets make sure that doesn't happen. Also do some general
// checks on the completion calculation stuff.
dbi := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", dbi, nil)
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
m.ServeBackground()
m := setupModel(defaultCfgWrapper)
defer m.Stop()
m.ScanFolder("default")
@ -2819,11 +2773,7 @@ func TestIssue3496(t *testing.T) {
}
func TestIssue3804(t *testing.T) {
dbi := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", dbi, nil)
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
m.ServeBackground()
m := setupModel(defaultCfgWrapper)
defer m.Stop()
// Subdirs ending in slash should be accepted
@ -2834,11 +2784,7 @@ func TestIssue3804(t *testing.T) {
}
func TestIssue3829(t *testing.T) {
dbi := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", dbi, nil)
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
m.ServeBackground()
m := setupModel(defaultCfgWrapper)
defer m.Stop()
// Empty subdirs should be accepted
@ -2853,8 +2799,6 @@ func TestNoRequestsFromPausedDevices(t *testing.T) {
testOs := &fatalOs{t}
dbi := db.OpenMemory()
wcfg := createTmpWrapper(defaultCfg)
wcfg.SetDevice(config.NewDeviceConfiguration(device2, "device2"))
fcfg := wcfg.FolderList()[0]
@ -2862,10 +2806,8 @@ func TestNoRequestsFromPausedDevices(t *testing.T) {
wcfg.SetFolder(fcfg)
defer testOs.Remove(wcfg.ConfigPath())
m := NewModel(wcfg, myID, "syncthing", "dev", dbi, nil)
m.AddFolder(fcfg)
m.StartFolder(fcfg.ID)
m.ServeBackground()
m := setupModel(wcfg)
defer m.Stop()
file := testDataExpected["foo"]
files := m.folderFiles["default"]
@ -2959,13 +2901,7 @@ func TestIssue2571(t *testing.T) {
fd.Close()
}
dbi := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", dbi, nil)
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
m.ServeBackground()
defer m.Stop()
m.ScanFolder("default")
m := setupModel(defaultCfgWrapper)
if err = testFs.RemoveAll("toLink"); err != nil {
t.Fatal(err)
@ -3011,13 +2947,7 @@ func TestIssue4573(t *testing.T) {
}
fd.Close()
dbi := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", dbi, nil)
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
m.ServeBackground()
defer m.Stop()
m.ScanFolder("default")
m := setupModel(defaultCfgWrapper)
err = defaultFs.Chmod("inaccessible", 0000)
if err != nil {
@ -3080,13 +3010,7 @@ func TestInternalScan(t *testing.T) {
}
}
dbi := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", dbi, nil)
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
m.ServeBackground()
defer m.Stop()
m.ScanFolder("default")
m := setupModel(defaultCfgWrapper)
for _, dir := range baseDirs {
if err = testFs.RemoveAll(dir); err != nil {
@ -3191,13 +3115,8 @@ func TestRemoveDirWithContent(t *testing.T) {
}
fd.Close()
dbi := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", dbi, nil)
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
m.ServeBackground()
m := setupModel(defaultCfgWrapper)
defer m.Stop()
m.ScanFolder("default")
dir, ok := m.CurrentFolderFile("default", "dirwith")
if !ok {
@ -3256,13 +3175,8 @@ func TestIssue4475(t *testing.T) {
t.Fatal(err)
}
dbi := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", dbi, nil)
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
m.ServeBackground()
m := setupModel(defaultCfgWrapper)
defer m.Stop()
m.ScanFolder("default")
// Scenario: Dir is deleted locally and before syncing/index exchange
// happens, a file is create in that dir on the remote.
@ -3331,8 +3245,6 @@ func TestVersionRestore(t *testing.T) {
}
defer testOs.RemoveAll(dir)
dbi := db.OpenMemory()
fcfg := config.NewFolderConfiguration(myID, "default", "default", fs.FilesystemTypeBasic, dir)
fcfg.Versioning.Type = "simple"
fcfg.FSWatcherEnabled = false
@ -3344,11 +3256,7 @@ func TestVersionRestore(t *testing.T) {
cfg := createTmpWrapper(rawConfig)
defer testOs.Remove(cfg.ConfigPath())
m := NewModel(cfg, myID, "syncthing", "dev", dbi, nil)
m.AddFolder(fcfg)
m.StartFolder("default")
m.ServeBackground()
defer m.Stop()
m := setupModel(cfg)
m.ScanFolder("default")
sentinel, err := time.ParseInLocation(versioner.TimeFormat, "20200101-010101", locationLocal)
@ -3536,15 +3444,10 @@ func TestPausedFolders(t *testing.T) {
testOs := &fatalOs{t}
// Create a separate wrapper not to pollute other tests.
cfg := defaultCfgWrapper.RawCopy()
wrapper := createTmpWrapper(cfg)
wrapper := createTmpWrapper(defaultCfgWrapper.RawCopy())
defer testOs.Remove(wrapper.ConfigPath())
db := db.OpenMemory()
m := NewModel(wrapper, myID, "syncthing", "dev", db, nil)
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
m.ServeBackground()
m := setupModel(wrapper)
defer m.Stop()
if err := m.ScanFolder("default"); err != nil {
@ -3649,12 +3552,7 @@ func TestIssue4903(t *testing.T) {
func TestIssue5002(t *testing.T) {
// recheckFile should not panic when given an index equal to the number of blocks
db := db.OpenMemory()
m := NewModel(defaultCfgWrapper, myID, "syncthing", "dev", db, nil)
m.AddFolder(defaultFolderConfig)
m.StartFolder("default")
m.ServeBackground()
m := setupModel(defaultCfgWrapper)
defer m.Stop()
if err := m.ScanFolder("default"); err != nil {
@ -3721,12 +3619,7 @@ func TestFolderRestartZombies(t *testing.T) {
folderCfg.FilesystemType = fs.FilesystemTypeFake
wrapper.SetFolder(folderCfg)
db := db.OpenMemory()
m := NewModel(wrapper, myID, "syncthing", "dev", db, nil)
m.AddFolder(folderCfg)
m.StartFolder("default")
m.ServeBackground()
m := setupModel(wrapper)
defer m.Stop()
// Make sure the folder is up and running, because we want to count it.

View File

@ -740,11 +740,7 @@ func setupModelWithConnection() (*Model, *fakeConnection, string, *config.Wrappe
}
func setupModelWithConnectionFromWrapper(w *config.Wrapper) (*Model, *fakeConnection) {
db := db.OpenMemory()
m := NewModel(w, myID, "syncthing", "dev", db, nil)
m.AddFolder(w.FolderList()[0])
m.ServeBackground()
m.StartFolder("default")
m := setupModel(w)
fc := addFakeConn(m, device1)
fc.folder = "default"