backend_test.go.no 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package main
  2. import (
  3. "testing"
  4. "os"
  5. "time"
  6. "io/ioutil"
  7. "github.com/flashmob/go-guerrilla/tests/testcert"
  8. "github.com/flashmob/go-guerrilla/log"
  9. "runtime"
  10. "github.com/spf13/cobra"
  11. "sync"
  12. "strings"
  13. "fmt"
  14. )
  15. func TestBadBackendReload2(t *testing.T) {
  16. testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
  17. os.Truncate("../../tests/testlog", 0)
  18. //mainlog, _ = log.GetLogger("../../tests/testlog")
  19. mainlog, _ = log.GetLogger("stdout")
  20. mainlog.SetLevel("debug")
  21. mainlog.Info("are u sure")
  22. mainlog.Info("not another word")
  23. select {
  24. case <-time.After(10 * time.Second):
  25. mainlog.Info("paabix")
  26. stacktrace := make([]byte, 8192)
  27. length := runtime.Stack(stacktrace, true)
  28. _ = length
  29. fmt.Fprintf(ioutil.Discard, (string(stacktrace[:length])))
  30. panic("timed out")
  31. }
  32. mainlog.Info("not another word")
  33. sigKill()
  34. ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644)
  35. cmd := &cobra.Command{}
  36. configPath = "configJsonA.json"
  37. var serveWG sync.WaitGroup
  38. serveWG.Add(1)
  39. go func() {
  40. mainlog.Info("start serve")
  41. serve(cmd, []string{})
  42. serveWG.Done()
  43. }()
  44. mainlog.Info("after start")
  45. time.Sleep(testPauseDuration)
  46. // change the config file to the one with a broken backend
  47. ioutil.WriteFile("configJsonA.json", []byte(configJsonE), 0644)
  48. // test SIGHUP via the kill command
  49. // Would not work on windows as kill is not available.
  50. // TODO: Implement an alternative test for windows.
  51. if runtime.GOOS != "windows" {
  52. sigHup()
  53. time.Sleep(testPauseDuration) // allow sighup to do its job
  54. // did the pidfile change as expected?
  55. if _, err := os.Stat("./pidfile2.pid"); os.IsNotExist(err) {
  56. t.Error("pidfile not changed after sighup SIGHUP", err)
  57. }
  58. }
  59. // send kill signal and wait for exit
  60. sigKill()
  61. serveWG.Wait()
  62. //time.Sleep(time.Second * 3)
  63. // did backend started as expected?
  64. fd, err := os.Open("../../tests/testlog")
  65. if err != nil {
  66. t.Error(err)
  67. }
  68. if read, err := ioutil.ReadAll(fd); err == nil {
  69. logOutput := string(read)
  70. if i := strings.Index(logOutput, "reverted to old backend config"); i < 0 {
  71. t.Error("did not revert to old backend config")
  72. }
  73. }
  74. // cleanup
  75. //os.Truncate("../../tests/testlog", 0)
  76. os.Remove("configJsonA.json")
  77. os.Remove("./pidfile.pid")
  78. os.Remove("./pidfile2.pid")
  79. }