2014-12-12 20:29:57 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-12-12 20:29:57 +00:00
|
|
|
|
|
|
|
// +build integration
|
|
|
|
|
2014-12-17 11:31:03 +00:00
|
|
|
package integration
|
2014-12-12 20:29:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-11-05 09:05:18 +00:00
|
|
|
"path/filepath"
|
2014-12-12 20:29:57 +00:00
|
|
|
"testing"
|
2014-12-16 21:55:44 +00:00
|
|
|
"time"
|
2014-12-12 20:29:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCLIReset(t *testing.T) {
|
2017-11-13 00:06:16 +00:00
|
|
|
dirs := []string{"h1/index-v0.14.0.db"}
|
2014-12-12 20:29:57 +00:00
|
|
|
|
|
|
|
// Create directories that reset will remove
|
|
|
|
|
|
|
|
for _, dir := range dirs {
|
|
|
|
err := os.Mkdir(dir, 0755)
|
|
|
|
if err != nil && !os.IsExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run reset to clean up
|
|
|
|
|
2017-11-13 00:06:16 +00:00
|
|
|
cmd := exec.Command("../bin/syncthing", "-no-browser", "-home", "h1", "-reset-database")
|
2014-12-12 20:29:57 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stdout
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that they're gone
|
|
|
|
|
|
|
|
for _, dir := range dirs {
|
|
|
|
_, err := os.Stat(dir)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("%s still exists", dir)
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 09:47:36 +00:00
|
|
|
|
|
|
|
// Clean up
|
|
|
|
|
2017-11-05 09:05:18 +00:00
|
|
|
dirs, err = filepath.Glob("*.syncthing-reset-*")
|
2014-12-30 09:47:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
removeAll(dirs...)
|
2014-12-12 20:29:57 +00:00
|
|
|
}
|
2014-12-12 20:39:03 +00:00
|
|
|
|
|
|
|
func TestCLIGenerate(t *testing.T) {
|
|
|
|
err := os.RemoveAll("home.out")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -generate should create a bunch of stuff
|
|
|
|
|
2015-05-07 18:53:44 +00:00
|
|
|
cmd := exec.Command("../bin/syncthing", "-no-browser", "-generate", "home.out")
|
2014-12-12 20:39:03 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stdout
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the files that should have been created have been
|
|
|
|
|
|
|
|
for _, f := range []string{"home.out/config.xml", "home.out/cert.pem", "home.out/key.pem"} {
|
|
|
|
_, err := os.Stat(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s is not correctly generated", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 21:55:44 +00:00
|
|
|
|
|
|
|
func TestCLIFirstStartup(t *testing.T) {
|
|
|
|
err := os.RemoveAll("home.out")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// First startup should create config, BEP certificate, and HTTP certificate.
|
|
|
|
|
2015-05-07 18:53:44 +00:00
|
|
|
cmd := exec.Command("../bin/syncthing", "-no-browser", "-home", "home.out")
|
2014-12-16 21:55:44 +00:00
|
|
|
cmd.Env = append(os.Environ(), "STNORESTART=1")
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stdout
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exitError := make(chan error, 1)
|
|
|
|
filesOk := make(chan struct{})
|
|
|
|
processDone := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Wait for process exit.
|
|
|
|
exitError <- cmd.Wait()
|
|
|
|
close(processDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
again:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-processDone:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// Verify that the files that should have been created have been
|
|
|
|
for _, f := range []string{"home.out/config.xml", "home.out/cert.pem", "home.out/key.pem", "home.out/https-cert.pem", "home.out/https-key.pem"} {
|
|
|
|
_, err := os.Stat(f)
|
|
|
|
if err != nil {
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
continue again
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the process doesn't exit with an error just after creating certificates.
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
filesOk <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case e := <-exitError:
|
|
|
|
t.Error(e)
|
|
|
|
case <-filesOk:
|
|
|
|
cmd.Process.Kill()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|