cli_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // +build integration
  16. package integration
  17. import (
  18. "os"
  19. "os/exec"
  20. "path/filepath"
  21. "testing"
  22. "time"
  23. )
  24. func TestCLIReset(t *testing.T) {
  25. dirs := []string{"s1", "s12-1", "h1/index"}
  26. // Create directories that reset will remove
  27. for _, dir := range dirs {
  28. err := os.Mkdir(dir, 0755)
  29. if err != nil && !os.IsExist(err) {
  30. t.Fatal(err)
  31. }
  32. }
  33. // Run reset to clean up
  34. cmd := exec.Command("../bin/syncthing", "-home", "h1", "-reset")
  35. cmd.Stdout = os.Stdout
  36. cmd.Stderr = os.Stdout
  37. err := cmd.Run()
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. // Verify that they're gone
  42. for _, dir := range dirs {
  43. _, err := os.Stat(dir)
  44. if err == nil {
  45. t.Errorf("%s still exists", dir)
  46. }
  47. }
  48. // Clean up
  49. dirs, err = filepath.Glob("*.syncthing-reset-*")
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. removeAll(dirs...)
  54. }
  55. func TestCLIGenerate(t *testing.T) {
  56. err := os.RemoveAll("home.out")
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. // -generate should create a bunch of stuff
  61. cmd := exec.Command("../bin/syncthing", "-generate", "home.out")
  62. cmd.Stdout = os.Stdout
  63. cmd.Stderr = os.Stdout
  64. err = cmd.Run()
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. // Verify that the files that should have been created have been
  69. for _, f := range []string{"home.out/config.xml", "home.out/cert.pem", "home.out/key.pem"} {
  70. _, err := os.Stat(f)
  71. if err != nil {
  72. t.Errorf("%s is not correctly generated", f)
  73. }
  74. }
  75. }
  76. func TestCLIFirstStartup(t *testing.T) {
  77. err := os.RemoveAll("home.out")
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. // First startup should create config, BEP certificate, and HTTP certificate.
  82. cmd := exec.Command("../bin/syncthing", "-home", "home.out")
  83. cmd.Env = append(os.Environ(), "STNORESTART=1")
  84. cmd.Stdout = os.Stdout
  85. cmd.Stderr = os.Stdout
  86. err = cmd.Start()
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. exitError := make(chan error, 1)
  91. filesOk := make(chan struct{})
  92. processDone := make(chan struct{})
  93. go func() {
  94. // Wait for process exit.
  95. exitError <- cmd.Wait()
  96. close(processDone)
  97. }()
  98. go func() {
  99. again:
  100. for {
  101. select {
  102. case <-processDone:
  103. return
  104. default:
  105. // Verify that the files that should have been created have been
  106. 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"} {
  107. _, err := os.Stat(f)
  108. if err != nil {
  109. time.Sleep(500 * time.Millisecond)
  110. continue again
  111. }
  112. }
  113. // Make sure the process doesn't exit with an error just after creating certificates.
  114. time.Sleep(time.Second)
  115. filesOk <- struct{}{}
  116. return
  117. }
  118. }
  119. }()
  120. select {
  121. case e := <-exitError:
  122. t.Error(e)
  123. case <-filesOk:
  124. cmd.Process.Kill()
  125. return
  126. }
  127. }