From 2c2a962da3e602121381261e7c216540e7140282 Mon Sep 17 00:00:00 2001 From: Krasi Georgiev Date: Thu, 30 Nov 2017 23:28:52 +0000 Subject: [PATCH 1/2] main.go integration test for Startup interrupting. --- cmd/prometheus/main_test.go | 87 +++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/cmd/prometheus/main_test.go b/cmd/prometheus/main_test.go index e327b90141..d1ece0c018 100644 --- a/cmd/prometheus/main_test.go +++ b/cmd/prometheus/main_test.go @@ -14,11 +14,98 @@ package main import ( + "flag" + "fmt" + "net/http" + "os" + "os/exec" + "path/filepath" "testing" + "time" "github.com/prometheus/prometheus/util/testutil" ) +var promPath string +var promConfig = filepath.Join("..", "..", "documentation", "examples", "prometheus.yml") + +func TestMain(m *testing.M) { + var err error + promPath, err = os.Getwd() + if err != nil { + fmt.Printf("can't get current dir :%s \n", err) + os.Exit(1) + } + promPath = filepath.Join(promPath, "prometheus") + + flag.Parse() + if !testing.Short() { + build := exec.Command("go", "build", "-o", promPath) + output, err := build.CombinedOutput() + if err != nil { + fmt.Printf("compilation error :%s \n", output) + os.Exit(1) + } + + exitCode := m.Run() + os.Remove(promPath) + os.Exit(exitCode) + } +} + +// As soon as prometheus starts responding to http request should be able to accept Interrupt signals for a gracefull shutdown. +func TestStartupInterrupt(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + + prom := exec.Command(promPath, "--config.file="+promConfig) + err := prom.Start() + if err != nil { + t.Errorf("execution error: %v", err) + return + } + + done := make(chan error) + go func() { + done <- prom.Wait() + }() + + var startedOk bool + var stoppedOk bool + var stoppedErr error + +Loop: + for x := 0; x < 10; x++ { + + // error=nil means prometheus has started so can send the interrupt signal and wait for the grace shutdown. + if _, err := http.Get("http://localhost:9090/graph"); err == nil { + startedOk = true + prom.Process.Signal(os.Interrupt) + select { + case stoppedErr = <-done: + stoppedOk = true + break Loop + case <-time.After(10 * time.Second): + + } + break Loop + + } + time.Sleep(500 * time.Millisecond) + } + + if !startedOk { + t.Errorf("prometheus didn't start in the specified timeout") + return + } + if err := prom.Process.Kill(); err == nil && !stoppedOk { + t.Errorf("prometheus didn't shutdown gracefully after sending the Interrupt signal") + } else if stoppedErr != nil { + t.Errorf("prometheus exited with an error:%v", stoppedErr) + } +} + func TestComputeExternalURL(t *testing.T) { tests := []struct { input string From 740662644e270ded30e16c3a49303f8166310643 Mon Sep 17 00:00:00 2001 From: Krasi Georgiev Date: Wed, 6 Dec 2017 10:45:58 +0000 Subject: [PATCH 2/2] write to temp dir and remove it at the end. Signed-off-by: Krasi Georgiev --- cmd/prometheus/main_test.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/cmd/prometheus/main_test.go b/cmd/prometheus/main_test.go index d1ece0c018..47524ea4c9 100644 --- a/cmd/prometheus/main_test.go +++ b/cmd/prometheus/main_test.go @@ -28,8 +28,13 @@ import ( var promPath string var promConfig = filepath.Join("..", "..", "documentation", "examples", "prometheus.yml") +var promData = filepath.Join(os.TempDir(), "data") func TestMain(m *testing.M) { + flag.Parse() + if testing.Short() { + os.Exit(m.Run()) + } var err error promPath, err = os.Getwd() if err != nil { @@ -38,19 +43,17 @@ func TestMain(m *testing.M) { } promPath = filepath.Join(promPath, "prometheus") - flag.Parse() - if !testing.Short() { - build := exec.Command("go", "build", "-o", promPath) - output, err := build.CombinedOutput() - if err != nil { - fmt.Printf("compilation error :%s \n", output) - os.Exit(1) - } - - exitCode := m.Run() - os.Remove(promPath) - os.Exit(exitCode) + build := exec.Command("go", "build", "-o", promPath) + output, err := build.CombinedOutput() + if err != nil { + fmt.Printf("compilation error :%s \n", output) + os.Exit(1) } + + exitCode := m.Run() + os.Remove(promPath) + os.RemoveAll(promData) + os.Exit(exitCode) } // As soon as prometheus starts responding to http request should be able to accept Interrupt signals for a gracefull shutdown. @@ -59,7 +62,7 @@ func TestStartupInterrupt(t *testing.T) { t.Skip("skipping test in short mode.") } - prom := exec.Command(promPath, "--config.file="+promConfig) + prom := exec.Command(promPath, "--config.file="+promConfig, "--storage.tsdb.path="+promData) err := prom.Start() if err != nil { t.Errorf("execution error: %v", err)