forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerDeletionServiceTest.php
More file actions
167 lines (130 loc) · 5.85 KB
/
ServerDeletionServiceTest.php
File metadata and controls
167 lines (130 loc) · 5.85 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
<?php
namespace Pterodactyl\Tests\Integration\Services\Servers;
use Mockery;
use Exception;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\DatabaseHost;
use GuzzleHttp\Exception\BadResponseException;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
use Pterodactyl\Services\Servers\ServerDeletionService;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class ServerDeletionServiceTest extends IntegrationTestCase
{
/** @var \Mockery\MockInterface */
private $daemonServerRepository;
/** @var \Mockery\MockInterface */
private $databaseManagementService;
private static $defaultLogger;
/**
* Stub out services that we don't want to test in here.
*/
public function setUp(): void
{
parent::setUp();
self::$defaultLogger = config('logging.default');
// There will be some log calls during this test, don't actually write to the disk.
config()->set('logging.default', 'null');
$this->daemonServerRepository = Mockery::mock(DaemonServerRepository::class);
$this->databaseManagementService = Mockery::mock(DatabaseManagementService::class);
$this->app->instance(DaemonServerRepository::class, $this->daemonServerRepository);
$this->app->instance(DatabaseManagementService::class, $this->databaseManagementService);
}
/**
* Reset the log driver.
*/
protected function tearDown(): void
{
config()->set('logging.default', self::$defaultLogger);
self::$defaultLogger = null;
parent::tearDown();
}
/**
* Test that a server is not deleted if the force option is not set and an error
* is returned by wings.
*/
public function testRegularDeleteFailsIfWingsReturnsError()
{
$server = $this->createServerModel();
$this->expectException(DaemonConnectionException::class);
$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andThrows(
new DaemonConnectionException(new BadResponseException('Bad request', new Request('GET', '/test'), new Response()))
);
$this->getService()->handle($server);
$this->assertDatabaseHas('servers', ['id' => $server->id]);
}
/**
* Test that a 404 from Wings while deleting a server does not cause the deletion to fail.
*/
public function testRegularDeleteIgnores404FromWings()
{
$server = $this->createServerModel();
$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andThrows(
new DaemonConnectionException(new BadResponseException('Bad request', new Request('GET', '/test'), new Response(404)))
);
$this->getService()->handle($server);
$this->assertDatabaseMissing('servers', ['id' => $server->id]);
}
/**
* Test that an error from Wings does not cause the deletion to fail if the server is being
* force deleted.
*/
public function testForceDeleteIgnoresExceptionFromWings()
{
$server = $this->createServerModel();
$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andThrows(
new DaemonConnectionException(new BadResponseException('Bad request', new Request('GET', '/test'), new Response(500)))
);
$this->getService()->withForce(true)->handle($server);
$this->assertDatabaseMissing('servers', ['id' => $server->id]);
}
/**
* Test that a non-force-delete call does not delete the server if one of the databases
* cannot be deleted from the host.
*/
public function testExceptionWhileDeletingStopsProcess()
{
$server = $this->createServerModel();
$host = DatabaseHost::factory()->create();
/** @var \Pterodactyl\Models\Database $db */
$db = Database::factory()->create(['database_host_id' => $host->id, 'server_id' => $server->id]);
$server->refresh();
$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andReturnUndefined();
$this->databaseManagementService->expects('delete')->with(Mockery::on(function ($value) use ($db) {
return $value instanceof Database && $value->id === $db->id;
}))->andThrows(new Exception());
$this->expectException(Exception::class);
$this->getService()->handle($server);
$this->assertDatabaseHas('servers', ['id' => $server->id]);
$this->assertDatabaseHas('databases', ['id' => $db->id]);
}
/**
* Test that a server is deleted even if the server databases cannot be deleted from the host.
*/
public function testExceptionWhileDeletingDatabasesDoesNotAbortIfForceDeleted()
{
$server = $this->createServerModel();
$host = DatabaseHost::factory()->create();
/** @var \Pterodactyl\Models\Database $db */
$db = Database::factory()->create(['database_host_id' => $host->id, 'server_id' => $server->id]);
$server->refresh();
$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andReturnUndefined();
$this->databaseManagementService->expects('delete')->with(Mockery::on(function ($value) use ($db) {
return $value instanceof Database && $value->id === $db->id;
}))->andThrows(new Exception());
$this->getService()->withForce(true)->handle($server);
$this->assertDatabaseMissing('servers', ['id' => $server->id]);
$this->assertDatabaseMissing('databases', ['id' => $db->id]);
}
/**
* @return \Pterodactyl\Services\Servers\ServerDeletionService
*/
private function getService()
{
return $this->app->make(ServerDeletionService::class);
}
}