lib/model: Improve filesystem operations during tests (fixes #5422)

* lib/fs, lib/model: Improve filesystem operations during tests (fixes #5422)

Introduces MustFilesystem that panics on errors and should be used for operations
during testing which must never fail.
Create temporary directories outside of testdata.

* don't do a filesystem, just a wrapper around os for testing

* fix copyright
This commit is contained in:
Simon Frei
2019-01-11 13:56:05 +01:00
committed by Audrius Butkevicius
parent 24ffd8be99
commit 0b03b6a9ec
7 changed files with 380 additions and 236 deletions

View File

@@ -196,6 +196,8 @@ func TestHandleFileWithTemp(t *testing.T) {
}
func TestCopierFinder(t *testing.T) {
testOs := &fatalOs{t}
// After diff between required and existing we should:
// Copy: 1, 2, 3, 4, 6, 7, 8
// Since there is no existing file, nor a temp file
@@ -204,11 +206,8 @@ func TestCopierFinder(t *testing.T) {
// Pull: 1, 5, 6, 8
tempFile := filepath.Join("testdata", fs.TempName("file2"))
err := os.Remove(tempFile)
if err != nil && !os.IsNotExist(err) {
t.Error(err)
}
defer os.Remove(tempFile)
testOs.Remove(tempFile)
defer testOs.Remove(tempFile)
existingBlocks := []int{0, 2, 3, 4, 0, 0, 7, 0}
existingFile := setUpFile(fs.TempName("file"), existingBlocks)
@@ -273,6 +272,8 @@ func TestCopierFinder(t *testing.T) {
}
func TestWeakHash(t *testing.T) {
testOs := &fatalOs{t}
tempFile := filepath.Join("testdata", fs.TempName("weakhash"))
var shift int64 = 10
var size int64 = 1 << 20
@@ -284,19 +285,16 @@ func TestWeakHash(t *testing.T) {
cleanup := func() {
for _, path := range []string{tempFile, "testdata/weakhash"} {
os.Remove(path)
testOs.Remove(path)
}
}
cleanup()
defer cleanup()
f, err := os.Create("testdata/weakhash")
if err != nil {
t.Error(err)
}
f, _ := testOs.Create("testdata/weakhash")
defer f.Close()
_, err = io.CopyN(f, rand.Reader, size)
_, err := io.CopyN(f, rand.Reader, size)
if err != nil {
t.Error(err)
}
@@ -373,9 +371,7 @@ func TestWeakHash(t *testing.T) {
}
finish.fd.Close()
if err := os.Remove(tempFile); err != nil && !os.IsNotExist(err) {
t.Error(err)
}
testOs.Remove(tempFile)
// Test 2 - using weak hash, expectPulls blocks pulled.
fo.WeakHashThresholdPct = -1
@@ -438,8 +434,10 @@ func TestCopierCleanup(t *testing.T) {
}
func TestDeregisterOnFailInCopy(t *testing.T) {
testOs := &fatalOs{t}
file := setUpFile("filex", []int{0, 2, 0, 0, 5, 0, 0, 8})
defer os.Remove("testdata/" + fs.TempName("filex"))
defer testOs.Remove("testdata/" + fs.TempName("filex"))
db := db.OpenMemory()
@@ -530,8 +528,10 @@ func TestDeregisterOnFailInCopy(t *testing.T) {
}
func TestDeregisterOnFailInPull(t *testing.T) {
testOs := &fatalOs{t}
file := setUpFile("filex", []int{0, 2, 0, 0, 5, 0, 0, 8})
defer os.Remove("testdata/" + fs.TempName("filex"))
defer testOs.Remove("testdata/" + fs.TempName("filex"))
db := db.OpenMemory()
m := NewModel(defaultCfgWrapper, protocol.LocalDeviceID, "syncthing", "dev", db, nil)