|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\Services\Databases; |
| 4 | + |
| 5 | +use Mockery as m; |
| 6 | +use Tests\TestCase; |
| 7 | +use Pterodactyl\Models\Server; |
| 8 | +use Pterodactyl\Models\Database; |
| 9 | +use Pterodactyl\Services\Databases\DatabaseManagementService; |
| 10 | +use Pterodactyl\Services\Databases\DeployServerDatabaseService; |
| 11 | +use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; |
| 12 | +use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; |
| 13 | + |
| 14 | +class DeployServerDatabaseServiceTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface|\Mockery\Mock |
| 18 | + */ |
| 19 | + private $databaseHostRepository; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Pterodactyl\Services\Databases\DatabaseManagementService|\Mockery\Mock |
| 23 | + */ |
| 24 | + private $managementService; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock |
| 28 | + */ |
| 29 | + private $repository; |
| 30 | + |
| 31 | + /** |
| 32 | + * Setup tests. |
| 33 | + */ |
| 34 | + public function setUp() |
| 35 | + { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + $this->databaseHostRepository = m::mock(DatabaseHostRepositoryInterface::class); |
| 39 | + $this->managementService = m::mock(DatabaseManagementService::class); |
| 40 | + $this->repository = m::mock(DatabaseRepositoryInterface::class); |
| 41 | + |
| 42 | + // Set configs for testing instances. |
| 43 | + config()->set('pterodactyl.client_features.databases.enabled', true); |
| 44 | + config()->set('pterodactyl.client_features.databases.allow_random', true); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Test handling of non-random hosts when a host is found. |
| 49 | + * |
| 50 | + * @dataProvider databaseLimitDataProvider |
| 51 | + */ |
| 52 | + public function testNonRandomFoundHost($limit, $count) |
| 53 | + { |
| 54 | + config()->set('pterodactyl.client_features.databases.allow_random', false); |
| 55 | + |
| 56 | + $server = factory(Server::class)->make(['database_limit' => $limit]); |
| 57 | + $model = factory(Database::class)->make(); |
| 58 | + |
| 59 | + $this->repository->shouldReceive('findCountWhere') |
| 60 | + ->once() |
| 61 | + ->with([['server_id', '=', $server->id]]) |
| 62 | + ->andReturn($count); |
| 63 | + |
| 64 | + $this->databaseHostRepository->shouldReceive('setColumns->findWhere') |
| 65 | + ->once() |
| 66 | + ->with([['node_id', '=', $server->node_id]]) |
| 67 | + ->andReturn(collect([$model])); |
| 68 | + |
| 69 | + $this->managementService->shouldReceive('create') |
| 70 | + ->once() |
| 71 | + ->with($server->id, [ |
| 72 | + 'database_host_id' => $model->id, |
| 73 | + 'database' => 'testdb', |
| 74 | + 'remote' => null, |
| 75 | + ]) |
| 76 | + ->andReturn($model); |
| 77 | + |
| 78 | + $response = $this->getService()->handle($server, ['database' => 'testdb']); |
| 79 | + |
| 80 | + $this->assertInstanceOf(Database::class, $response); |
| 81 | + $this->assertSame($model, $response); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Test that an exception is thrown if in non-random mode and no host is found. |
| 86 | + * |
| 87 | + * @expectedException \Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException |
| 88 | + */ |
| 89 | + public function testNonRandomNoHost() |
| 90 | + { |
| 91 | + config()->set('pterodactyl.client_features.databases.allow_random', false); |
| 92 | + |
| 93 | + $server = factory(Server::class)->make(['database_limit' => 1]); |
| 94 | + |
| 95 | + $this->repository->shouldReceive('findCountWhere') |
| 96 | + ->once() |
| 97 | + ->with([['server_id', '=', $server->id]]) |
| 98 | + ->andReturn(0); |
| 99 | + |
| 100 | + $this->databaseHostRepository->shouldReceive('setColumns->findWhere') |
| 101 | + ->once() |
| 102 | + ->with([['node_id', '=', $server->node_id]]) |
| 103 | + ->andReturn(collect()); |
| 104 | + |
| 105 | + $this->getService()->handle($server, []); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Test handling of random host selection. |
| 110 | + */ |
| 111 | + public function testRandomFoundHost() |
| 112 | + { |
| 113 | + $server = factory(Server::class)->make(['database_limit' => 1]); |
| 114 | + $model = factory(Database::class)->make(); |
| 115 | + |
| 116 | + $this->repository->shouldReceive('findCountWhere') |
| 117 | + ->once() |
| 118 | + ->with([['server_id', '=', $server->id]]) |
| 119 | + ->andReturn(0); |
| 120 | + |
| 121 | + $this->databaseHostRepository->shouldReceive('setColumns->findWhere') |
| 122 | + ->once() |
| 123 | + ->with([['node_id', '=', $server->node_id]]) |
| 124 | + ->andReturn(collect()); |
| 125 | + |
| 126 | + $this->databaseHostRepository->shouldReceive('setColumns->all') |
| 127 | + ->once() |
| 128 | + ->andReturn(collect([$model])); |
| 129 | + |
| 130 | + $this->managementService->shouldReceive('create') |
| 131 | + ->once() |
| 132 | + ->with($server->id, [ |
| 133 | + 'database_host_id' => $model->id, |
| 134 | + 'database' => 'testdb', |
| 135 | + 'remote' => null, |
| 136 | + ]) |
| 137 | + ->andReturn($model); |
| 138 | + |
| 139 | + $response = $this->getService()->handle($server, ['database' => 'testdb']); |
| 140 | + |
| 141 | + $this->assertInstanceOf(Database::class, $response); |
| 142 | + $this->assertSame($model, $response); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Test that an exception is thrown when no host is found and random is allowed. |
| 147 | + * |
| 148 | + * @expectedException \Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException |
| 149 | + */ |
| 150 | + public function testRandomNoHost() |
| 151 | + { |
| 152 | + $server = factory(Server::class)->make(['database_limit' => 1]); |
| 153 | + |
| 154 | + $this->repository->shouldReceive('findCountWhere') |
| 155 | + ->once() |
| 156 | + ->with([['server_id', '=', $server->id]]) |
| 157 | + ->andReturn(0); |
| 158 | + |
| 159 | + $this->databaseHostRepository->shouldReceive('setColumns->findWhere') |
| 160 | + ->once() |
| 161 | + ->with([['node_id', '=', $server->node_id]]) |
| 162 | + ->andReturn(collect()); |
| 163 | + |
| 164 | + $this->databaseHostRepository->shouldReceive('setColumns->all') |
| 165 | + ->once() |
| 166 | + ->andReturn(collect()); |
| 167 | + |
| 168 | + $this->getService()->handle($server, []); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Test that a server over the database limit throws an exception. |
| 173 | + * |
| 174 | + * @dataProvider databaseExceedingLimitDataProvider |
| 175 | + * @expectedException \Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException |
| 176 | + */ |
| 177 | + public function testServerOverDatabaseLimit($limit, $count) |
| 178 | + { |
| 179 | + $server = factory(Server::class)->make(['database_limit' => $limit]); |
| 180 | + |
| 181 | + $this->repository->shouldReceive('findCountWhere') |
| 182 | + ->once() |
| 183 | + ->with([['server_id', '=', $server->id]]) |
| 184 | + ->andReturn($count); |
| 185 | + |
| 186 | + $this->getService()->handle($server, []); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Test that an exception is thrown if the feature is not enabled. |
| 191 | + * |
| 192 | + * @expectedException \Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException |
| 193 | + */ |
| 194 | + public function testFeatureNotEnabled() |
| 195 | + { |
| 196 | + config()->set('pterodactyl.client_features.databases.enabled', false); |
| 197 | + |
| 198 | + $this->getService()->handle(factory(Server::class)->make(), []); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Provide limits and current database counts for testing. |
| 203 | + * |
| 204 | + * @return array |
| 205 | + */ |
| 206 | + public function databaseLimitDataProvider(): array |
| 207 | + { |
| 208 | + return [ |
| 209 | + [null, 10], |
| 210 | + [1, 0], |
| 211 | + ]; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Provide data for servers over their database limit. |
| 216 | + * |
| 217 | + * @return array |
| 218 | + */ |
| 219 | + public function databaseExceedingLimitDataProvider(): array |
| 220 | + { |
| 221 | + return [ |
| 222 | + [2, 2], |
| 223 | + [2, 3], |
| 224 | + ]; |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Return an instance of the service with mocked dependencies for testing. |
| 229 | + * |
| 230 | + * @return \Pterodactyl\Services\Databases\DeployServerDatabaseService |
| 231 | + */ |
| 232 | + private function getService(): DeployServerDatabaseService |
| 233 | + { |
| 234 | + return new DeployServerDatabaseService($this->repository, $this->databaseHostRepository, $this->managementService); |
| 235 | + } |
| 236 | +} |
0 commit comments