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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := syncthingProcess{ // id1
|
|
|
|
instance: "1",
|
|
|
|
argv: []string{"-home", "h1"},
|
|
|
|
port: 8081,
|
|
|
|
apiKey: apiKey,
|
|
|
|
}
|
|
|
|
err = p.start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer p.stop()
|
|
|
|
|
|
|
|
// Wait for one scan to succeed, or up to 20 seconds... This is to let
|
|
|
|
// startup, UPnP etc complete and make sure that we've performed folder
|
|
|
|
// error checking which creates the folder path if it's missing.
|
|
|
|
log.Println("Starting...")
|
2015-05-23 20:56:43 +00:00
|
|
|
waitForScan(p)
|
2015-04-03 18:06:03 +00:00
|
|
|
|
|
|
|
log.Println("Creating files...")
|
|
|
|
size := createFiles(t)
|
|
|
|
|
|
|
|
log.Println("Scanning files...")
|
2015-05-23 20:56:43 +00:00
|
|
|
waitForScan(p)
|
2015-04-03 18:06:03 +00:00
|
|
|
|
|
|
|
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/*", "h1/index*")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
os.Create("s1/.stfolder")
|
|
|
|
|
|
|
|
// Reset indexes of an invalid folder
|
|
|
|
log.Println("Reset invalid folder")
|
|
|
|
err = p.reset("invalid")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Cannot reset indexes of an invalid folder")
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset indexes of the default folder
|
|
|
|
log.Println("Reset indexes of default folder")
|
|
|
|
err = p.reset("default")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to reset indexes of the default folder:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for ST and scan
|
|
|
|
p.start()
|
2015-05-23 20:56:43 +00:00
|
|
|
waitForScan(p)
|
2015-04-03 18:06:03 +00:00
|
|
|
|
|
|
|
// Verify that we see them
|
|
|
|
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)
|
2015-05-23 20:56:43 +00:00
|
|
|
waitForScan(p)
|
2015-04-03 18:06:03 +00:00
|
|
|
|
|
|
|
// 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...")
|
|
|
|
err = p.reset("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to reset indexes", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for ST and scan
|
|
|
|
p.start()
|
2015-05-23 20:56:43 +00:00
|
|
|
waitForScan(p)
|
2015-04-03 18:06:03 +00:00
|
|
|
|
|
|
|
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 eight empty files and directories
|
|
|
|
files := []string{"f1", "f2", "f3", "f4", "f11", "f12", "f13", "f14"}
|
|
|
|
dirs := []string{"d1", "d2", "d3", "d4", "d11", "d12", "d13", "d14"}
|
|
|
|
all := append(files, dirs...)
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
fd, err := os.Create(filepath.Join("s1", file))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dir := range dirs {
|
|
|
|
err := os.Mkdir(filepath.Join("s1", dir), 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(all)
|
|
|
|
}
|