package main import ( "testing" "os" "time" "io/ioutil" "github.com/flashmob/go-guerrilla/tests/testcert" "github.com/flashmob/go-guerrilla/log" "runtime" "github.com/spf13/cobra" "sync" "strings" "fmt" ) func TestBadBackendReload2(t *testing.T) { testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/") os.Truncate("../../tests/testlog", 0) //mainlog, _ = log.GetLogger("../../tests/testlog") mainlog, _ = log.GetLogger("stdout") mainlog.SetLevel("debug") mainlog.Info("are u sure") mainlog.Info("not another word") select { case <-time.After(10 * time.Second): mainlog.Info("paabix") stacktrace := make([]byte, 8192) length := runtime.Stack(stacktrace, true) _ = length fmt.Fprintf(ioutil.Discard, (string(stacktrace[:length]))) panic("timed out") } mainlog.Info("not another word") sigKill() ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644) cmd := &cobra.Command{} configPath = "configJsonA.json" var serveWG sync.WaitGroup serveWG.Add(1) go func() { mainlog.Info("start serve") serve(cmd, []string{}) serveWG.Done() }() mainlog.Info("after start") time.Sleep(testPauseDuration) // change the config file to the one with a broken backend ioutil.WriteFile("configJsonA.json", []byte(configJsonE), 0644) // test SIGHUP via the kill command // Would not work on windows as kill is not available. // TODO: Implement an alternative test for windows. if runtime.GOOS != "windows" { sigHup() time.Sleep(testPauseDuration) // allow sighup to do its job // did the pidfile change as expected? if _, err := os.Stat("./pidfile2.pid"); os.IsNotExist(err) { t.Error("pidfile not changed after sighup SIGHUP", err) } } // send kill signal and wait for exit sigKill() serveWG.Wait() //time.Sleep(time.Second * 3) // did backend started as expected? fd, err := os.Open("../../tests/testlog") if err != nil { t.Error(err) } if read, err := ioutil.ReadAll(fd); err == nil { logOutput := string(read) if i := strings.Index(logOutput, "reverted to old backend config"); i < 0 { t.Error("did not revert to old backend config") } } // cleanup //os.Truncate("../../tests/testlog", 0) os.Remove("configJsonA.json") os.Remove("./pidfile.pid") os.Remove("./pidfile2.pid") }