lib/model: Consistently cleanup model in tests (#5724)

This commit is contained in:
Simon Frei 2019-05-19 14:29:07 +02:00 committed by GitHub
parent f593ac387c
commit 5b306510a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 178 additions and 355 deletions

View File

@ -10,7 +10,6 @@ import (
"bytes" "bytes"
"context" "context"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
"time" "time"
@ -30,10 +29,7 @@ func TestRecvOnlyRevertDeletes(t *testing.T) {
m, f := setupROFolder() m, f := setupROFolder()
ffs := f.Filesystem() ffs := f.Filesystem()
defer os.Remove(m.cfg.ConfigPath()) defer cleanupModelAndRemoveDir(m, ffs.URI())
defer os.Remove(ffs.URI())
defer m.db.Close()
defer m.Stop()
// Create some test data // Create some test data
@ -114,10 +110,7 @@ func TestRecvOnlyRevertNeeds(t *testing.T) {
m, f := setupROFolder() m, f := setupROFolder()
ffs := f.Filesystem() ffs := f.Filesystem()
defer os.Remove(m.cfg.ConfigPath()) defer cleanupModelAndRemoveDir(m, ffs.URI())
defer os.Remove(ffs.URI())
defer m.db.Close()
defer m.Stop()
// Create some test data // Create some test data
@ -208,10 +201,7 @@ func TestRecvOnlyUndoChanges(t *testing.T) {
m, f := setupROFolder() m, f := setupROFolder()
ffs := f.Filesystem() ffs := f.Filesystem()
defer os.Remove(m.cfg.ConfigPath()) defer cleanupModelAndRemoveDir(m, ffs.URI())
defer os.Remove(ffs.URI())
defer m.db.Close()
defer m.Stop()
// Create some test data // Create some test data

File diff suppressed because it is too large Load Diff

View File

@ -28,14 +28,9 @@ 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, fcfg, w := setupModelWithConnection() m, fc, fcfg := setupModelWithConnection()
tfs := fcfg.Filesystem() tfs := fcfg.Filesystem()
defer func() { defer cleanupModelAndRemoveDir(m, tfs.URI())
m.Stop()
m.db.Close()
os.RemoveAll(tfs.URI())
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.
@ -76,13 +71,8 @@ func TestSymlinkTraversalRead(t *testing.T) {
return return
} }
m, fc, fcfg, w := setupModelWithConnection() m, fc, fcfg := setupModelWithConnection()
defer func() { defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
m.Stop()
m.db.Close()
os.RemoveAll(fcfg.Filesystem().URI())
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.
@ -124,13 +114,8 @@ func TestSymlinkTraversalWrite(t *testing.T) {
return return
} }
m, fc, fcfg, w := setupModelWithConnection() m, fc, fcfg := setupModelWithConnection()
defer func() { defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
m.Stop()
m.db.Close()
os.RemoveAll(fcfg.Filesystem().URI())
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.
@ -188,13 +173,8 @@ 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, fcfg, w := setupModelWithConnection() m, fc, fcfg := setupModelWithConnection()
defer func() { defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
m.Stop()
m.db.Close()
os.RemoveAll(fcfg.Filesystem().URI())
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.
@ -243,8 +223,7 @@ func TestRequestVersioningSymlinkAttack(t *testing.T) {
fcfg.Versioning = config.VersioningConfiguration{Type: "trashcan"} fcfg.Versioning = config.VersioningConfiguration{Type: "trashcan"}
w.SetFolder(fcfg) w.SetFolder(fcfg)
m, fc := setupModelWithConnectionFromWrapper(w) m, fc := setupModelWithConnectionFromWrapper(w)
defer m.db.Close() defer cleanupModel(m)
defer m.Stop()
// Create a temporary directory that we will use as target to see if // Create a temporary directory that we will use as target to see if
// we can escape to it // we can escape to it
@ -312,12 +291,7 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
fcfg.Type = ft fcfg.Type = ft
w.SetFolder(fcfg) w.SetFolder(fcfg)
m := setupModel(w) m := setupModel(w)
defer func() { defer cleanupModelAndRemoveDir(m, fss.URI())
m.Stop()
m.db.Close()
os.RemoveAll(fss.URI())
os.Remove(w.ConfigPath())
}()
m.RemoveFolder(fcfg) m.RemoveFolder(fcfg)
m.AddFolder(fcfg) m.AddFolder(fcfg)
@ -437,13 +411,8 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
} }
func TestIssue4841(t *testing.T) { func TestIssue4841(t *testing.T) {
m, fc, fcfg, w := setupModelWithConnection() m, fc, fcfg := setupModelWithConnection()
defer func() { defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
m.Stop()
m.db.Close()
os.RemoveAll(fcfg.Filesystem().URI())
os.Remove(w.ConfigPath())
}()
received := make(chan protocol.FileInfo) received := make(chan protocol.FileInfo)
fc.mut.Lock() fc.mut.Lock()
@ -480,14 +449,9 @@ func TestIssue4841(t *testing.T) {
} }
func TestRescanIfHaveInvalidContent(t *testing.T) { func TestRescanIfHaveInvalidContent(t *testing.T) {
m, fc, fcfg, w := setupModelWithConnection() m, fc, fcfg := setupModelWithConnection()
tmpDir := fcfg.Filesystem().URI() tmpDir := fcfg.Filesystem().URI()
defer func() { defer cleanupModelAndRemoveDir(m, tmpDir)
m.Stop()
m.db.Close()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
payload := []byte("hello") payload := []byte("hello")
@ -546,14 +510,9 @@ func TestRescanIfHaveInvalidContent(t *testing.T) {
} }
func TestParentDeletion(t *testing.T) { func TestParentDeletion(t *testing.T) {
m, fc, fcfg, w := setupModelWithConnection() m, fc, fcfg := setupModelWithConnection()
testFs := fcfg.Filesystem() testFs := fcfg.Filesystem()
defer func() { defer cleanupModelAndRemoveDir(m, testFs.URI())
m.Stop()
m.db.Close()
os.RemoveAll(testFs.URI())
os.Remove(w.ConfigPath())
}()
parent := "foo" parent := "foo"
child := filepath.Join(parent, "bar") child := filepath.Join(parent, "bar")
@ -630,13 +589,8 @@ func TestRequestSymlinkWindows(t *testing.T) {
t.Skip("windows specific test") t.Skip("windows specific test")
} }
m, fc, fcfg, w := setupModelWithConnection() m, fc, fcfg := setupModelWithConnection()
defer func() { defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
m.Stop()
m.db.Close()
os.RemoveAll(fcfg.Filesystem().URI())
os.Remove(w.ConfigPath())
}()
done := make(chan struct{}) done := make(chan struct{})
fc.mut.Lock() fc.mut.Lock()
@ -702,15 +656,10 @@ func equalContents(path string, contents []byte) error {
} }
func TestRequestRemoteRenameChanged(t *testing.T) { func TestRequestRemoteRenameChanged(t *testing.T) {
m, fc, fcfg, w := setupModelWithConnection() m, fc, fcfg := setupModelWithConnection()
tfs := fcfg.Filesystem() tfs := fcfg.Filesystem()
tmpDir := tfs.URI() tmpDir := tfs.URI()
defer func() { defer cleanupModelAndRemoveDir(m, tfs.URI())
m.Stop()
m.db.Close()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
done := make(chan struct{}) done := make(chan struct{})
fc.mut.Lock() fc.mut.Lock()
@ -835,15 +784,10 @@ func TestRequestRemoteRenameChanged(t *testing.T) {
} }
func TestRequestRemoteRenameConflict(t *testing.T) { func TestRequestRemoteRenameConflict(t *testing.T) {
m, fc, fcfg, w := setupModelWithConnection() m, fc, fcfg := setupModelWithConnection()
tfs := fcfg.Filesystem() tfs := fcfg.Filesystem()
tmpDir := tfs.URI() tmpDir := tfs.URI()
defer func() { defer cleanupModelAndRemoveDir(m, tmpDir)
m.Stop()
m.db.Close()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
recv := make(chan int) recv := make(chan int)
fc.mut.Lock() fc.mut.Lock()
@ -931,14 +875,9 @@ func TestRequestRemoteRenameConflict(t *testing.T) {
} }
func TestRequestDeleteChanged(t *testing.T) { func TestRequestDeleteChanged(t *testing.T) {
m, fc, fcfg, w := setupModelWithConnection() m, fc, fcfg := setupModelWithConnection()
tfs := fcfg.Filesystem() tfs := fcfg.Filesystem()
defer func() { defer cleanupModelAndRemoveDir(m, tfs.URI())
m.Stop()
m.db.Close()
os.RemoveAll(tfs.URI())
os.Remove(w.ConfigPath())
}()
done := make(chan struct{}) done := make(chan struct{})
fc.mut.Lock() fc.mut.Lock()

View File

@ -8,6 +8,7 @@ package model
import ( import (
"io/ioutil" "io/ioutil"
"os"
"time" "time"
"github.com/syncthing/syncthing/lib/config" "github.com/syncthing/syncthing/lib/config"
@ -82,10 +83,10 @@ func testFolderConfig(path string) config.FolderConfiguration {
return cfg return cfg
} }
func setupModelWithConnection() (*model, *fakeConnection, config.FolderConfiguration, config.Wrapper) { func setupModelWithConnection() (*model, *fakeConnection, config.FolderConfiguration) {
w, fcfg := tmpDefaultWrapper() w, fcfg := tmpDefaultWrapper()
m, fc := setupModelWithConnectionFromWrapper(w) m, fc := setupModelWithConnectionFromWrapper(w)
return m, fc, fcfg, w return m, fc, fcfg
} }
func setupModelWithConnectionFromWrapper(w config.Wrapper) (*model, *fakeConnection) { func setupModelWithConnectionFromWrapper(w config.Wrapper) (*model, *fakeConnection) {
@ -115,6 +116,21 @@ func setupModel(w config.Wrapper) *model {
return m return m
} }
func newModel(cfg config.Wrapper, id protocol.DeviceID, clientName, clientVersion string, ldb *db.Lowlevel, protectedFiles []string) *model {
return NewModel(cfg, id, clientName, clientVersion, ldb, protectedFiles).(*model)
}
func cleanupModel(m *model) {
m.Stop()
m.db.Close()
os.Remove(m.cfg.ConfigPath())
}
func cleanupModelAndRemoveDir(m *model, dir string) {
cleanupModel(m)
os.RemoveAll(dir)
}
func createTmpDir() string { func createTmpDir() string {
tmpDir, err := ioutil.TempDir("", "syncthing_testFolder-") tmpDir, err := ioutil.TempDir("", "syncthing_testFolder-")
if err != nil { if err != nil {