diff --git a/app/Services/Deployment/FindViableNodesService.php b/app/Services/Deployment/FindViableNodesService.php index d89c73f5e..8c3d7c757 100644 --- a/app/Services/Deployment/FindViableNodesService.php +++ b/app/Services/Deployment/FindViableNodesService.php @@ -4,7 +4,6 @@ namespace Pterodactyl\Services\Deployment; use Webmozart\Assert\Assert; use Pterodactyl\Models\Node; -use Illuminate\Support\LazyCollection; use Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException; class FindViableNodesService @@ -32,7 +31,7 @@ class FindViableNodesService */ public function setLocations(array $locations): self { - Assert::allInteger($locations, 'An array of location IDs should be provided when calling setLocations.'); + Assert::allIntegerish($locations, 'An array of location IDs should be provided when calling setLocations.'); $this->locations = $locations; @@ -97,8 +96,8 @@ class FindViableNodesService } $results = $query->groupBy('nodes.id') - ->havingRaw('(IFNULL(SUM(servers.memory), 0) + ?) <= (nodes.memory * (1 + (nodes.memory_overallocate / 100)))', [ $this->memory ]) - ->havingRaw('(IFNULL(SUM(servers.disk), 0) + ?) <= (nodes.disk * (1 + (nodes.disk_overallocate / 100)))', [ $this->disk ]) + ->havingRaw('(IFNULL(SUM(servers.memory), 0) + ?) <= (nodes.memory * (1 + (nodes.memory_overallocate / 100)))', [$this->memory]) + ->havingRaw('(IFNULL(SUM(servers.disk), 0) + ?) <= (nodes.disk * (1 + (nodes.disk_overallocate / 100)))', [$this->disk]) ->get() ->toBase(); diff --git a/tests/Integration/Services/Deployment/FindViableNodesServiceTest.php b/tests/Integration/Services/Deployment/FindViableNodesServiceTest.php index 47261dbe8..abc0bd9a7 100644 --- a/tests/Integration/Services/Deployment/FindViableNodesServiceTest.php +++ b/tests/Integration/Services/Deployment/FindViableNodesServiceTest.php @@ -2,6 +2,7 @@ namespace Pterodactyl\Tests\Integration\Services\Deployment; +use Exception; use Pterodactyl\Models\Node; use InvalidArgumentException; use Pterodactyl\Models\Server; @@ -39,6 +40,34 @@ class FindViableNodesServiceTest extends IntegrationTestCase $this->getService()->setDisk(10)->handle(); } + /** + * Ensure that errors are not thrown back when passing in expected values. + * + * @see https://github.com/pterodactyl/panel/issues/2529 + */ + public function testNoExceptionIsThrownIfStringifiedIntegersArePassedForLocations() + { + $this->getService()->setLocations([1, 2, 3]); + $this->getService()->setLocations(['1', '2', '3']); + $this->getService()->setLocations(['1', 2, 3]); + + try { + $this->getService()->setLocations(['a']); + $this->assertTrue(false, 'This expectation should not be called.'); + } catch (Exception $exception) { + $this->assertInstanceOf(InvalidArgumentException::class, $exception); + $this->assertSame('An array of location IDs should be provided when calling setLocations.', $exception->getMessage()); + } + + try { + $this->getService()->setLocations(['1.2', '1', 2]); + $this->assertTrue(false, 'This expectation should not be called.'); + } catch (Exception $exception) { + $this->assertInstanceOf(InvalidArgumentException::class, $exception); + $this->assertSame('An array of location IDs should be provided when calling setLocations.', $exception->getMessage()); + } + } + public function testExpectedNodeIsReturnedForLocation() { /** @var \Pterodactyl\Models\Location[] $locations */