forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeployServerDatabaseServiceTest.php
More file actions
182 lines (150 loc) · 5.58 KB
/
DeployServerDatabaseServiceTest.php
File metadata and controls
182 lines (150 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Tests\Unit\Services\Databases;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Database;
use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Services\Databases\DeployServerDatabaseService;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
class DeployServerDatabaseServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface|\Mockery\Mock
*/
private $databaseHostRepository;
/**
* @var \Pterodactyl\Services\Databases\DatabaseManagementService|\Mockery\Mock
*/
private $managementService;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock
*/
private $repository;
/**
* Setup tests.
*/
public function setUp(): void
{
parent::setUp();
$this->databaseHostRepository = m::mock(DatabaseHostRepositoryInterface::class);
$this->managementService = m::mock(DatabaseManagementService::class);
$this->repository = m::mock(DatabaseRepositoryInterface::class);
// Set configs for testing instances.
config()->set('pterodactyl.client_features.databases.enabled', true);
config()->set('pterodactyl.client_features.databases.allow_random', true);
}
/**
* Test handling of non-random hosts when a host is found.
*
* @dataProvider databaseLimitDataProvider
*/
public function testNonRandomFoundHost($limit, $count)
{
$server = factory(Server::class)->make(['database_limit' => $limit]);
$model = factory(Database::class)->make();
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
->once()
->with([['node_id', '=', $server->node_id]])
->andReturn(collect([$model]));
$this->managementService->shouldReceive('create')
->once()
->with($server, [
'database_host_id' => $model->id,
'database' => 'testdb',
'remote' => null,
])
->andReturn($model);
$response = $this->getService()->handle($server, ['database' => 'testdb']);
$this->assertInstanceOf(Database::class, $response);
$this->assertSame($model, $response);
}
/**
* Test that an exception is thrown if in non-random mode and no host is found.
*/
public function testNonRandomNoHost()
{
$this->expectException(NoSuitableDatabaseHostException::class);
$server = factory(Server::class)->make(['database_limit' => 1]);
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
->once()
->with([['node_id', '=', $server->node_id]])
->andReturn(collect());
$this->databaseHostRepository->expects('setColumns->all')->withNoArgs()->andReturn(collect());
$this->getService()->handle($server, []);
}
/**
* Test handling of random host selection.
*/
public function testRandomFoundHost()
{
$server = factory(Server::class)->make(['database_limit' => 1]);
$model = factory(Database::class)->make();
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
->once()
->with([['node_id', '=', $server->node_id]])
->andReturn(collect());
$this->databaseHostRepository->shouldReceive('setColumns->all')
->once()
->andReturn(collect([$model]));
$this->managementService->shouldReceive('create')
->once()
->with($server, [
'database_host_id' => $model->id,
'database' => 'testdb',
'remote' => null,
])
->andReturn($model);
$response = $this->getService()->handle($server, ['database' => 'testdb']);
$this->assertInstanceOf(Database::class, $response);
$this->assertSame($model, $response);
}
/**
* Test that an exception is thrown when no host is found and random is allowed.
*/
public function testRandomNoHost()
{
$this->expectException(NoSuitableDatabaseHostException::class);
$server = factory(Server::class)->make(['database_limit' => 1]);
$this->databaseHostRepository->expects('setColumns->findWhere')
->with([['node_id', '=', $server->node_id]])
->andReturn(collect());
$this->databaseHostRepository->expects('setColumns->all')->withNoArgs()->andReturn(collect());
$this->getService()->handle($server, []);
}
/**
* Provide limits and current database counts for testing.
*
* @return array
*/
public function databaseLimitDataProvider(): array
{
return [
[null, 10],
[1, 0],
];
}
/**
* Provide data for servers over their database limit.
*
* @return array
*/
public function databaseExceedingLimitDataProvider(): array
{
return [
[2, 2],
[2, 3],
];
}
/**
* Return an instance of the service with mocked dependencies for testing.
*
* @return \Pterodactyl\Services\Databases\DeployServerDatabaseService
*/
private function getService(): DeployServerDatabaseService
{
return new DeployServerDatabaseService($this->repository, $this->databaseHostRepository, $this->managementService);
}
}