44
55use Pterodactyl \Models \Node ;
66use InvalidArgumentException ;
7+ use Pterodactyl \Models \Server ;
78use Pterodactyl \Models \Location ;
9+ use Pterodactyl \Models \Database ;
810use Illuminate \Support \Collection ;
911use Pterodactyl \Tests \Integration \IntegrationTestCase ;
1012use Pterodactyl \Services \Deployment \FindViableNodesService ;
1113use Pterodactyl \Exceptions \Service \Deployment \NoViableNodeException ;
1214
1315class FindViableNodesServiceTest extends IntegrationTestCase
1416{
17+ public function setUp (): void
18+ {
19+ parent ::setUp ();
20+
21+ Database::query ()->delete ();
22+ Server::query ()->delete ();
23+ Node::query ()->delete ();
24+ }
25+
1526 public function testExceptionIsThrownIfNoDiskSpaceHasBeenSet ()
1627 {
1728 $ this ->expectException (InvalidArgumentException::class);
@@ -35,10 +46,11 @@ public function testExpectedNodeIsReturnedForLocation()
3546
3647 /** @var \Pterodactyl\Models\Node[] $nodes */
3748 $ nodes = [
38- // This node should never be returned.
49+ // This node should never be returned once we've completed the initial test which
50+ // runs without a location filter.
3951 factory (Node::class)->create ([
4052 'location_id ' => $ locations [0 ]->id ,
41- 'memory ' => 10240 ,
53+ 'memory ' => 2048 ,
4254 'disk ' => 1024 * 100 ,
4355 ]),
4456 factory (Node::class)->create ([
@@ -55,53 +67,89 @@ public function testExpectedNodeIsReturnedForLocation()
5567 ]),
5668 ];
5769
70+ // Expect that all of the nodes are returned as we're under all of their limits
71+ // and there is no location filter being provided.
72+ $ response = $ this ->getService ()->setDisk (512 )->setMemory (512 )->handle ();
73+ $ this ->assertInstanceOf (Collection::class, $ response );
74+ $ this ->assertCount (3 , $ response );
75+ $ this ->assertInstanceOf (Node::class, $ response [0 ]);
76+
77+ // Expect that only the last node is returned because it is the only one with enough
78+ // memory available to this instance.
79+ $ response = $ this ->getService ()->setDisk (512 )->setMemory (2049 )->handle ();
80+ $ this ->assertInstanceOf (Collection::class, $ response );
81+ $ this ->assertCount (1 , $ response );
82+ $ this ->assertSame ($ nodes [2 ]->id , $ response [0 ]->id );
83+
84+ // Helper, I am lazy.
5885 $ base = function () use ($ locations ) {
5986 return $ this ->getService ()->setLocations ([ $ locations [1 ]->id ])->setDisk (512 );
6087 };
6188
89+ // Expect that we can create this server on either node since the disk and memory
90+ // limits are below the allowed amount.
6291 $ response = $ base ()->setMemory (512 )->handle ();
63- $ this ->assertInstanceOf (Collection::class, $ response );
64- $ this ->assertFalse ($ response ->isEmpty ());
65- $ this ->assertSame (2 , $ response ->count ());
92+ $ this ->assertCount (2 , $ response );
6693 $ this ->assertSame (2 , $ response ->where ('location_id ' , $ locations [1 ]->id )->count ());
6794
95+ // Expect that we can only create this server on the second node since the memory
96+ // allocated is over the amount of memory available to the first node.
6897 $ response = $ base ()->setMemory (2048 )->handle ();
69- $ this ->assertSame (1 , $ response-> count () );
98+ $ this ->assertCount (1 , $ response );
7099 $ this ->assertSame ($ nodes [2 ]->id , $ response [0 ]->id );
71100
101+ // Expect that we can only create this server on the second node since the disk
102+ // allocated is over the limit assigned to the first node (even with the overallocate).
72103 $ response = $ base ()->setDisk (20480 )->setMemory (256 )->handle ();
73- $ this ->assertSame (1 , $ response-> count () );
104+ $ this ->assertCount (1 , $ response );
74105 $ this ->assertSame ($ nodes [2 ]->id , $ response [0 ]->id );
75106
76- $ response = $ base ()->setDisk (11263 )->setMemory (256 )->handle ();
77- $ this ->assertSame (2 , $ response ->count ());
107+ // Expect that we could create the server on either node since the disk allocated is
108+ // right at the limit for Node 1 when the overallocate value is included in the calc.
109+ $ response = $ base ()->setDisk (11264 )->setMemory (256 )->handle ();
110+ $ this ->assertCount (2 , $ response );
78111
112+ // Create two servers on the first node so that the disk space used is equal to the
113+ // base amount available to the node (without overallocation included).
79114 $ servers = Collection::make ([
80115 $ this ->createServerModel (['node_id ' => $ nodes [1 ]->id , 'disk ' => 5120 ]),
81116 $ this ->createServerModel (['node_id ' => $ nodes [1 ]->id , 'disk ' => 5120 ]),
82117 ]);
83118
119+ // Expect that we cannot create a server with a 1GB disk on the first node since there
120+ // is not enough space (even with the overallocate) available to the node.
84121 $ response = $ base ()->setDisk (1024 )->setMemory (256 )->handle ();
85- $ this ->assertSame (1 , $ response-> count () );
122+ $ this ->assertCount (1 , $ response );
86123 $ this ->assertSame ($ nodes [2 ]->id , $ response [0 ]->id );
124+
125+ // Cleanup servers since we need to test some other stuff with memory here.
87126 $ servers ->each ->delete ();
88127
128+ // Expect that no viable node can be found when the memory limit for the given instance
129+ // is greater than either node can support, even with the overallocation limits taken
130+ // into account.
89131 $ this ->expectException (NoViableNodeException::class);
90132 $ base ()->setMemory (10000 )->handle ();
91133
134+ // Create four servers so that the memory used for the second node is equal to the total
135+ // limit for that node (pre-overallocate calculation).
92136 Collection::make ([
93137 $ this ->createServerModel (['node_id ' => $ nodes [2 ]->id , 'memory ' => 1024 ]),
94138 $ this ->createServerModel (['node_id ' => $ nodes [2 ]->id , 'memory ' => 1024 ]),
95139 $ this ->createServerModel (['node_id ' => $ nodes [2 ]->id , 'memory ' => 1024 ]),
96140 $ this ->createServerModel (['node_id ' => $ nodes [2 ]->id , 'memory ' => 1024 ]),
97141 ]);
98142
143+ // Expect that either node can support this server when we account for the overallocate
144+ // value of the second node.
99145 $ response = $ base ()->setMemory (500 )->handle ();
100- $ this ->assertSame (2 , $ response-> count () );
146+ $ this ->assertCount (2 , $ response );
101147 $ this ->assertSame (2 , $ response ->where ('location_id ' , $ locations [1 ]->id )->count ());
102148
103- $ response = $ base ()->setMemory (512 )->handle ();
104- $ this ->assertSame (1 , $ response ->count ());
149+ // Expect that only the first node can support this server when we go over the remaining
150+ // memory for the second nodes overallocate calculation.
151+ $ response = $ base ()->setMemory (640 )->handle ();
152+ $ this ->assertCount (1 , $ response );
105153 $ this ->assertSame ($ nodes [1 ]->id , $ response [0 ]->id );
106154 }
107155
0 commit comments