Skip to content

Commit 1a9701f

Browse files
cuppettclaude
andcommitted
Fix test resource leakage and add timeouts to prevent hangs
Fixed critical test isolation issue where DeploymentTest resources were being reused by ReplicaSetTest, causing infinite loops and CI timeouts. Changes: - ReplicaSetTest: Changed label from 'tier: backend' to 'tier: backend-rs' to prevent Deployment controller from adopting the ReplicaSet - DeploymentTest: Added tearDown() method to ensure cleanup of Deployment and HPA resources after each test, preventing resource leakage - Both tests: Added 60-second timeouts to all wait loops (scaling, deletion) to prevent infinite hangs and provide clear error messages Root cause: DeploymentTest created a Deployment with selector 'tier: backend' which adopted the ReplicaSet from ReplicaSetTest (also labeled 'tier: backend'), causing the Deployment controller to scale the ReplicaSet to 0. ReplicaSetTest then waited indefinitely for 2 replicas that would never appear. This fix ensures tests are properly isolated and will fail fast with descriptive errors rather than hanging for 25+ minutes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 3db69a5 commit 1a9701f

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed

tests/DeploymentTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,42 @@
1010

1111
class DeploymentTest extends TestCase
1212
{
13+
protected function tearDown(): void
14+
{
15+
// Clean up deployment and HPA if they exist
16+
try {
17+
$dep = $this->cluster->getDeploymentByName('mariadb');
18+
if ($dep->exists()) {
19+
$dep->delete();
20+
21+
$timeout = 30;
22+
$start = time();
23+
while ($dep->exists() && (time() - $start < $timeout)) {
24+
sleep(1);
25+
}
26+
}
27+
} catch (\Exception $e) {
28+
// Deployment doesn't exist, that's fine
29+
}
30+
31+
try {
32+
$hpa = $this->cluster->getHorizontalPodAutoscalerByName('deploy-mariadb');
33+
if ($hpa->exists()) {
34+
$hpa->delete();
35+
36+
$timeout = 30;
37+
$start = time();
38+
while ($hpa->exists() && (time() - $start < $timeout)) {
39+
sleep(1);
40+
}
41+
}
42+
} catch (\Exception $e) {
43+
// HPA doesn't exist, that's fine
44+
}
45+
46+
parent::tearDown();
47+
}
48+
1349
public function test_deployment_build()
1450
{
1551
$mariadb = $this->createMariadbContainer();
@@ -233,15 +269,32 @@ public function runDeletionTests()
233269
$this->assertTrue($dep->delete());
234270
$this->assertTrue($hpa->delete());
235271

272+
$timeout = 60; // 60 second timeout
273+
$start = time();
274+
236275
while ($hpa->exists()) {
276+
if (time() - $start > $timeout) {
277+
$this->fail('Timeout waiting for HPA to be deleted');
278+
}
237279
sleep(1);
238280
}
239281

282+
$start = time();
240283
while ($dep->exists()) {
284+
if (time() - $start > $timeout) {
285+
$this->fail('Timeout waiting for Deployment to be deleted');
286+
}
241287
sleep(1);
242288
}
243289

290+
$start = time();
244291
while ($dep->getPods()->count() > 0) {
292+
if (time() - $start > $timeout) {
293+
$this->fail(sprintf(
294+
'Timeout waiting for Deployment pods to be deleted. Remaining: %d',
295+
$dep->getPods()->count()
296+
));
297+
}
245298
sleep(1);
246299
}
247300

@@ -277,7 +330,17 @@ public function runScalingTests()
277330

278331
$scaler = $dep->scale(2);
279332

333+
$timeout = 60; // 60 second timeout
334+
$start = time();
335+
280336
while ($dep->getReadyReplicasCount() < 2 || $scaler->getReplicas() < 2) {
337+
if (time() - $start > $timeout) {
338+
$this->fail(sprintf(
339+
'Timeout waiting for deployment to scale to 2. Current state: ready=%d, scaler=%d',
340+
$dep->getReadyReplicasCount(),
341+
$scaler->getReplicas()
342+
));
343+
}
281344
$scaler->refresh();
282345
$dep->refresh();
283346
sleep(1);

tests/ReplicaSetTest.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function test_replica_set_build()
2727

2828
$this->assertEquals('apps/v1', $rs->getApiVersion());
2929
$this->assertEquals('mariadb-rs', $rs->getName());
30-
$this->assertEquals(['tier' => 'backend'], $rs->getLabels());
30+
$this->assertEquals(['tier' => 'backend-rs'], $rs->getLabels());
3131
$this->assertEquals(['mariadb/annotation' => 'yes'], $rs->getAnnotations());
3232
$this->assertEquals(3, $rs->getReplicas());
3333
$this->assertEquals($pod->getName(), $rs->getTemplate()->getName());
@@ -47,7 +47,7 @@ public function test_replica_set_from_yaml()
4747

4848
$this->assertEquals('apps/v1', $rs->getApiVersion());
4949
$this->assertEquals('mariadb-rs', $rs->getName());
50-
$this->assertEquals(['tier' => 'backend'], $rs->getLabels());
50+
$this->assertEquals(['tier' => 'backend-rs'], $rs->getLabels());
5151
$this->assertEquals(['mariadb/annotation' => 'yes'], $rs->getAnnotations());
5252
$this->assertEquals(3, $rs->getReplicas());
5353
$this->assertEquals($pod->getName(), $rs->getTemplate()->getName());
@@ -85,7 +85,7 @@ public function runCreationTests()
8585

8686
$rs = $this->cluster->replicaSet()
8787
->setName('mariadb-rs')
88-
->setLabels(['tier' => 'backend'])
88+
->setLabels(['tier' => 'backend-rs'])
8989
->setAnnotations(['mariadb/annotation' => 'yes'])
9090
->setSelectors(['matchLabels' => ['app' => 'mariadb-rs']])
9191
->setReplicas(1)
@@ -103,7 +103,7 @@ public function runCreationTests()
103103

104104
$this->assertEquals('apps/v1', $rs->getApiVersion());
105105
$this->assertEquals('mariadb-rs', $rs->getName());
106-
$this->assertEquals(['tier' => 'backend'], $rs->getLabels());
106+
$this->assertEquals(['tier' => 'backend-rs'], $rs->getLabels());
107107
$this->assertEquals(['mariadb/annotation' => 'yes'], $rs->getAnnotations());
108108
$this->assertEquals(1, $rs->getReplicas());
109109
$this->assertEquals($pod->getName(), $rs->getTemplate()->getName());
@@ -170,7 +170,7 @@ public function runGetTests()
170170

171171
$this->assertEquals('apps/v1', $rs->getApiVersion());
172172
$this->assertEquals('mariadb-rs', $rs->getName());
173-
$this->assertEquals(['tier' => 'backend'], $rs->getLabels());
173+
$this->assertEquals(['tier' => 'backend-rs'], $rs->getLabels());
174174
$this->assertEquals(['mariadb/annotation' => 'yes'], $rs->getAnnotations());
175175
$this->assertEquals(1, $rs->getReplicas());
176176

@@ -187,7 +187,18 @@ public function runScalingTests()
187187

188188
$this->assertTrue($rs->isSynced());
189189

190+
$timeout = 60; // 60 second timeout
191+
$start = time();
192+
190193
while ($rs->getReadyReplicasCount() < 2 || $scaler->getReplicas() < 2) {
194+
if (time() - $start > $timeout) {
195+
$this->fail(sprintf(
196+
'Timeout waiting for replicas to scale to 2. Current state: ready=%d, scaler=%d',
197+
$rs->getReadyReplicasCount(),
198+
$scaler->getReplicas()
199+
));
200+
}
201+
191202
$scaler->refresh();
192203
$rs->refresh();
193204

@@ -212,7 +223,7 @@ public function runUpdateTests()
212223

213224
$this->assertEquals('apps/v1', $rs->getApiVersion());
214225
$this->assertEquals('mariadb-rs', $rs->getName());
215-
$this->assertEquals(['tier' => 'backend'], $rs->getLabels());
226+
$this->assertEquals(['tier' => 'backend-rs'], $rs->getLabels());
216227
$this->assertEquals([], $rs->getAnnotations());
217228
$this->assertEquals(2, $rs->getReplicas());
218229

tests/yaml/replicaset.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ kind: ReplicaSet
33
metadata:
44
name: mariadb-rs
55
labels:
6-
tier: backend
6+
tier: backend-rs
77
annotations:
88
mariadb/annotation: "yes"
99
spec:

0 commit comments

Comments
 (0)