2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-08-05 02:12:20 -07:00
|
|
|
|
2014-08-05 06:50:05 -07:00
|
|
|
// +build integration
|
|
|
|
|
2014-12-17 04:31:03 -07:00
|
|
|
package integration
|
2014-08-05 02:12:20 -07:00
|
|
|
|
|
|
|
import (
|
2014-11-29 14:17:54 -07:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2014-08-05 02:12:20 -07:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
2014-09-16 14:14:19 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var env = []string{
|
|
|
|
"HOME=.",
|
|
|
|
"STGUIAPIKEY=" + apiKey,
|
2014-09-18 02:45:48 -07:00
|
|
|
"STNORESTART=1",
|
2014-09-16 14:14:19 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 02:12:20 -07:00
|
|
|
type syncthingProcess struct {
|
2015-01-11 01:26:20 -07:00
|
|
|
instance string
|
2014-09-16 14:14:19 -07:00
|
|
|
argv []string
|
|
|
|
port int
|
|
|
|
apiKey string
|
|
|
|
csrfToken string
|
2014-09-27 11:09:36 -07:00
|
|
|
lastEvent int
|
2014-08-05 02:12:20 -07:00
|
|
|
|
|
|
|
cmd *exec.Cmd
|
|
|
|
logfd *os.File
|
|
|
|
}
|
|
|
|
|
2014-10-06 03:03:49 -07:00
|
|
|
func (p *syncthingProcess) start() error {
|
2014-08-05 02:12:20 -07:00
|
|
|
if p.logfd == nil {
|
2015-01-21 16:59:08 -07:00
|
|
|
logfd, err := os.Create("logs/" + getTestName() + "-" + p.instance + ".out")
|
2014-08-05 02:12:20 -07:00
|
|
|
if err != nil {
|
2014-10-06 03:03:49 -07:00
|
|
|
return err
|
2014-08-05 02:12:20 -07:00
|
|
|
}
|
|
|
|
p.logfd = logfd
|
|
|
|
}
|
|
|
|
|
2015-01-11 01:28:45 -07: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"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(binary, p.argv...)
|
2014-08-05 02:12:20 -07:00
|
|
|
cmd.Stdout = p.logfd
|
|
|
|
cmd.Stderr = p.logfd
|
2014-12-08 14:15:10 -07:00
|
|
|
cmd.Env = append(os.Environ(), env...)
|
2014-08-05 02:12:20 -07:00
|
|
|
|
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
2014-10-06 03:03:49 -07:00
|
|
|
return err
|
2014-08-05 02:12:20 -07:00
|
|
|
}
|
|
|
|
p.cmd = cmd
|
2014-09-16 14:14:19 -07:00
|
|
|
|
|
|
|
for {
|
2014-10-06 03:03:49 -07:00
|
|
|
resp, err := p.get("/")
|
2014-09-16 14:14:19 -07:00
|
|
|
if err == nil {
|
2014-10-06 03:03:49 -07:00
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
2014-09-16 14:14:19 -07:00
|
|
|
}
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
}
|
2014-08-05 02:12:20 -07:00
|
|
|
}
|
|
|
|
|
2014-11-29 14:17:54 -07:00
|
|
|
func (p *syncthingProcess) stop() error {
|
2014-12-08 14:15:10 -07:00
|
|
|
p.cmd.Process.Signal(os.Kill)
|
2014-08-05 02:12:20 -07:00
|
|
|
p.cmd.Wait()
|
2014-11-29 14:17:54 -07:00
|
|
|
|
2015-01-22 15:42:39 -07:00
|
|
|
fd, err := os.Open(p.logfd.Name())
|
2014-11-29 14:17:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
2014-12-02 23:47:40 -07:00
|
|
|
raceConditionStart := []byte("WARNING: DATA RACE")
|
|
|
|
raceConditionSep := []byte("==================")
|
2014-11-29 14:17:54 -07:00
|
|
|
sc := bufio.NewScanner(fd)
|
2014-12-02 23:47:40 -07:00
|
|
|
race := false
|
2014-11-29 14:17:54 -07:00
|
|
|
for sc.Scan() {
|
|
|
|
line := sc.Bytes()
|
2014-12-02 23:47:40 -07:00
|
|
|
if race {
|
|
|
|
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")
|
|
|
|
}
|
2014-11-29 14:17:54 -07:00
|
|
|
}
|
|
|
|
}
|
2014-12-02 23:47:40 -07:00
|
|
|
return err
|
2014-08-05 02:12:20 -07:00
|
|
|
}
|
|
|
|
|
2014-09-16 14:14:19 -07:00
|
|
|
func (p *syncthingProcess) get(path string) (*http.Response, error) {
|
2014-09-17 08:11:53 -07:00
|
|
|
client := &http.Client{
|
2014-12-17 04:31:03 -07:00
|
|
|
Timeout: 30 * time.Second,
|
2014-10-06 01:44:46 -07:00
|
|
|
Transport: &http.Transport{
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
},
|
2014-09-17 08:11:53 -07:00
|
|
|
}
|
2014-09-16 14:14:19 -07: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 08:11:53 -07:00
|
|
|
resp, err := client.Do(req)
|
2014-09-16 14:14:19 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2014-10-20 22:37:38 -07:00
|
|
|
func (p *syncthingProcess) post(path string, data io.Reader) (*http.Response, error) {
|
|
|
|
client := &http.Client{
|
2014-12-02 14:13:08 -07:00
|
|
|
Timeout: 600 * time.Second,
|
2014-10-20 22:37:38 -07: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-08-05 02:12:20 -07:00
|
|
|
func (p *syncthingProcess) peerCompletion() (map[string]int, error) {
|
2014-09-16 14:14:19 -07:00
|
|
|
resp, err := p.get("/rest/debug/peerCompletion")
|
2014-08-05 02:12:20 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
comp := map[string]int{}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&comp)
|
|
|
|
return comp, err
|
|
|
|
}
|
|
|
|
|
2014-12-21 01:26:54 -07: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) {
|
|
|
|
resp, err := p.get("/rest/model?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
|
|
|
|
}
|
|
|
|
|
2014-09-27 11:09:36 -07: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 14:14:19 -07:00
|
|
|
func (p *syncthingProcess) version() (string, error) {
|
|
|
|
resp, err := p.get("/rest/version")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-09-27 11:09:36 -07:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var v versionResp
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&v)
|
2014-09-16 14:14:19 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-09-27 11:09:36 -07:00
|
|
|
return v.Version, nil
|
2014-09-16 14:14:19 -07:00
|
|
|
}
|