Newer
Older

Adam G-H
committed
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
<?php
namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation;
use Drupal\automatic_updates\Event\UpdateEvent;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\automatic_updates\Validator\DiskSpaceValidator;
use Drupal\Component\Utility\Bytes;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait;
/**
* @covers \Drupal\automatic_updates\Validator\DiskSpaceValidator
*
* @group automatic_updates
*/
class DiskSpaceValidatorTest extends KernelTestBase {
use ValidationTestTrait;
/**
* The validator under test.
*
* @var \Drupal\automatic_updates\Validator\DiskSpaceValidator
*/
private $validator;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$path_locator = $this->prophesize('\Drupal\automatic_updates\PathLocator');
$path_locator->getProjectRoot()->willReturn('root');
$path_locator->getVendorDirectory()->willReturn('vendor');
// Create a mocked version of the validator which can be rigged up to return
// specific values for various filesystem checks.
$this->validator = new class ($path_locator->reveal()) extends DiskSpaceValidator {
/**
* Whether the root and vendor directories are on the same logical disk.
*
* @var bool
*/
public $sharedDisk;
/**
* The amount of free space, keyed by location.
*
* @var float[]
*/
public $freeSpace = [];
/**
* {@inheritdoc}
*/
protected function stat(string $path): array {
return [
'dev' => $this->sharedDisk ? 'disk' : uniqid(),
];
}
/**
* {@inheritdoc}
*/
protected function freeSpace(string $path): float {
return $this->freeSpace[$path];
}
/**
* {@inheritdoc}
*/
protected function temporaryDirectory(): string {
return 'temp';
}
};
}
/**
* Data provider for ::testDiskSpaceValidation().
*
* @return mixed[][]
* Sets of arguments to pass to the test method.
*/
public function providerDiskSpaceValidation(): array {
$root_insufficient = t('Drupal root filesystem "root" has insufficient space. There must be at least 1024 megabytes free.');
$vendor_insufficient = t('Vendor filesystem "vendor" has insufficient space. There must be at least 1024 megabytes free.');
$temp_insufficient = t('Directory "temp" has insufficient space. There must be at least 1024 megabytes free.');
$summary = t("There is not enough disk space to perform an automatic update.");
return [
'shared, vendor and temp sufficient, root insufficient' => [
TRUE,
[
'root' => '1M',
'vendor' => '2G',
'temp' => '4G',
],
[
ValidationResult::createError([$root_insufficient]),
],
],
'shared, root and vendor insufficient, temp sufficient' => [
TRUE,
[
'root' => '1M',
'vendor' => '2M',
'temp' => '2G',
],
[
ValidationResult::createError([$root_insufficient]),
],
],
'shared, vendor and root sufficient, temp insufficient' => [
TRUE,
[
'root' => '2G',
'vendor' => '4G',
'temp' => '1M',
],
[
ValidationResult::createError([$temp_insufficient]),
],
],
'shared, root and temp insufficient, vendor sufficient' => [
TRUE,
[
'root' => '1M',
'vendor' => '2G',
'temp' => '2M',
],
[
ValidationResult::createError([
$root_insufficient,
$temp_insufficient,
], $summary),
],
],
'not shared, root insufficient, vendor and temp sufficient' => [
FALSE,
[
'root' => '5M',
'vendor' => '1G',
'temp' => '4G',
],
[
ValidationResult::createError([$root_insufficient]),
],
],
'not shared, vendor insufficient, root and temp sufficient' => [
FALSE,
[
'root' => '2G',
'vendor' => '10M',
'temp' => '4G',
],
[
ValidationResult::createError([$vendor_insufficient]),
],
],
'not shared, root and vendor sufficient, temp insufficient' => [
FALSE,
[
'root' => '1G',
'vendor' => '2G',
'temp' => '3M',
],
[
ValidationResult::createError([$temp_insufficient]),
],
],
'not shared, root and vendor insufficient, temp sufficient' => [
FALSE,
[
'root' => '500M',
'vendor' => '75M',
'temp' => '2G',
],
[
ValidationResult::createError([
$root_insufficient,
$vendor_insufficient,
], $summary),
],
],
];
}
/**
* Tests disk space validation.
*
* @param bool $shared_disk
* Whether the root and vendor directories are on the same logical disk.
* @param array $free_space
* The free space that should be reported for various locations. The keys
* are the locations (only 'root', 'vendor', and 'temp' are supported), and
* the values are the space that should be reported, in a format that can be
* parsed by \Drupal\Component\Utility\Bytes::toNumber().
* @param \Drupal\automatic_updates\Validation\ValidationResult[] $expected_results
* The expected validation results.
*
* @dataProvider providerDiskSpaceValidation
*/
public function testDiskSpaceValidation(bool $shared_disk, array $free_space, array $expected_results): void {
$this->validator->sharedDisk = $shared_disk;
$this->validator->freeSpace = array_map([Bytes::class, 'toNumber'], $free_space);
$event = new UpdateEvent();
$this->validator->checkDiskSpace($event);
$this->assertValidationResultsEqual($expected_results, $event->getResults());
}
}