2015-04-03 18:06:03 +00:00
|
|
|
// 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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-04-03 18:06:03 +00:00
|
|
|
|
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2015-06-21 07:35:41 +00:00
|
|
|
"bytes"
|
2017-11-17 12:11:45 +00:00
|
|
|
"fmt"
|
2015-08-04 07:20:46 +00:00
|
|
|
"io"
|
2017-11-17 12:11:45 +00:00
|
|
|
"io/ioutil"
|
2015-04-03 18:06:03 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2019-08-20 08:17:11 +00:00
|
|
|
"time"
|
2015-04-03 18:06:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2015-06-18 13:22:45 +00:00
|
|
|
if err := os.Mkdir("s1", 0755); err != nil {
|
2015-04-03 18:06:03 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Creating files...")
|
|
|
|
size := createFiles(t)
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
p := startInstance(t, 1)
|
2017-07-20 13:10:46 +00:00
|
|
|
defer p.Stop() // Not checkedStop, because Syncthing will exit on its own
|
2015-04-03 18:06:03 +00:00
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
m, err := p.Model("default")
|
2015-04-03 18:06:03 +00:00
|
|
|
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...")
|
2015-06-18 13:22:45 +00:00
|
|
|
err = removeAll("s1")
|
2015-04-03 18:06:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-18 13:22:45 +00:00
|
|
|
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()
|
|
|
|
}
|
2015-04-03 18:06:03 +00:00
|
|
|
|
|
|
|
// Reset indexes of an invalid folder
|
|
|
|
log.Println("Reset invalid folder")
|
2015-06-18 13:22:45 +00:00
|
|
|
_, err = p.Post("/rest/system/reset?folder=invalid", nil)
|
2015-04-03 18:06:03 +00:00
|
|
|
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")
|
2015-06-21 07:35:41 +00:00
|
|
|
bs, err := p.Post("/rest/system/reset?folder=default", nil)
|
2015-08-04 07:20:46 +00:00
|
|
|
if err != nil && err != io.ErrUnexpectedEOF {
|
2015-06-21 07:35:41 +00:00
|
|
|
t.Fatalf("Failed to reset indexes (default): %v (%s)", err, bytes.TrimSpace(bs))
|
2015-04-03 18:06:03 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 17:31:01 +00:00
|
|
|
// ---- Syncthing exits here ----
|
2019-08-20 08:17:11 +00:00
|
|
|
select {
|
|
|
|
case <-p.Stopped():
|
|
|
|
case <-time.After(20 * time.Second):
|
|
|
|
t.Fatal("timed out before Syncthing stopped")
|
|
|
|
}
|
2015-07-28 17:31:01 +00:00
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
p = startInstance(t, 1)
|
2017-07-20 13:10:46 +00:00
|
|
|
defer p.Stop() // Not checkedStop, because Syncthing will exit on its own
|
2015-04-03 18:06:03 +00:00
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
m, err = p.Model("default")
|
2015-04-03 18:06:03 +00:00
|
|
|
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)
|
2015-06-18 13:22:45 +00:00
|
|
|
|
|
|
|
if err := p.Rescan("default"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-03 18:06:03 +00:00
|
|
|
|
|
|
|
// Verify that we see them
|
2015-06-18 13:22:45 +00:00
|
|
|
m, err = p.Model("default")
|
2015-04-03 18:06:03 +00:00
|
|
|
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...")
|
2015-06-21 07:35:41 +00:00
|
|
|
bs, err = p.Post("/rest/system/reset", nil)
|
2015-09-24 12:19:21 +00:00
|
|
|
if err != nil && err != io.ErrUnexpectedEOF {
|
2015-06-21 07:35:41 +00:00
|
|
|
t.Fatalf("Failed to reset indexes (all): %v (%s)", err, bytes.TrimSpace(bs))
|
2015-04-03 18:06:03 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 17:31:01 +00:00
|
|
|
// ---- Syncthing exits here ----
|
2019-08-20 08:17:11 +00:00
|
|
|
select {
|
|
|
|
case <-p.Stopped():
|
|
|
|
case <-time.After(20 * time.Second):
|
|
|
|
t.Fatal("timed out before Syncthing stopped")
|
|
|
|
}
|
2015-07-28 17:31:01 +00:00
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
p = startInstance(t, 1)
|
|
|
|
defer checkedStop(t, p)
|
2015-04-03 18:06:03 +00:00
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
m, err = p.Model("default")
|
2015-04-03 18:06:03 +00:00
|
|
|
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 {
|
2017-11-17 12:11:45 +00:00
|
|
|
// Create a few files
|
2015-04-03 18:06:03 +00:00
|
|
|
|
2017-11-17 12:11:45 +00:00
|
|
|
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 {
|
2015-04-03 18:06:03 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 12:11:45 +00:00
|
|
|
return n
|
2015-04-03 18:06:03 +00:00
|
|
|
}
|