forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetricsEndpointTest.php
More file actions
202 lines (152 loc) · 6.74 KB
/
Copy pathMetricsEndpointTest.php
File metadata and controls
202 lines (152 loc) · 6.74 KB
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
<?php
use App\Models\Result;
use App\Services\PrometheusMetricsService;
use App\Settings\DataIntegrationSettings;
use Illuminate\Support\Facades\Cache;
beforeEach(function () {
Cache::flush();
});
describe('metrics endpoint', function () {
test('returns 404 when prometheus is disabled', function () {
app(DataIntegrationSettings::class)->fill(['prometheus_enabled' => false])->save();
$response = $this->get('/prometheus');
$response->assertNotFound();
});
test('returns metrics when prometheus is enabled and no IP restrictions', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => [],
])->save();
$result = Result::factory()->create();
// Simulate the listener updating metrics
app(PrometheusMetricsService::class)->updateMetrics($result);
$response = $this->get('/prometheus');
$response->assertSuccessful();
$response->assertHeader('Content-Type', 'text/plain; version=0.0.4; charset=utf-8');
});
test('returns 403 when IP is not in allowed list', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => ['192.168.1.100', '10.0.0.1'],
])->save();
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '192.168.1.50',
]);
$response->assertForbidden();
});
test('returns metrics when IP is in allowed list', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => ['192.168.1.100', '10.0.0.1'],
])->save();
$result = Result::factory()->create();
// Simulate the listener updating metrics
app(PrometheusMetricsService::class)->updateMetrics($result);
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '192.168.1.100',
]);
$response->assertSuccessful();
$response->assertHeader('Content-Type', 'text/plain; version=0.0.4; charset=utf-8');
});
test('allows access with empty array', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => [],
])->save();
$result = Result::factory()->create();
// Simulate the listener updating metrics
app(PrometheusMetricsService::class)->updateMetrics($result);
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '10.0.0.1',
]);
$response->assertSuccessful();
});
test('allows access when IP is in CIDR range', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => ['192.168.1.0/24'],
])->save();
$result = Result::factory()->create();
// Simulate the listener updating metrics
app(PrometheusMetricsService::class)->updateMetrics($result);
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '192.168.1.150',
]);
$response->assertSuccessful();
});
test('denies access when IP is not in CIDR range', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => ['192.168.1.0/24'],
])->save();
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '192.168.2.1',
]);
$response->assertForbidden();
});
test('supports mixed IP addresses and CIDR ranges', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => ['10.0.0.1', '192.168.1.0/24'],
])->save();
$result = Result::factory()->create();
// Simulate the listener updating metrics
app(PrometheusMetricsService::class)->updateMetrics($result);
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '192.168.1.50',
]);
$response->assertSuccessful();
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '10.0.0.1',
]);
$response->assertSuccessful();
});
test('completed result emits expected metric names in response body', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => [],
])->save();
$result = Result::factory()->create([
'status' => \App\Enums\ResultStatus::Completed,
'download' => 115625000,
'upload' => 113750000,
]);
app(PrometheusMetricsService::class)->updateMetrics($result);
$response = $this->get('/prometheus');
$response->assertSuccessful();
$response->assertSee('speedtest_tracker_up');
$response->assertSee('speedtest_tracker_info');
$response->assertSee('speedtest_tracker_download_bytes_per_second');
$response->assertSee('speedtest_tracker_upload_bytes_per_second');
$response->assertSee('speedtest_tracker_download_bits_per_second');
$response->assertSee('speedtest_tracker_upload_bits_per_second');
});
test('failed result does not emit speed gauge metrics', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => [],
])->save();
$result = Result::factory()->create([
'status' => \App\Enums\ResultStatus::Failed,
'download' => null,
'upload' => null,
'ping' => null,
'data' => ['type' => 'log', 'level' => 'error', 'message' => 'Test error.', 'timestamp' => '2024-03-01T01:00:00Z'],
]);
app(PrometheusMetricsService::class)->updateMetrics($result);
$response = $this->get('/prometheus');
$response->assertSuccessful();
$response->assertSee('speedtest_tracker_up');
$response->assertSee('speedtest_tracker_info');
$response->assertDontSee('speedtest_tracker_download_bytes_per_second');
$response->assertDontSee('speedtest_tracker_upload_bytes_per_second');
$response->assertDontSee('speedtest_tracker_download_bits_per_second');
$response->assertDontSee('speedtest_tracker_upload_bits_per_second');
});
test('generateMetrics returns up and build_info metrics when no cache exists', function () {
$metrics = app(PrometheusMetricsService::class)->generateMetrics();
expect($metrics)
->toContain('speedtest_tracker_up 1')
->toContain('speedtest_tracker_build_info');
});
});