From d1ad778a64da29b614f4676326bfc9e8bba3220f Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Tue, 16 Sep 2014 23:14:19 +0200 Subject: [PATCH] Improve integration tests --- integration/all.sh | 2 +- integration/common_test.go | 80 ++++++++++++++++++++++++++++------- integration/reconnect_test.go | 47 ++++++++------------ 3 files changed, 83 insertions(+), 46 deletions(-) diff --git a/integration/all.sh b/integration/all.sh index d7945ac24..0ffec5844 100755 --- a/integration/all.sh +++ b/integration/all.sh @@ -2,7 +2,7 @@ set -euo pipefail IFS=$'\n\t' +go test -tags integration -v ./test-http.sh ./test-merge.sh ./test-delupd.sh -go test -tags integration -v diff --git a/integration/common_test.go b/integration/common_test.go index eda0ec537..921557ba7 100644 --- a/integration/common_test.go +++ b/integration/common_test.go @@ -13,30 +13,44 @@ import ( "errors" "fmt" "io" + "io/ioutil" "log" mr "math/rand" "net/http" "os" "os/exec" "path/filepath" - "runtime" "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 { - log string - argv []string - port int + log string + argv []string + port int + apiKey string + csrfToken string cmd *exec.Cmd logfd *os.File } -func (p *syncthingProcess) start() error { +func (p *syncthingProcess) start() (string, error) { if p.logfd == nil { logfd, err := os.Create(p.log) if err != nil { - return err + return "", err } p.logfd = logfd } @@ -48,23 +62,44 @@ func (p *syncthingProcess) start() error { err := cmd.Start() if err != nil { - return err + return "", err } 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() { - if runtime.GOOS != "windows" { - p.cmd.Process.Signal(os.Interrupt) - } else { - p.cmd.Process.Kill() - } + p.cmd.Process.Signal(os.Interrupt) 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) { - 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 { return nil, err } @@ -75,6 +110,19 @@ func (p *syncthingProcess) peerCompletion() (map[string]int, error) { 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 { files int maxexp int @@ -202,7 +250,7 @@ func compareDirectories(dirs ...string) error { type fileInfo struct { name string mode os.FileMode - mod time.Time + mod int64 hash [16]byte } @@ -228,7 +276,7 @@ func startWalker(dir string, res chan<- fileInfo, abort <-chan struct{}) { f = fileInfo{ name: rn, mode: info.Mode(), - mod: info.ModTime(), + mod: info.ModTime().Unix(), } sum, err := md5file(path) if err != nil { diff --git a/integration/reconnect_test.go b/integration/reconnect_test.go index 480ec195e..18172f551 100644 --- a/integration/reconnect_test.go +++ b/integration/reconnect_test.go @@ -13,17 +13,6 @@ import ( "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) { testRestartDuringTransfer(t, false, true, 0, 0) } @@ -33,48 +22,50 @@ func TestRestartSenderDuringTransfer(t *testing.T) { } func TestRestartSenderAndReceiverDuringTransfer(t *testing.T) { - // // Give the receiver some time to rot with needed files but - // // without any peer. This triggers - // // https://github.com/syncthing/syncthing/issues/463 + // Give the receiver some time to rot with needed files but + // without any peer. This triggers + // https://github.com/syncthing/syncthing/issues/463 testRestartDuringTransfer(t, true, true, 10*time.Second, 0) } func testRestartDuringTransfer(t *testing.T, restartSender, restartReceiver bool, senderDelay, receiverDelay time.Duration) { log.Println("Cleaning...") - err := removeAll("s1", "s2", "f1/index", "f2/index") + err := removeAll("s1", "s2", "h1/index", "h2/index") if err != nil { t.Fatal(err) } log.Println("Generating files...") - err = generateFiles("s1", 1000, 20, "../bin/syncthing") + err = generateFiles("s1", 1000, 22, "../bin/syncthing") if err != nil { t.Fatal(err) } log.Println("Starting up...") sender := syncthingProcess{ // id1 - log: "1.out", - argv: []string{"-home", "f1"}, - port: 8081, + log: "1.out", + argv: []string{"-home", "h1"}, + port: 8081, + apiKey: apiKey, } - err = sender.start() + ver, err := sender.start() if err != nil { t.Fatal(err) } + log.Println(ver) receiver := syncthingProcess{ // id2 - log: "2.out", - argv: []string{"-home", "f2"}, - port: 8082, + log: "2.out", + argv: []string{"-home", "h2"}, + port: 8082, + apiKey: apiKey, } - err = receiver.start() + ver, err = receiver.start() if err != nil { + sender.stop() t.Fatal(err) } - - // Give them time to start up - time.Sleep(1 * time.Second) + log.Println(ver) var prevComp int for { @@ -130,8 +121,6 @@ func testRestartDuringTransfer(t *testing.T, restartSender, restartReceiver bool prevComp = curComp } - - time.Sleep(1 * time.Second) } sender.stop()