/
test_pagerduty_server.go
86 lines (68 loc) · 2.17 KB
/
test_pagerduty_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package e2e
import (
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/httptest"
"strings"
"github.com/base-org/pessimism/internal/client"
"github.com/base-org/pessimism/internal/logging"
"go.uber.org/zap"
)
// TestPagerDutyServer ... Mock server for testing pagerduty alerts
type TestPagerDutyServer struct {
Server *httptest.Server
Payloads []*client.PagerDutyRequest
}
// NewTestPagerDutyServer ... Creates a new mock pagerduty server
func NewTestPagerDutyServer(url string, port int) *TestPagerDutyServer { //nolint:dupl //This will be addressed
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", url, port))
if err != nil {
panic(err)
}
pds := &TestPagerDutyServer{
Payloads: []*client.PagerDutyRequest{},
}
pds.Server = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch strings.TrimSpace(r.URL.Path) {
case "/":
pds.mockPagerDutyPost(w, r)
default:
http.NotFoundHandler().ServeHTTP(w, r)
}
}))
err = pds.Server.Listener.Close()
if err != nil {
panic(err)
}
pds.Server.Listener = l
pds.Server.Start()
logging.NoContext().Info("Test pagerduty server started", zap.String("url", url), zap.Int("port", port))
return pds
}
// Close ... Closes the server
func (svr *TestPagerDutyServer) Close() {
svr.Server.Close()
}
// mockPagerDutyPost ... Mocks a pagerduty post request
func (svr *TestPagerDutyServer) mockPagerDutyPost(w http.ResponseWriter, r *http.Request) {
var alert *client.PagerDutyRequest
if err := json.NewDecoder(r.Body).Decode(&alert); err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"status":"failure"", "message":"could not decode pagerduty payload"}`))
return
}
svr.Payloads = append(svr.Payloads, alert)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"status":"success", "message":""}`))
}
// PagerDutyAlerts ... Returns the pagerduty alerts
func (svr *TestPagerDutyServer) PagerDutyAlerts() []*client.PagerDutyRequest {
logging.NoContext().Info("Payloads", zap.Any("payloads", svr.Payloads))
return svr.Payloads
}
// ClearAlerts ... Clears the alerts
func (svr *TestPagerDutyServer) ClearAlerts() {
svr.Payloads = []*client.PagerDutyRequest{}
}