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

* 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>
This commit is contained in:
Alexander Weaver 2022-04-29 12:07:19 -05:00 committed by GitHub
parent 54acec69a3
commit a96510d03c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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