main.go integration test for Startup interrupting.

This commit is contained in:
Krasi Georgiev 2017-11-30 23:28:52 +00:00
parent b3ff5f6b0e
commit 2c2a962da3
1 changed files with 87 additions and 0 deletions

View File

@ -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