Alerting: Fix test intermittency caused by port conflicts (#48552) (#48556)

* Get golang to find an open port for us

* Update pkg/tests/api/alerting/api_notification_channel_test.go

Co-authored-by: gotjosh <josue.abreu@gmail.com>

* Fix merge

Co-authored-by: gotjosh <josue.abreu@gmail.com>
(cherry picked from commit a96510d03c)

Co-authored-by: Alexander Weaver <weaver.alex.d@gmail.com>
This commit is contained in:
Grot (@grafanabot)
2022-04-29 13:17:37 -05:00
committed by GitHub
co-authored by Alexander Weaver
parent 0af09cd081
commit 6fed38226d
@@ -9,6 +9,7 @@ import (
"io"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
"regexp"
"strings"
@@ -922,13 +923,13 @@ type mockNotificationChannel struct {
}
func newMockNotificationChannel(t *testing.T, grafanaListedAddr string) *mockNotificationChannel {
lastDigit := grafanaListedAddr[len(grafanaListedAddr)-1] - 48
lastDigit = (lastDigit + 1) % 10
newAddr := fmt.Sprintf("%s%01d", grafanaListedAddr[:len(grafanaListedAddr)-1], lastDigit)
// Spin up a separate webserver to receive notifications emitted by Grafana.
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
nc := &mockNotificationChannel{
server: &http.Server{
Addr: newAddr,
Addr: listener.Addr().String(),
},
receivedNotifications: make(map[string][]string),
t: t,
@@ -936,7 +937,7 @@ func newMockNotificationChannel(t *testing.T, grafanaListedAddr string) *mockNot
nc.server.Handler = nc
go func() {
require.Equal(t, http.ErrServerClosed, nc.server.ListenAndServe())
require.EqualError(t, nc.server.Serve(listener), http.ErrServerClosed.Error())
}()
return nc