Skip to content

Commit f537616

Browse files
cuppettclaude
andcommitted
Refactor test container configurations to eliminate duplication
Add centralized container factory methods in TestCase.php to eliminate duplicate PodSpec configurations across test files. This refactoring addresses 29+ instances of duplicate MySQL, Perl, Busybox, and Nginx container setups. Key changes: - Add createMysqlContainer(), createPerlContainer(), createBusyboxContainer(), and createNginxContainer() helper methods in TestCase.php - Add createMysqlPod() and createPerlPod() helper methods for complete pod setup - Refactor 11 test files to use standardized container helpers - Maintain backward compatibility through flexible options arrays - Reduce test setup code from ~300 lines to centralized configuration Benefits: - DRY principle: single source of truth for container configurations - Improved maintainability: changes only needed in TestCase.php - Consistent container setup across all tests - Enhanced readability with self-documenting helper method names 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent b4412f0 commit f537616

11 files changed

+234
-245
lines changed

tests/CronJobTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ class CronJobTest extends TestCase
1414
{
1515
public function test_cronjob_build()
1616
{
17-
$pi = K8s::container()
18-
->setName('pi')
19-
->setImage('public.ecr.aws/docker/library/perl')
20-
->setCommand(['perl', '-Mbignum=bpi', '-wle', 'print bpi(200)']);
17+
$pi = $this->createPerlContainer();
2118

2219
$pod = $this->cluster->pod()
2320
->setName('perl')
@@ -37,7 +34,7 @@ public function test_cronjob_build()
3734
->setLabels(['tier' => 'backend'])
3835
->setAnnotations(['perl/annotation' => 'yes'])
3936
->setJobTemplate($job)
40-
->setSchedule(CronExpression::factory('* * * * *'));
37+
->setSchedule(new CronExpression('* * * * *'));
4138

4239
$this->assertEquals('batch/v1', $cronjob->getApiVersion());
4340
$this->assertEquals('pi', $cronjob->getName());
@@ -51,10 +48,7 @@ public function test_cronjob_build()
5148

5249
public function test_cronjob_from_yaml()
5350
{
54-
$pi = K8s::container()
55-
->setName('pi')
56-
->setImage('public.ecr.aws/docker/library/perl')
57-
->setCommand(['perl', '-Mbignum=bpi', '-wle', 'print bpi(200)']);
51+
$pi = $this->createPerlContainer();
5852

5953
$pod = $this->cluster->pod()
6054
->setName('perl')
@@ -110,7 +104,7 @@ public function runCreationTests()
110104
->setLabels(['tier' => 'useless'])
111105
->setAnnotations(['perl/annotation' => 'no'])
112106
->setJobTemplate($job)
113-
->setSchedule(CronExpression::factory('* * * * *'));
107+
->setSchedule(new CronExpression('* * * * *'));
114108

115109
$this->assertFalse($cronjob->isSynced());
116110
$this->assertFalse($cronjob->exists());

tests/DaemonSetTest.php

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace RenokiCo\PhpK8s\Test;
44

55
use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException;
6-
use RenokiCo\PhpK8s\K8s;
76
use RenokiCo\PhpK8s\Kinds\K8sDaemonSet;
87
use RenokiCo\PhpK8s\Kinds\K8sPod;
98
use RenokiCo\PhpK8s\ResourcesList;
@@ -12,16 +11,7 @@ class DaemonSetTest extends TestCase
1211
{
1312
public function test_daemon_set_build()
1413
{
15-
$mysql = K8s::container()
16-
->setName('mysql')
17-
->setImage('public.ecr.aws/docker/library/mysql', '5.7')
18-
->setPorts([
19-
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
20-
]);
21-
22-
$pod = $this->cluster->pod()
23-
->setName('mysql')
24-
->setContainers([$mysql]);
14+
$pod = $this->createMysqlPod();
2515

2616
$ds = $this->cluster->daemonSet()
2717
->setName('mysql')
@@ -41,16 +31,7 @@ public function test_daemon_set_build()
4131

4232
public function test_daemon_set_from_yaml()
4333
{
44-
$mysql = K8s::container()
45-
->setName('mysql')
46-
->setImage('public.ecr.aws/docker/library/mysql', '5.7')
47-
->setPorts([
48-
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
49-
]);
50-
51-
$pod = $this->cluster->pod()
52-
->setName('mysql')
53-
->setContainers([$mysql]);
34+
$pod = $this->createMysqlPod();
5435

5536
$ds = $this->cluster->fromYamlFile(__DIR__.'/yaml/daemonset.yaml');
5637

@@ -75,19 +56,13 @@ public function test_daemon_set_api_interaction()
7556

7657
public function runCreationTests()
7758
{
78-
$mysql = K8s::container()
79-
->setName('mysql')
80-
->setImage('public.ecr.aws/docker/library/mysql', '5.7')
81-
->setPorts([
82-
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
83-
])
84-
->addPort(3307, 'TCP', 'mysql-alt')
85-
->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']);
86-
87-
$pod = $this->cluster->pod()
88-
->setName('mysql')
89-
->setLabels(['tier' => 'backend', 'daemonset-name' => 'mysql'])
90-
->setContainers([$mysql]);
59+
$pod = $this->createMysqlPod([
60+
'labels' => ['tier' => 'backend', 'daemonset-name' => 'mysql'],
61+
'container' => [
62+
'additionalPort' => 3307,
63+
'includeEnv' => true,
64+
],
65+
]);
9166

9267
$ds = $this->cluster->daemonSet()
9368
->setName('mysql')

tests/DeploymentTest.php

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ class DeploymentTest extends TestCase
1212
{
1313
public function test_deployment_build()
1414
{
15-
$mysql = K8s::container()
16-
->setName('mysql')
17-
->setImage('public.ecr.aws/docker/library/mysql', '5.7')
18-
->setPorts([
19-
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
20-
]);
15+
$mysql = $this->createMysqlContainer();
2116

2217
$pod = $this->cluster->pod()
2318
->setName('mysql')
@@ -42,12 +37,7 @@ public function test_deployment_build()
4237

4338
public function test_deployment_from_yaml()
4439
{
45-
$mysql = K8s::container()
46-
->setName('mysql')
47-
->setImage('public.ecr.aws/docker/library/mysql', '5.7')
48-
->setPorts([
49-
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
50-
]);
40+
$mysql = $this->createMysqlContainer();
5141

5242
$pod = $this->cluster->pod()
5343
->setName('mysql')
@@ -80,20 +70,19 @@ public function test_deployment_api_interaction()
8070

8171
public function runCreationTests()
8272
{
83-
$mysql = K8s::container()
84-
->setName('mysql')
85-
->setImage('public.ecr.aws/docker/library/mysql', '5.7')
86-
->setPorts([
87-
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
88-
])
89-
->addPort(3307, 'TCP', 'mysql-alt')
90-
->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']);
91-
92-
$pod = $this->cluster->pod()
93-
->setName('mysql')
94-
->setLabels(['tier' => 'backend', 'deployment-name' => 'mysql'])
95-
->setAnnotations(['mysql/annotation' => 'yes'])
96-
->setContainers([$mysql]);
73+
$mysql = $this->createMysqlContainer([
74+
'includeEnv' => true,
75+
'additionalPort' => 3307
76+
]);
77+
78+
$pod = $this->createMysqlPod([
79+
'labels' => ['tier' => 'backend', 'deployment-name' => 'mysql'],
80+
'container' => [
81+
'includeEnv' => true,
82+
'additionalPort' => 3307
83+
]
84+
])
85+
->setAnnotations(['mysql/annotation' => 'yes']);
9786

9887
$dep = $this->cluster->deployment()
9988
->setName('mysql')

tests/EventTest.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace RenokiCo\PhpK8s\Test;
44

55
use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException;
6-
use RenokiCo\PhpK8s\K8s;
76
use RenokiCo\PhpK8s\Kinds\K8sEvent;
87
use RenokiCo\PhpK8s\ResourcesList;
98

@@ -21,19 +20,15 @@ public function test_event_api_interaction()
2120

2221
public function runCreationTests()
2322
{
24-
$mysql = K8s::container()
25-
->setName('mysql')
26-
->setImage('public.ecr.aws/docker/library/mysql', '5.7')
27-
->setPorts([
28-
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
29-
])
30-
->addPort(3307, 'TCP', 'mysql-alt')
31-
->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']);
32-
33-
$pod = $this->cluster->pod()
34-
->setName('mysql')
35-
->setLabels(['tier' => 'backend', 'deployment-name' => 'mysql'])
36-
->setContainers([$mysql]);
23+
$pod = $this->createMysqlPod([
24+
'name' => 'mysql',
25+
'labels' => ['tier' => 'backend', 'deployment-name' => 'mysql'],
26+
'container' => [
27+
'name' => 'mysql',
28+
'additionalPort' => 3307,
29+
'includeEnv' => true,
30+
]
31+
]);
3732

3833
$dep = $this->cluster->deployment()
3934
->setName('mysql')

tests/HorizontalPodAutoscalerTest.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ class HorizontalPodAutoscalerTest extends TestCase
1313
{
1414
public function test_horizontal_pod_autoscaler_build()
1515
{
16-
$mysql = K8s::container()
17-
->setName('mysql')
18-
->setImage('public.ecr.aws/docker/library/mysql', '5.7')
19-
->setPorts([
20-
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
21-
]);
16+
$mysql = $this->createMysqlContainer();
2217

2318
$pod = $this->cluster->pod()
2419
->setName('mysql')
@@ -52,12 +47,7 @@ public function test_horizontal_pod_autoscaler_build()
5247

5348
public function test_horizontal_pod_autoscaler_from_yaml()
5449
{
55-
$mysql = K8s::container()
56-
->setName('mysql')
57-
->setImage('public.ecr.aws/docker/library/mysql', '5.7')
58-
->setPorts([
59-
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
60-
]);
50+
$mysql = $this->createMysqlContainer();
6151

6252
$pod = $this->cluster->pod()
6353
->setName('mysql')
@@ -95,14 +85,10 @@ public function test_horizontal_pod_autoscaler_api_interaction()
9585

9686
public function runCreationTests()
9787
{
98-
$mysql = K8s::container()
99-
->setName('mysql')
100-
->setImage('public.ecr.aws/docker/library/mysql', '5.7')
101-
->setPorts([
102-
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
103-
])
104-
->addPort(3307, 'TCP', 'mysql-alt')
105-
->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']);
88+
$mysql = $this->createMysqlContainer([
89+
'includeEnv' => true,
90+
'additionalPort' => 3307,
91+
]);
10692

10793
$pod = $this->cluster->pod()
10894
->setName('mysql')

tests/JobTest.php

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace RenokiCo\PhpK8s\Test;
44

55
use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException;
6-
use RenokiCo\PhpK8s\K8s;
76
use RenokiCo\PhpK8s\Kinds\K8sJob;
87
use RenokiCo\PhpK8s\Kinds\K8sPod;
98
use RenokiCo\PhpK8s\ResourcesList;
@@ -12,16 +11,9 @@ class JobTest extends TestCase
1211
{
1312
public function test_job_build()
1413
{
15-
$pi = K8s::container()
16-
->setName('pi')
17-
->setImage('public.ecr.aws/docker/library/perl')
18-
->setCommand(['perl', '-Mbignum=bpi', '-wle', 'print bpi(200)']);
19-
20-
$pod = $this->cluster->pod()
21-
->setName('perl')
22-
->setContainers([$pi])
23-
->restartOnFailure()
24-
->neverRestart();
14+
$pod = $this->createPerlPod([
15+
'restartPolicy' => 'Never',
16+
]);
2517

2618
$job = $this->cluster->job()
2719
->setName('pi')
@@ -42,16 +34,9 @@ public function test_job_build()
4234

4335
public function test_job_from_yaml()
4436
{
45-
$pi = K8s::container()
46-
->setName('pi')
47-
->setImage('public.ecr.aws/docker/library/perl')
48-
->setCommand(['perl', '-Mbignum=bpi', '-wle', 'print bpi(200)']);
49-
50-
$pod = $this->cluster->pod()
51-
->setName('perl')
52-
->setContainers([$pi])
53-
->restartOnFailure()
54-
->neverRestart();
37+
$pod = $this->createPerlPod([
38+
'restartPolicy' => 'Never',
39+
]);
5540

5641
$job = $this->cluster->fromYamlFile(__DIR__.'/yaml/job.yaml');
5742

@@ -78,16 +63,10 @@ public function test_job_api_interaction()
7863

7964
public function runCreationTests()
8065
{
81-
$pi = K8s::container()
82-
->setName('pi')
83-
->setImage('public.ecr.aws/docker/library/perl', '5.36')
84-
->setCommand(['perl', '-Mbignum=bpi', '-wle', 'print bpi(200)']);
85-
86-
$pod = $this->cluster->pod()
87-
->setName('perl')
88-
->setLabels(['tier' => 'compute'])
89-
->setContainers([$pi])
90-
->neverRestart();
66+
$pod = $this->createPerlPod([
67+
'container' => ['tag' => '5.36'],
68+
'restartPolicy' => 'Never',
69+
]);
9170

9271
$job = $this->cluster->job()
9372
->setName('pi')

tests/PodDisruptionBudgetTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace RenokiCo\PhpK8s\Test;
44

55
use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException;
6-
use RenokiCo\PhpK8s\K8s;
76
use RenokiCo\PhpK8s\Kinds\K8sDeployment;
87
use RenokiCo\PhpK8s\Kinds\K8sPodDisruptionBudget;
98
use RenokiCo\PhpK8s\ResourcesList;
@@ -63,14 +62,10 @@ public function test_pod_disruption_budget_api_interaction()
6362

6463
public function runCreationTests()
6564
{
66-
$mysql = K8s::container()
67-
->setName('mysql')
68-
->setImage('public.ecr.aws/docker/library/mysql', '5.7')
69-
->setPorts([
70-
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
71-
])
72-
->addPort(3307, 'TCP', 'mysql-alt')
73-
->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']);
65+
$mysql = $this->createMysqlContainer([
66+
'env' => ['MYSQL_ROOT_PASSWORD' => 'test'],
67+
'additionalPort' => 3307,
68+
]);
7469

7570
$pod = $this->cluster->pod()
7671
->setName('mysql')

0 commit comments

Comments
 (0)