Improve integration tests

This commit is contained in:
Jakob Borg 2014-09-16 23:14:19 +02:00
parent ce5ad296ae
commit d1ad778a64
3 changed files with 83 additions and 46 deletions

View File

@ -2,7 +2,7 @@
set -euo pipefail set -euo pipefail
IFS=$'\n\t' IFS=$'\n\t'
go test -tags integration -v
./test-http.sh ./test-http.sh
./test-merge.sh ./test-merge.sh
./test-delupd.sh ./test-delupd.sh
go test -tags integration -v

View File

@ -13,30 +13,44 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
mr "math/rand" mr "math/rand"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"time" "time"
) )
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",
"STGUIAPIKEY=" + apiKey,
}
type syncthingProcess struct { type syncthingProcess struct {
log string log string
argv []string argv []string
port int port int
apiKey string
csrfToken string
cmd *exec.Cmd cmd *exec.Cmd
logfd *os.File logfd *os.File
} }
func (p *syncthingProcess) start() error { func (p *syncthingProcess) start() (string, error) {
if p.logfd == nil { if p.logfd == nil {
logfd, err := os.Create(p.log) logfd, err := os.Create(p.log)
if err != nil { if err != nil {
return err return "", err
} }
p.logfd = logfd p.logfd = logfd
} }
@ -48,23 +62,44 @@ func (p *syncthingProcess) start() error {
err := cmd.Start() err := cmd.Start()
if err != nil { if err != nil {
return err return "", err
} }
p.cmd = cmd p.cmd = cmd
return nil
for {
ver, err := p.version()
if err == nil {
return ver, nil
}
time.Sleep(250 * time.Millisecond)
}
} }
func (p *syncthingProcess) stop() { func (p *syncthingProcess) stop() {
if runtime.GOOS != "windows" { p.cmd.Process.Signal(os.Interrupt)
p.cmd.Process.Signal(os.Interrupt)
} else {
p.cmd.Process.Kill()
}
p.cmd.Wait() p.cmd.Wait()
} }
func (p *syncthingProcess) get(path string) (*http.Response, error) {
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 := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func (p *syncthingProcess) peerCompletion() (map[string]int, error) { func (p *syncthingProcess) peerCompletion() (map[string]int, error) {
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/rest/debug/peerCompletion", p.port)) resp, err := p.get("/rest/debug/peerCompletion")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -75,6 +110,19 @@ func (p *syncthingProcess) peerCompletion() (map[string]int, error) {
return comp, err return comp, err
} }
func (p *syncthingProcess) version() (string, error) {
resp, err := p.get("/rest/version")
if err != nil {
return "", err
}
bs, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return "", err
}
return string(bs), nil
}
type fileGenerator struct { type fileGenerator struct {
files int files int
maxexp int maxexp int
@ -202,7 +250,7 @@ func compareDirectories(dirs ...string) error {
type fileInfo struct { type fileInfo struct {
name string name string
mode os.FileMode mode os.FileMode
mod time.Time mod int64
hash [16]byte hash [16]byte
} }
@ -228,7 +276,7 @@ func startWalker(dir string, res chan<- fileInfo, abort <-chan struct{}) {
f = fileInfo{ f = fileInfo{
name: rn, name: rn,
mode: info.Mode(), mode: info.Mode(),
mod: info.ModTime(), mod: info.ModTime().Unix(),
} }
sum, err := md5file(path) sum, err := md5file(path)
if err != nil { if err != nil {

View File

@ -13,17 +13,6 @@ import (
"time" "time"
) )
const (
apiKey = "abc123" // Used when talking to the processes under test
id1 = "I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU"
id2 = "JMFJCXB-GZDE4BN-OCJE3VF-65GYZNU-AIVJRET-3J6HMRQ-AUQIGJO-FKNHMQU"
)
var env = []string{
"HOME=.",
"STTRACE=model",
}
func TestRestartReceiverDuringTransfer(t *testing.T) { func TestRestartReceiverDuringTransfer(t *testing.T) {
testRestartDuringTransfer(t, false, true, 0, 0) testRestartDuringTransfer(t, false, true, 0, 0)
} }
@ -33,48 +22,50 @@ func TestRestartSenderDuringTransfer(t *testing.T) {
} }
func TestRestartSenderAndReceiverDuringTransfer(t *testing.T) { func TestRestartSenderAndReceiverDuringTransfer(t *testing.T) {
// // Give the receiver some time to rot with needed files but // Give the receiver some time to rot with needed files but
// // without any peer. This triggers // without any peer. This triggers
// // https://github.com/syncthing/syncthing/issues/463 // https://github.com/syncthing/syncthing/issues/463
testRestartDuringTransfer(t, true, true, 10*time.Second, 0) testRestartDuringTransfer(t, true, true, 10*time.Second, 0)
} }
func testRestartDuringTransfer(t *testing.T, restartSender, restartReceiver bool, senderDelay, receiverDelay time.Duration) { func testRestartDuringTransfer(t *testing.T, restartSender, restartReceiver bool, senderDelay, receiverDelay time.Duration) {
log.Println("Cleaning...") log.Println("Cleaning...")
err := removeAll("s1", "s2", "f1/index", "f2/index") err := removeAll("s1", "s2", "h1/index", "h2/index")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
log.Println("Generating files...") log.Println("Generating files...")
err = generateFiles("s1", 1000, 20, "../bin/syncthing") err = generateFiles("s1", 1000, 22, "../bin/syncthing")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
log.Println("Starting up...") log.Println("Starting up...")
sender := syncthingProcess{ // id1 sender := syncthingProcess{ // id1
log: "1.out", log: "1.out",
argv: []string{"-home", "f1"}, argv: []string{"-home", "h1"},
port: 8081, port: 8081,
apiKey: apiKey,
} }
err = sender.start() ver, err := sender.start()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
log.Println(ver)
receiver := syncthingProcess{ // id2 receiver := syncthingProcess{ // id2
log: "2.out", log: "2.out",
argv: []string{"-home", "f2"}, argv: []string{"-home", "h2"},
port: 8082, port: 8082,
apiKey: apiKey,
} }
err = receiver.start() ver, err = receiver.start()
if err != nil { if err != nil {
sender.stop()
t.Fatal(err) t.Fatal(err)
} }
log.Println(ver)
// Give them time to start up
time.Sleep(1 * time.Second)
var prevComp int var prevComp int
for { for {
@ -130,8 +121,6 @@ func testRestartDuringTransfer(t *testing.T, restartSender, restartReceiver bool
prevComp = curComp prevComp = curComp
} }
time.Sleep(1 * time.Second)
} }
sender.stop() sender.stop()