73 lines
2.7 KiB
Python
73 lines
2.7 KiB
Python
import os
|
|
import tempfile
|
|
import unittest
|
|
|
|
from poc import lab
|
|
|
|
NT = os.name == "nt"
|
|
|
|
|
|
class HostPathTest(unittest.TestCase):
|
|
def test_windows_branch_replaces_backslashes(self):
|
|
self.assertEqual(lab.host_path("C:\\Users\\x\\out", nt=True), "C:/Users/x/out")
|
|
|
|
def test_windows_branch_leaves_forward_slashes(self):
|
|
self.assertEqual(lab.host_path("C:/Users/x/out", nt=True), "C:/Users/x/out")
|
|
|
|
def test_posix_branch_is_identity(self):
|
|
self.assertEqual(lab.host_path("/srv/poc/out", nt=False), "/srv/poc/out")
|
|
|
|
def test_default_matches_platform(self):
|
|
p = os.path.join(tempfile.gettempdir(), "out")
|
|
got = lab.host_path(p)
|
|
self.assertNotIn("\\", got)
|
|
if not NT:
|
|
self.assertEqual(got, p)
|
|
|
|
|
|
class BuildRunArgsTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.root = os.path.join(tempfile.gettempdir(), "p2s-repo")
|
|
self.args = lab.build_run_args(self.root, "php:8.4.26-apache", "p2s-lab", 18110, nt=NT)
|
|
|
|
def test_three_volume_mounts(self):
|
|
vols = [self.args[i + 1] for i, a in enumerate(self.args) if a == "-v"]
|
|
self.assertEqual(len(vols), 3)
|
|
for vol in vols:
|
|
self.assertTrue(vol.endswith(":ro") or vol.endswith(":/out"))
|
|
|
|
def test_destinations(self):
|
|
vols = [self.args[i + 1] for i, a in enumerate(self.args) if a == "-v"]
|
|
joined = " ".join(vols)
|
|
self.assertIn(":/var/www/html/index.php:ro", joined)
|
|
self.assertIn(":/etc/apache2/conf-enabled/zz-p2s.conf:ro", joined)
|
|
self.assertTrue(joined.endswith(":/out"))
|
|
|
|
def test_volume_separators_are_docker_safe(self):
|
|
vols = [self.args[i + 1] for i, a in enumerate(self.args) if a == "-v"]
|
|
for vol in vols:
|
|
self.assertNotIn("\\", vol)
|
|
|
|
def test_image_precedes_entrypoint(self):
|
|
idx = self.args.index("php:8.4.26-apache")
|
|
self.assertEqual(self.args[idx + 1:idx + 3], ["/bin/sh", "-c"])
|
|
|
|
def test_port_publish_binds_loopback(self):
|
|
self.assertIn("127.0.0.1:18110:80", self.args)
|
|
|
|
def test_no_network_flag_by_default(self):
|
|
self.assertNotIn("--network", self.args)
|
|
|
|
def test_network_flag_when_requested(self):
|
|
args = lab.build_run_args(self.root, "img", "c", 1, net="p2s", nt=NT)
|
|
self.assertEqual(args[args.index("--network") + 1], "p2s")
|
|
|
|
def test_init_script_has_no_crlf(self):
|
|
self.assertNotIn("\r", lab.INIT)
|
|
self.assertIn("exec apache2-foreground", lab.INIT)
|
|
self.assertIn("php.ini-production", lab.INIT)
|
|
|
|
def test_no_shell_interpolation(self):
|
|
self.assertEqual(self.args.count("-c"), 1)
|
|
self.assertEqual(len([a for a in self.args if "$" in a]), 0)
|