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) + } +}