Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Services/Servers/ServerCreationServiceTest.php
7460 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Services\Servers;
4
5
use Mockery\MockInterface;
6
use Pterodactyl\Models\Egg;
7
use GuzzleHttp\Psr7\Request;
8
use Pterodactyl\Models\Node;
9
use Pterodactyl\Models\User;
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 GuzzleHttp\Exception\BadResponseException;
16
use Illuminate\Validation\ValidationException;
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
protected MockInterface $daemonServerRepository;
28
29
protected Egg $bungeecord;
30
31
/**
32
* Stub the calls to Wings so that we don't actually hit those API endpoints.
33
*/
34
public function setUp(): void
35
{
36
parent::setUp();
37
38
/* @noinspection PhpFieldAssignmentTypeMismatchInspection */
39
$this->bungeecord = Egg::query()
40
->where('author', '[email protected]')
41
->where('name', 'Bungeecord')
42
->firstOrFail();
43
44
$this->daemonServerRepository = \Mockery::mock(DaemonServerRepository::class);
45
$this->swap(DaemonServerRepository::class, $this->daemonServerRepository);
46
}
47
48
/**
49
* Test that a server can be created when a deployment object is provided to the service.
50
*
51
* This doesn't really do anything super complicated, we'll rely on other more specific
52
* tests to cover that the logic being used does indeed find suitable nodes and ports. For
53
* this test we just care that it is recognized and passed off to those functions.
54
*/
55
public function testServerIsCreatedWithDeploymentObject()
56
{
57
/** @var User $user */
58
$user = User::factory()->create();
59
60
/** @var Location $location */
61
$location = Location::factory()->create();
62
63
/** @var Node $node */
64
$node = Node::factory()->create([
65
'location_id' => $location->id,
66
]);
67
68
/** @var \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations */
69
$allocations = Allocation::factory()->times(5)->create([
70
'node_id' => $node->id,
71
]);
72
73
$deployment = (new DeploymentObject())->setDedicated(true)->setLocations([$node->location_id])->setPorts([
74
$allocations[0]->port,
75
]);
76
77
$egg = $this->cloneEggAndVariables($this->bungeecord);
78
// We want to make sure that the validator service runs as an admin, and not as a regular
79
// user when saving variables.
80
$egg->variables()->first()->update([
81
'user_editable' => false,
82
]);
83
84
$data = [
85
'name' => $this->faker->name,
86
'description' => $this->faker->sentence,
87
'owner_id' => $user->id,
88
'memory' => 256,
89
'swap' => 128,
90
'disk' => 100,
91
'io' => 500,
92
'cpu' => 0,
93
'startup' => 'java server2.jar',
94
'image' => 'java:8',
95
'egg_id' => $egg->id,
96
'allocation_additional' => [
97
$allocations[4]->id,
98
],
99
'environment' => [
100
'BUNGEE_VERSION' => '123',
101
'SERVER_JARFILE' => 'server2.jar',
102
],
103
'start_on_completion' => true,
104
];
105
106
$this->daemonServerRepository->expects('setServer->create')->with(true)->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
116
$this->fail('This execution pathway should not be reached.');
117
} catch (ValidationException $exception) {
118
$this->assertCount(1, $exception->errors());
119
$this->assertArrayHasKey('environment.BUNGEE_VERSION', $exception->errors());
120
$this->assertSame('The Bungeecord Version variable field is required.', $exception->errors()['environment.BUNGEE_VERSION'][0]);
121
}
122
123
$response = $this->getService()->handle($data, $deployment);
124
125
$this->assertInstanceOf(Server::class, $response);
126
$this->assertNotNull($response->uuid);
127
$this->assertSame($response->uuidShort, substr($response->uuid, 0, 8));
128
$this->assertSame($egg->id, $response->egg_id);
129
$this->assertCount(2, $response->variables);
130
$this->assertSame('123', $response->variables[0]->server_value);
131
$this->assertSame('server2.jar', $response->variables[1]->server_value);
132
133
foreach ($data as $key => $value) {
134
if (in_array($key, ['allocation_additional', 'environment', 'start_on_completion'])) {
135
continue;
136
}
137
138
$this->assertSame($value, $response->{$key}, "Failed asserting equality of '$key' in server response. Got: [{$response->{$key}}] Expected: [$value]");
139
}
140
141
$this->assertCount(2, $response->allocations);
142
$this->assertSame($response->allocation_id, $response->allocations[0]->id);
143
$this->assertSame($allocations[0]->id, $response->allocations[0]->id);
144
$this->assertSame($allocations[4]->id, $response->allocations[1]->id);
145
146
$this->assertFalse($response->isSuspended());
147
$this->assertTrue($response->oom_disabled);
148
$this->assertSame(0, $response->database_limit);
149
$this->assertSame(0, $response->allocation_limit);
150
$this->assertSame(0, $response->backup_limit);
151
}
152
153
/**
154
* Test that a server is deleted from the Panel if Wings returns an error during the creation
155
* process.
156
*/
157
public function testErrorEncounteredByWingsCausesServerToBeDeleted()
158
{
159
/** @var User $user */
160
$user = User::factory()->create();
161
162
/** @var Location $location */
163
$location = Location::factory()->create();
164
165
/** @var Node $node */
166
$node = Node::factory()->create([
167
'location_id' => $location->id,
168
]);
169
170
/** @var Allocation $allocation */
171
$allocation = Allocation::factory()->create([
172
'node_id' => $node->id,
173
]);
174
175
$data = [
176
'name' => $this->faker->name,
177
'description' => $this->faker->sentence,
178
'owner_id' => $user->id,
179
'allocation_id' => $allocation->id,
180
'node_id' => $allocation->node_id,
181
'memory' => 256,
182
'swap' => 128,
183
'disk' => 100,
184
'io' => 500,
185
'cpu' => 0,
186
'startup' => 'java server2.jar',
187
'image' => 'java:8',
188
'egg_id' => $this->bungeecord->id,
189
'environment' => [
190
'BUNGEE_VERSION' => '123',
191
'SERVER_JARFILE' => 'server2.jar',
192
],
193
];
194
195
$this->daemonServerRepository->expects('setServer->create')->andThrows(
196
new DaemonConnectionException(
197
new BadResponseException('Bad request', new Request('POST', '/create'), new Response(500))
198
)
199
);
200
201
$this->daemonServerRepository->expects('setServer->delete')->andReturnUndefined();
202
203
$this->expectException(DaemonConnectionException::class);
204
205
$this->getService()->handle($data);
206
207
$this->assertDatabaseMissing('servers', ['owner_id' => $user->id]);
208
}
209
210
private function getService(): ServerCreationService
211
{
212
return $this->app->make(ServerCreationService::class);
213
}
214
}
215
216