Repository navigation
Expand file tree
/
Copy pathGenericDockerContainerTest.php
More file actions
1251 lines (1025 loc) · 51.9 KB
/
Copy pathGenericDockerContainerTest.php
File metadata and controls
1251 lines (1025 loc) · 51.9 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace Test\Unit;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Test\Unit\Mocks\ClientMock;
use Test\Unit\Mocks\InspectResponseFixture;
use Test\Unit\Mocks\TestableGenericDockerContainer;
use TinyBlocks\DockerContainer\GenericDockerContainer;
use TinyBlocks\DockerContainer\Internal\Containers\ShutdownHook;
use TinyBlocks\DockerContainer\Internal\Exceptions\ContainerWaitTimeout;
use TinyBlocks\DockerContainer\Internal\Exceptions\DockerCommandExecutionFailed;
use TinyBlocks\DockerContainer\Internal\Exceptions\DockerContainerNotFound;
use TinyBlocks\DockerContainer\Waits\Conditions\ContainerReady;
use TinyBlocks\DockerContainer\Waits\ContainerWaitForDependency;
use TinyBlocks\DockerContainer\Waits\ContainerWaitForTime;
final class GenericDockerContainerTest extends TestCase
{
private ClientMock $client;
protected function setUp(): void
{
$this->client = new ClientMock();
}
public function testRunContainerSuccessfully(): void
{
/** @Given a container configured with an image and a name */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'test-alpine',
client: $this->client
);
/** @And the Docker daemon returns a valid container ID and inspect response */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(
hostname: 'test-alpine',
environment: ['PATH=/usr/local/bin']
)
);
/** @When the container is started */
$started = $container->run();
/** @Then the container should be running with the expected properties */
self::assertSame(expected: InspectResponseFixture::shortContainerId(), actual: $started->getId());
self::assertSame(expected: 'test-alpine', actual: $started->getName());
self::assertSame(expected: 'test-alpine', actual: $started->getAddress()->getHostname());
self::assertSame(expected: '172.22.0.2', actual: $started->getAddress()->getIp());
}
public function testRunContainerWithFullConfiguration(): void
{
/** @Given a fully configured container */
$container = TestableGenericDockerContainer::createWith(
image: 'nginx:latest',
name: 'web-server',
client: $this->client
)
->withNetwork(name: 'my-network')
->withPortMapping(portOnHost: 8080, portOnContainer: 80)
->withVolumeMapping(pathOnHost: '/var/www', pathOnContainer: '/usr/share/nginx/html')
->withEnvironmentVariable(key: 'NGINX_HOST', value: 'localhost')
->withEnvironmentVariable(key: 'NGINX_PORT', value: '80');
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(
hostname: 'web-server',
environment: ['NGINX_HOST=localhost', 'NGINX_PORT=80'],
networkName: 'my-network',
exposedPorts: ['80/tcp' => (object)[]]
)
);
/** @When the container is started */
$started = $container->run();
/** @Then the container should expose the configured environment variables */
self::assertSame(
expected: 'localhost',
actual: $started->getEnvironmentVariables()->getValueBy(
key: 'NGINX_HOST'
)
);
self::assertSame(expected: '80', actual: $started->getEnvironmentVariables()->getValueBy(key: 'NGINX_PORT'));
/** @And the address should reflect the exposed port */
self::assertSame(expected: 80, actual: $started->getAddress()->getPorts()->firstExposedPort());
self::assertSame(expected: [80], actual: $started->getAddress()->getPorts()->exposedPorts());
}
public function testRunContainerWithMultiplePortMappings(): void
{
/** @Given a container with multiple port mappings */
$container = TestableGenericDockerContainer::createWith(
image: 'nginx:latest',
name: 'multi-port',
client: $this->client
)
->withPortMapping(portOnHost: 8080, portOnContainer: 80)
->withPortMapping(portOnHost: 8443, portOnContainer: 443);
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(
hostname: 'multi-port',
exposedPorts: ['80/tcp' => (object)[], '443/tcp' => (object)[]]
)
);
/** @When the container is started */
$started = $container->run();
/** @Then both ports should be exposed */
self::assertSame(expected: [80, 443], actual: $started->getAddress()->getPorts()->exposedPorts());
self::assertSame(expected: 80, actual: $started->getAddress()->getPorts()->firstExposedPort());
}
public function testRunContainerWithoutAutoRemove(): void
{
/** @Given a container with auto-remove disabled */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'persistent',
client: $this->client
)->withoutAutoRemove();
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'persistent'));
/** @When the container is started */
$started = $container->run();
/** @Then the container should be running */
self::assertSame(expected: 'persistent', actual: $started->getName());
}
public function testRunContainerWithCopyToContainer(): void
{
/** @Given a container with files to copy */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'copy-test',
client: $this->client
)->copyToContainer(pathOnHost: '/host/config', pathOnContainer: '/app/config');
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'copy-test'));
/** @When the container is started (docker cp is automatically called) */
$started = $container->run();
/** @Then the container should be running */
self::assertSame(expected: 'copy-test', actual: $started->getName());
}
public function testRunContainerWithCommands(): void
{
/** @Given a container */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'cmd-test',
client: $this->client
);
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'cmd-test'));
/** @When the container is started with commands */
$started = $container->run(commands: ['echo', 'hello']);
/** @Then the container should be running */
self::assertSame(expected: 'cmd-test', actual: $started->getName());
}
public function testRunContainerWithWaitBeforeRun(): void
{
/** @Given a condition that is immediately ready */
$condition = $this->createMock(ContainerReady::class);
$condition->expects(self::once())->method('isReady')->willReturn(true);
/** @And a container with a wait-before-run condition */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'wait-test',
client: $this->client
)->withWaitBeforeRun(wait: ContainerWaitForDependency::untilReady(condition: $condition));
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'wait-test'));
/** @When the container is started */
$started = $container->run();
/** @Then the container should be running (wait was called) */
self::assertSame(expected: 'wait-test', actual: $started->getName());
}
public function testRunIfNotExistsCreatesNewContainer(): void
{
/** @Given a container that does not exist */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'new-container',
client: $this->client
)->withEnvironmentVariable(key: 'APP_ENV', value: 'test');
/** @And the Docker list returns empty (container does not exist) */
$this->client->withDockerListResponse(output: '');
/** @And the Docker daemon returns valid run and inspect responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(
hostname: 'new-container',
environment: ['APP_ENV=test']
)
);
/** @When runIfNotExists is called */
$started = $container->runIfNotExists();
/** @Then a new container should be created */
self::assertSame(expected: 'new-container', actual: $started->getName());
self::assertSame(expected: 'test', actual: $started->getEnvironmentVariables()->getValueBy(key: 'APP_ENV'));
}
public function testRunIfNotExistsReturnsExistingContainer(): void
{
/** @Given a container that already exists */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'existing',
client: $this->client
);
/** @And the Docker list returns the existing container ID */
$this->client->withDockerListResponse(output: InspectResponseFixture::containerId());
/** @And the Docker inspect returns the container details */
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(
hostname: 'existing',
environment: ['EXISTING=true']
)
);
/** @When runIfNotExists is called */
$started = $container->runIfNotExists();
/** @Then the existing container should be returned */
self::assertSame(expected: 'existing', actual: $started->getName());
self::assertSame(expected: InspectResponseFixture::shortContainerId(), actual: $started->getId());
self::assertSame(expected: 'true', actual: $started->getEnvironmentVariables()->getValueBy(key: 'EXISTING'));
}
public function testStopContainer(): void
{
/** @Given a running container */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'stop-test',
client: $this->client
);
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'stop-test'));
$this->client->withDockerStopResponse(output: '');
/** @And the container is started */
$started = $container->run();
/** @When the container is stopped */
$stopped = $started->stop();
/** @Then the stop should be successful */
self::assertTrue($stopped->isSuccessful());
}
public function testExecuteAfterStarted(): void
{
/** @Given a running container */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'exec-test',
client: $this->client
);
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'exec-test'));
$this->client->withDockerExecuteResponse(output: 'command output');
/** @And the container is started */
$started = $container->run();
/** @When commands are executed inside the running container */
$execution = $started->executeAfterStarted(commands: ['ls', '-la']);
/** @Then the execution should be successful */
self::assertTrue($execution->isSuccessful());
self::assertSame(expected: 'command output', actual: $execution->getOutput());
}
public function testExceptionWhenRunFails(): void
{
/** @Given a container that will fail to start */
$container = TestableGenericDockerContainer::createWith(
image: 'invalid:image',
name: 'fail-test',
client: $this->client
);
/** @And the Docker daemon returns a failure */
$this->client->withDockerRunResponse(output: 'Cannot connect to the Docker daemon.', isSuccessful: false);
/** @Then a DockerCommandExecutionFailed exception should be thrown */
$this->expectException(DockerCommandExecutionFailed::class);
$this->expectExceptionMessageMatches('/Cannot connect to the Docker daemon/');
/** @When the container is started */
$container->run();
}
public function testExceptionWhenContainerInspectReturnsEmpty(): void
{
/** @Given a container whose inspect returns empty data */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'ghost',
client: $this->client
);
/** @And the Docker daemon returns a valid ID but empty inspect */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: []);
/** @Then a DockerContainerNotFound exception should be thrown */
$this->expectException(DockerContainerNotFound::class);
$this->expectExceptionMessage('Docker container with name <ghost> was not found.');
/** @When the container is started */
$container->run();
}
public function testAddressDefaultsWhenNetworkInfoIsEmpty(): void
{
/** @Given a container with empty network info */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'no-net',
client: $this->client
);
/** @And the Docker daemon returns a response with empty address data */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(
hostname: '',
ipAddress: ''
)
);
/** @When the container is started */
$started = $container->run();
/** @Then the address should fall back to defaults */
self::assertSame(expected: '127.0.0.1', actual: $started->getAddress()->getIp());
self::assertSame(expected: 'localhost', actual: $started->getAddress()->getHostname());
}
public function testContainerWithNoExposedPortsReturnsNull(): void
{
/** @Given a container with no exposed ports */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'no-ports',
client: $this->client
);
/** @And the Docker daemon returns valid responses without exposed ports */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'no-ports'));
/** @When the container is started */
$started = $container->run();
/** @Then firstExposedPort should return null */
self::assertNull($started->getAddress()->getPorts()->firstExposedPort());
self::assertEmpty($started->getAddress()->getPorts()->exposedPorts());
/** @And firstHostPort should return null */
self::assertNull($started->getAddress()->getPorts()->firstHostPort());
self::assertEmpty($started->getAddress()->getPorts()->hostPorts());
}
public function testContainerWithHostPortMapping(): void
{
/** @Given a container with a host port mapping */
$container = TestableGenericDockerContainer::createWith(
image: 'mysql:8.4',
name: 'host-port',
client: $this->client
)->withPortMapping(portOnHost: 33060, portOnContainer: 3306);
/** @And the Docker daemon returns a response with host port bindings */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(
hostname: 'host-port',
exposedPorts: ['3306/tcp' => (object)[]],
hostPortBindings: [
'3306/tcp' => [['HostIp' => '0.0.0.0', 'HostPort' => '33060']]
]
)
);
/** @When the container is started */
$started = $container->run();
/** @Then the exposed port should be the container-internal port */
self::assertSame(expected: 3306, actual: $started->getAddress()->getPorts()->firstExposedPort());
/** @And the host port should be the host-mapped port */
self::assertSame(expected: 33060, actual: $started->getAddress()->getPorts()->firstHostPort());
self::assertSame(expected: [33060], actual: $started->getAddress()->getPorts()->hostPorts());
}
public function testContainerWithMultipleHostPortMappings(): void
{
/** @Given a container with multiple host port mappings */
$container = TestableGenericDockerContainer::createWith(
image: 'nginx:latest',
name: 'multi-host-port',
client: $this->client
)
->withPortMapping(portOnHost: 8080, portOnContainer: 80)
->withPortMapping(portOnHost: 8443, portOnContainer: 443);
/** @And the Docker daemon returns a response with multiple host port bindings */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(
hostname: 'multi-host-port',
exposedPorts: ['80/tcp' => (object)[], '443/tcp' => (object)[]],
hostPortBindings: [
'80/tcp' => [['HostIp' => '0.0.0.0', 'HostPort' => '8080']],
'443/tcp' => [['HostIp' => '0.0.0.0', 'HostPort' => '8443']]
]
)
);
/** @When the container is started */
$started = $container->run();
/** @Then both exposed and host ports should be available */
self::assertSame(expected: [80, 443], actual: $started->getAddress()->getPorts()->exposedPorts());
self::assertSame(expected: [8080, 8443], actual: $started->getAddress()->getPorts()->hostPorts());
self::assertSame(expected: 8080, actual: $started->getAddress()->getPorts()->firstHostPort());
}
public function testContainerWithExposedPortButNoHostBinding(): void
{
/** @Given a container with an exposed port but no host binding */
$container = TestableGenericDockerContainer::createWith(
image: 'redis:latest',
name: 'no-host-bind',
client: $this->client
);
/** @And the Docker daemon returns a response with exposed port but null host bindings */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(
hostname: 'no-host-bind',
exposedPorts: ['6379/tcp' => (object)[]],
hostPortBindings: ['6379/tcp' => null]
)
);
/** @When the container is started */
$started = $container->run();
/** @Then the exposed port should be available */
self::assertSame(expected: 6379, actual: $started->getAddress()->getPorts()->firstExposedPort());
/** @And the host port should be null since there is no binding */
self::assertNull($started->getAddress()->getPorts()->firstHostPort());
self::assertEmpty($started->getAddress()->getPorts()->hostPorts());
}
public function testEnvironmentVariableReturnsEmptyStringForMissingKey(): void
{
/** @Given a running container with known environment variables */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'env-test',
client: $this->client
);
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(
hostname: 'env-test',
environment: ['KNOWN=value']
)
);
/** @And the container is started */
$started = $container->run();
/** @When querying for a missing environment variable */
$missingValue = $started->getEnvironmentVariables()->getValueBy(key: 'MISSING');
/** @Then it should return an empty string */
self::assertSame(expected: '', actual: $missingValue);
}
public function testRunContainerWithAutoGeneratedName(): void
{
/** @Given a container without a name */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: null,
client: $this->client
);
/** @And the Docker daemon returns valid responses (with any hostname from KSUID) */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(
hostname: 'auto-generated'
)
);
/** @When the container is started */
$started = $container->run();
/** @Then the container should have an auto-generated name (non-empty) */
self::assertNotEmpty($started->getName());
}
public function testRunContainerWithWaitAfterStarted(): void
{
/** @Given a container */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'wait-after',
client: $this->client
);
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'wait-after'));
/** @When the container is started with a wait-after condition */
$start = microtime(true);
$started = $container->run(waitAfterStarted: ContainerWaitForTime::forSeconds(seconds: 1));
$elapsed = microtime(true) - $start;
/** @Then the container should have waited after starting */
self::assertSame(expected: 'wait-after', actual: $started->getName());
self::assertGreaterThanOrEqual(minimum: 0.9, actual: $elapsed);
}
public function testStopContainerWithCustomTimeout(): void
{
/** @Given a running container */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'stop-timeout',
client: $this->client
);
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(hostname: 'stop-timeout')
);
$this->client->withDockerStopResponse(output: '');
/** @And the container is started */
$started = $container->run();
/** @When the container is stopped with a custom timeout */
$stopped = $started->stop(timeoutInWholeSeconds: 10);
/** @Then the stop should be successful */
self::assertTrue($stopped->isSuccessful());
}
public function testExecuteAfterStartedReturnsFailure(): void
{
/** @Given a running container */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'exec-fail',
client: $this->client
);
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'exec-fail'));
$this->client->withDockerExecuteResponse(output: 'command not found', isSuccessful: false);
/** @And the container is started */
$started = $container->run();
/** @When an invalid command is executed */
$execution = $started->executeAfterStarted(commands: ['invalid-command']);
/** @Then the result should indicate failure */
self::assertFalse($execution->isSuccessful());
self::assertSame(expected: 'command not found', actual: $execution->getOutput());
}
public function testRunIfNotExistsWithWaitBeforeRun(): void
{
/** @Given a condition that is immediately ready */
$condition = $this->createMock(ContainerReady::class);
$condition->expects(self::once())->method('isReady')->willReturn(true);
/** @And a container with a wait-before-run that does not exist */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'wait-new',
client: $this->client
)->withWaitBeforeRun(wait: ContainerWaitForDependency::untilReady(condition: $condition));
/** @And the Docker list returns empty */
$this->client->withDockerListResponse(output: '');
/** @And the Docker daemon returns valid run and inspect responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'wait-new'));
/** @When runIfNotExists is called */
$started = $container->runIfNotExists();
/** @Then the wait-before-run should have been evaluated and the container created */
self::assertSame(expected: 'wait-new', actual: $started->getName());
}
public function testExceptionWhenWaitBeforeRunTimesOut(): void
{
/** @Given a condition that never becomes ready */
$condition = $this->createMock(ContainerReady::class);
$condition->method('isReady')->willReturn(false);
/** @And a container with a wait-before-run that has a short timeout */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'timeout-wait',
client: $this->client
)->withWaitBeforeRun(
wait: ContainerWaitForDependency::untilReady(
condition: $condition,
timeoutInSeconds: 1,
pollIntervalInMicroseconds: 50_000
)
);
/** @Then a ContainerWaitTimeout exception should be thrown */
$this->expectException(ContainerWaitTimeout::class);
/** @When the container is started */
$container->run();
}
public function testRunContainerWithMultipleVolumeMappings(): void
{
/** @Given a container with multiple volume mappings */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'multi-vol',
client: $this->client
)
->withVolumeMapping(pathOnHost: '/data', pathOnContainer: '/app/data')
->withVolumeMapping(pathOnHost: '/config', pathOnContainer: '/app/config');
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'multi-vol'));
/** @When the container is started */
$started = $container->run();
/** @Then the container should be running */
self::assertSame(expected: 'multi-vol', actual: $started->getName());
}
public function testRunContainerWithMultipleEnvironmentVariables(): void
{
/** @Given a container with multiple environment variables */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'multi-env',
client: $this->client
)
->withEnvironmentVariable(key: 'DB_HOST', value: 'localhost')
->withEnvironmentVariable(key: 'DB_PORT', value: '5432')
->withEnvironmentVariable(key: 'DB_NAME', value: 'mydb');
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(
hostname: 'multi-env',
environment: ['DB_HOST=localhost', 'DB_PORT=5432', 'DB_NAME=mydb']
)
);
/** @When the container is started */
$started = $container->run();
/** @Then all environment variables should be accessible */
self::assertSame(
expected: 'localhost',
actual: $started->getEnvironmentVariables()->getValueBy(key: 'DB_HOST')
);
self::assertSame(expected: '5432', actual: $started->getEnvironmentVariables()->getValueBy(key: 'DB_PORT'));
self::assertSame(expected: 'mydb', actual: $started->getEnvironmentVariables()->getValueBy(key: 'DB_NAME'));
}
public function testRunContainerWithMultipleCopyInstructions(): void
{
/** @Given a container with multiple copy instructions */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'multi-copy',
client: $this->client
)
->copyToContainer(pathOnHost: '/host/sql', pathOnContainer: '/app/sql')
->copyToContainer(pathOnHost: '/host/config', pathOnContainer: '/app/config');
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'multi-copy'));
/** @When the container is started */
$started = $container->run();
/** @Then the container should be running (both docker cp calls were made) */
self::assertSame(expected: 'multi-copy', actual: $started->getName());
}
public function testExceptionWhenImageNameIsEmpty(): void
{
/** @Then an InvalidArgumentException should be thrown */
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Image name cannot be empty.');
/** @When creating a container with an empty image name */
GenericDockerContainer::from(image: '');
}
public function testExceptionWhenDockerReturnsEmptyContainerId(): void
{
/** @Given a container */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'empty-id',
client: $this->client
);
/** @And the Docker daemon returns an empty container ID */
$this->client->withDockerRunResponse(output: ' ');
/** @Then an InvalidArgumentException should be thrown */
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Container ID cannot be empty.');
/** @When the container is started */
$container->run();
}
public function testExceptionWhenDockerReturnsTooShortContainerId(): void
{
/** @Given a container */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'short-id',
client: $this->client
);
/** @And the Docker daemon returns a too-short container ID */
$this->client->withDockerRunResponse(output: 'abc123');
/** @Then an InvalidArgumentException should be thrown */
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Container ID <abc123> is too short. Minimum length is <12> characters.');
/** @When the container is started */
$container->run();
}
public function testRunCommandLineIncludesPortMapping(): void
{
/** @Given a container with a port mapping */
$container = TestableGenericDockerContainer::createWith(
image: 'nginx:latest',
name: 'port-cmd',
client: $this->client
)->withPortMapping(portOnHost: 8080, portOnContainer: 80);
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'port-cmd'));
/** @When the container is started */
$container->run();
/** @Then the executed docker run command should contain the port mapping argument */
$runCommand = $this->client->getExecutedCommandLines()[0];
self::assertStringContainsString(needle: '--publish 8080:80', haystack: $runCommand);
}
public function testRunCommandLineIncludesMultiplePortMappings(): void
{
/** @Given a container with multiple port mappings */
$container = TestableGenericDockerContainer::createWith(
image: 'nginx:latest',
name: 'multi-port-cmd',
client: $this->client
)
->withPortMapping(portOnHost: 8080, portOnContainer: 80)
->withPortMapping(portOnHost: 8443, portOnContainer: 443);
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(
inspectResult: InspectResponseFixture::build(
hostname: 'multi-port-cmd'
)
);
/** @When the container is started */
$container->run();
/** @Then the docker run command should contain both port mapping arguments */
$runCommand = $this->client->getExecutedCommandLines()[0];
self::assertStringContainsString(needle: '--publish 8080:80', haystack: $runCommand);
self::assertStringContainsString(needle: '--publish 8443:443', haystack: $runCommand);
}
public function testRunCommandLineIncludesVolumeMapping(): void
{
/** @Given a container with a volume mapping */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'vol-cmd',
client: $this->client
)->withVolumeMapping(pathOnHost: '/host/data', pathOnContainer: '/app/data');
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'vol-cmd'));
/** @When the container is started */
$container->run();
/** @Then the docker run command should contain the volume mapping argument */
$runCommand = $this->client->getExecutedCommandLines()[0];
self::assertStringContainsString(needle: '--volume /host/data:/app/data', haystack: $runCommand);
}
public function testRunCommandLineIncludesEnvironmentVariable(): void
{
/** @Given a container with an environment variable */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'env-cmd',
client: $this->client
)->withEnvironmentVariable(key: 'APP_ENV', value: 'production');
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'env-cmd'));
/** @When the container is started */
$container->run();
/** @Then the docker run command should contain the environment variable argument */
$runCommand = $this->client->getExecutedCommandLines()[0];
self::assertStringContainsString(needle: "--env APP_ENV='production'", haystack: $runCommand);
}
public function testRunCommandLineIncludesNetwork(): void
{
/** @Given a container with a network */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'net-cmd',
client: $this->client
)->withNetwork(name: 'my-network');
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'net-cmd'));
/** @When the container is started */
$container->run();
/** @Then the first command should be the network creation */
$networkCommand = $this->client->getExecutedCommandLines()[0];
self::assertStringContainsString(
needle: 'docker network create --label tiny-blocks.docker-container=true my-network',
haystack: $networkCommand
);
/** @And the docker run command should contain the network argument */
$runCommand = $this->client->getExecutedCommandLines()[1];
self::assertStringContainsString(needle: '--network=my-network', haystack: $runCommand);
}
public function testRunCommandLineIncludesAutoRemoveByDefault(): void
{
/** @Given a container with default configuration */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'rm-cmd',
client: $this->client
);
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'rm-cmd'));
/** @When the container is started */
$container->run();
/** @Then the docker run command should contain --rm */
$runCommand = $this->client->getExecutedCommandLines()[0];
self::assertStringContainsString(needle: '--rm', haystack: $runCommand);
}
public function testRunCommandLineExcludesAutoRemoveWhenDisabled(): void
{
/** @Given a container with auto-remove disabled */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'no-rm-cmd',
client: $this->client
)->withoutAutoRemove();
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'no-rm-cmd'));
/** @When the container is started */
$container->run();
/** @Then the docker run command should NOT contain --rm */
$runCommand = $this->client->getExecutedCommandLines()[0];
self::assertStringNotContainsString(needle: '--rm', haystack: $runCommand);
}
public function testRunCommandLineIncludesCommands(): void
{
/** @Given a container */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'args-cmd',
client: $this->client
);
/** @And the Docker daemon returns valid responses */
$this->client->withDockerRunResponse(output: InspectResponseFixture::containerId());
$this->client->withDockerInspectResponse(inspectResult: InspectResponseFixture::build(hostname: 'args-cmd'));
/** @When the container is started with commands */
$container->run(commands: ['-connectRetries=15', 'clean', 'migrate']);
/** @Then the docker run command should end with the commands */
$runCommand = $this->client->getExecutedCommandLines()[0];
self::assertStringContainsString(needle: '-connectRetries=15 clean migrate', haystack: $runCommand);
}
public function testCopyToContainerExecutesDockerCpCommand(): void
{
/** @Given a container with a copy instruction */
$container = TestableGenericDockerContainer::createWith(
image: 'alpine:latest',
name: 'cp-cmd',
client: $this->client