cli_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. // +build integration
  7. package integration
  8. import (
  9. "os"
  10. "os/exec"
  11. "path/filepath"
  12. "testing"
  13. "time"
  14. )
  15. func TestCLIReset(t *testing.T) {
  16. dirs := []string{"h1/index-v0.14.0.db"}
  17. // Create directories that reset will remove
  18. for _, dir := range dirs {
  19. err := os.Mkdir(dir, 0755)
  20. if err != nil && !os.IsExist(err) {
  21. t.Fatal(err)
  22. }
  23. }
  24. // Run reset to clean up
  25. cmd := exec.Command("../bin/syncthing", "-no-browser", "-home", "h1", "-reset-database")
  26. cmd.Stdout = os.Stdout
  27. cmd.Stderr = os.Stdout
  28. err := cmd.Run()
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. // Verify that they're gone
  33. for _, dir := range dirs {
  34. _, err := os.Stat(dir)
  35. if err == nil {
  36. t.Errorf("%s still exists", dir)
  37. }
  38. }
  39. // Clean up
  40. dirs, err = filepath.Glob("*.syncthing-reset-*")
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. removeAll(dirs...)
  45. }
  46. func TestCLIGenerate(t *testing.T) {
  47. err := os.RemoveAll("home.out")
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. // -generate should create a bunch of stuff
  52. cmd := exec.Command("../bin/syncthing", "-no-browser", "-generate", "home.out")
  53. cmd.Stdout = os.Stdout
  54. cmd.Stderr = os.Stdout
  55. err = cmd.Run()
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. // Verify that the files that should have been created have been
  60. for _, f := range []string{"home.out/config.xml", "home.out/cert.pem", "home.out/key.pem"} {
  61. _, err := os.Stat(f)
  62. if err != nil {
  63. t.Errorf("%s is not correctly generated", f)
  64. }
  65. }
  66. }
  67. func TestCLIFirstStartup(t *testing.T) {
  68. err := os.RemoveAll("home.out")
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. // First startup should create config, BEP certificate, and HTTP certificate.
  73. cmd := exec.Command("../bin/syncthing", "-no-browser", "-home", "home.out")
  74. cmd.Env = append(os.Environ(), "STNORESTART=1")
  75. cmd.Stdout = os.Stdout
  76. cmd.Stderr = os.Stdout
  77. err = cmd.Start()
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. exitError := make(chan error, 1)
  82. filesOk := make(chan struct{})
  83. processDone := make(chan struct{})
  84. go func() {
  85. // Wait for process exit.
  86. exitError <- cmd.Wait()
  87. close(processDone)
  88. }()
  89. go func() {
  90. again:
  91. for {
  92. select {
  93. case <-processDone:
  94. return
  95. default:
  96. // Verify that the files that should have been created have been
  97. 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"} {
  98. _, err := os.Stat(f)
  99. if err != nil {
  100. time.Sleep(500 * time.Millisecond)
  101. continue again
  102. }
  103. }
  104. // Make sure the process doesn't exit with an error just after creating certificates.
  105. time.Sleep(time.Second)
  106. filesOk <- struct{}{}
  107. return
  108. }
  109. }
  110. }()
  111. select {
  112. case e := <-exitError:
  113. t.Error(e)
  114. case <-filesOk:
  115. cmd.Process.Kill()
  116. return
  117. }
  118. }