2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// 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/.
|
2014-08-05 09:12:20 +00:00
|
|
|
|
2014-08-05 13:50:05 +00:00
|
|
|
// +build integration
|
|
|
|
|
2014-12-17 11:31:03 +00:00
|
|
|
package integration
|
2014-08-05 09:12:20 +00:00
|
|
|
|
|
|
|
import (
|
2014-11-29 21:17:54 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2014-08-05 09:12:20 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-04-01 08:43:19 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2014-08-05 09:12:20 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-05-01 12:30:17 +00:00
|
|
|
"strconv"
|
2014-08-05 09:12:20 +00:00
|
|
|
"time"
|
2015-04-01 08:43:19 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/protocol"
|
2014-09-16 21:14:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var env = []string{
|
|
|
|
"HOME=.",
|
|
|
|
"STGUIAPIKEY=" + apiKey,
|
2014-09-18 09:45:48 +00:00
|
|
|
"STNORESTART=1",
|
2014-09-16 21:14:19 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 09:12:20 +00:00
|
|
|
type syncthingProcess struct {
|
2015-01-11 08:26:20 +00:00
|
|
|
instance string
|
2014-09-16 21:14:19 +00:00
|
|
|
argv []string
|
|
|
|
port int
|
|
|
|
apiKey string
|
|
|
|
csrfToken string
|
2014-09-27 18:09:36 +00:00
|
|
|
lastEvent int
|
2015-04-01 08:43:19 +00:00
|
|
|
id protocol.DeviceID
|
2014-08-05 09:12:20 +00:00
|
|
|
|
|
|
|
cmd *exec.Cmd
|
|
|
|
logfd *os.File
|
|
|
|
}
|
|
|
|
|
2014-10-06 10:03:49 +00:00
|
|
|
func (p *syncthingProcess) start() error {
|
2014-08-05 09:12:20 +00:00
|
|
|
if p.logfd == nil {
|
2015-01-21 23:59:08 +00:00
|
|
|
logfd, err := os.Create("logs/" + getTestName() + "-" + p.instance + ".out")
|
2014-08-05 09:12:20 +00:00
|
|
|
if err != nil {
|
2014-10-06 10:03:49 +00:00
|
|
|
return err
|
2014-08-05 09:12:20 +00:00
|
|
|
}
|
|
|
|
p.logfd = logfd
|
|
|
|
}
|
|
|
|
|
2015-01-11 08:28:45 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:56:43 +00:00
|
|
|
argv := append(p.argv, "-no-browser", "-verbose")
|
2015-05-07 18:53:44 +00:00
|
|
|
cmd := exec.Command(binary, argv...)
|
2014-08-05 09:12:20 +00:00
|
|
|
cmd.Stdout = p.logfd
|
|
|
|
cmd.Stderr = p.logfd
|
2014-12-08 21:15:10 +00:00
|
|
|
cmd.Env = append(os.Environ(), env...)
|
2014-08-05 09:12:20 +00:00
|
|
|
|
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
2014-10-06 10:03:49 +00:00
|
|
|
return err
|
2014-08-05 09:12:20 +00:00
|
|
|
}
|
|
|
|
p.cmd = cmd
|
2014-09-16 21:14:19 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
2015-04-01 08:43:19 +00:00
|
|
|
|
2015-04-06 08:23:27 +00:00
|
|
|
resp, err := p.get("/rest/system/status")
|
2015-04-01 08:43:19 +00:00
|
|
|
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.
|
2015-04-06 08:23:27 +00:00
|
|
|
log.Println("/rest/system/status (JSON):", err)
|
2015-04-01 08:43:19 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := protocol.DeviceIDFromString(sysData["myID"].(string))
|
|
|
|
if err != nil {
|
|
|
|
// This one is unexpected. Print it.
|
2015-04-06 08:23:27 +00:00
|
|
|
log.Println("/rest/system/status (myID):", err)
|
2015-04-01 08:43:19 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
p.id = id
|
|
|
|
|
|
|
|
return nil
|
2014-09-16 21:14:19 +00:00
|
|
|
}
|
2014-08-05 09:12:20 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 06:43:57 +00:00
|
|
|
func (p *syncthingProcess) stop() (*os.ProcessState, error) {
|
2014-12-08 21:15:10 +00:00
|
|
|
p.cmd.Process.Signal(os.Kill)
|
2014-08-05 09:12:20 +00:00
|
|
|
p.cmd.Wait()
|
2014-11-29 21:17:54 +00:00
|
|
|
|
2015-01-22 22:42:39 +00:00
|
|
|
fd, err := os.Open(p.logfd.Name())
|
2014-11-29 21:17:54 +00:00
|
|
|
if err != nil {
|
2015-04-30 06:43:57 +00:00
|
|
|
return p.cmd.ProcessState, err
|
2014-11-29 21:17:54 +00:00
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
2014-12-03 06:47:40 +00:00
|
|
|
raceConditionStart := []byte("WARNING: DATA RACE")
|
|
|
|
raceConditionSep := []byte("==================")
|
2015-04-09 10:49:02 +00:00
|
|
|
panicConditionStart := []byte("panic:")
|
|
|
|
panicConditionSep := []byte(p.id.String()[:5])
|
2014-11-29 21:17:54 +00:00
|
|
|
sc := bufio.NewScanner(fd)
|
2014-12-03 06:47:40 +00:00
|
|
|
race := false
|
2015-04-09 10:49:02 +00:00
|
|
|
_panic := false
|
2014-11-29 21:17:54 +00:00
|
|
|
for sc.Scan() {
|
|
|
|
line := sc.Bytes()
|
2015-04-09 10:49:02 +00:00
|
|
|
if race || _panic {
|
|
|
|
if bytes.Contains(line, panicConditionSep) {
|
|
|
|
_panic = false
|
|
|
|
continue
|
|
|
|
}
|
2014-12-03 06:47:40 +00:00
|
|
|
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")
|
|
|
|
}
|
2015-04-09 10:49:02 +00:00
|
|
|
} else if bytes.Contains(line, panicConditionStart) {
|
|
|
|
_panic = true
|
|
|
|
if err == nil {
|
|
|
|
err = errors.New("Panic detected")
|
|
|
|
}
|
2014-11-29 21:17:54 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-30 06:43:57 +00:00
|
|
|
return p.cmd.ProcessState, err
|
2014-08-05 09:12:20 +00:00
|
|
|
}
|
|
|
|
|
2014-09-16 21:14:19 +00:00
|
|
|
func (p *syncthingProcess) get(path string) (*http.Response, error) {
|
2014-09-17 15:11:53 +00:00
|
|
|
client := &http.Client{
|
2014-12-17 11:31:03 +00:00
|
|
|
Timeout: 30 * time.Second,
|
2014-10-06 08:44:46 +00:00
|
|
|
Transport: &http.Transport{
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
},
|
2014-09-17 15:11:53 +00:00
|
|
|
}
|
2014-09-16 21:14:19 +00:00
|
|
|
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)
|
|
|
|
}
|
2014-09-17 15:11:53 +00:00
|
|
|
resp, err := client.Do(req)
|
2014-09-16 21:14:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2014-10-21 05:37:38 +00:00
|
|
|
func (p *syncthingProcess) post(path string, data io.Reader) (*http.Response, error) {
|
|
|
|
client := &http.Client{
|
2014-12-02 21:13:08 +00:00
|
|
|
Timeout: 600 * time.Second,
|
2014-10-21 05:37:38 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-12-21 08:26:54 +00:00
|
|
|
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) {
|
2015-04-06 08:23:27 +00:00
|
|
|
resp, err := p.get("/rest/db/status?folder=" + folder)
|
2014-12-21 08:26:54 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-09-27 18:09:36 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-09-16 21:14:19 +00:00
|
|
|
func (p *syncthingProcess) version() (string, error) {
|
2015-04-06 08:23:27 +00:00
|
|
|
resp, err := p.get("/rest/system/version")
|
2014-09-16 21:14:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-09-27 18:09:36 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var v versionResp
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&v)
|
2014-09-16 21:14:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-09-27 18:09:36 +00:00
|
|
|
return v.Version, nil
|
2014-09-16 21:14:19 +00:00
|
|
|
}
|
2015-04-01 08:43:19 +00:00
|
|
|
|
2015-05-23 20:56:43 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-01 08:43:19 +00:00
|
|
|
func (p *syncthingProcess) rescan(folder string) error {
|
2015-04-06 08:23:27 +00:00
|
|
|
resp, err := p.post("/rest/db/scan?folder="+folder, nil)
|
2015-04-01 08:43:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-01 12:30:17 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-03 18:06:03 +00:00
|
|
|
func (p *syncthingProcess) reset(folder string) error {
|
2015-04-06 08:23:27 +00:00
|
|
|
resp, err := p.post("/rest/system/reset?folder="+folder, nil)
|
2015-04-03 18:06:03 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:56:43 +00:00
|
|
|
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
|
|
|
|
}
|
2015-04-01 08:43:19 +00:00
|
|
|
}
|
2015-05-23 20:56:43 +00:00
|
|
|
|
|
|
|
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
|
2015-04-01 08:43:19 +00:00
|
|
|
}
|
|
|
|
}
|