Skip to content

Commit 839e277

Browse files
committed
Fix exception when passing location IDs to creation service; closes pterodactyl#2529
1 parent f54151e commit 839e277

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

app/Services/Deployment/FindViableNodesService.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Webmozart\Assert\Assert;
66
use Pterodactyl\Models\Node;
7-
use Illuminate\Support\LazyCollection;
87
use Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException;
98

109
class FindViableNodesService
@@ -32,7 +31,7 @@ class FindViableNodesService
3231
*/
3332
public function setLocations(array $locations): self
3433
{
35-
Assert::allInteger($locations, 'An array of location IDs should be provided when calling setLocations.');
34+
Assert::allIntegerish($locations, 'An array of location IDs should be provided when calling setLocations.');
3635

3736
$this->locations = $locations;
3837

@@ -97,8 +96,8 @@ public function handle()
9796
}
9897

9998
$results = $query->groupBy('nodes.id')
100-
->havingRaw('(IFNULL(SUM(servers.memory), 0) + ?) <= (nodes.memory * (1 + (nodes.memory_overallocate / 100)))', [ $this->memory ])
101-
->havingRaw('(IFNULL(SUM(servers.disk), 0) + ?) <= (nodes.disk * (1 + (nodes.disk_overallocate / 100)))', [ $this->disk ])
99+
->havingRaw('(IFNULL(SUM(servers.memory), 0) + ?) <= (nodes.memory * (1 + (nodes.memory_overallocate / 100)))', [$this->memory])
100+
->havingRaw('(IFNULL(SUM(servers.disk), 0) + ?) <= (nodes.disk * (1 + (nodes.disk_overallocate / 100)))', [$this->disk])
102101
->get()
103102
->toBase();
104103

tests/Integration/Services/Deployment/FindViableNodesServiceTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Pterodactyl\Tests\Integration\Services\Deployment;
44

5+
use Exception;
56
use Pterodactyl\Models\Node;
67
use InvalidArgumentException;
78
use Pterodactyl\Models\Server;
@@ -39,6 +40,34 @@ public function testExceptionIsThrownIfNoMemoryHasBeenSet()
3940
$this->getService()->setDisk(10)->handle();
4041
}
4142

43+
/**
44+
* Ensure that errors are not thrown back when passing in expected values.
45+
*
46+
* @see https://github.com/pterodactyl/panel/issues/2529
47+
*/
48+
public function testNoExceptionIsThrownIfStringifiedIntegersArePassedForLocations()
49+
{
50+
$this->getService()->setLocations([1, 2, 3]);
51+
$this->getService()->setLocations(['1', '2', '3']);
52+
$this->getService()->setLocations(['1', 2, 3]);
53+
54+
try {
55+
$this->getService()->setLocations(['a']);
56+
$this->assertTrue(false, 'This expectation should not be called.');
57+
} catch (Exception $exception) {
58+
$this->assertInstanceOf(InvalidArgumentException::class, $exception);
59+
$this->assertSame('An array of location IDs should be provided when calling setLocations.', $exception->getMessage());
60+
}
61+
62+
try {
63+
$this->getService()->setLocations(['1.2', '1', 2]);
64+
$this->assertTrue(false, 'This expectation should not be called.');
65+
} catch (Exception $exception) {
66+
$this->assertInstanceOf(InvalidArgumentException::class, $exception);
67+
$this->assertSame('An array of location IDs should be provided when calling setLocations.', $exception->getMessage());
68+
}
69+
}
70+
4271
public function testExpectedNodeIsReturnedForLocation()
4372
{
4473
/** @var \Pterodactyl\Models\Location[] $locations */

0 commit comments

Comments
 (0)