forked from syncthing/syncthing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reset_test.go
144 lines (120 loc) · 3.23 KB
/
reset_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (C) 2014 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
// +build integration
package integration
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
)
func TestReset(t *testing.T) {
// Clean and start a syncthing instance
log.Println("Cleaning...")
err := removeAll("s1", "h1/index*")
if err != nil {
t.Fatal(err)
}
if err := os.Mkdir("s1", 0755); err != nil {
t.Fatal(err)
}
log.Println("Creating files...")
size := createFiles(t)
p := startInstance(t, 1)
defer p.Stop() // Not checkedStop, because Syncthing will exit on its own
m, err := p.Model("default")
if err != nil {
t.Fatal(err)
}
expected := size
if m.LocalFiles != expected {
t.Fatalf("Incorrect number of files after initial scan, %d != %d", m.LocalFiles, expected)
}
// Clear all files but restore the folder marker
log.Println("Cleaning...")
err = removeAll("s1")
if err != nil {
t.Fatal(err)
}
if err := os.Mkdir("s1", 0755); err != nil {
t.Fatal(err)
}
if fd, err := os.Create("s1/.stfolder"); err != nil {
t.Fatal(err)
} else {
fd.Close()
}
// Reset indexes of an invalid folder
log.Println("Reset invalid folder")
_, err = p.Post("/rest/system/reset?folder=invalid", nil)
if err == nil {
t.Fatalf("Cannot reset indexes of an invalid folder")
}
// Reset indexes of the default folder
log.Println("Reset indexes of default folder")
bs, err := p.Post("/rest/system/reset?folder=default", nil)
if err != nil && err != io.ErrUnexpectedEOF {
t.Fatalf("Failed to reset indexes (default): %v (%s)", err, bytes.TrimSpace(bs))
}
// ---- Syncthing exits here ----
p = startInstance(t, 1)
defer p.Stop() // Not checkedStop, because Syncthing will exit on its own
m, err = p.Model("default")
if err != nil {
t.Fatal(err)
}
expected = 0
if m.LocalFiles != expected {
t.Fatalf("Incorrect number of files after initial scan, %d != %d", m.LocalFiles, expected)
}
// Recreate the files and scan
log.Println("Creating files...")
size = createFiles(t)
if err := p.Rescan("default"); err != nil {
t.Fatal(err)
}
// Verify that we see them
m, err = p.Model("default")
if err != nil {
t.Fatal(err)
}
expected = size
if m.LocalFiles != expected {
t.Fatalf("Incorrect number of files after second creation phase, %d != %d", m.LocalFiles, expected)
}
// Reset all indexes
log.Println("Reset DB...")
bs, err = p.Post("/rest/system/reset", nil)
if err != nil && err != io.ErrUnexpectedEOF {
t.Fatalf("Failed to reset indexes (all): %v (%s)", err, bytes.TrimSpace(bs))
}
// ---- Syncthing exits here ----
p = startInstance(t, 1)
defer checkedStop(t, p)
m, err = p.Model("default")
if err != nil {
t.Fatal(err)
}
expected = size
if m.LocalFiles != expected {
t.Fatalf("Incorrect number of files after initial scan, %d != %d", m.LocalFiles, expected)
}
}
func createFiles(t *testing.T) int {
// Create a few files
const n = 8
for i := 0; i < n; i++ {
file := fmt.Sprintf("f%d", i)
if err := ioutil.WriteFile(filepath.Join("s1", file), []byte("data"), 0644); err != nil {
t.Fatal(err)
}
}
return n
}