syncthing/test/common_test.go

448 lines
8.4 KiB
Go
Raw Normal View History

2014-11-16 20:13:20 +00:00
// Copyright (C) 2014 The Syncthing Authors.
2014-09-29 19:43:32 +00: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/>.
// +build integration
package integration_test
import (
"bufio"
"bytes"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io"
"log"
2014-09-27 18:09:36 +00:00
"math/rand"
"net/http"
"os"
"os/exec"
"path/filepath"
"time"
2014-11-19 04:18:19 +00:00
"github.com/syncthing/syncthing/internal/symlinks"
)
2014-09-27 18:09:36 +00:00
func init() {
rand.Seed(42)
}
2014-09-16 21:14:19 +00:00
const (
id1 = "I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU"
id2 = "JMFJCXB-GZDE4BN-OCJE3VF-65GYZNU-AIVJRET-3J6HMRQ-AUQIGJO-FKNHMQU"
apiKey = "abc123"
)
var env = []string{
"HOME=.",
"STTRACE=model,protocol",
2014-09-16 21:14:19 +00:00
"STGUIAPIKEY=" + apiKey,
"STNORESTART=1",
2014-09-27 18:09:36 +00:00
"STPERFSTATS=1",
2014-09-16 21:14:19 +00:00
}
type syncthingProcess struct {
2014-09-16 21:14:19 +00:00
log string
argv []string
port int
apiKey string
csrfToken string
2014-09-27 18:09:36 +00:00
lastEvent int
cmd *exec.Cmd
logfd *os.File
}
2014-10-06 10:03:49 +00:00
func (p *syncthingProcess) start() error {
if p.logfd == nil {
logfd, err := os.Create(p.log)
if err != nil {
2014-10-06 10:03:49 +00:00
return err
}
p.logfd = logfd
}
cmd := exec.Command("../bin/syncthing", p.argv...)
cmd.Stdout = p.logfd
cmd.Stderr = p.logfd
cmd.Env = append(env, fmt.Sprintf("STPROFILER=:%d", p.port+1000))
err := cmd.Start()
if err != nil {
2014-10-06 10:03:49 +00:00
return err
}
p.cmd = cmd
2014-09-16 21:14:19 +00:00
for {
2014-10-06 10:03:49 +00:00
resp, err := p.get("/")
2014-09-16 21:14:19 +00:00
if err == nil {
2014-10-06 10:03:49 +00:00
resp.Body.Close()
return nil
2014-09-16 21:14:19 +00:00
}
time.Sleep(250 * time.Millisecond)
}
}
func (p *syncthingProcess) stop() error {
2014-09-16 21:14:19 +00:00
p.cmd.Process.Signal(os.Interrupt)
p.cmd.Wait()
fd, err := os.Open(p.log)
if err != nil {
return err
}
defer fd.Close()
raceConditionStart := []byte("WARNING: DATA RACE")
raceConditionSep := []byte("==================")
sc := bufio.NewScanner(fd)
race := false
for sc.Scan() {
line := sc.Bytes()
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")
}
}
}
return err
}
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{
Timeout: 2 * time.Second,
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
}
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,
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
}
func (p *syncthingProcess) peerCompletion() (map[string]int, error) {
2014-09-16 21:14:19 +00:00
resp, err := p.get("/rest/debug/peerCompletion")
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-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) {
resp, err := p.get("/rest/version")
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
}
func generateFiles(dir string, files, maxexp int, srcname string) error {
fd, err := os.Open(srcname)
if err != nil {
return err
}
for i := 0; i < files; i++ {
n := randomName()
p0 := filepath.Join(dir, string(n[0]), n[0:2])
err = os.MkdirAll(p0, 0755)
if err != nil {
log.Fatal(err)
}
2014-09-27 18:09:36 +00:00
s := 1 << uint(rand.Intn(maxexp))
a := 128 * 1024
if a > s {
a = s
}
2014-09-27 18:09:36 +00:00
s += rand.Intn(a)
src := io.LimitReader(&inifiteReader{fd}, int64(s))
p1 := filepath.Join(p0, n)
dst, err := os.Create(p1)
if err != nil {
return err
}
_, err = io.Copy(dst, src)
if err != nil {
return err
}
err = dst.Close()
if err != nil {
return err
}
2014-09-27 18:09:36 +00:00
err = os.Chmod(p1, os.FileMode(rand.Intn(0777)|0400))
if err != nil {
return err
}
2014-09-27 18:09:36 +00:00
t := time.Now().Add(-time.Duration(rand.Intn(30*86400)) * time.Second)
err = os.Chtimes(p1, t, t)
if err != nil {
return err
}
}
return nil
}
2014-09-27 18:09:36 +00:00
func ReadRand(bs []byte) (int, error) {
var r uint32
for i := range bs {
if i%4 == 0 {
r = uint32(rand.Int63())
}
bs[i] = byte(r >> uint((i%4)*8))
}
return len(bs), nil
}
func randomName() string {
var b [16]byte
2014-09-27 18:09:36 +00:00
ReadRand(b[:])
return fmt.Sprintf("%x", b[:])
}
type inifiteReader struct {
rd io.ReadSeeker
}
func (i *inifiteReader) Read(bs []byte) (int, error) {
n, err := i.rd.Read(bs)
if err == io.EOF {
err = nil
i.rd.Seek(0, 0)
}
return n, err
}
// rm -rf
func removeAll(dirs ...string) error {
for _, dir := range dirs {
2014-08-25 15:45:13 +00:00
os.RemoveAll(dir)
}
return nil
}
// Compare a number of directories. Returns nil if the contents are identical,
// otherwise an error describing the first found difference.
func compareDirectories(dirs ...string) error {
chans := make([]chan fileInfo, len(dirs))
for i := range chans {
chans[i] = make(chan fileInfo)
}
abort := make(chan struct{})
for i := range dirs {
startWalker(dirs[i], chans[i], abort)
}
res := make([]fileInfo, len(dirs))
for {
numDone := 0
for i := range chans {
fi, ok := <-chans[i]
if !ok {
numDone++
}
res[i] = fi
}
for i := 1; i < len(res); i++ {
if res[i] != res[0] {
close(abort)
return fmt.Errorf("Mismatch; %#v (%s) != %#v (%s)", res[i], dirs[i], res[0], dirs[0])
}
}
if numDone == len(dirs) {
return nil
}
}
}
type fileInfo struct {
name string
mode os.FileMode
2014-09-16 21:14:19 +00:00
mod int64
hash [16]byte
}
func startWalker(dir string, res chan<- fileInfo, abort <-chan struct{}) {
walker := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
rn, _ := filepath.Rel(dir, path)
if rn == "." || rn == ".stfolder" {
return nil
}
if rn == ".stversions" {
return filepath.SkipDir
}
var f fileInfo
if info.Mode()&os.ModeSymlink != 0 {
2014-11-19 04:18:19 +00:00
f = fileInfo{
name: rn,
mode: os.ModeSymlink,
}
tgt, _, err := symlinks.Read(path)
if err != nil {
return err
}
h := md5.New()
h.Write([]byte(tgt))
hash := h.Sum(nil)
copy(f.hash[:], hash)
} else if info.IsDir() {
f = fileInfo{
name: rn,
mode: info.Mode(),
// hash and modtime zero for directories
}
} else {
f = fileInfo{
name: rn,
mode: info.Mode(),
2014-09-16 21:14:19 +00:00
mod: info.ModTime().Unix(),
}
sum, err := md5file(path)
if err != nil {
return err
}
f.hash = sum
}
select {
case res <- f:
return nil
case <-abort:
return errors.New("abort")
}
}
go func() {
filepath.Walk(dir, walker)
close(res)
}()
}
func md5file(fname string) (hash [16]byte, err error) {
f, err := os.Open(fname)
if err != nil {
return
}
defer f.Close()
h := md5.New()
io.Copy(h, f)
hb := h.Sum(nil)
copy(hash[:], hb)
return
}