cli_test.go 3.0 KB

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