-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDeployServerDatabaseServiceTest.php
158 lines (128 loc) · 4.99 KB
/
DeployServerDatabaseServiceTest.php
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
<?php
namespace Pterodactyl\Tests\Integration\Services\Databases;
use Mockery\MockInterface;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\DatabaseHost;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Services\Databases\DeployServerDatabaseService;
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
class DeployServerDatabaseServiceTest extends IntegrationTestCase
{
private MockInterface $managementService;
/**
* Setup tests.
*/
protected function setUp(): void
{
parent::setUp();
$this->managementService = \Mockery::mock(DatabaseManagementService::class);
$this->swap(DatabaseManagementService::class, $this->managementService);
}
/**
* Ensure we reset the config to the expected value.
*/
protected function tearDown(): void
{
config()->set('pterodactyl.client_features.databases.allow_random', true);
Database::query()->delete();
DatabaseHost::query()->delete();
parent::tearDown();
}
/**
* Test that an error is thrown if either the database name or the remote host are empty.
*
* @dataProvider invalidDataProvider
*/
public function testErrorIsThrownIfDatabaseNameIsEmpty(array $data)
{
$server = $this->createServerModel();
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/^Expected a non-empty value\. Got: /');
$this->getService()->handle($server, $data);
}
/**
* Test that an error is thrown if there are no database hosts on the same node as the
* server and the allow_random config value is false.
*/
public function testErrorIsThrownIfNoDatabaseHostsExistOnNode()
{
$server = $this->createServerModel();
$node = Node::factory()->create(['location_id' => $server->location->id]);
DatabaseHost::factory()->create(['node_id' => $node->id]);
config()->set('pterodactyl.client_features.databases.allow_random', false);
$this->expectException(NoSuitableDatabaseHostException::class);
$this->getService()->handle($server, [
'database' => 'something',
'remote' => '%',
]);
}
/**
* Test that an error is thrown if no database hosts exist at all on the system.
*/
public function testErrorIsThrownIfNoDatabaseHostsExistOnSystem()
{
$server = $this->createServerModel();
$this->expectException(NoSuitableDatabaseHostException::class);
$this->getService()->handle($server, [
'database' => 'something',
'remote' => '%',
]);
}
/**
* Test that a database host on the same node as the server is preferred.
*/
public function testDatabaseHostOnSameNodeIsPreferred()
{
$server = $this->createServerModel();
$node = Node::factory()->create(['location_id' => $server->location->id]);
DatabaseHost::factory()->create(['node_id' => $node->id]);
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]);
$this->managementService->expects('create')->with($server, [
'database_host_id' => $host->id,
'database' => "s{$server->id}_something",
'remote' => '%',
])->andReturns(new Database());
$response = $this->getService()->handle($server, [
'database' => 'something',
'remote' => '%',
]);
$this->assertInstanceOf(Database::class, $response);
}
/**
* Test that a database host not assigned to the same node as the server is used if
* there are no same-node hosts and the allow_random configuration value is set to
* true.
*/
public function testDatabaseHostIsSelectedIfNoSuitableHostExistsOnSameNode()
{
$server = $this->createServerModel();
$node = Node::factory()->create(['location_id' => $server->location->id]);
$host = DatabaseHost::factory()->create(['node_id' => $node->id]);
$this->managementService->expects('create')->with($server, [
'database_host_id' => $host->id,
'database' => "s{$server->id}_something",
'remote' => '%',
])->andReturns(new Database());
$response = $this->getService()->handle($server, [
'database' => 'something',
'remote' => '%',
]);
$this->assertInstanceOf(Database::class, $response);
}
public function invalidDataProvider(): array
{
return [
[['remote' => '%']],
[['database' => null, 'remote' => '%']],
[['database' => '', 'remote' => '%']],
[['database' => '']],
[['database' => '', 'remote' => '']],
];
}
private function getService(): DeployServerDatabaseService
{
return $this->app->make(DeployServerDatabaseService::class);
}
}