107 lines
5.1 KiB
Python
107 lines
5.1 KiB
Python
import io
|
|
import json
|
|
from contextlib import redirect_stdout
|
|
from pathlib import Path
|
|
import tempfile
|
|
from types import SimpleNamespace
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
import build_artifact
|
|
from test_bootstrap import RUNNER, container_info
|
|
|
|
|
|
def nas_capabilities():
|
|
return {"Architecture": "x86_64", "CPUSet": True, "CPUShares": True,
|
|
"CgroupVersion": "1", "CpuCfsPeriod": False, "CpuCfsQuota": False,
|
|
"KernelVersion": "4.4.302+", "MemTotal": 16743616512,
|
|
"MemoryLimit": True, "NCPU": 8, "OSType": "linux",
|
|
"PidsLimit": False, "ServerVersion": "24.0.2", "SwapLimit": True}
|
|
|
|
|
|
def v4_container():
|
|
info = container_info()
|
|
info["State"]["Pid"] = 1234
|
|
info["HostConfig"].update({"NanoCpus": 0, "CpuShares": 64, "CpusetCpus": "7",
|
|
"PidsLimit": None, "MemorySwap": 512 * 1024 ** 2})
|
|
return info
|
|
|
|
|
|
class SynologyV4Tests(unittest.TestCase):
|
|
def query(self, info, affinity=None):
|
|
with mock.patch.object(RUNNER.subprocess, "run", return_value=SimpleNamespace(stdout=json.dumps(info))):
|
|
with mock.patch.object(RUNNER.os, "sched_getaffinity", create=True, return_value=set(range(8)) if affinity is None else affinity):
|
|
with redirect_stdout(io.StringIO()):
|
|
return RUNNER.gitea_ci_host_capabilities(RUNNER.GITEA_CI_V4_ENTRIES)
|
|
|
|
def test_actual_nas_capability_matrix_passes_without_cfs_or_pids(self):
|
|
report = self.query(nas_capabilities())
|
|
self.assertFalse(report["PidsLimit"])
|
|
self.assertFalse(report["CpuCfsQuota"])
|
|
self.assertTrue(report["CPUSet"])
|
|
|
|
def test_required_supported_controls_are_not_optional(self):
|
|
for key in ("CPUSet", "CPUShares", "MemoryLimit", "SwapLimit"):
|
|
info = nas_capabilities()
|
|
info[key] = False
|
|
with self.subTest(key=key), self.assertRaisesRegex(RUNNER.DeployError, key):
|
|
self.query(info)
|
|
|
|
def test_cpu7_must_exist_and_be_available(self):
|
|
info = nas_capabilities()
|
|
info["NCPU"] = 4
|
|
with self.assertRaisesRegex(RUNNER.DeployError, "eight logical"):
|
|
self.query(info)
|
|
with self.assertRaisesRegex(RUNNER.DeployError, "CPU7 affinity"):
|
|
self.query(nas_capabilities(), affinity={0, 1})
|
|
|
|
def test_exact_runtime_cpu_affinity_memory_swap_and_no_pids_contract(self):
|
|
with mock.patch.object(RUNNER.os, "sched_getaffinity", create=True, return_value={7}) as affinity:
|
|
RUNNER.accept_gitea_ci_container(v4_container(), RUNNER.GITEA_CI_V4_ENTRIES)
|
|
affinity.assert_called_once_with(1234)
|
|
|
|
def test_broad_affinity_unbounded_swap_or_reintroduced_unsupported_limit_rejected(self):
|
|
for field, value in (("CpusetCpus", "0-7"), ("MemorySwap", -1), ("Memory", 0),
|
|
("NanoCpus", 500000000), ("PidsLimit", 128), ("CpuShares", 0)):
|
|
info = v4_container()
|
|
info["HostConfig"][field] = value
|
|
with mock.patch.object(RUNNER.os, "sched_getaffinity", create=True, return_value={7}):
|
|
with self.subTest(field=field), self.assertRaises(RUNNER.DeployError):
|
|
RUNNER.accept_gitea_ci_container(info, RUNNER.GITEA_CI_V4_ENTRIES)
|
|
with mock.patch.object(RUNNER.os, "sched_getaffinity", create=True, return_value={0, 7}):
|
|
with self.assertRaisesRegex(RUNNER.DeployError, "CPU7 affinity"):
|
|
RUNNER.accept_gitea_ci_container(v4_container(), RUNNER.GITEA_CI_V4_ENTRIES)
|
|
|
|
def test_affinity_requires_a_positive_numeric_runtime_pid(self):
|
|
for pid in (None, 0, -1, True, "1234"):
|
|
with self.subTest(pid=pid), self.assertRaisesRegex(RUNNER.DeployError, "process identity"):
|
|
RUNNER.gitea_ci_check_affinity(pid, exact=True)
|
|
|
|
def test_v4_bundle_pins_same_single_cpu_for_daemon_and_job(self):
|
|
patch = "gitea-ci-bootstrap-v4-test-001"
|
|
data = build_artifact.bundle(patch)
|
|
self.assertEqual(data, build_artifact.bundle(patch))
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
artifact = root / "test.tgz"
|
|
artifact.write_bytes(data)
|
|
target = root / "unpacked"
|
|
target.mkdir()
|
|
_, entries, payload = RUNNER.load_artifact(artifact, target)
|
|
self.assertEqual(tuple(entries), RUNNER.GITEA_CI_V4_ENTRIES)
|
|
compose = (payload / "docker-compose.gitea-ci.yml").read_text()
|
|
runner = (payload / "runner.yaml").read_text()
|
|
self.assertIn('cpuset: "7"', compose)
|
|
self.assertIn('memswap_limit: 512m', compose)
|
|
self.assertNotIn('pids_limit:', compose)
|
|
self.assertIn('--cpuset-cpus=7', runner)
|
|
self.assertIn('--memory=2g --memory-swap=2g', runner)
|
|
self.assertNotIn('--pids-limit', runner)
|
|
(payload / "runner.yaml").write_text(runner.replace('--cpuset-cpus=7', '--cpuset-cpus=0-7'))
|
|
with self.assertRaisesRegex(RUNNER.DeployError, "immutable configuration"):
|
|
RUNNER.validate_gitea_ci_payload(payload, entries, patch)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|