forked from base-org/pessimism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_servers.go
167 lines (134 loc) · 4.23 KB
/
mock_servers.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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 {
Port int
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
// get port from listener
pds.Port = pds.Server.Listener.Addr().(*net.TCPAddr).Port
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.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{}
}
// TestSlackServer ... Mock server for testing slack alerts
type TestSlackServer struct {
Server *httptest.Server
Payloads []*client.SlackPayload
Port int
Unstructured bool
}
// NewTestSlackServer ... Creates a new mock slack server
func NewTestSlackServer(url string, port int) *TestSlackServer { //nolint:dupl //This will be addressed
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", url, port))
if err != nil {
panic(err)
}
ss := &TestSlackServer{
Payloads: []*client.SlackPayload{},
}
ss.Server = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch strings.TrimSpace(r.URL.Path) {
case "/":
ss.mockSlackPost(w, r)
default:
http.NotFoundHandler().ServeHTTP(w, r)
}
}))
err = ss.Server.Listener.Close()
if err != nil {
panic(err)
}
ss.Server.Listener = l
// get port from listener
ss.Port = ss.Server.Listener.Addr().(*net.TCPAddr).Port
ss.Server.Start()
logging.NoContext().Info("Test slack server started", zap.String("url", url), zap.Int("port", port))
return ss
}
// Close ... Closes the server
func (svr *TestSlackServer) Close() {
svr.Server.Close()
}
// mockSlackPost ... Mocks a slack post request
func (svr *TestSlackServer) mockSlackPost(w http.ResponseWriter, r *http.Request) {
var alert *client.SlackPayload
if err := json.NewDecoder(r.Body).Decode(&alert); err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"message":"", "error":"could not decode slack payload"}`))
return
}
svr.Payloads = append(svr.Payloads, alert)
w.WriteHeader(http.StatusOK)
if svr.Unstructured {
_, _ = w.Write([]byte(`ok`))
} else {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"message":"ok", "error":""}`))
}
}
// SlackAlerts ... Returns the slack alerts
func (svr *TestSlackServer) SlackAlerts() []*client.SlackPayload {
return svr.Payloads
}
// ClearAlerts ... Clears the alerts
func (svr *TestSlackServer) ClearAlerts() {
svr.Payloads = []*client.SlackPayload{}
}