cli_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_test
  17. import (
  18. "os"
  19. "os/exec"
  20. "testing"
  21. "time"
  22. )
  23. func TestCLIReset(t *testing.T) {
  24. dirs := []string{"s1", "s12-1", "h1/index"}
  25. // Create directories that reset will remove
  26. for _, dir := range dirs {
  27. err := os.Mkdir(dir, 0755)
  28. if err != nil && !os.IsExist(err) {
  29. t.Fatal(err)
  30. }
  31. }
  32. // Run reset to clean up
  33. cmd := exec.Command("../bin/syncthing", "-home", "h1", "-reset")
  34. cmd.Stdout = os.Stdout
  35. cmd.Stderr = os.Stdout
  36. err := cmd.Run()
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. // Verify that they're gone
  41. for _, dir := range dirs {
  42. _, err := os.Stat(dir)
  43. if err == nil {
  44. t.Errorf("%s still exists", dir)
  45. }
  46. }
  47. }
  48. func TestCLIGenerate(t *testing.T) {
  49. err := os.RemoveAll("home.out")
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. // -generate should create a bunch of stuff
  54. cmd := exec.Command("../bin/syncthing", "-generate", "home.out")
  55. cmd.Stdout = os.Stdout
  56. cmd.Stderr = os.Stdout
  57. err = cmd.Run()
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. // Verify that the files that should have been created have been
  62. for _, f := range []string{"home.out/config.xml", "home.out/cert.pem", "home.out/key.pem"} {
  63. _, err := os.Stat(f)
  64. if err != nil {
  65. t.Errorf("%s is not correctly generated", f)
  66. }
  67. }
  68. }
  69. func TestCLIFirstStartup(t *testing.T) {
  70. err := os.RemoveAll("home.out")
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. // First startup should create config, BEP certificate, and HTTP certificate.
  75. cmd := exec.Command("../bin/syncthing", "-home", "home.out")
  76. cmd.Env = append(os.Environ(), "STNORESTART=1")
  77. cmd.Stdout = os.Stdout
  78. cmd.Stderr = os.Stdout
  79. err = cmd.Start()
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. exitError := make(chan error, 1)
  84. filesOk := make(chan struct{})
  85. processDone := make(chan struct{})
  86. go func() {
  87. // Wait for process exit.
  88. exitError <- cmd.Wait()
  89. close(processDone)
  90. }()
  91. go func() {
  92. again:
  93. for {
  94. select {
  95. case <-processDone:
  96. return
  97. default:
  98. // Verify that the files that should have been created have been
  99. 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"} {
  100. _, err := os.Stat(f)
  101. if err != nil {
  102. time.Sleep(500 * time.Millisecond)
  103. continue again
  104. }
  105. }
  106. // Make sure the process doesn't exit with an error just after creating certificates.
  107. time.Sleep(time.Second)
  108. filesOk <- struct{}{}
  109. return
  110. }
  111. }
  112. }()
  113. select {
  114. case e := <-exitError:
  115. t.Error(e)
  116. case <-filesOk:
  117. cmd.Process.Kill()
  118. return
  119. }
  120. }