2424
2525namespace Tests \Unit \Services \Servers ;
2626
27+ use Exception ;
28+ use GuzzleHttp \Exception \RequestException ;
2729use Mockery as m ;
30+ use Pterodactyl \Exceptions \DisplayException ;
2831use Tests \TestCase ;
29- use Ramsey \Uuid \Uuid ;
3032use Illuminate \Log \Writer ;
3133use phpmock \phpunit \PHPMock ;
3234use Illuminate \Database \DatabaseManager ;
@@ -54,11 +56,40 @@ class CreationServiceTest extends TestCase
5456 */
5557 protected $ daemonServerRepository ;
5658
59+ /**
60+ * @var array
61+ */
62+ protected $ data = [
63+ 'node_id ' => 1 ,
64+ 'name ' => 'SomeName ' ,
65+ 'description ' => null ,
66+ 'owner_id ' => 1 ,
67+ 'memory ' => 128 ,
68+ 'disk ' => 128 ,
69+ 'swap ' => 0 ,
70+ 'io ' => 500 ,
71+ 'cpu ' => 0 ,
72+ 'allocation_id ' => 1 ,
73+ 'allocation_additional ' => [2 , 3 ],
74+ 'environment ' => [
75+ 'TEST_VAR_1 ' => 'var1-value ' ,
76+ ],
77+ 'service_id ' => 1 ,
78+ 'option_id ' => 1 ,
79+ 'startup ' => 'startup-param ' ,
80+ 'docker_image ' => 'some/image ' ,
81+ ];
82+
5783 /**
5884 * @var \Illuminate\Database\DatabaseManager
5985 */
6086 protected $ database ;
6187
88+ /**
89+ * @var \GuzzleHttp\Exception\RequestException
90+ */
91+ protected $ exception ;
92+
6293 /**
6394 * @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
6495 */
@@ -114,6 +145,7 @@ public function setUp()
114145 $ this ->allocationRepository = m::mock (AllocationRepositoryInterface::class);
115146 $ this ->daemonServerRepository = m::mock (DaemonServerRepositoryInterface::class);
116147 $ this ->database = m::mock (DatabaseManager::class);
148+ $ this ->exception = m::mock (RequestException::class);
117149 $ this ->nodeRepository = m::mock (NodeRepositoryInterface::class);
118150 $ this ->repository = m::mock (ServerRepositoryInterface::class);
119151 $ this ->serverVariableRepository = m::mock (ServerVariableRepositoryInterface::class);
@@ -148,67 +180,46 @@ public function setUp()
148180 */
149181 public function testCreateShouldHitAllOfTheNecessaryServicesAndStoreTheServer ()
150182 {
151- $ data = [
152- 'node_id ' => 1 ,
153- 'name ' => 'SomeName ' ,
154- 'description ' => null ,
155- 'owner_id ' => 1 ,
156- 'memory ' => 128 ,
157- 'disk ' => 128 ,
158- 'swap ' => 0 ,
159- 'io ' => 500 ,
160- 'cpu ' => 0 ,
161- 'allocation_id ' => 1 ,
162- 'allocation_additional ' => [2 , 3 ],
163- 'environment ' => [
164- 'TEST_VAR_1 ' => 'var1-value ' ,
165- ],
166- 'service_id ' => 1 ,
167- 'option_id ' => 1 ,
168- 'startup ' => 'startup-param ' ,
169- 'docker_image ' => 'some/image ' ,
170- ];
171-
172183 $ this ->validatorService ->shouldReceive ('isAdmin ' )->withNoArgs ()->once ()->andReturnSelf ()
173- ->shouldReceive ('setFields ' )->with ($ data ['environment ' ])->once ()->andReturnSelf ()
174- ->shouldReceive ('validate ' )->with ($ data ['option_id ' ])->once ()->andReturnSelf ();
184+ ->shouldReceive ('setFields ' )->with ($ this -> data ['environment ' ])->once ()->andReturnSelf ()
185+ ->shouldReceive ('validate ' )->with ($ this -> data ['option_id ' ])->once ()->andReturnSelf ();
175186
176187 $ this ->database ->shouldReceive ('beginTransaction ' )->withNoArgs ()->once ()->andReturnNull ();
177188 $ this ->uuid ->shouldReceive ('uuid4 ' )->withNoArgs ()->once ()->andReturnSelf ()
178189 ->shouldReceive ('toString ' )->withNoArgs ()->once ()->andReturn ('uuid-0000 ' );
179- $ this ->usernameService ->shouldReceive ('generate ' )->with ($ data ['name ' ], 'randomstring ' )
190+ $ this ->usernameService ->shouldReceive ('generate ' )->with ($ this -> data ['name ' ], 'randomstring ' )
180191 ->once ()->andReturn ('user_name ' );
181192
182193 $ this ->repository ->shouldReceive ('create ' )->with ([
183194 'uuid ' => 'uuid-0000 ' ,
184195 'uuidShort ' => 'randomstring ' ,
185- 'node_id ' => $ data ['node_id ' ],
186- 'name ' => $ data ['name ' ],
187- 'description ' => $ data ['description ' ],
196+ 'node_id ' => $ this -> data ['node_id ' ],
197+ 'name ' => $ this -> data ['name ' ],
198+ 'description ' => $ this -> data ['description ' ],
188199 'skip_scripts ' => false ,
189200 'suspended ' => false ,
190- 'owner_id ' => $ data ['owner_id ' ],
191- 'memory ' => $ data ['memory ' ],
192- 'swap ' => $ data ['swap ' ],
193- 'disk ' => $ data ['disk ' ],
194- 'io ' => $ data ['io ' ],
195- 'cpu ' => $ data ['cpu ' ],
201+ 'owner_id ' => $ this -> data ['owner_id ' ],
202+ 'memory ' => $ this -> data ['memory ' ],
203+ 'swap ' => $ this -> data ['swap ' ],
204+ 'disk ' => $ this -> data ['disk ' ],
205+ 'io ' => $ this -> data ['io ' ],
206+ 'cpu ' => $ this -> data ['cpu ' ],
196207 'oom_disabled ' => false ,
197- 'allocation_id ' => $ data ['allocation_id ' ],
198- 'service_id ' => $ data ['service_id ' ],
199- 'option_id ' => $ data ['option_id ' ],
208+ 'allocation_id ' => $ this -> data ['allocation_id ' ],
209+ 'service_id ' => $ this -> data ['service_id ' ],
210+ 'option_id ' => $ this -> data ['option_id ' ],
200211 'pack_id ' => null ,
201- 'startup ' => $ data ['startup ' ],
212+ 'startup ' => $ this -> data ['startup ' ],
202213 'daemonSecret ' => 'randomstring ' ,
203- 'image ' => $ data ['docker_image ' ],
214+ 'image ' => $ this -> data ['docker_image ' ],
204215 'username ' => 'user_name ' ,
205216 'sftp_password ' => null ,
206217 ])->once ()->andReturn ((object ) [
207218 'node_id ' => 1 ,
208219 'id ' => 1 ,
209220 ]);
210221
211- $ this ->allocationRepository ->shouldReceive ('assignAllocationsToServer ' )->with (1 , [1 , 2 , 3 ]);
222+ $ this ->allocationRepository ->shouldReceive ('assignAllocationsToServer ' )->with (1 , [1 , 2 , 3 ])-> once ()-> andReturnNull () ;
212223 $ this ->validatorService ->shouldReceive ('getResults ' )->withNoArgs ()->once ()->andReturn ([[
213224 'id ' => 1 ,
214225 'key ' => 'TEST_VAR_1 ' ,
@@ -224,9 +235,40 @@ public function testCreateShouldHitAllOfTheNecessaryServicesAndStoreTheServer()
224235 ->shouldReceive ('create ' )->with (1 )->once ()->andReturnNull ();
225236 $ this ->database ->shouldReceive ('commit ' )->withNoArgs ()->once ()->andReturnNull ();
226237
227- $ response = $ this ->service ->create ($ data );
238+ $ response = $ this ->service ->create ($ this -> data );
228239
229240 $ this ->assertEquals (1 , $ response ->id );
230241 $ this ->assertEquals (1 , $ response ->node_id );
231242 }
243+
244+ /**
245+ * Test handling of node timeout or other daemon error.
246+ */
247+ public function testExceptionShouldBeThrownIfTheRequestFails ()
248+ {
249+ $ this ->validatorService ->shouldReceive ('isAdmin->setFields->validate->getResults ' )->once ()->andReturn ([]);
250+ $ this ->database ->shouldReceive ('beginTransaction ' )->withNoArgs ()->once ()->andReturnNull ();
251+ $ this ->uuid ->shouldReceive ('uuid4->toString ' )->once ()->andReturn ('uuid-0000 ' );
252+ $ this ->usernameService ->shouldReceive ('generate ' )->once ()->andReturn ('user_name ' );
253+ $ this ->repository ->shouldReceive ('create ' )->once ()->andReturn ((object ) [
254+ 'node_id ' => 1 ,
255+ 'id ' => 1 ,
256+ ]);
257+
258+ $ this ->allocationRepository ->shouldReceive ('assignAllocationsToServer ' )->once ()->andReturnNull ();
259+ $ this ->serverVariableRepository ->shouldReceive ('insert ' )->with ([])->once ()->andReturnNull ();
260+ $ this ->daemonServerRepository ->shouldReceive ('setNode->create ' )->once ()->andThrow ($ this ->exception );
261+ $ this ->exception ->shouldReceive ('getResponse ' )->withNoArgs ()->once ()->andReturnNull ();
262+ $ this ->writer ->shouldReceive ('warning ' )->with ($ this ->exception )->once ()->andReturnNull ();
263+ $ this ->database ->shouldReceive ('rollBack ' )->withNoArgs ()->once ()->andReturnNull ();
264+
265+ try {
266+ $ this ->service ->create ($ this ->data );
267+ } catch (Exception $ exception ) {
268+ $ this ->assertInstanceOf (DisplayException::class, $ exception );
269+ $ this ->assertEquals (trans ('admin/server.exceptions.daemon_exception ' , [
270+ 'code ' => 'E_CONN_REFUSED ' ,
271+ ]), $ exception ->getMessage ());
272+ }
273+ }
232274}
0 commit comments