Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic in acceptance tests #3592

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions test/with_api_v2/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The panic occurred because the test called Fatal which calls runtime.Goexit. This caused the stack to unwind and the defer function to be called. When this happened, am.cmd contained a nil Process field.

t.T.Fail()
return
}

// Set the reference time right before running the test actions to avoid
Expand Down Expand Up @@ -249,10 +247,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
}

Expand Down Expand Up @@ -381,8 +379,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 {
grobinson-grafana marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}

Expand All @@ -396,8 +398,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)
}
}
}

Expand Down
Loading