From 4d6ddd25c931485b803b2a4df2e9f878b7536abb Mon Sep 17 00:00:00 2001 From: George Robinson Date: Tue, 13 Feb 2024 15:38:44 +0000 Subject: [PATCH] Fix panic in acceptance tests (#3592) * Fix panic in acceptance tests This commit attempts to address a panic that occurs in acceptance tests if a server in the cluster fails to start. Signed-off-by: George Robinson * Remove started and check am.cmd.Process != nil Signed-off-by: George Robinson --------- Signed-off-by: George Robinson --- test/with_api_v2/acceptance.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/test/with_api_v2/acceptance.go b/test/with_api_v2/acceptance.go index a6c4abd8..de59ab85 100644 --- a/test/with_api_v2/acceptance.go +++ b/test/with_api_v2/acceptance.go @@ -169,17 +169,15 @@ func (t *AcceptanceTest) Run() { for _, am := range t.amc.ams { am.errc = errc - defer func(am *Alertmanager) { - am.Terminate() - am.cleanup() - t.Logf("stdout:\n%v", am.cmd.Stdout) - t.Logf("stderr:\n%v", am.cmd.Stderr) - }(am) + t.T.Cleanup(am.Terminate) + t.T.Cleanup(am.cleanup) } err := t.amc.Start() if err != nil { - t.T.Fatal(err) + t.T.Log(err) + t.T.Fail() + return } // Set the reference time right before running the test actions to avoid @@ -251,10 +249,10 @@ type Alertmanager struct { apiAddr string clusterAddr string clientV2 *apiclient.AlertmanagerAPI - cmd *exec.Cmd confFile *os.File dir string + cmd *exec.Cmd errc chan<- error } @@ -386,8 +384,12 @@ func (amc *AlertmanagerCluster) Terminate() { // data. func (am *Alertmanager) Terminate() { am.t.Helper() - if err := syscall.Kill(am.cmd.Process.Pid, syscall.SIGTERM); err != nil { - am.t.Logf("Error sending SIGTERM to Alertmanager process: %v", err) + if am.cmd.Process != nil { + if err := syscall.Kill(am.cmd.Process.Pid, syscall.SIGTERM); err != nil { + am.t.Logf("Error sending SIGTERM to Alertmanager process: %v", err) + } + am.t.Logf("stdout:\n%v", am.cmd.Stdout) + am.t.Logf("stderr:\n%v", am.cmd.Stderr) } } @@ -401,8 +403,10 @@ func (amc *AlertmanagerCluster) Reload() { // Reload sends the reloading signal to the Alertmanager process. func (am *Alertmanager) Reload() { am.t.Helper() - if err := syscall.Kill(am.cmd.Process.Pid, syscall.SIGHUP); err != nil { - am.t.Fatalf("Error sending SIGHUP to Alertmanager process: %v", err) + if am.cmd.Process != nil { + if err := syscall.Kill(am.cmd.Process.Pid, syscall.SIGHUP); err != nil { + am.t.Fatalf("Error sending SIGHUP to Alertmanager process: %v", err) + } } }