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"
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
@ -30,10 +29,7 @@ func TestRecvOnlyRevertDeletes(t *testing.T) {
m, f := setupROFolder()
ffs := f.Filesystem()
defer os.Remove(m.cfg.ConfigPath())
defer os.Remove(ffs.URI())
defer m.db.Close()
defer m.Stop()
defer cleanupModelAndRemoveDir(m, ffs.URI())
// Create some test data
@ -114,10 +110,7 @@ func TestRecvOnlyRevertNeeds(t *testing.T) {
m, f := setupROFolder()
ffs := f.Filesystem()
defer os.Remove(m.cfg.ConfigPath())
defer os.Remove(ffs.URI())
defer m.db.Close()
defer m.Stop()
defer cleanupModelAndRemoveDir(m, ffs.URI())
// Create some test data
@ -208,10 +201,7 @@ func TestRecvOnlyUndoChanges(t *testing.T) {
m, f := setupROFolder()
ffs := f.Filesystem()
defer os.Remove(m.cfg.ConfigPath())
defer os.Remove(ffs.URI())
defer m.db.Close()
defer m.Stop()
defer cleanupModelAndRemoveDir(m, ffs.URI())
// 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
// an incoming index update.
m, fc, fcfg, w := setupModelWithConnection()
m, fc, fcfg := setupModelWithConnection()
tfs := fcfg.Filesystem()
defer func() {
m.Stop()
m.db.Close()
os.RemoveAll(tfs.URI())
os.Remove(w.ConfigPath())
}()
defer cleanupModelAndRemoveDir(m, tfs.URI())
// We listen for incoming index updates and trigger when we see one for
// the expected test file.
@ -76,13 +71,8 @@ func TestSymlinkTraversalRead(t *testing.T) {
return
}
m, fc, fcfg, w := setupModelWithConnection()
defer func() {
m.Stop()
m.db.Close()
os.RemoveAll(fcfg.Filesystem().URI())
os.Remove(w.ConfigPath())
}()
m, fc, fcfg := setupModelWithConnection()
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
// We listen for incoming index updates and trigger when we see one for
// the expected test file.
@ -124,13 +114,8 @@ func TestSymlinkTraversalWrite(t *testing.T) {
return
}
m, fc, fcfg, w := setupModelWithConnection()
defer func() {
m.Stop()
m.db.Close()
os.RemoveAll(fcfg.Filesystem().URI())
os.Remove(w.ConfigPath())
}()
m, fc, fcfg := setupModelWithConnection()
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
// We listen for incoming index updates and trigger when we see one for
// the expected names.
@ -188,13 +173,8 @@ func TestSymlinkTraversalWrite(t *testing.T) {
func TestRequestCreateTmpSymlink(t *testing.T) {
// Test that an update for a temporary file is invalidated
m, fc, fcfg, w := setupModelWithConnection()
defer func() {
m.Stop()
m.db.Close()
os.RemoveAll(fcfg.Filesystem().URI())
os.Remove(w.ConfigPath())
}()
m, fc, fcfg := setupModelWithConnection()
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
// We listen for incoming index updates and trigger when we see one for
// the expected test file.
@ -243,8 +223,7 @@ func TestRequestVersioningSymlinkAttack(t *testing.T) {
fcfg.Versioning = config.VersioningConfiguration{Type: "trashcan"}
w.SetFolder(fcfg)
m, fc := setupModelWithConnectionFromWrapper(w)
defer m.db.Close()
defer m.Stop()
defer cleanupModel(m)
// Create a temporary directory that we will use as target to see if
// we can escape to it
@ -312,12 +291,7 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
fcfg.Type = ft
w.SetFolder(fcfg)
m := setupModel(w)
defer func() {
m.Stop()
m.db.Close()
os.RemoveAll(fss.URI())
os.Remove(w.ConfigPath())
}()
defer cleanupModelAndRemoveDir(m, fss.URI())
m.RemoveFolder(fcfg)
m.AddFolder(fcfg)
@ -437,13 +411,8 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
}
func TestIssue4841(t *testing.T) {
m, fc, fcfg, w := setupModelWithConnection()
defer func() {
m.Stop()
m.db.Close()
os.RemoveAll(fcfg.Filesystem().URI())
os.Remove(w.ConfigPath())
}()
m, fc, fcfg := setupModelWithConnection()
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
received := make(chan protocol.FileInfo)
fc.mut.Lock()
@ -480,14 +449,9 @@ func TestIssue4841(t *testing.T) {
}
func TestRescanIfHaveInvalidContent(t *testing.T) {
m, fc, fcfg, w := setupModelWithConnection()
m, fc, fcfg := setupModelWithConnection()
tmpDir := fcfg.Filesystem().URI()
defer func() {
m.Stop()
m.db.Close()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
defer cleanupModelAndRemoveDir(m, tmpDir)
payload := []byte("hello")
@ -546,14 +510,9 @@ func TestRescanIfHaveInvalidContent(t *testing.T) {
}
func TestParentDeletion(t *testing.T) {
m, fc, fcfg, w := setupModelWithConnection()
m, fc, fcfg := setupModelWithConnection()
testFs := fcfg.Filesystem()
defer func() {
m.Stop()
m.db.Close()
os.RemoveAll(testFs.URI())
os.Remove(w.ConfigPath())
}()
defer cleanupModelAndRemoveDir(m, testFs.URI())
parent := "foo"
child := filepath.Join(parent, "bar")
@ -630,13 +589,8 @@ func TestRequestSymlinkWindows(t *testing.T) {
t.Skip("windows specific test")
}
m, fc, fcfg, w := setupModelWithConnection()
defer func() {
m.Stop()
m.db.Close()
os.RemoveAll(fcfg.Filesystem().URI())
os.Remove(w.ConfigPath())
}()
m, fc, fcfg := setupModelWithConnection()
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
done := make(chan struct{})
fc.mut.Lock()
@ -702,15 +656,10 @@ func equalContents(path string, contents []byte) error {
}
func TestRequestRemoteRenameChanged(t *testing.T) {
m, fc, fcfg, w := setupModelWithConnection()
m, fc, fcfg := setupModelWithConnection()
tfs := fcfg.Filesystem()
tmpDir := tfs.URI()
defer func() {
m.Stop()
m.db.Close()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
defer cleanupModelAndRemoveDir(m, tfs.URI())
done := make(chan struct{})
fc.mut.Lock()
@ -835,15 +784,10 @@ func TestRequestRemoteRenameChanged(t *testing.T) {
}
func TestRequestRemoteRenameConflict(t *testing.T) {
m, fc, fcfg, w := setupModelWithConnection()
m, fc, fcfg := setupModelWithConnection()
tfs := fcfg.Filesystem()
tmpDir := tfs.URI()
defer func() {
m.Stop()
m.db.Close()
os.RemoveAll(tmpDir)
os.Remove(w.ConfigPath())
}()
defer cleanupModelAndRemoveDir(m, tmpDir)
recv := make(chan int)
fc.mut.Lock()
@ -931,14 +875,9 @@ func TestRequestRemoteRenameConflict(t *testing.T) {
}
func TestRequestDeleteChanged(t *testing.T) {
m, fc, fcfg, w := setupModelWithConnection()
m, fc, fcfg := setupModelWithConnection()
tfs := fcfg.Filesystem()
defer func() {
m.Stop()
m.db.Close()
os.RemoveAll(tfs.URI())
os.Remove(w.ConfigPath())
}()
defer cleanupModelAndRemoveDir(m, tfs.URI())
done := make(chan struct{})
fc.mut.Lock()

View File

@ -8,6 +8,7 @@ package model
import (
"io/ioutil"
"os"
"time"
"github.com/syncthing/syncthing/lib/config"
@ -82,10 +83,10 @@ func testFolderConfig(path string) config.FolderConfiguration {
return cfg
}
func setupModelWithConnection() (*model, *fakeConnection, config.FolderConfiguration, config.Wrapper) {
func setupModelWithConnection() (*model, *fakeConnection, config.FolderConfiguration) {
w, fcfg := tmpDefaultWrapper()
m, fc := setupModelWithConnectionFromWrapper(w)
return m, fc, fcfg, w
return m, fc, fcfg
}
func setupModelWithConnectionFromWrapper(w config.Wrapper) (*model, *fakeConnection) {
@ -115,6 +116,21 @@ func setupModel(w config.Wrapper) *model {
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 {
tmpDir, err := ioutil.TempDir("", "syncthing_testFolder-")
if err != nil {