Repository navigation
Expand file tree
/
Copy pathTelemetryOfflineTest.php
More file actions
222 lines (180 loc) · 8.26 KB
/
Copy pathTelemetryOfflineTest.php
File metadata and controls
222 lines (180 loc) · 8.26 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace Tests\Feature\Telemetry;
use App\Jobs\SendTelemetryJob;
use App\Services\Telemetry\TelemetryErrorBuffer;
use App\Support\TelemetrySettings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Tests\TestCase;
/**
* Telemetry switched on, and nothing gets out.
*
* This is not an edge case. A factory network that blocks outbound traffic is
* the normal condition for the kind of customer OpenMES is for, and most of
* them will never touch the setting. Such an installation has to behave
* *identically* to a connected one: no errors, no warnings in the interface, no
* slowdown, and a log that does not fill up with a year of failed attempts.
*
* Every refusal a firewall can produce is covered — refused, dropped, poisoned
* DNS, a proxy's own certificate, a captive portal answering 200 with HTML.
*/
class TelemetryOfflineTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
config()->set('telemetry.enabled', true);
config()->set('telemetry.endpoint', 'https://getopenmes.com/telemetry.php');
$path = storage_path('installed');
if (! is_file($path)) {
@file_put_contents($path, date('Y-m-d H:i:s'));
}
TelemetryErrorBuffer::clear();
// Opt-in jest teraz domyslnie wylaczone, wiec przypadek, ktory cwiczy
// sciezke raportowania, musi ja wlaczyc wprost. Wczesniej brak wiersza
// znaczyl zgode i testy korzystaly z tego milczaco.
TelemetrySettings::put(TelemetrySettings::SETTING_KEY, true);
TelemetrySettings::forget();
}
private function runJob(): void
{
// Straight call rather than the queue: the point is that handle()
// itself never lets anything escape.
(new SendTelemetryJob)->handle();
}
private function assertNothingBroke(): void
{
$this->assertSame(0, DB::table('failed_jobs')->count(), 'Telemetry must never leave a failed job behind.');
}
public static function unreachableNetworks(): array
{
return [
'connection refused' => [fn () => Http::fake(fn () => throw new ConnectionException('Connection refused'))],
'dropped by firewall (timeout)' => [fn () => Http::fake(fn () => throw new ConnectionException('cURL error 28: Operation timed out'))],
'proxy substituted the certificate' => [fn () => Http::fake(fn () => throw new ConnectionException('cURL error 60: SSL certificate problem'))],
'captive portal answers with HTML' => [fn () => Http::fake(fn () => Http::response('<html>Sign in to the guest network</html>', 200, ['Content-Type' => 'text/html']))],
'proxy returns bad gateway' => [fn () => Http::fake(fn () => Http::response('', 502))],
'receiver is having a bad day' => [fn () => Http::fake(fn () => Http::response('', 500))],
];
}
/**
* @dataProvider unreachableNetworks
*/
public function test_an_unreachable_network_costs_the_installation_nothing(\Closure $arrange): void
{
$arrange();
$this->runJob();
$this->assertNothingBroke();
$this->assertSame(1, (int) TelemetrySettings::setting('telemetry_consecutive_failures', 0));
}
public function test_a_missing_endpoint_is_not_mistaken_for_a_network_failure(): void
{
// Happens on an upgrade whose config cache still predates this feature.
// Counting it as a failed attempt would back the installation off for a
// month over a setting, and hide the real state behind a fake one.
config()->set('telemetry.endpoint', '');
Http::fake();
$this->runJob();
Http::assertNothingSent();
$this->assertSame(
0,
(int) TelemetrySettings::setting('telemetry_consecutive_failures', 0),
'Nothing was attempted, so nothing failed.',
);
}
public function test_a_name_that_does_not_resolve_is_handled_by_the_guard_not_the_client(): void
{
// The URL guard resolves the host before any request is made, so on a
// network with no DNS it is the guard that throws first. That path has
// to be caught too — it is not an HTTP failure.
config()->set('telemetry.endpoint', 'https://telemetry.invalid-host-'.uniqid().'.example/telemetry.php');
Http::fake();
$this->runJob();
Http::assertNothingSent();
$this->assertNothingBroke();
}
public function test_repeated_silence_makes_the_installation_try_less_often(): void
{
Http::fake(fn () => throw new ConnectionException('Connection refused'));
for ($i = 0; $i < 3; $i++) {
$this->runJob();
}
$this->assertSame(3, (int) TelemetrySettings::setting('telemetry_consecutive_failures', 0));
$this->assertTrue(
now()->addDays(6)->lessThan(TelemetrySettings::setting('telemetry_retry_after')),
'After three failures the next attempt should be a week out, not tomorrow.',
);
for ($i = 0; $i < 7; $i++) {
$this->runJob();
}
$this->assertTrue(
now()->addDays(29)->lessThan(TelemetrySettings::setting('telemetry_retry_after')),
'An installation with no route out should stop knocking daily.',
);
}
public function test_only_the_first_failure_of_a_run_is_logged_and_only_at_debug(): void
{
// A year of daily failures must not become a year of log lines. On an
// air-gapped site this would otherwise be the only thing in the log.
Log::spy();
Http::fake(fn () => throw new ConnectionException('Connection refused'));
$this->runJob();
$this->runJob();
$this->runJob();
Log::shouldHaveReceived('debug')->once();
Log::shouldNotHaveReceived('error');
Log::shouldNotHaveReceived('warning');
Log::shouldNotHaveReceived('critical');
}
public function test_the_buffered_errors_survive_a_failed_send(): void
{
TelemetryErrorBuffer::record(new \RuntimeException('a real fault'));
Http::fake(fn () => throw new ConnectionException('Connection refused'));
$this->runJob();
$this->assertCount(
1,
TelemetryErrorBuffer::collect()['items'],
'Errors are kept for the next window; they are only dropped once delivered.',
);
}
public function test_a_successful_send_clears_the_slate(): void
{
TelemetryErrorBuffer::record(new \RuntimeException('a real fault'));
TelemetrySettings::put('telemetry_consecutive_failures', 5);
Http::fake(fn () => Http::response('', 204));
$this->runJob();
$this->assertSame([], TelemetryErrorBuffer::collect()['items']);
$this->assertSame(0, (int) TelemetrySettings::setting('telemetry_consecutive_failures'));
$this->assertNotNull(TelemetrySettings::setting('telemetry_last_sent_at'));
}
public function test_the_receiver_can_switch_this_installation_off_remotely(): void
{
Http::fake(fn () => Http::response('', 410));
$this->runJob();
$this->assertFalse(
TelemetrySettings::setting(TelemetrySettings::SETTING_KEY),
'A retired endpoint must be able to stop installations without a release.',
);
}
public function test_a_busy_receiver_is_obeyed_and_the_errors_are_kept(): void
{
TelemetryErrorBuffer::record(new \RuntimeException('a real fault'));
Http::fake(fn () => Http::response('', 429, ['Retry-After' => '3600']));
$this->runJob();
$this->assertNotNull(TelemetrySettings::setting('telemetry_retry_after'));
$this->assertCount(1, TelemetryErrorBuffer::collect()['items']);
}
public function test_an_absurd_retry_after_cannot_silence_an_installation_for_ever(): void
{
Http::fake(fn () => Http::response('', 503, ['Retry-After' => (string) (86400 * 3650)]));
$this->runJob();
$this->assertTrue(
now()->addDays(8)->greaterThan(TelemetrySettings::setting('telemetry_retry_after')),
'A ten-year Retry-After is capped at a week.',
);
}
}