2020-10-10 05:01:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Services\Deployment;
|
|
|
|
|
2020-10-17 18:52:21 +00:00
|
|
|
use Exception;
|
2020-10-10 05:01:25 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
|
|
|
use InvalidArgumentException;
|
2020-10-10 05:12:45 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Models\Database;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Pterodactyl\Models\Location;
|
2020-10-10 05:01:25 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
|
|
|
use Pterodactyl\Services\Deployment\FindViableNodesService;
|
|
|
|
use Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException;
|
|
|
|
|
|
|
|
class FindViableNodesServiceTest extends IntegrationTestCase
|
|
|
|
{
|
2020-10-10 05:12:45 +00:00
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Database::query()->delete();
|
|
|
|
Server::query()->delete();
|
|
|
|
Node::query()->delete();
|
|
|
|
}
|
|
|
|
|
2020-10-10 05:01:25 +00:00
|
|
|
public function testExceptionIsThrownIfNoDiskSpaceHasBeenSet()
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('Disk space must be an int, got NULL');
|
|
|
|
|
|
|
|
$this->getService()->handle();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExceptionIsThrownIfNoMemoryHasBeenSet()
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('Memory usage must be an int, got NULL');
|
|
|
|
|
|
|
|
$this->getService()->setDisk(10)->handle();
|
|
|
|
}
|
|
|
|
|
2020-10-17 18:52:21 +00:00
|
|
|
/**
|
|
|
|
* 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']);
|
2022-10-14 16:59:20 +00:00
|
|
|
$this->fail('This expectation should not be called.');
|
2020-10-17 18:52:21 +00:00
|
|
|
} 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]);
|
2022-10-14 16:59:20 +00:00
|
|
|
$this->fail('This expectation should not be called.');
|
2020-10-17 18:52:21 +00:00
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->assertInstanceOf(InvalidArgumentException::class, $exception);
|
|
|
|
$this->assertSame('An array of location IDs should be provided when calling setLocations.', $exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 05:01:25 +00:00
|
|
|
public function testExpectedNodeIsReturnedForLocation()
|
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\Location[] $locations */
|
2021-01-23 20:09:16 +00:00
|
|
|
$locations = Location::factory()->times(2)->create();
|
2020-10-10 05:01:25 +00:00
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Node[] $nodes */
|
|
|
|
$nodes = [
|
2020-10-10 05:12:45 +00:00
|
|
|
// This node should never be returned once we've completed the initial test which
|
|
|
|
// runs without a location filter.
|
2021-01-23 20:09:16 +00:00
|
|
|
Node::factory()->create([
|
2020-10-10 05:01:25 +00:00
|
|
|
'location_id' => $locations[0]->id,
|
2020-10-10 05:12:45 +00:00
|
|
|
'memory' => 2048,
|
2020-10-10 05:01:25 +00:00
|
|
|
'disk' => 1024 * 100,
|
|
|
|
]),
|
2021-01-23 20:09:16 +00:00
|
|
|
Node::factory()->create([
|
2020-10-10 05:01:25 +00:00
|
|
|
'location_id' => $locations[1]->id,
|
|
|
|
'memory' => 1024,
|
|
|
|
'disk' => 10240,
|
|
|
|
'disk_overallocate' => 10,
|
|
|
|
]),
|
2021-01-23 20:09:16 +00:00
|
|
|
Node::factory()->create([
|
2020-10-10 05:01:25 +00:00
|
|
|
'location_id' => $locations[1]->id,
|
|
|
|
'memory' => 1024 * 4,
|
|
|
|
'memory_overallocate' => 50,
|
|
|
|
'disk' => 102400,
|
|
|
|
]),
|
|
|
|
];
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
// Expect that all the nodes are returned as we're under all of their limits
|
2020-10-10 05:12:45 +00:00
|
|
|
// and there is no location filter being provided.
|
|
|
|
$response = $this->getService()->setDisk(512)->setMemory(512)->handle();
|
|
|
|
$this->assertInstanceOf(Collection::class, $response);
|
|
|
|
$this->assertCount(3, $response);
|
|
|
|
$this->assertInstanceOf(Node::class, $response[0]);
|
|
|
|
|
|
|
|
// Expect that only the last node is returned because it is the only one with enough
|
|
|
|
// memory available to this instance.
|
|
|
|
$response = $this->getService()->setDisk(512)->setMemory(2049)->handle();
|
|
|
|
$this->assertInstanceOf(Collection::class, $response);
|
|
|
|
$this->assertCount(1, $response);
|
|
|
|
$this->assertSame($nodes[2]->id, $response[0]->id);
|
|
|
|
|
|
|
|
// Helper, I am lazy.
|
2020-10-10 05:01:25 +00:00
|
|
|
$base = function () use ($locations) {
|
2021-01-23 20:09:16 +00:00
|
|
|
return $this->getService()->setLocations([$locations[1]->id])->setDisk(512);
|
2020-10-10 05:01:25 +00:00
|
|
|
};
|
|
|
|
|
2020-10-10 05:12:45 +00:00
|
|
|
// Expect that we can create this server on either node since the disk and memory
|
|
|
|
// limits are below the allowed amount.
|
2020-10-10 05:01:25 +00:00
|
|
|
$response = $base()->setMemory(512)->handle();
|
2020-10-10 05:12:45 +00:00
|
|
|
$this->assertCount(2, $response);
|
2020-10-10 05:01:25 +00:00
|
|
|
$this->assertSame(2, $response->where('location_id', $locations[1]->id)->count());
|
|
|
|
|
2020-10-10 05:12:45 +00:00
|
|
|
// Expect that we can only create this server on the second node since the memory
|
|
|
|
// allocated is over the amount of memory available to the first node.
|
2020-10-10 05:01:25 +00:00
|
|
|
$response = $base()->setMemory(2048)->handle();
|
2020-10-10 05:12:45 +00:00
|
|
|
$this->assertCount(1, $response);
|
2020-10-10 05:01:25 +00:00
|
|
|
$this->assertSame($nodes[2]->id, $response[0]->id);
|
|
|
|
|
2020-10-10 05:12:45 +00:00
|
|
|
// Expect that we can only create this server on the second node since the disk
|
|
|
|
// allocated is over the limit assigned to the first node (even with the overallocate).
|
2020-10-10 05:01:25 +00:00
|
|
|
$response = $base()->setDisk(20480)->setMemory(256)->handle();
|
2020-10-10 05:12:45 +00:00
|
|
|
$this->assertCount(1, $response);
|
2020-10-10 05:01:25 +00:00
|
|
|
$this->assertSame($nodes[2]->id, $response[0]->id);
|
|
|
|
|
2020-10-10 05:12:45 +00:00
|
|
|
// Expect that we could create the server on either node since the disk allocated is
|
|
|
|
// right at the limit for Node 1 when the overallocate value is included in the calc.
|
|
|
|
$response = $base()->setDisk(11264)->setMemory(256)->handle();
|
|
|
|
$this->assertCount(2, $response);
|
2020-10-10 05:01:25 +00:00
|
|
|
|
2020-10-10 05:12:45 +00:00
|
|
|
// Create two servers on the first node so that the disk space used is equal to the
|
|
|
|
// base amount available to the node (without overallocation included).
|
2020-10-10 05:01:25 +00:00
|
|
|
$servers = Collection::make([
|
|
|
|
$this->createServerModel(['node_id' => $nodes[1]->id, 'disk' => 5120]),
|
|
|
|
$this->createServerModel(['node_id' => $nodes[1]->id, 'disk' => 5120]),
|
|
|
|
]);
|
|
|
|
|
2020-10-10 05:12:45 +00:00
|
|
|
// Expect that we cannot create a server with a 1GB disk on the first node since there
|
|
|
|
// is not enough space (even with the overallocate) available to the node.
|
2020-10-10 05:01:25 +00:00
|
|
|
$response = $base()->setDisk(1024)->setMemory(256)->handle();
|
2020-10-10 05:12:45 +00:00
|
|
|
$this->assertCount(1, $response);
|
2020-10-10 05:01:25 +00:00
|
|
|
$this->assertSame($nodes[2]->id, $response[0]->id);
|
2020-10-10 05:12:45 +00:00
|
|
|
|
|
|
|
// Cleanup servers since we need to test some other stuff with memory here.
|
2020-10-10 05:01:25 +00:00
|
|
|
$servers->each->delete();
|
|
|
|
|
2020-10-10 05:12:45 +00:00
|
|
|
// Expect that no viable node can be found when the memory limit for the given instance
|
|
|
|
// is greater than either node can support, even with the overallocation limits taken
|
|
|
|
// into account.
|
2020-10-10 05:01:25 +00:00
|
|
|
$this->expectException(NoViableNodeException::class);
|
|
|
|
$base()->setMemory(10000)->handle();
|
|
|
|
|
2020-10-10 05:12:45 +00:00
|
|
|
// Create four servers so that the memory used for the second node is equal to the total
|
|
|
|
// limit for that node (pre-overallocate calculation).
|
2020-10-10 05:01:25 +00:00
|
|
|
Collection::make([
|
|
|
|
$this->createServerModel(['node_id' => $nodes[2]->id, 'memory' => 1024]),
|
|
|
|
$this->createServerModel(['node_id' => $nodes[2]->id, 'memory' => 1024]),
|
|
|
|
$this->createServerModel(['node_id' => $nodes[2]->id, 'memory' => 1024]),
|
|
|
|
$this->createServerModel(['node_id' => $nodes[2]->id, 'memory' => 1024]),
|
|
|
|
]);
|
|
|
|
|
2020-10-10 05:12:45 +00:00
|
|
|
// Expect that either node can support this server when we account for the overallocate
|
|
|
|
// value of the second node.
|
2020-10-10 05:01:25 +00:00
|
|
|
$response = $base()->setMemory(500)->handle();
|
2020-10-10 05:12:45 +00:00
|
|
|
$this->assertCount(2, $response);
|
2020-10-10 05:01:25 +00:00
|
|
|
$this->assertSame(2, $response->where('location_id', $locations[1]->id)->count());
|
|
|
|
|
2020-10-10 05:12:45 +00:00
|
|
|
// Expect that only the first node can support this server when we go over the remaining
|
|
|
|
// memory for the second nodes overallocate calculation.
|
|
|
|
$response = $base()->setMemory(640)->handle();
|
|
|
|
$this->assertCount(1, $response);
|
2020-10-10 05:01:25 +00:00
|
|
|
$this->assertSame($nodes[1]->id, $response[0]->id);
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
private function getService(): FindViableNodesService
|
2020-10-10 05:01:25 +00:00
|
|
|
{
|
|
|
|
return $this->app->make(FindViableNodesService::class);
|
|
|
|
}
|
|
|
|
}
|