-
Notifications
You must be signed in to change notification settings - Fork 78
/
integration_metrics_test.go
238 lines (213 loc) · 10.2 KB
/
integration_metrics_test.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main_test
import (
"fmt"
"os/exec"
"strconv"
"strings"
"time"
"xip/xip"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)
var _ = Describe("IntegrationMetrics", func() {
When("the server is queried", func() {
// One big `It()` block because these tests cannot be run in parallel (singleton)
It("should update metrics", func() {
var actualMetrics xip.Metrics
expectedMetrics := getMetrics(port)
// A updates .Queries, UDPQueries .AnsweredQueries, .AnsweredAQueries
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics.AnsweredQueries++
expectedMetrics.AnsweredAQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost 127.0.0.1.sslip.io +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// A updates .Queries, TCPQueries .AnsweredQueries, .AnsweredAQueries
expectedMetrics.Queries++
expectedMetrics.TCPQueries++
expectedMetrics.AnsweredQueries++
expectedMetrics.AnsweredAQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
// "+vc" runs the lookup over TCP, not UDP
actualMetrics = digAndGetMetrics("@localhost 127.0.0.1.sslip.io +short +vc -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// A (non-existent) record updates .Queries
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost non-existent.sslip.io +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// A blocked updates .Queries, .AnsweredQueries, .AnsweredBlockedQueries
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics.AnsweredQueries++
expectedMetrics.AnsweredBlockedQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
dig("@localhost bank-of-raiffeisen.127.0.0.1.sslip.io +short -p " + strconv.Itoa(port))
actualMetrics = getMetrics(port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// AAAA updates .Queries, .AnsweredQueries, .AnsweredAAAAQueries
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics.AnsweredQueries++
expectedMetrics.AnsweredAAAAQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost 2600--.sslip.io aaaa +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// AAAA (non-existent) updates .Queries
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost non-existent.sslip.io aaaa +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// MX (customized) updates .Queries, .AnsweredQueries
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics.AnsweredQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost sslip.io mx +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// MX updates .Queries, AnsweredQueries
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics.AnsweredQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost non-existent.sslip.io mx +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// NS updates .Queries, AnsweredQueries
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics.AnsweredQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost non-existent.sslip.io ns +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// NS DNS-01 challenge record updates .Queries, .AnsweredNSDNS01ChallengeQueries
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
// DNS-01 challenges don't count as successful because we're not authoritative; we're delegating
expectedMetrics.AnsweredNSDNS01ChallengeQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost _acme-challenge.fe80--.sslip.io NS +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// Always successful: SOA
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics.AnsweredQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
dig("@localhost non-existent.sslip.io soa +short -p " + strconv.Itoa(port))
actualMetrics = getMetrics(port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// TXT sslip.io (customized) updates .Queries, .AnsweredQueries,
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics.AnsweredQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost sslip.io txt +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// TXT sslip.io (non-existent) updates .Queries, .AnsweredQueries,
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost non-existent.sslip.io txt +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// TXT ip.sslip.io updates .Queries, .AnsweredQueries, .AnsweredTXTSrcIPQueries
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics.AnsweredQueries++
expectedMetrics.AnsweredTXTSrcIPQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost ip.sslip.io txt +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// TXT version.sslip.io updates .Queries, .AnsweredQueries, .AnsweredTXTVersionQueries
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics.AnsweredQueries++
expectedMetrics.AnsweredTXTVersionQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost version.status.sslip.io txt +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// PTR version.sslip.io updates .Queries, .AnsweredQueries, .AnsweredPTRQueriesIPv4
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics.AnsweredQueries++
expectedMetrics.AnsweredPTRQueriesIPv4++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost 1.2.3.4.in-addr.arpa ptr +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// PTR version.sslip.io updates .Queries, .AnsweredQueries, .AnsweredPTRQueriesIPv6
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics.AnsweredQueries++
expectedMetrics.AnsweredPTRQueriesIPv6++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost 2.a.b.b.4.0.2.9.a.e.e.6.e.c.4.1.0.f.9.6.0.0.1.0.6.4.6.0.1.0.6.2.ip6.arpa ptr +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
// TXT DNS-01 challenge record updates .Queries, .AnsweredNSDNS01ChallengeQueries
expectedMetrics.Queries++
expectedMetrics.UDPQueries++
expectedMetrics.AnsweredNSDNS01ChallengeQueries++
expectedMetrics = bumpExpectedToAccountForMetricsQuery(expectedMetrics)
actualMetrics = digAndGetMetrics("@localhost _acme-challenge.fe80--.sslip.io txt +short -p "+strconv.Itoa(port), port)
Expect(expectedMetrics.MostlyEquals(actualMetrics)).To(BeTrue())
})
})
})
// bumpExpectedToAccountForMetricsQuery takes into account that
// digging for the metrics endpoint affects the metrics. It's like
// the Heisenberg uncertainty principle (observing changes the values)
func bumpExpectedToAccountForMetricsQuery(metrics xip.Metrics) xip.Metrics {
metrics.Queries++
metrics.UDPQueries++
metrics.AnsweredQueries++
return metrics
}
func digAndGetMetrics(digArgs string, port int) xip.Metrics {
dig(digArgs)
return getMetrics(port)
}
func dig(digArgs string) {
digCmd := exec.Command("dig", strings.Split(digArgs, " ")...)
digSession, err := Start(digCmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(digSession, 1).Should(Exit(0))
}
func getMetrics(port int) (m xip.Metrics) {
digArgs := "@localhost metrics.status.sslip.io txt +short -p " + strconv.Itoa(port)
digCmd := exec.Command("dig", strings.Split(digArgs, " ")...)
stdout, err := digCmd.Output()
Expect(err).ToNot(HaveOccurred())
var uptime int
var junk string
_, err = fmt.Sscanf(string(stdout),
"\"Uptime: %d\"\n"+
"\"Blocklist: %s %s %s\n"+
"\"Queries: %d (%s\n"+ // %s "swallows" the `/s"` at the end
"\"TCP/UDP: %d/%d\"\n"+
"\"Answer > 0: %d (%s\n"+ // %s "swallows" the `/s"` at the end
"\"A: %d\"\n"+
"\"AAAA: %d\"\n"+
"\"TXT Source: %d\"\n"+
"\"TXT Version: %d\"\n"+
"\"PTR IPv4/IPv6: %d/%d\"\n"+
"\"NS DNS-01: %d\"\n"+
"\"Blocked: %d\"\n",
&uptime,
&junk, &junk, &junk,
&m.Queries, &junk,
&m.TCPQueries, &m.UDPQueries,
&m.AnsweredQueries, &junk,
&m.AnsweredAQueries,
&m.AnsweredAAAAQueries,
&m.AnsweredTXTSrcIPQueries,
&m.AnsweredTXTVersionQueries,
&m.AnsweredPTRQueriesIPv4, &m.AnsweredPTRQueriesIPv6,
&m.AnsweredNSDNS01ChallengeQueries,
&m.AnsweredBlockedQueries,
)
Expect(err).ToNot(HaveOccurred())
m.Start = time.Now().Add(-time.Duration(uptime) * time.Second)
//_, err = fmt.Fscanf(digSession.Out, "queries: %d", &m.Queries)
return m
}