|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Tests\Integration\Services\Servers; |
| 4 | + |
| 5 | +use Mockery; |
| 6 | +use Pterodactyl\Models\Egg; |
| 7 | +use Pterodactyl\Models\Node; |
| 8 | +use Pterodactyl\Models\User; |
| 9 | +use GuzzleHttp\Psr7\Request; |
| 10 | +use GuzzleHttp\Psr7\Response; |
| 11 | +use Pterodactyl\Models\Server; |
| 12 | +use Pterodactyl\Models\Location; |
| 13 | +use Pterodactyl\Models\Allocation; |
| 14 | +use Illuminate\Foundation\Testing\WithFaker; |
| 15 | +use Illuminate\Validation\ValidationException; |
| 16 | +use GuzzleHttp\Exception\BadResponseException; |
| 17 | +use Pterodactyl\Models\Objects\DeploymentObject; |
| 18 | +use Pterodactyl\Tests\Integration\IntegrationTestCase; |
| 19 | +use Pterodactyl\Services\Servers\ServerCreationService; |
| 20 | +use Pterodactyl\Repositories\Wings\DaemonServerRepository; |
| 21 | +use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; |
| 22 | + |
| 23 | +class ServerCreationServiceTest extends IntegrationTestCase |
| 24 | +{ |
| 25 | + use WithFaker; |
| 26 | + |
| 27 | + /** @var \Mockery\MockInterface */ |
| 28 | + private $daemonServerRepository; |
| 29 | + |
| 30 | + /** |
| 31 | + * Stub the calls to Wings so that we don't actually hit those API endpoints. |
| 32 | + */ |
| 33 | + public function setUp(): void |
| 34 | + { |
| 35 | + parent::setUp(); |
| 36 | + |
| 37 | + $this->daemonServerRepository = Mockery::mock(DaemonServerRepository::class); |
| 38 | + $this->swap(DaemonServerRepository::class, $this->daemonServerRepository); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Test that a server can be created when a deployment object is provided to the service. |
| 43 | + * |
| 44 | + * This doesn't really do anything super complicated, we'll rely on other more specific |
| 45 | + * tests to cover that the logic being used does indeed find suitable nodes and ports. For |
| 46 | + * this test we just care that it is recognized and passed off to those functions. |
| 47 | + */ |
| 48 | + public function testServerIsCreatedWithDeploymentObject() |
| 49 | + { |
| 50 | + /** @var \Pterodactyl\Models\User $user */ |
| 51 | + $user = factory(User::class)->create(); |
| 52 | + |
| 53 | + /** @var \Pterodactyl\Models\Node $node */ |
| 54 | + $node = factory(Node::class)->create([ |
| 55 | + 'location_id' => factory(Location::class)->create()->id, |
| 56 | + ]); |
| 57 | + |
| 58 | + /** @var \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations */ |
| 59 | + $allocations = factory(Allocation::class)->times(5)->create([ |
| 60 | + 'node_id' => $node->id, |
| 61 | + ]); |
| 62 | + |
| 63 | + $deployment = (new DeploymentObject())->setDedicated(true)->setLocations([$node->location_id])->setPorts([ |
| 64 | + $allocations[0]->port, |
| 65 | + ]); |
| 66 | + |
| 67 | + /** @noinspection PhpParamsInspection */ |
| 68 | + $egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1)); |
| 69 | + // We want to make sure that the validator service runs as an admin, and not as a regular |
| 70 | + // user when saving variables. |
| 71 | + $egg->variables()->first()->update([ |
| 72 | + 'user_editable' => false, |
| 73 | + ]); |
| 74 | + |
| 75 | + $data = [ |
| 76 | + 'name' => $this->faker->name, |
| 77 | + 'description' => $this->faker->sentence, |
| 78 | + 'owner_id' => $user->id, |
| 79 | + 'memory' => 256, |
| 80 | + 'swap' => 128, |
| 81 | + 'disk' => 100, |
| 82 | + 'io' => 500, |
| 83 | + 'cpu' => 0, |
| 84 | + 'startup' => 'java server2.jar', |
| 85 | + 'image' => 'java:8', |
| 86 | + 'egg_id' => $egg->id, |
| 87 | + 'allocation_additional' => [ |
| 88 | + $allocations[4]->id, |
| 89 | + ], |
| 90 | + 'environment' => [ |
| 91 | + 'BUNGEE_VERSION' => '123', |
| 92 | + 'SERVER_JARFILE' => 'server2.jar', |
| 93 | + ], |
| 94 | + ]; |
| 95 | + |
| 96 | + $this->daemonServerRepository->expects('setServer')->andReturnSelf(); |
| 97 | + $this->daemonServerRepository->expects('create')->with(Mockery::on(function ($value) { |
| 98 | + $this->assertIsArray($value); |
| 99 | + // Just check for some keys to make sure we're getting the expected configuration |
| 100 | + // structure back. Other tests exist to confirm it is the correct structure. |
| 101 | + $this->assertArrayHasKey('uuid', $value); |
| 102 | + $this->assertArrayHasKey('environment', $value); |
| 103 | + $this->assertArrayHasKey('invocation', $value); |
| 104 | + |
| 105 | + return true; |
| 106 | + }))->andReturnUndefined(); |
| 107 | + |
| 108 | + try { |
| 109 | + $this->getService()->handle(array_merge($data, [ |
| 110 | + 'environment' => [ |
| 111 | + 'BUNGEE_VERSION' => '', |
| 112 | + 'SERVER_JARFILE' => 'server2.jar', |
| 113 | + ], |
| 114 | + ]), $deployment); |
| 115 | + $this->assertTrue(false, 'This statement 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 | + } |
| 121 | + |
| 122 | + $response = $this->getService()->handle($data, $deployment); |
| 123 | + |
| 124 | + $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); |
| 131 | + |
| 132 | + foreach ($data as $key => $value) { |
| 133 | + if (in_array($key, ['allocation_additional', 'environment'])) { |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + $this->assertSame($value, $response->{$key}); |
| 138 | + } |
| 139 | + |
| 140 | + $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); |
| 144 | + |
| 145 | + $this->assertFalse($response->suspended); |
| 146 | + $this->assertTrue($response->oom_disabled); |
| 147 | + $this->assertEmpty($response->database_limit); |
| 148 | + $this->assertEmpty($response->allocation_limit); |
| 149 | + $this->assertEmpty($response->backup_limit); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Test that a server is deleted from the Panel if Wings returns an error during the creation |
| 154 | + * process. |
| 155 | + */ |
| 156 | + public function testErrorEncounteredByWingsCausesServerToBeDeleted() |
| 157 | + { |
| 158 | + /** @var \Pterodactyl\Models\User $user */ |
| 159 | + $user = factory(User::class)->create(); |
| 160 | + |
| 161 | + /** @var \Pterodactyl\Models\Node $node */ |
| 162 | + $node = factory(Node::class)->create([ |
| 163 | + 'location_id' => factory(Location::class)->create()->id, |
| 164 | + ]); |
| 165 | + |
| 166 | + /** @var \Pterodactyl\Models\Allocation $allocation */ |
| 167 | + $allocation = factory(Allocation::class)->create([ |
| 168 | + 'node_id' => $node->id, |
| 169 | + ]); |
| 170 | + |
| 171 | + $data = [ |
| 172 | + 'name' => $this->faker->name, |
| 173 | + 'description' => $this->faker->sentence, |
| 174 | + 'owner_id' => $user->id, |
| 175 | + 'allocation_id' => $allocation->id, |
| 176 | + 'node_id' => $allocation->node_id, |
| 177 | + 'memory' => 256, |
| 178 | + 'swap' => 128, |
| 179 | + 'disk' => 100, |
| 180 | + 'io' => 500, |
| 181 | + 'cpu' => 0, |
| 182 | + 'startup' => 'java server2.jar', |
| 183 | + 'image' => 'java:8', |
| 184 | + 'egg_id' => 1, |
| 185 | + 'environment' => [ |
| 186 | + 'BUNGEE_VERSION' => '123', |
| 187 | + 'SERVER_JARFILE' => 'server2.jar', |
| 188 | + ], |
| 189 | + ]; |
| 190 | + |
| 191 | + $this->daemonServerRepository->expects('setServer->create')->andThrows( |
| 192 | + new DaemonConnectionException( |
| 193 | + new BadResponseException('Bad request', new Request('POST', '/create'), new Response(500)) |
| 194 | + ) |
| 195 | + ); |
| 196 | + |
| 197 | + $this->daemonServerRepository->expects('setServer->delete')->andReturnUndefined(); |
| 198 | + |
| 199 | + $this->expectException(DaemonConnectionException::class); |
| 200 | + |
| 201 | + $this->getService()->handle($data); |
| 202 | + |
| 203 | + $this->assertDatabaseMissing('servers', ['owner_id' => $user->id]); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * @return \Pterodactyl\Services\Servers\ServerCreationService |
| 208 | + */ |
| 209 | + private function getService() |
| 210 | + { |
| 211 | + return $this->app->make(ServerCreationService::class); |
| 212 | + } |
| 213 | +} |
0 commit comments