Refactored integration tests

Added internal/rc to remote control a Syncthing process and made the
"awaiting sync" determination reliable.
This commit is contained in:
Jakob Borg 2015-06-18 15:22:45 +02:00
parent 69af77a3bd
commit c3d07d60ca
19 changed files with 868 additions and 1142 deletions

View File

@ -454,8 +454,6 @@ func syncthingMain() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
events.Default.Log(events.Starting, map[string]string{"home": baseDirs["config"]})
// Ensure that that we have a certificate and key.
cert, err := tls.LoadX509KeyPair(locations[locCertFile], locations[locKeyFile])
if err != nil {
@ -475,6 +473,13 @@ func syncthingMain() {
l.Infoln(LongVersion)
l.Infoln("My ID:", myID)
// Emit the Starting event, now that we know who we are.
events.Default.Log(events.Starting, map[string]string{
"home": baseDirs["config"],
"myID": myID.String(),
})
// Prepare to be able to save configuration
cfgFile := locations[locConfigFile]

19
internal/rc/debug.go Normal file
View File

@ -0,0 +1,19 @@
// Copyright (C) 2015 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/.
package rc
import (
"os"
"strings"
"github.com/calmh/logger"
)
var (
debug = strings.Contains(os.Getenv("STTRACE"), "rc") || os.Getenv("STTRACE") == "all"
l = logger.DefaultLogger
)

496
internal/rc/rc.go Normal file
View File

@ -0,0 +1,496 @@
// Copyright (C) 2015 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/.
// Package rc provides remote control of a Syncthing process via the REST API.
package rc
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
stdsync "sync"
"time"
"github.com/syncthing/protocol"
"github.com/syncthing/syncthing/internal/config"
"github.com/syncthing/syncthing/internal/sync"
)
// We set the API key via the STGUIAPIKEY variable when we launch the binary,
// to ensure that we have API access regardless of authentication settings.
const APIKey = "592A47BC-A7DF-4C2F-89E0-A80B3E5094EE"
type Process struct {
// Set at initialization
addr string
// Set by eventLoop()
eventMut sync.Mutex
id protocol.DeviceID
folders []string
startComplete bool
startCompleteCond *stdsync.Cond
stop bool
localVersion map[string]map[string]int64 // Folder ID => Device ID => LocalVersion
done map[string]bool // Folder ID => 100%
cmd *exec.Cmd
logfd *os.File
}
// NewProcess returns a new Process talking to Syncthing at the specified address.
// Example: NewProcess("127.0.0.1:8082")
func NewProcess(addr string) *Process {
p := &Process{
addr: addr,
localVersion: make(map[string]map[string]int64),
done: make(map[string]bool),
eventMut: sync.NewMutex(),
}
p.startCompleteCond = stdsync.NewCond(p.eventMut)
return p
}
// LogTo creates the specified log file and ensures that stdout and stderr
// from the Start()ed process is redirected there. Must be called before
// Start().
func (p *Process) LogTo(filename string) error {
if p.cmd != nil {
panic("logfd cannot be set with an existing cmd")
}
if p.logfd != nil {
p.logfd.Close()
}
fd, err := os.Create(filename)
if err != nil {
return err
}
p.logfd = fd
return nil
}
// Start runs the specified Syncthing binary with the given arguments.
// Syncthing should be configured to provide an API on the address given to
// NewProcess. Event processing is started.
func (p *Process) Start(bin string, args ...string) error {
cmd := exec.Command(bin, args...)
if p.logfd != nil {
cmd.Stdout = p.logfd
cmd.Stderr = p.logfd
}
cmd.Env = append(os.Environ(), "STNORESTART=1", "STGUIAPIKEY="+APIKey)
err := cmd.Start()
if err != nil {
return err
}
p.cmd = cmd
go p.eventLoop()
return nil
}
// AwaitStartup waits for the Syncthing process to start and perform initial
// scans of all folders.
func (p *Process) AwaitStartup() {
p.eventMut.Lock()
for !p.startComplete {
p.startCompleteCond.Wait()
}
p.eventMut.Unlock()
return
}
// Stop stops the running Syncthing process. If the process was logging to a
// local file (set by LogTo), the log file will be opened and checked for
// panics and data races. The presence of either will be signalled in the form
// of a returned error.
func (p *Process) Stop() (*os.ProcessState, error) {
p.eventMut.Lock()
if p.stop {
p.eventMut.Unlock()
return p.cmd.ProcessState, nil
}
p.stop = true
p.eventMut.Unlock()
if err := p.cmd.Process.Signal(os.Kill); err != nil {
return nil, err
}
p.cmd.Wait()
var err error
if p.logfd != nil {
err = p.checkForProblems(p.logfd)
}
return p.cmd.ProcessState, err
}
// Get performs an HTTP GET and returns the bytes and/or an error. Any non-200
// return code is returned as an error.
func (p *Process) Get(path string) ([]byte, error) {
client := &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
DisableKeepAlives: true,
},
}
url := fmt.Sprintf("http://%s%s", p.addr, path)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("X-API-Key", APIKey)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return p.readResponse(resp)
}
// Post performs an HTTP POST and returns the bytes and/or an error. Any
// non-200 return code is returned as an error.
func (p *Process) Post(path string, data io.Reader) ([]byte, error) {
client := &http.Client{
Timeout: 600 * time.Second,
Transport: &http.Transport{
DisableKeepAlives: true,
},
}
url := fmt.Sprintf("http://%s%s", p.addr, path)
req, err := http.NewRequest("POST", url, data)
if err != nil {
return nil, err
}
req.Header.Add("X-API-Key", APIKey)
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return p.readResponse(resp)
}
type Event struct {
ID int
Time time.Time
Type string
Data interface{}
}
func (p *Process) Events(since int) ([]Event, error) {
bs, err := p.Get(fmt.Sprintf("/rest/events?since=%d", since))
if err != nil {
return nil, err
}
var evs []Event
dec := json.NewDecoder(bytes.NewReader(bs))
dec.UseNumber()
err = dec.Decode(&evs)
if err != nil {
return nil, fmt.Errorf("Events: %s in %q", err, bs)
}
return evs, err
}
func (p *Process) Rescan(folder string) error {
_, err := p.Post("/rest/db/scan?folder="+folder, nil)
return err
}
func (p *Process) RescanDelay(folder string, delaySeconds int) error {
_, err := p.Post(fmt.Sprintf("/rest/db/scan?folder=%s&next=%d", folder, delaySeconds), nil)
return err
}
func InSync(folder string, ps ...*Process) bool {
for _, p := range ps {
p.eventMut.Lock()
}
defer func() {
for _, p := range ps {
p.eventMut.Unlock()
}
}()
for i := range ps {
// If our latest FolderSummary didn't report 100%, then we are not done.
if !ps[i].done[folder] {
return false
}
// Check LocalVersion for each device. The local version seen by remote
// devices should be the same as what it has locally, or the index
// hasn't been sent yet.
sourceID := ps[i].id.String()
sourceVersion := ps[i].localVersion[folder][sourceID]
for j := range ps {
if i != j {
remoteVersion := ps[j].localVersion[folder][sourceID]
if remoteVersion != sourceVersion {
return false
}
}
}
}
return true
}
func AwaitSync(folder string, ps ...*Process) {
for {
time.Sleep(250 * time.Millisecond)
if InSync(folder, ps...) {
return
}
}
}
type Model struct {
GlobalBytes int
GlobalDeleted int
GlobalFiles int
InSyncBytes int
InSyncFiles int
Invalid string
LocalBytes int
LocalDeleted int
LocalFiles int
NeedBytes int
NeedFiles int
State string
StateChanged time.Time
Version int
}
func (p *Process) Model(folder string) (Model, error) {
bs, err := p.Get("/rest/db/status?folder=" + folder)
if err != nil {
return Model{}, err
}
var res Model
if err := json.Unmarshal(bs, &res); err != nil {
return Model{}, err
}
if debug {
l.Debugf("%+v", res)
}
return res, nil
}
func (p *Process) readResponse(resp *http.Response) ([]byte, error) {
bs, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return bs, err
}
if resp.StatusCode != 200 {
return bs, fmt.Errorf("%s", resp.Status)
}
return bs, nil
}
func (p *Process) checkForProblems(logfd *os.File) error {
fd, err := os.Open(logfd.Name())
if err != nil {
return err
}
defer fd.Close()
raceConditionStart := []byte("WARNING: DATA RACE")
raceConditionSep := []byte("==================")
panicConditionStart := []byte("panic:")
panicConditionSep := []byte(p.id.String()[:5])
sc := bufio.NewScanner(fd)
race := false
_panic := false
for sc.Scan() {
line := sc.Bytes()
if race || _panic {
if bytes.Contains(line, panicConditionSep) {
_panic = false
continue
}
fmt.Printf("%s\n", line)
if bytes.Contains(line, raceConditionSep) {
race = false
}
} else if bytes.Contains(line, raceConditionStart) {
fmt.Printf("%s\n", raceConditionSep)
fmt.Printf("%s\n", raceConditionStart)
race = true
if err == nil {
err = errors.New("Race condition detected")
}
} else if bytes.Contains(line, panicConditionStart) {
_panic = true
if err == nil {
err = errors.New("Panic detected")
}
}
}
return err
}
func (p *Process) eventLoop() {
since := 0
notScanned := make(map[string]struct{})
start := time.Now()
for {
p.eventMut.Lock()
if p.stop {
p.eventMut.Unlock()
return
}
p.eventMut.Unlock()
time.Sleep(250 * time.Millisecond)
events, err := p.Events(since)
if err != nil {
if time.Since(start) < 5*time.Second {
// The API has probably not started yet, lets give it some time.
continue
}
// If we're stopping, no need to print the error.
p.eventMut.Lock()
if p.stop {
p.eventMut.Unlock()
return
}
p.eventMut.Unlock()
log.Println("eventLoop: events:", err)
continue
}
since = events[len(events)-1].ID
for _, ev := range events {
switch ev.Type {
case "Starting":
// The Starting event tells us where the configuration is. Load
// it and populate our list of folders.
data := ev.Data.(map[string]interface{})
id, err := protocol.DeviceIDFromString(data["myID"].(string))
if err != nil {
log.Println("eventLoop: DeviceIdFromString:", err)
continue
}
p.id = id
home := data["home"].(string)
w, err := config.Load(filepath.Join(home, "config.xml"), protocol.LocalDeviceID)
if err != nil {
log.Println("eventLoop: Starting:", err)
continue
}
for id := range w.Folders() {
p.eventMut.Lock()
p.folders = append(p.folders, id)
p.eventMut.Unlock()
notScanned[id] = struct{}{}
}
case "StateChanged":
// When a folder changes to idle, we tick it off by removing
// it from p.notScanned.
if !p.startComplete {
data := ev.Data.(map[string]interface{})
to := data["to"].(string)
if to == "idle" {
folder := data["folder"].(string)
delete(notScanned, folder)
if len(notScanned) == 0 {
p.eventMut.Lock()
p.startComplete = true
p.startCompleteCond.Broadcast()
p.eventMut.Unlock()
}
}
}
case "LocalIndexUpdated":
data := ev.Data.(map[string]interface{})
folder := data["folder"].(string)
version, _ := data["version"].(json.Number).Int64()
p.eventMut.Lock()
m := p.localVersion[folder]
if m == nil {
m = make(map[string]int64)
}
m[p.id.String()] = version
p.localVersion[folder] = m
p.done[folder] = false
if debug {
l.Debugf("LocalIndexUpdated %v %v done=false\n\t%+v", p.id, folder, m)
}
p.eventMut.Unlock()
case "RemoteIndexUpdated":
data := ev.Data.(map[string]interface{})
device := data["device"].(string)
folder := data["folder"].(string)
version, _ := data["version"].(json.Number).Int64()
p.eventMut.Lock()
m := p.localVersion[folder]
if m == nil {
m = make(map[string]int64)
}
m[device] = version
p.localVersion[folder] = m
p.done[folder] = false
if debug {
l.Debugf("RemoteIndexUpdated %v %v done=false\n\t%+v", p.id, folder, m)
}
p.eventMut.Unlock()
case "FolderSummary":
data := ev.Data.(map[string]interface{})
folder := data["folder"].(string)
summary := data["summary"].(map[string]interface{})
need, _ := summary["needBytes"].(json.Number).Int64()
done := need == 0
p.eventMut.Lock()
p.done[folder] = done
if debug {
l.Debugf("Foldersummary %v %v\n\t%+v", p.id, folder, p.done)
}
p.eventMut.Unlock()
}
}
}
}

View File

@ -16,9 +16,10 @@ import (
"time"
"github.com/syncthing/syncthing/internal/osutil"
"github.com/syncthing/syncthing/internal/rc"
)
func TestConflict(t *testing.T) {
func TestConflictsDefault(t *testing.T) {
log.Println("Cleaning...")
err := removeAll("s1", "s2", "h1/index*", "h2/index*")
if err != nil {
@ -49,16 +50,21 @@ func TestConflict(t *testing.T) {
t.Fatal(err)
}
sender, receiver := coSenderReceiver(t)
defer sender.stop()
defer receiver.stop()
sender := startInstance(t, 1)
defer checkedStop(t, sender)
receiver := startInstance(t, 2)
defer checkedStop(t, receiver)
if err = awaitCompletion("default", sender, receiver); err != nil {
// Rescan with a delay on the next one, so we are not surprised by a
// sudden rescan while we're trying to introduce conflicts.
if err := sender.RescanDelay("default", 86400); err != nil {
t.Fatal(err)
}
sender.stop()
receiver.stop()
if err := receiver.RescanDelay("default", 86400); err != nil {
t.Fatal(err)
}
rc.AwaitSync("default", sender, receiver)
log.Println("Verifying...")
@ -101,22 +107,13 @@ func TestConflict(t *testing.T) {
log.Println("Syncing...")
err = receiver.start()
err = sender.start()
if err != nil {
if err := sender.RescanDelay("default", 86400); err != nil {
t.Fatal(err)
}
if err != nil {
sender.stop()
if err := receiver.RescanDelay("default", 86400); err != nil {
t.Fatal(err)
}
if err = awaitCompletion("default", sender, receiver); err != nil {
t.Fatal(err)
}
sender.stop()
receiver.stop()
rc.AwaitSync("default", sender, receiver)
// The conflict is expected on the s2 side due to how we calculate which
// file is the winner (based on device ID)
@ -151,22 +148,13 @@ func TestConflict(t *testing.T) {
log.Println("Syncing...")
err = receiver.start()
err = sender.start()
if err != nil {
if err := sender.RescanDelay("default", 86400); err != nil {
t.Fatal(err)
}
if err != nil {
sender.stop()
if err := receiver.RescanDelay("default", 86400); err != nil {
t.Fatal(err)
}
if err = awaitCompletion("default", sender, receiver); err != nil {
t.Fatal(err)
}
sender.stop()
receiver.stop()
rc.AwaitSync("default", sender, receiver)
// The conflict should manifest on the s2 side again, where we should have
// moved the file to a conflict copy instead of just deleting it.
@ -180,7 +168,7 @@ func TestConflict(t *testing.T) {
}
}
func TestInitialMergeConflicts(t *testing.T) {
func TestConflictsInitialMerge(t *testing.T) {
log.Println("Cleaning...")
err := removeAll("s1", "s2", "h1/index*", "h2/index*")
if err != nil {
@ -224,18 +212,17 @@ func TestInitialMergeConflicts(t *testing.T) {
// Let them sync
sender, receiver := coSenderReceiver(t)
defer sender.stop()
defer receiver.stop()
sender := startInstance(t, 1)
defer checkedStop(t, sender)
receiver := startInstance(t, 2)
defer checkedStop(t, receiver)
log.Println("Syncing...")
if err = awaitCompletion("default", sender, receiver); err != nil {
t.Fatal(err)
}
rc.AwaitSync("default", sender, receiver)
sender.stop()
receiver.stop()
checkedStop(t, sender)
checkedStop(t, receiver)
log.Println("Verifying...")
@ -270,7 +257,7 @@ func TestInitialMergeConflicts(t *testing.T) {
}
}
func TestResetConflicts(t *testing.T) {
func TestConflictsIndexReset(t *testing.T) {
log.Println("Cleaning...")
err := removeAll("s1", "s2", "h1/index*", "h2/index*")
if err != nil {
@ -303,15 +290,14 @@ func TestResetConflicts(t *testing.T) {
// Let them sync
sender, receiver := coSenderReceiver(t)
defer sender.stop()
defer receiver.stop()
sender := startInstance(t, 1)
defer checkedStop(t, sender)
receiver := startInstance(t, 2)
defer checkedStop(t, receiver)
log.Println("Syncing...")
if err = awaitCompletion("default", sender, receiver); err != nil {
t.Fatal(err)
}
rc.AwaitSync("default", sender, receiver)
log.Println("Verifying...")
@ -341,43 +327,25 @@ func TestResetConflicts(t *testing.T) {
// This will make the file on the cluster look newer than what we have
// locally after we rest the index, unless we have a fix for that.
for i := 0; i < 5; i++ {
err = ioutil.WriteFile("s2/file2", []byte("hello1\n"), 0644)
if err != nil {
t.Fatal(err)
}
err = receiver.rescan("default")
err = receiver.Rescan("default")
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)
err = ioutil.WriteFile("s2/file2", []byte("hello2\n"), 0644)
if err != nil {
t.Fatal(err)
}
err = receiver.rescan("default")
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)
err = ioutil.WriteFile("s2/file2", []byte("hello3\n"), 0644)
if err != nil {
t.Fatal(err)
}
err = receiver.rescan("default")
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)
if err = awaitCompletion("default", sender, receiver); err != nil {
t.Fatal(err)
}
rc.AwaitSync("default", sender, receiver)
// Now nuke the index
log.Println("Resetting...")
receiver.stop()
checkedStop(t, receiver)
removeAll("h2/index*")
// s1/file1 (remote) changes while receiver is down
@ -388,7 +356,7 @@ func TestResetConflicts(t *testing.T) {
}
// s1 must know about it
err = sender.rescan("default")
err = sender.Rescan("default")
if err != nil {
t.Fatal(err)
}
@ -400,13 +368,12 @@ func TestResetConflicts(t *testing.T) {
t.Fatal(err)
}
receiver.start()
receiver = startInstance(t, 2)
defer checkedStop(t, receiver)
log.Println("Syncing...")
if err = awaitCompletion("default", sender, receiver); err != nil {
t.Fatal(err)
}
rc.AwaitSync("default", sender, receiver)
// s2 should have five files (three plus two conflicts)
@ -438,32 +405,3 @@ func TestResetConflicts(t *testing.T) {
t.Errorf("Expected 2 'file2' files in s2 instead of %d", len(files))
}
}
func coSenderReceiver(t *testing.T) (syncthingProcess, syncthingProcess) {
log.Println("Starting sender...")
sender := syncthingProcess{ // id1
instance: "1",
argv: []string{"-home", "h1"},
port: 8081,
apiKey: apiKey,
}
err := sender.start()
if err != nil {
t.Fatal(err)
}
log.Println("Starting receiver...")
receiver := syncthingProcess{ // id2
instance: "2",
argv: []string{"-home", "h2"},
port: 8082,
apiKey: apiKey,
}
err = receiver.start()
if err != nil {
sender.stop()
t.Fatal(err)
}
return sender, receiver
}

View File

@ -16,7 +16,7 @@ import (
"time"
)
func TestDelayScan(t *testing.T) {
func TestRescanWithDelay(t *testing.T) {
log.Println("Cleaning...")
err := removeAll("s1", "h1/index*")
if err != nil {
@ -36,30 +36,8 @@ func TestDelayScan(t *testing.T) {
}
log.Println("Starting up...")
st := syncthingProcess{ // id1
instance: "1",
argv: []string{"-home", "h1"},
port: 8081,
apiKey: apiKey,
}
err = st.start()
if err != nil {
t.Fatal(err)
}
// Wait for one scan to succeed, or up to 20 seconds...
// This is to let startup, UPnP etc complete.
for i := 0; i < 20; i++ {
err := st.rescan("default")
if err != nil {
time.Sleep(time.Second)
continue
}
break
}
// Wait for UPnP and stuff
time.Sleep(10 * time.Second)
st := startInstance(t, 1)
var wg sync.WaitGroup
log.Println("Starting scans...")
@ -68,7 +46,7 @@ func TestDelayScan(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
err := st.rescanNext("default", time.Duration(1)*time.Second)
err := st.RescanDelay("default", 1)
log.Println(j)
if err != nil {
log.Println(err)
@ -84,8 +62,5 @@ func TestDelayScan(t *testing.T) {
// This is where the real test is currently, since stop() checks for data
// race output in the log.
log.Println("Stopping...")
_, err = st.stop()
if err != nil {
t.Fatal(err)
}
checkedStop(t, st)
}

View File

@ -15,6 +15,7 @@ import (
"github.com/syncthing/protocol"
"github.com/syncthing/syncthing/internal/config"
"github.com/syncthing/syncthing/internal/rc"
)
func TestFileTypeChange(t *testing.T) {
@ -73,11 +74,11 @@ func testFileTypeChange(t *testing.T) {
// A file that we will replace with a directory later
fd, err := os.Create("s1/fileToReplace")
if err != nil {
if fd, err := os.Create("s1/fileToReplace"); err != nil {
t.Fatal(err)
}
} else {
fd.Close()
}
// A directory that we will replace with a file later
@ -92,52 +93,26 @@ func testFileTypeChange(t *testing.T) {
if err != nil {
t.Fatal(err)
}
fd, err = os.Create("s1/dirToReplace/emptyFile")
if err != nil {
if fd, err := os.Create("s1/dirToReplace/emptyFile"); err != nil {
t.Fatal(err)
}
} else {
fd.Close()
}
// Verify that the files and directories sync to the other side
sender := startInstance(t, 1)
defer checkedStop(t, sender)
receiver := startInstance(t, 2)
defer checkedStop(t, receiver)
log.Println("Syncing...")
sender := syncthingProcess{ // id1
instance: "1",
argv: []string{"-home", "h1"},
port: 8081,
apiKey: apiKey,
}
err = sender.start()
if err != nil {
t.Fatal(err)
}
defer sender.stop()
rc.AwaitSync("default", sender, receiver)
receiver := syncthingProcess{ // id2
instance: "2",
argv: []string{"-home", "h2"},
port: 8082,
apiKey: apiKey,
}
err = receiver.start()
if err != nil {
sender.stop()
t.Fatal(err)
}
defer receiver.stop()
err = awaitCompletion("default", sender, receiver)
if err != nil {
t.Fatal(err)
}
_, err = sender.stop()
if err != nil {
t.Fatal(err)
}
_, err = receiver.stop()
if err != nil {
// Delay scans for the moment
if err := sender.RescanDelay("default", 86400); err != nil {
t.Fatal(err)
}
@ -166,11 +141,11 @@ func testFileTypeChange(t *testing.T) {
if err != nil {
t.Fatal(err)
}
fd, err = os.Create("s1/emptyDirToReplace")
if err != nil {
if fd, err := os.Create("s1/emptyDirToReplace"); err != nil {
t.Fatal(err)
}
} else {
fd.Close()
}
// Clear directory and replace with file
@ -178,30 +153,21 @@ func testFileTypeChange(t *testing.T) {
if err != nil {
t.Fatal(err)
}
fd, err = os.Create("s1/dirToReplace")
if err != nil {
if fd, err := os.Create("s1/dirToReplace"); err != nil {
t.Fatal(err)
}
} else {
fd.Close()
}
// Sync these changes and recheck
log.Println("Syncing...")
err = sender.start()
if err != nil {
if err := sender.Rescan("default"); err != nil {
t.Fatal(err)
}
err = receiver.start()
if err != nil {
t.Fatal(err)
}
err = awaitCompletion("default", sender, receiver)
if err != nil {
t.Fatal(err)
}
rc.AwaitSync("default", sender, receiver)
log.Println("Comparing directories...")
err = compareDirectories("s1", "s2")

View File

@ -14,11 +14,10 @@ import (
"io/ioutil"
"net/http"
"strings"
"sync"
"testing"
"time"
"github.com/syncthing/protocol"
"github.com/syncthing/syncthing/internal/rc"
)
var jsonEndpoints = []string{
@ -46,18 +45,12 @@ var jsonEndpoints = []string{
}
func TestGetIndex(t *testing.T) {
st := syncthingProcess{
argv: []string{"-home", "h2"},
port: 8082,
instance: "2",
}
err := st.start()
if err != nil {
t.Fatal(err)
}
defer st.stop()
p := startInstance(t, 2)
defer checkedStop(t, p)
res, err := st.get("/index.html")
// Check for explicint index.html
res, err := http.Get("http://localhost:8082/index.html")
if err != nil {
t.Fatal(err)
}
@ -79,7 +72,9 @@ func TestGetIndex(t *testing.T) {
}
res.Body.Close()
res, err = st.get("/")
// Check for implicit index.html
res, err = http.Get("http://localhost:8082/")
if err != nil {
t.Fatal(err)
}
@ -103,17 +98,8 @@ func TestGetIndex(t *testing.T) {
}
func TestGetIndexAuth(t *testing.T) {
st := syncthingProcess{
argv: []string{"-home", "h1"},
port: 8081,
instance: "1",
apiKey: "abc123",
}
err := st.start()
if err != nil {
t.Fatal(err)
}
defer st.stop()
p := startInstance(t, 1)
defer checkedStop(t, p)
// Without auth should give 401
@ -162,19 +148,11 @@ func TestGetIndexAuth(t *testing.T) {
}
func TestGetJSON(t *testing.T) {
st := syncthingProcess{
argv: []string{"-home", "h2"},
port: 8082,
instance: "2",
}
err := st.start()
if err != nil {
t.Fatal(err)
}
defer st.stop()
p := startInstance(t, 2)
defer checkedStop(t, p)
for _, path := range jsonEndpoints {
res, err := st.get(path)
res, err := http.Get("http://127.0.0.1:8082" + path)
if err != nil {
t.Error(path, err)
continue
@ -196,16 +174,8 @@ func TestGetJSON(t *testing.T) {
}
func TestPOSTWithoutCSRF(t *testing.T) {
st := syncthingProcess{
argv: []string{"-home", "h2"},
port: 8082,
instance: "2",
}
err := st.start()
if err != nil {
t.Fatal(err)
}
defer st.stop()
p := startInstance(t, 2)
defer checkedStop(t, p)
// Should fail without CSRF
@ -271,12 +241,7 @@ func TestPOSTWithoutCSRF(t *testing.T) {
}
}
var (
initOnce sync.Once
proc syncthingProcess
)
func setupAPIBench() {
func setupAPIBench() *rc.Process {
err := removeAll("s1", "s2", "h1/index*", "h2/index*")
if err != nil {
panic(err)
@ -292,47 +257,20 @@ func setupAPIBench() {
panic(err)
}
proc = syncthingProcess{ // id1
instance: "1",
argv: []string{"-home", "h1"},
port: 8081,
apiKey: apiKey,
}
err = proc.start()
if err != nil {
panic(err)
}
// Wait for one scan to succeed, or up to 20 seconds... This is to let
// startup, UPnP etc complete and make sure the sender has the full index
// before they connect.
for i := 0; i < 20; i++ {
resp, err := proc.post("/rest/scan?folder=default", nil)
if err != nil {
time.Sleep(time.Second)
continue
}
if resp.StatusCode != 200 {
resp.Body.Close()
time.Sleep(time.Second)
continue
}
break
}
// This will panic if there is an actual failure to start, when we try to
// call nil.Fatal(...)
return startInstance(nil, 1)
}
func benchmarkURL(b *testing.B, url string) {
initOnce.Do(setupAPIBench)
p := setupAPIBench()
defer p.Stop()
b.ResetTimer()
for i := 0; i < b.N; i++ {
resp, err := proc.get(url)
_, err := p.Get(url)
if err != nil {
b.Fatal(err)
}
if resp.StatusCode != 200 {
b.Fatal(resp.Status)
}
resp.Body.Close()
}
}

View File

@ -29,16 +29,9 @@ func TestStressHTTP(t *testing.T) {
}
log.Println("Starting up...")
sender := syncthingProcess{ // id1
instance: "2",
argv: []string{"-home", "h2"},
port: 8082,
apiKey: apiKey,
}
err = sender.start()
if err != nil {
t.Fatal(err)
}
p := startInstance(t, 2)
defer checkedStop(t, p)
// Create a client with reasonable timeouts on all stages of the request.
@ -147,9 +140,4 @@ func TestStressHTTP(t *testing.T) {
if firstError != nil {
t.Error(firstError)
}
_, err = sender.stop()
if err != nil {
t.Error(err)
}
}

View File

@ -13,7 +13,6 @@ import (
"os"
"path/filepath"
"testing"
"time"
"github.com/syncthing/syncthing/internal/symlinks"
)
@ -27,30 +26,8 @@ func TestIgnores(t *testing.T) {
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)
}
// 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.
for i := 0; i < 20; i++ {
err := p.rescan("default")
if err != nil {
time.Sleep(time.Second)
continue
}
break
}
defer p.stop()
p := startInstance(t, 1)
defer checkedStop(t, p)
// Create eight empty files and directories
@ -85,18 +62,11 @@ func TestIgnores(t *testing.T) {
// Rescan and verify that we see them all
// Wait for one scan to succeed, or up to 20 seconds...
// This is to let startup, UPnP etc complete.
for i := 0; i < 20; i++ {
err := p.rescan("default")
if err != nil {
time.Sleep(time.Second)
continue
}
break
if err := p.Rescan("default"); err != nil {
t.Fatal(err)
}
m, err := p.model("default")
m, err := p.Model("default")
if err != nil {
t.Fatal(err)
}
@ -122,8 +92,11 @@ func TestIgnores(t *testing.T) {
// Rescan and verify that we see them
p.rescan("default")
m, err = p.model("default")
if err := p.Rescan("default"); err != nil {
t.Fatal(err)
}
m, err = p.Model("default")
if err != nil {
t.Fatal(err)
}
@ -149,8 +122,11 @@ func TestIgnores(t *testing.T) {
// Rescan and verify that we see them
p.rescan("default")
m, err = p.model("default")
if err := p.Rescan("default"); err != nil {
t.Fatal(err)
}
m, err = p.Model("default")
if err != nil {
t.Fatal(err)
}

View File

@ -17,6 +17,7 @@ import (
"github.com/syncthing/protocol"
"github.com/syncthing/syncthing/internal/config"
"github.com/syncthing/syncthing/internal/osutil"
"github.com/syncthing/syncthing/internal/rc"
)
func TestManyPeers(t *testing.T) {
@ -32,29 +33,18 @@ func TestManyPeers(t *testing.T) {
t.Fatal(err)
}
receiver := syncthingProcess{ // id2
instance: "2",
argv: []string{"-home", "h2"},
port: 8082,
apiKey: apiKey,
}
err = receiver.start()
if err != nil {
t.Fatal(err)
}
defer receiver.stop()
receiver := startInstance(t, 2)
defer checkedStop(t, receiver)
resp, err := receiver.get("/rest/system/config")
bs, err := receiver.Get("/rest/system/config")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Fatalf("Code %d != 200", resp.StatusCode)
}
var cfg config.Configuration
json.NewDecoder(resp.Body).Decode(&cfg)
resp.Body.Close()
if err := json.Unmarshal(bs, &cfg); err != nil {
t.Fatal(err)
}
for len(cfg.Devices) < 100 {
bs := make([]byte, 16)
@ -69,32 +59,15 @@ func TestManyPeers(t *testing.T) {
var buf bytes.Buffer
json.NewEncoder(&buf).Encode(cfg)
resp, err = receiver.post("/rest/system/config", &buf)
_, err = receiver.Post("/rest/system/config", &buf)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Fatalf("Code %d != 200", resp.StatusCode)
}
resp.Body.Close()
log.Println("Starting up...")
sender := syncthingProcess{ // id1
instance: "1",
argv: []string{"-home", "h1"},
port: 8081,
apiKey: apiKey,
}
err = sender.start()
if err != nil {
t.Fatal(err)
}
defer sender.stop()
sender := startInstance(t, 1)
defer checkedStop(t, sender)
err = awaitCompletion("default", sender, receiver)
if err != nil {
t.Fatal(err)
}
rc.AwaitSync("default", sender, receiver)
log.Println("Comparing directories...")
err = compareDirectories("s1", "s2")

View File

@ -18,6 +18,7 @@ import (
"github.com/syncthing/protocol"
"github.com/syncthing/syncthing/internal/config"
"github.com/syncthing/syncthing/internal/rc"
)
func TestOverride(t *testing.T) {
@ -61,51 +62,15 @@ func TestOverride(t *testing.T) {
t.Fatal(err)
}
log.Println("Starting master...")
master := syncthingProcess{ // id1
instance: "1",
argv: []string{"-home", "h1"},
port: 8081,
apiKey: apiKey,
}
err = master.start()
if err != nil {
t.Fatal(err)
}
defer master.stop()
master := startInstance(t, 1)
defer checkedStop(t, master)
// Wait for one scan to succeed, or up to 20 seconds... This is to let
// startup, UPnP etc complete and make sure the master has the full index
// before they connect.
for i := 0; i < 20; i++ {
err := master.rescan("default")
if err != nil {
time.Sleep(time.Second)
continue
}
break
}
log.Println("Starting slave...")
slave := syncthingProcess{ // id2
instance: "2",
argv: []string{"-home", "h2"},
port: 8082,
apiKey: apiKey,
}
err = slave.start()
if err != nil {
master.stop()
t.Fatal(err)
}
defer slave.stop()
slave := startInstance(t, 2)
defer checkedStop(t, slave)
log.Println("Syncing...")
err = awaitCompletion("default", master, slave)
if err != nil {
t.Fatal(err)
}
rc.AwaitSync("default", master, slave)
log.Println("Verifying...")
@ -133,8 +98,7 @@ func TestOverride(t *testing.T) {
t.Fatal(err)
}
err = slave.rescan("default")
if err != nil {
if err := slave.Rescan("default"); err != nil {
t.Fatal(err)
}
@ -144,20 +108,13 @@ func TestOverride(t *testing.T) {
log.Println("Hitting Override on master...")
resp, err := master.post("/rest/db/override?folder=default", nil)
if err != nil {
if _, err := master.Post("/rest/db/override?folder=default", nil); err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Fatal(resp.Status)
}
log.Println("Syncing...")
err = awaitCompletion("default", master, slave)
if err != nil {
t.Fatal(err)
}
rc.AwaitSync("default", master, slave)
// Verify that the override worked

View File

@ -16,7 +16,7 @@ import (
"time"
)
func TestParallellScan(t *testing.T) {
func TestRescanInParallel(t *testing.T) {
log.Println("Cleaning...")
err := removeAll("s1", "h1/index*")
if err != nil {
@ -35,31 +35,8 @@ func TestParallellScan(t *testing.T) {
t.Fatal(err)
}
log.Println("Starting up...")
st := syncthingProcess{ // id1
instance: "1",
argv: []string{"-home", "h1"},
port: 8081,
apiKey: apiKey,
}
err = st.start()
if err != nil {
t.Fatal(err)
}
// Wait for one scan to succeed, or up to 20 seconds...
// This is to let startup, UPnP etc complete.
for i := 0; i < 20; i++ {
err := st.rescan("default")
if err != nil {
time.Sleep(time.Second)
continue
}
break
}
// Wait for UPnP and stuff
time.Sleep(10 * time.Second)
st := startInstance(t, 1)
defer checkedStop(t, st)
var wg sync.WaitGroup
log.Println("Starting scans...")
@ -68,7 +45,7 @@ func TestParallellScan(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
err := st.rescan("default")
err := st.Rescan("default")
log.Println(j)
if err != nil {
log.Println(err)
@ -84,8 +61,5 @@ func TestParallellScan(t *testing.T) {
// This is where the real test is currently, since stop() checks for data
// race output in the log.
log.Println("Stopping...")
_, err = st.stop()
if err != nil {
t.Fatal(err)
}
checkedStop(t, st)
}

View File

@ -44,35 +44,23 @@ func testRestartDuringTransfer(t *testing.T, restartSender, restartReceiver bool
}
log.Println("Starting up...")
sender := syncthingProcess{ // id1
instance: "1",
argv: []string{"-home", "h1"},
port: 8081,
apiKey: apiKey,
}
err = sender.start()
if err != nil {
t.Fatal(err)
}
defer sender.stop()
sender := startInstance(t, 1)
defer func() {
// We need a closure over sender, since we'll update it later to point
// at another process.
checkedStop(t, sender)
}()
waitForScan(sender)
receiver := syncthingProcess{ // id2
instance: "2",
argv: []string{"-home", "h2"},
port: 8082,
apiKey: apiKey,
}
err = receiver.start()
if err != nil {
t.Fatal(err)
}
defer receiver.stop()
receiver := startInstance(t, 2)
defer func() {
// We need a receiver over sender, since we'll update it later to
// point at another process.
checkedStop(t, receiver)
}()
var prevBytes int
for {
recv, err := receiver.dbStatus("default")
recv, err := receiver.Model("default")
if err != nil {
t.Fatal(err)
}
@ -86,18 +74,12 @@ func testRestartDuringTransfer(t *testing.T, restartSender, restartReceiver bool
if restartReceiver {
log.Printf("Stopping receiver...")
_, err = receiver.stop()
if err != nil {
t.Fatal(err)
}
checkedStop(t, receiver)
}
if restartSender {
log.Printf("Stopping sender...")
_, err = sender.stop()
if err != nil {
t.Fatal(err)
}
checkedStop(t, sender)
}
var wg sync.WaitGroup
@ -106,8 +88,7 @@ func testRestartDuringTransfer(t *testing.T, restartSender, restartReceiver bool
wg.Add(1)
go func() {
time.Sleep(receiverDelay)
log.Printf("Starting receiver...")
receiver.start()
receiver = startInstance(t, 2)
wg.Done()
}()
}
@ -116,8 +97,7 @@ func testRestartDuringTransfer(t *testing.T, restartSender, restartReceiver bool
wg.Add(1)
go func() {
time.Sleep(senderDelay)
log.Printf("Starting sender...")
sender.start()
sender = startInstance(t, 1)
wg.Done()
}()
}
@ -128,14 +108,8 @@ func testRestartDuringTransfer(t *testing.T, restartSender, restartReceiver bool
time.Sleep(time.Second)
}
_, err = sender.stop()
if err != nil {
t.Fatal(err)
}
_, err = receiver.stop()
if err != nil {
t.Fatal(err)
}
checkedStop(t, sender)
checkedStop(t, receiver)
log.Println("Comparing directories...")
err = compareDirectories("s1", "s2")

View File

@ -13,6 +13,7 @@ import (
"os"
"path/filepath"
"testing"
"time"
)
func TestReset(t *testing.T) {
@ -23,32 +24,17 @@ func TestReset(t *testing.T) {
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 {
if err := os.Mkdir("s1", 0755); 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...")
waitForScan(p)
log.Println("Creating files...")
size := createFiles(t)
log.Println("Scanning files...")
waitForScan(p)
p := startInstance(t, 1)
defer checkedStop(t, p)
m, err := p.model("default")
m, err := p.Model("default")
if err != nil {
t.Fatal(err)
}
@ -59,40 +45,41 @@ func TestReset(t *testing.T) {
// Clear all files but restore the folder marker
log.Println("Cleaning...")
err = removeAll("s1/*", "h1/index*")
err = removeAll("s1")
if err != nil {
t.Fatal(err)
}
os.Create("s1/.stfolder")
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.reset("invalid")
_, err = p.Post("/rest/system/reset?folder=invalid", nil)
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")
_, err = p.Post("/rest/system/reset?folder=default", nil)
if err != nil {
t.Fatal("Failed to reset indexes of the default folder:", err)
}
// Wait for ST and scan
p.start()
waitForScan(p)
// Syncthing restarts on reset. But we set STNORESTART=1 for the tests. So
// we wait for it to exit, then do a stop so the rc.Process is happy and
// restart it again.
time.Sleep(time.Second)
checkedStop(t, p)
p = startInstance(t, 1)
// Verify that we see them
m, err = p.model("default")
m, err = p.Model("default")
if err != nil {
t.Fatal(err)
}
@ -104,10 +91,13 @@ func TestReset(t *testing.T) {
// Recreate the files and scan
log.Println("Creating files...")
size = createFiles(t)
waitForScan(p)
if err := p.Rescan("default"); err != nil {
t.Fatal(err)
}
// Verify that we see them
m, err = p.model("default")
m, err = p.Model("default")
if err != nil {
t.Fatal(err)
}
@ -118,16 +108,21 @@ func TestReset(t *testing.T) {
// Reset all indexes
log.Println("Reset DB...")
err = p.reset("")
_, err = p.Post("/rest/system/reset?folder=default", nil)
if err != nil {
t.Fatalf("Failed to reset indexes", err)
}
// Wait for ST and scan
p.start()
waitForScan(p)
// Syncthing restarts on reset. But we set STNORESTART=1 for the tests. So
// we wait for it to exit, then do a stop so the rc.Process is happy and
// restart it again.
time.Sleep(time.Second)
checkedStop(t, p)
m, err = p.model("default")
p = startInstance(t, 1)
defer checkedStop(t, p)
m, err = p.Model("default")
if err != nil {
t.Fatal(err)
}

View File

@ -17,6 +17,7 @@ import (
"github.com/syncthing/protocol"
"github.com/syncthing/syncthing/internal/config"
"github.com/syncthing/syncthing/internal/rc"
"github.com/syncthing/syncthing/internal/symlinks"
)
@ -164,45 +165,14 @@ func testSymlinks(t *testing.T) {
// Verify that the files and symlinks sync to the other side
sender := startInstance(t, 1)
defer checkedStop(t, sender)
receiver := startInstance(t, 2)
defer checkedStop(t, receiver)
log.Println("Syncing...")
sender := syncthingProcess{ // id1
instance: "1",
argv: []string{"-home", "h1"},
port: 8081,
apiKey: apiKey,
}
err = sender.start()
if err != nil {
t.Fatal(err)
}
defer sender.stop()
receiver := syncthingProcess{ // id2
instance: "2",
argv: []string{"-home", "h2"},
port: 8082,
apiKey: apiKey,
}
err = receiver.start()
if err != nil {
t.Fatal(err)
}
defer receiver.stop()
err = awaitCompletion("default", sender, receiver)
if err != nil {
t.Fatal(err)
}
_, err = sender.stop()
if err != nil {
t.Fatal(err)
}
_, err = receiver.stop()
if err != nil {
t.Fatal(err)
}
rc.AwaitSync("default", sender, receiver)
log.Println("Comparing directories...")
err = compareDirectories("s1", "s2")
@ -288,29 +258,11 @@ func testSymlinks(t *testing.T) {
log.Println("Syncing...")
err = sender.start()
if err != nil {
if err := sender.Rescan("default"); err != nil {
t.Fatal(err)
}
err = receiver.start()
if err != nil {
t.Fatal(err)
}
err = awaitCompletion("default", sender, receiver)
if err != nil {
t.Fatal(err)
}
_, err = sender.stop()
if err != nil {
t.Fatal(err)
}
_, err = receiver.stop()
if err != nil {
t.Fatal(err)
}
rc.AwaitSync("default", sender, receiver)
log.Println("Comparing directories...")
err = compareDirectories("s1", "s2")

View File

@ -11,12 +11,19 @@ package integration
import (
"fmt"
"log"
"math/rand"
"os"
"testing"
"time"
"github.com/syncthing/protocol"
"github.com/syncthing/syncthing/internal/config"
"github.com/syncthing/syncthing/internal/rc"
)
const (
longTimeLimit = 5 * time.Minute
shortTimeLimit = 45 * time.Second
)
func TestSyncClusterWithoutVersioning(t *testing.T) {
@ -46,6 +53,21 @@ func TestSyncClusterSimpleVersioning(t *testing.T) {
testSyncCluster(t)
}
func TestSyncClusterTrashcanVersioning(t *testing.T) {
// Use simple versioning
id, _ := protocol.DeviceIDFromString(id2)
cfg, _ := config.Load("h2/config.xml", id)
fld := cfg.Folders()["default"]
fld.Versioning = config.VersioningConfiguration{
Type: "trashcan",
Params: map[string]string{"cleanoutDays": "1"},
}
cfg.SetFolder(fld)
cfg.Save()
testSyncCluster(t)
}
func TestSyncClusterStaggeredVersioning(t *testing.T) {
// Use staggered versioning
id, _ := protocol.DeviceIDFromString(id2)
@ -68,12 +90,19 @@ func testSyncCluster(t *testing.T) {
// Another folder is shared between 1 and 2 only, in s12-1 and s12-2. A
// third folders is shared between 2 and 3, in s23-2 and s23-3.
// When -short is passed, keep it more reasonable.
timeLimit := longTimeLimit
if testing.Short() {
timeLimit = shortTimeLimit
}
const (
numFiles = 100
fileSizeExp = 20
iterations = 3
)
log.Printf("Testing with numFiles=%d, fileSizeExp=%d, iterations=%d", numFiles, fileSizeExp, iterations)
rand.Seed(42)
log.Printf("Testing with numFiles=%d, fileSizeExp=%d, timeLimit=%v", numFiles, fileSizeExp, timeLimit)
log.Println("Cleaning...")
err := removeAll("s1", "s12-1",
@ -152,36 +181,38 @@ func testSyncCluster(t *testing.T) {
expected := [][]fileInfo{e1, e2, e3}
// Start the syncers
p, err := scStartProcesses()
if err != nil {
t.Fatal(err)
}
defer func() {
for i := range p {
p[i].stop()
}
}()
log.Println("Waiting for startup...")
for _, dev := range p {
waitForScan(dev)
}
log.Println("Starting Syncthing...")
p0 := startInstance(t, 1)
defer checkedStop(t, p0)
p1 := startInstance(t, 2)
defer checkedStop(t, p1)
p2 := startInstance(t, 3)
defer checkedStop(t, p2)
p := []*rc.Process{p0, p1, p2}
start := time.Now()
iteration := 0
for time.Since(start) < timeLimit {
iteration++
log.Println("Iteration", iteration)
for count := 0; count < iterations; count++ {
log.Println("Forcing rescan...")
// Force rescan of folders
for i, device := range p {
if err := device.rescan("default"); err != nil {
if err := device.RescanDelay("default", 86400); err != nil {
t.Fatal(err)
}
if i < 2 {
if err := device.rescan("s12"); err != nil {
if i == 0 || i == 1 {
if err := device.RescanDelay("s12", 86400); err != nil {
t.Fatal(err)
}
}
if i > 1 {
if err := device.rescan("s23"); err != nil {
if i == 1 || i == 2 {
if err := device.RescanDelay("s23", 86400); err != nil {
t.Fatal(err)
}
}
@ -256,65 +287,22 @@ func testSyncCluster(t *testing.T) {
}
}
func scStartProcesses() ([]syncthingProcess, error) {
p := make([]syncthingProcess, 3)
p[0] = syncthingProcess{ // id1
instance: "1",
argv: []string{"-home", "h1"},
port: 8081,
apiKey: apiKey,
}
err := p[0].start()
if err != nil {
return nil, err
}
p[1] = syncthingProcess{ // id2
instance: "2",
argv: []string{"-home", "h2"},
port: 8082,
apiKey: apiKey,
}
err = p[1].start()
if err != nil {
p[0].stop()
return nil, err
}
p[2] = syncthingProcess{ // id3
instance: "3",
argv: []string{"-home", "h3"},
port: 8083,
apiKey: apiKey,
}
err = p[2].start()
if err != nil {
p[0].stop()
p[1].stop()
return nil, err
}
return p, nil
}
func scSyncAndCompare(p []syncthingProcess, expected [][]fileInfo) error {
func scSyncAndCompare(p []*rc.Process, expected [][]fileInfo) error {
log.Println("Syncing...")
// Special handling because we know which devices share which folders...
if err := awaitCompletion("default", p...); err != nil {
return err
for {
time.Sleep(250 * time.Millisecond)
if !rc.InSync("default", p...) {
continue
}
if err := awaitCompletion("s12", p[0], p[1]); err != nil {
return err
if !rc.InSync("s12", p[0], p[1]) {
continue
}
if err := awaitCompletion("s23", p[1], p[2]); err != nil {
return err
if !rc.InSync("s23", p[1], p[2]) {
continue
}
break
}
// This is necessary, or all files won't be in place even when everything
// is already reported in sync. Why?!
time.Sleep(5 * time.Second)
log.Println("Checking...")
@ -328,6 +316,7 @@ func scSyncAndCompare(p []syncthingProcess, expected [][]fileInfo) error {
}
}
if len(expected) > 1 {
for _, dir := range []string{"s12-1", "s12-2"} {
actual, err := directoryContents(dir)
if err != nil {
@ -337,7 +326,9 @@ func scSyncAndCompare(p []syncthingProcess, expected [][]fileInfo) error {
return fmt.Errorf("%s: %v", dir, err)
}
}
}
if len(expected) > 2 {
for _, dir := range []string{"s23-2", "s23-3"} {
actual, err := directoryContents(dir)
if err != nil {
@ -347,6 +338,7 @@ func scSyncAndCompare(p []syncthingProcess, expected [][]fileInfo) error {
return fmt.Errorf("%s: %v", dir, err)
}
}
}
return nil
}

View File

@ -1,390 +0,0 @@
// 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 (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"time"
"github.com/syncthing/protocol"
)
var env = []string{
"HOME=.",
"STGUIAPIKEY=" + apiKey,
"STNORESTART=1",
}
type syncthingProcess struct {
instance string
argv []string
port int
apiKey string
csrfToken string
lastEvent int
id protocol.DeviceID
cmd *exec.Cmd
logfd *os.File
}
func (p *syncthingProcess) start() error {
if p.logfd == nil {
logfd, err := os.Create("logs/" + getTestName() + "-" + p.instance + ".out")
if err != nil {
return err
}
p.logfd = logfd
}
binary := "../bin/syncthing"
// We check to see if there's an instance specific binary we should run,
// for example if we are running integration tests between different
// versions. If there isn't, we just go with the default.
if _, err := os.Stat(binary + "-" + p.instance); err == nil {
binary = binary + "-" + p.instance
}
if _, err := os.Stat(binary + "-" + p.instance + ".exe"); err == nil {
binary = binary + "-" + p.instance + ".exe"
}
argv := append(p.argv, "-no-browser", "-verbose")
cmd := exec.Command(binary, argv...)
cmd.Stdout = p.logfd
cmd.Stderr = p.logfd
cmd.Env = append(os.Environ(), env...)
err := cmd.Start()
if err != nil {
return err
}
p.cmd = cmd
for {
time.Sleep(250 * time.Millisecond)
resp, err := p.get("/rest/system/status")
if err != nil {
continue
}
var sysData map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&sysData)
resp.Body.Close()
if err != nil {
// This one is unexpected. Print it.
log.Println("/rest/system/status (JSON):", err)
continue
}
id, err := protocol.DeviceIDFromString(sysData["myID"].(string))
if err != nil {
// This one is unexpected. Print it.
log.Println("/rest/system/status (myID):", err)
continue
}
p.id = id
return nil
}
}
func (p *syncthingProcess) stop() (*os.ProcessState, error) {
p.cmd.Process.Signal(os.Kill)
p.cmd.Wait()
fd, err := os.Open(p.logfd.Name())
if err != nil {
return p.cmd.ProcessState, err
}
defer fd.Close()
raceConditionStart := []byte("WARNING: DATA RACE")
raceConditionSep := []byte("==================")
panicConditionStart := []byte("panic:")
panicConditionSep := []byte(p.id.String()[:5])
sc := bufio.NewScanner(fd)
race := false
_panic := false
for sc.Scan() {
line := sc.Bytes()
if race || _panic {
if bytes.Contains(line, panicConditionSep) {
_panic = false
continue
}
fmt.Printf("%s\n", line)
if bytes.Contains(line, raceConditionSep) {
race = false
}
} else if bytes.Contains(line, raceConditionStart) {
fmt.Printf("%s\n", raceConditionSep)
fmt.Printf("%s\n", raceConditionStart)
race = true
if err == nil {
err = errors.New("Race condition detected")
}
} else if bytes.Contains(line, panicConditionStart) {
_panic = true
if err == nil {
err = errors.New("Panic detected")
}
}
}
return p.cmd.ProcessState, err
}
func (p *syncthingProcess) get(path string) (*http.Response, error) {
client := &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
DisableKeepAlives: true,
},
}
req, err := http.NewRequest("GET", fmt.Sprintf("http://127.0.0.1:%d%s", p.port, path), nil)
if err != nil {
return nil, err
}
if p.apiKey != "" {
req.Header.Add("X-API-Key", p.apiKey)
}
if p.csrfToken != "" {
req.Header.Add("X-CSRF-Token", p.csrfToken)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func (p *syncthingProcess) post(path string, data io.Reader) (*http.Response, error) {
client := &http.Client{
Timeout: 600 * time.Second,
Transport: &http.Transport{
DisableKeepAlives: true,
},
}
req, err := http.NewRequest("POST", fmt.Sprintf("http://127.0.0.1:%d%s", p.port, path), data)
if err != nil {
return nil, err
}
if p.apiKey != "" {
req.Header.Add("X-API-Key", p.apiKey)
}
if p.csrfToken != "" {
req.Header.Add("X-CSRF-Token", p.csrfToken)
}
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
type model struct {
GlobalBytes int
GlobalDeleted int
GlobalFiles int
InSyncBytes int
InSyncFiles int
Invalid string
LocalBytes int
LocalDeleted int
LocalFiles int
NeedBytes int
NeedFiles int
State string
StateChanged time.Time
Version int
}
func (p *syncthingProcess) model(folder string) (model, error) {
resp, err := p.get("/rest/db/status?folder=" + folder)
if err != nil {
return model{}, err
}
var res model
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return model{}, err
}
return res, nil
}
type event struct {
ID int
Time time.Time
Type string
Data interface{}
}
func (p *syncthingProcess) events() ([]event, error) {
resp, err := p.get(fmt.Sprintf("/rest/events?since=%d", p.lastEvent))
if err != nil {
return nil, err
}
defer resp.Body.Close()
var evs []event
err = json.NewDecoder(resp.Body).Decode(&evs)
if err != nil {
return nil, err
}
p.lastEvent = evs[len(evs)-1].ID
return evs, err
}
type versionResp struct {
Version string
}
func (p *syncthingProcess) version() (string, error) {
resp, err := p.get("/rest/system/version")
if err != nil {
return "", err
}
defer resp.Body.Close()
var v versionResp
err = json.NewDecoder(resp.Body).Decode(&v)
if err != nil {
return "", err
}
return v.Version, nil
}
type statusResp struct {
GlobalBytes int
InSyncBytes int
Version int
}
func (p *syncthingProcess) dbStatus(folder string) (statusResp, error) {
resp, err := p.get("/rest/db/status?folder=" + folder)
if err != nil {
return statusResp{}, err
}
defer resp.Body.Close()
var s statusResp
err = json.NewDecoder(resp.Body).Decode(&s)
if err != nil {
return statusResp{}, err
}
return s, nil
}
func (p *syncthingProcess) insync(folder string) (bool, int, error) {
s, err := p.dbStatus(folder)
if err != nil {
return false, 0, err
}
return s.GlobalBytes == s.InSyncBytes, s.Version, nil
}
func (p *syncthingProcess) rescan(folder string) error {
resp, err := p.post("/rest/db/scan?folder="+folder, nil)
if err != nil {
return err
}
data, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("Rescan %q: status code %d: %s", folder, resp.StatusCode, data)
}
return nil
}
func (p *syncthingProcess) rescanNext(folder string, next time.Duration) error {
resp, err := p.post("/rest/db/scan?folder="+folder+"&next="+strconv.Itoa(int(next.Seconds())), nil)
if err != nil {
return err
}
data, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("Rescan %q: status code %d: %s", folder, resp.StatusCode, data)
}
return nil
}
func (p *syncthingProcess) reset(folder string) error {
resp, err := p.post("/rest/system/reset?folder="+folder, nil)
if err != nil {
return err
}
data, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("Reset %q: status code %d: %s", folder, resp.StatusCode, data)
}
return nil
}
func awaitCompletion(folder string, ps ...syncthingProcess) error {
mainLoop:
for {
time.Sleep(2500 * time.Millisecond)
expectedVersion := 0
for _, p := range ps {
insync, version, err := p.insync(folder)
if err != nil {
if isTimeout(err) {
continue mainLoop
}
return err
}
if !insync {
continue mainLoop
}
if expectedVersion == 0 {
expectedVersion = version
} else if version != expectedVersion {
// Version number mismatch between devices, so not in sync.
continue mainLoop
}
}
return nil
}
}
func waitForScan(p syncthingProcess) {
// Wait for one scan to succeed, or up to 20 seconds...
for i := 0; i < 20; i++ {
err := p.rescan("default")
if err != nil {
time.Sleep(time.Second)
continue
}
break
}
}

View File

@ -79,58 +79,20 @@ func benchmarkTransfer(t *testing.T, files, sizeExp int) {
}
log.Printf("Total %.01f MiB in %d files", float64(total)/1024/1024, nfiles)
log.Println("Starting sender...")
sender := syncthingProcess{ // id1
instance: "1",
argv: []string{"-home", "h1"},
port: 8081,
apiKey: apiKey,
}
err = sender.start()
if err != nil {
t.Fatal(err)
}
// Wait for one scan to succeed, or up to 20 seconds... This is to let
// startup, UPnP etc complete and make sure the sender has the full index
// before they connect.
for i := 0; i < 20; i++ {
resp, err := sender.post("/rest/scan?folder=default", nil)
if err != nil {
time.Sleep(time.Second)
continue
}
if resp.StatusCode != 200 {
resp.Body.Close()
time.Sleep(time.Second)
continue
}
break
}
log.Println("Starting receiver...")
receiver := syncthingProcess{ // id2
instance: "2",
argv: []string{"-home", "h2"},
port: 8082,
apiKey: apiKey,
}
err = receiver.start()
if err != nil {
sender.stop()
t.Fatal(err)
}
sender := startInstance(t, 1)
defer checkedStop(t, sender)
receiver := startInstance(t, 2)
defer checkedStop(t, receiver)
var t0, t1 time.Time
lastEvent := 0
loop:
for {
evs, err := receiver.events()
evs, err := receiver.Events(lastEvent)
if err != nil {
if isTimeout(err) {
continue
}
sender.stop()
receiver.stop()
t.Fatal(err)
}
@ -150,16 +112,17 @@ loop:
break loop
}
}
lastEvent = ev.ID
}
time.Sleep(250 * time.Millisecond)
}
sendProc, err := sender.stop()
sendProc, err := sender.Stop()
if err != nil {
t.Fatal(err)
}
recvProc, err := receiver.stop()
recvProc, err := receiver.Stop()
if err != nil {
t.Fatal(err)
}

View File

@ -22,10 +22,12 @@ import (
"runtime"
"sort"
"strings"
"testing"
"time"
"unicode"
"github.com/syncthing/syncthing/internal/osutil"
"github.com/syncthing/syncthing/internal/rc"
"github.com/syncthing/syncthing/internal/symlinks"
)
@ -170,6 +172,13 @@ func alterFiles(dir string) error {
// Change capitalization
case r == 2 && comps > 3 && rand.Float64() < 0.2:
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
// Syncthing is currently broken for case-only renames on case-
// insensitive platforms.
// https://github.com/syncthing/syncthing/issues/1787
return nil
}
base := []rune(filepath.Base(path))
for i, r := range base {
if rand.Float64() < 0.5 {
@ -208,6 +217,10 @@ func alterFiles(dir string) error {
}
return err
/*
This fails. Bug?
// Rename the file, while potentially moving it up in the directory hiearachy
case r == 4 && comps > 2 && (info.Mode().IsRegular() || rand.Float64() < 0.2):
rpath := filepath.Dir(path)
if rand.Float64() < 0.2 {
@ -216,7 +229,9 @@ func alterFiles(dir string) error {
}
}
return osutil.TryRename(path, filepath.Join(rpath, randomName()))
*/
}
return nil
})
if err != nil {
@ -506,3 +521,23 @@ func getTestName() string {
}
return time.Now().String()
}
func checkedStop(t *testing.T, p *rc.Process) {
if _, err := p.Stop(); err != nil {
t.Fatal(err)
}
}
func startInstance(t *testing.T, i int) *rc.Process {
log.Printf("Starting instance %d...", i)
addr := fmt.Sprintf("127.0.0.1:%d", 8080+i)
log := fmt.Sprintf("logs/%s-%d-%d.out", getTestName(), i, time.Now().Unix()%86400)
p := rc.NewProcess(addr)
p.LogTo(log)
if err := p.Start("../bin/syncthing", "-home", fmt.Sprintf("h%d", i), "-audit", "-no-browser"); err != nil {
t.Fatal(err)
}
p.AwaitStartup()
return p
}