Fix exception when passing location IDs to creation service; closes #2529

This commit is contained in:
Dane Everitt 2020-10-17 11:52:21 -07:00
parent f54151e0f6
commit 839e277763
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 32 additions and 4 deletions

View file

@ -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();

View file

@ -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 */