From 5e9056d2f37bb0383bf236756e6628c27feedded Mon Sep 17 00:00:00 2001 From: Michael Khalil Date: Thu, 21 Jun 2018 00:32:26 -0700 Subject: [PATCH] return error exit status in prometheus cli (#4296) Signed-off-by: mikeykhalil --- cmd/prometheus/main.go | 1 + cmd/prometheus/main_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 1188a7bbe..a5985d543 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -595,6 +595,7 @@ func main() { } if err := g.Run(); err != nil { level.Error(logger).Log("err", err) + os.Exit(1) } level.Info(logger).Log("msg", "See you next time!") } diff --git a/cmd/prometheus/main_test.go b/cmd/prometheus/main_test.go index ee805674a..605ba816e 100644 --- a/cmd/prometheus/main_test.go +++ b/cmd/prometheus/main_test.go @@ -20,6 +20,7 @@ import ( "os" "os/exec" "path/filepath" + "syscall" "testing" "time" @@ -155,3 +156,20 @@ func TestComputeExternalURL(t *testing.T) { } } } + +// Let's provide an invalid configuration file and verify the exit status indicates the error. +func TestFailedStartupExitCode(t *testing.T) { + fakeInputFile := "fake-input-file" + expectedExitStatus := 1 + + prom := exec.Command(promPath, "--config.file="+fakeInputFile) + err := prom.Run() + testutil.NotOk(t, err, "") + + if exitError, ok := err.(*exec.ExitError); ok { + status := exitError.Sys().(syscall.WaitStatus) + testutil.Equals(t, expectedExitStatus, status.ExitStatus()) + } else { + t.Errorf("unable to retrieve the exit status for prometheus: %v", err) + } +}