Path: blob/1.0-develop/tests/Integration/Services/Servers/ServerCreationServiceTest.php
7460 views
<?php12namespace Pterodactyl\Tests\Integration\Services\Servers;34use Mockery\MockInterface;5use Pterodactyl\Models\Egg;6use GuzzleHttp\Psr7\Request;7use Pterodactyl\Models\Node;8use Pterodactyl\Models\User;9use GuzzleHttp\Psr7\Response;10use Pterodactyl\Models\Server;11use Pterodactyl\Models\Location;12use Pterodactyl\Models\Allocation;13use Illuminate\Foundation\Testing\WithFaker;14use GuzzleHttp\Exception\BadResponseException;15use Illuminate\Validation\ValidationException;16use Pterodactyl\Models\Objects\DeploymentObject;17use Pterodactyl\Tests\Integration\IntegrationTestCase;18use Pterodactyl\Services\Servers\ServerCreationService;19use Pterodactyl\Repositories\Wings\DaemonServerRepository;20use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;2122class ServerCreationServiceTest extends IntegrationTestCase23{24use WithFaker;2526protected MockInterface $daemonServerRepository;2728protected Egg $bungeecord;2930/**31* Stub the calls to Wings so that we don't actually hit those API endpoints.32*/33public function setUp(): void34{35parent::setUp();3637/* @noinspection PhpFieldAssignmentTypeMismatchInspection */38$this->bungeecord = Egg::query()39->where('author', '[email protected]')40->where('name', 'Bungeecord')41->firstOrFail();4243$this->daemonServerRepository = \Mockery::mock(DaemonServerRepository::class);44$this->swap(DaemonServerRepository::class, $this->daemonServerRepository);45}4647/**48* Test that a server can be created when a deployment object is provided to the service.49*50* This doesn't really do anything super complicated, we'll rely on other more specific51* tests to cover that the logic being used does indeed find suitable nodes and ports. For52* this test we just care that it is recognized and passed off to those functions.53*/54public function testServerIsCreatedWithDeploymentObject()55{56/** @var User $user */57$user = User::factory()->create();5859/** @var Location $location */60$location = Location::factory()->create();6162/** @var Node $node */63$node = Node::factory()->create([64'location_id' => $location->id,65]);6667/** @var \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations */68$allocations = Allocation::factory()->times(5)->create([69'node_id' => $node->id,70]);7172$deployment = (new DeploymentObject())->setDedicated(true)->setLocations([$node->location_id])->setPorts([73$allocations[0]->port,74]);7576$egg = $this->cloneEggAndVariables($this->bungeecord);77// We want to make sure that the validator service runs as an admin, and not as a regular78// user when saving variables.79$egg->variables()->first()->update([80'user_editable' => false,81]);8283$data = [84'name' => $this->faker->name,85'description' => $this->faker->sentence,86'owner_id' => $user->id,87'memory' => 256,88'swap' => 128,89'disk' => 100,90'io' => 500,91'cpu' => 0,92'startup' => 'java server2.jar',93'image' => 'java:8',94'egg_id' => $egg->id,95'allocation_additional' => [96$allocations[4]->id,97],98'environment' => [99'BUNGEE_VERSION' => '123',100'SERVER_JARFILE' => 'server2.jar',101],102'start_on_completion' => true,103];104105$this->daemonServerRepository->expects('setServer->create')->with(true)->andReturnUndefined();106107try {108$this->getService()->handle(array_merge($data, [109'environment' => [110'BUNGEE_VERSION' => '',111'SERVER_JARFILE' => 'server2.jar',112],113]), $deployment);114115$this->fail('This execution pathway should not be reached.');116} catch (ValidationException $exception) {117$this->assertCount(1, $exception->errors());118$this->assertArrayHasKey('environment.BUNGEE_VERSION', $exception->errors());119$this->assertSame('The Bungeecord Version variable field is required.', $exception->errors()['environment.BUNGEE_VERSION'][0]);120}121122$response = $this->getService()->handle($data, $deployment);123124$this->assertInstanceOf(Server::class, $response);125$this->assertNotNull($response->uuid);126$this->assertSame($response->uuidShort, substr($response->uuid, 0, 8));127$this->assertSame($egg->id, $response->egg_id);128$this->assertCount(2, $response->variables);129$this->assertSame('123', $response->variables[0]->server_value);130$this->assertSame('server2.jar', $response->variables[1]->server_value);131132foreach ($data as $key => $value) {133if (in_array($key, ['allocation_additional', 'environment', 'start_on_completion'])) {134continue;135}136137$this->assertSame($value, $response->{$key}, "Failed asserting equality of '$key' in server response. Got: [{$response->{$key}}] Expected: [$value]");138}139140$this->assertCount(2, $response->allocations);141$this->assertSame($response->allocation_id, $response->allocations[0]->id);142$this->assertSame($allocations[0]->id, $response->allocations[0]->id);143$this->assertSame($allocations[4]->id, $response->allocations[1]->id);144145$this->assertFalse($response->isSuspended());146$this->assertTrue($response->oom_disabled);147$this->assertSame(0, $response->database_limit);148$this->assertSame(0, $response->allocation_limit);149$this->assertSame(0, $response->backup_limit);150}151152/**153* Test that a server is deleted from the Panel if Wings returns an error during the creation154* process.155*/156public function testErrorEncounteredByWingsCausesServerToBeDeleted()157{158/** @var User $user */159$user = User::factory()->create();160161/** @var Location $location */162$location = Location::factory()->create();163164/** @var Node $node */165$node = Node::factory()->create([166'location_id' => $location->id,167]);168169/** @var Allocation $allocation */170$allocation = Allocation::factory()->create([171'node_id' => $node->id,172]);173174$data = [175'name' => $this->faker->name,176'description' => $this->faker->sentence,177'owner_id' => $user->id,178'allocation_id' => $allocation->id,179'node_id' => $allocation->node_id,180'memory' => 256,181'swap' => 128,182'disk' => 100,183'io' => 500,184'cpu' => 0,185'startup' => 'java server2.jar',186'image' => 'java:8',187'egg_id' => $this->bungeecord->id,188'environment' => [189'BUNGEE_VERSION' => '123',190'SERVER_JARFILE' => 'server2.jar',191],192];193194$this->daemonServerRepository->expects('setServer->create')->andThrows(195new DaemonConnectionException(196new BadResponseException('Bad request', new Request('POST', '/create'), new Response(500))197)198);199200$this->daemonServerRepository->expects('setServer->delete')->andReturnUndefined();201202$this->expectException(DaemonConnectionException::class);203204$this->getService()->handle($data);205206$this->assertDatabaseMissing('servers', ['owner_id' => $user->id]);207}208209private function getService(): ServerCreationService210{211return $this->app->make(ServerCreationService::class);212}213}214215216