112 lines
5.5 KiB
Python
112 lines
5.5 KiB
Python
import copy
|
|
import hashlib
|
|
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 capabilities():
|
|
return {"OSType": "linux", "Architecture": "x86_64", "NCPU": 8,
|
|
"MemoryLimit": True, "SwapLimit": False, "PidsLimit": True,
|
|
"CPUShares": True, "CPUSet": True, "CpuCfsQuota": False,
|
|
"CpuCfsPeriod": False, "KernelVersion": "fixture-kernel",
|
|
"RegistryConfig": {"do-not-log": "fixture-secret"},
|
|
"HttpProxy": "fixture-private-proxy"}
|
|
|
|
|
|
class SynologyV3Tests(unittest.TestCase):
|
|
def query(self, info, entries=RUNNER.GITEA_CI_V3_ENTRIES):
|
|
result = SimpleNamespace(stdout=json.dumps(info))
|
|
output = io.StringIO()
|
|
with mock.patch.object(RUNNER.subprocess, "run", return_value=result) as run:
|
|
with redirect_stdout(output):
|
|
report = RUNNER.gitea_ci_host_capabilities(entries)
|
|
run.assert_called_once_with([str(RUNNER.DOCKER), "info", "--format", "{{json .}}"],
|
|
capture_output=True, text=True, check=True, timeout=30)
|
|
return report, output.getvalue()
|
|
|
|
def test_v3_supports_no_cfs_no_swap_without_claiming_cpu_quota(self):
|
|
report, output = self.query(capabilities())
|
|
self.assertFalse(report["CpuCfsQuota"])
|
|
self.assertTrue(report["CPUShares"])
|
|
self.assertNotIn("fixture-secret", output)
|
|
self.assertNotIn("fixture-private-proxy", output)
|
|
|
|
def test_missing_memory_pids_or_shares_is_rejected_before_container_creation(self):
|
|
for key in ("MemoryLimit", "PidsLimit", "CPUShares"):
|
|
for value in (False, None, "true"):
|
|
info = capabilities()
|
|
info[key] = value
|
|
with self.subTest(key=key, value=value), self.assertRaisesRegex(RUNNER.DeployError, key):
|
|
self.query(info)
|
|
|
|
def test_cfs_profile_remains_rejected_on_incompatible_kernel(self):
|
|
for entries in (RUNNER.GITEA_CI_ENTRIES, RUNNER.GITEA_CI_V2_ENTRIES):
|
|
with self.assertRaisesRegex(RUNNER.DeployError, "CpuCfsPeriod,CpuCfsQuota,SwapLimit"):
|
|
self.query(capabilities(), entries)
|
|
|
|
def test_wrong_architecture_is_rejected(self):
|
|
info = capabilities()
|
|
info["Architecture"] = "aarch64"
|
|
with self.assertRaisesRegex(RUNNER.DeployError, "Linux amd64"):
|
|
self.query(info)
|
|
|
|
def test_runtime_accepts_only_exact_cpu_shares_profile(self):
|
|
info = container_info()
|
|
info["HostConfig"].update({"NanoCpus": 0, "CpuShares": 64, "CpuQuota": 0, "CpuPeriod": 0})
|
|
RUNNER.accept_gitea_ci_container(info, RUNNER.GITEA_CI_V3_ENTRIES)
|
|
for field, value in (("CpuShares", 0), ("CpuShares", 1024), ("NanoCpus", 500000000),
|
|
("CpuQuota", 50000), ("Memory", 0), ("PidsLimit", 0)):
|
|
candidate = copy.deepcopy(info)
|
|
candidate["HostConfig"][field] = value
|
|
with self.subTest(field=field), self.assertRaisesRegex(RUNNER.DeployError, "resource limit"):
|
|
RUNNER.accept_gitea_ci_container(candidate, RUNNER.GITEA_CI_V3_ENTRIES)
|
|
with self.assertRaisesRegex(RUNNER.DeployError, "resource limit"):
|
|
RUNNER.accept_gitea_ci_container(info, RUNNER.GITEA_CI_V2_ENTRIES)
|
|
|
|
def test_v3_artifact_pins_both_daemon_and_job_configuration(self):
|
|
patch = "gitea-ci-bootstrap-v3-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_V3_ENTRIES)
|
|
self.assertEqual(RUNNER.component_services("gitea-ci", entries), ("build-runner",))
|
|
compose = (payload / "docker-compose.gitea-ci.yml").read_text()
|
|
runner = (payload / "runner.yaml").read_text()
|
|
self.assertIn("cpu_shares: 64", compose)
|
|
self.assertNotIn("cpus:", compose)
|
|
self.assertIn("--cpu-shares=128", runner)
|
|
self.assertNotIn("--cpus=", runner)
|
|
self.assertNotIn("--memory-swap", runner)
|
|
self.assertIn("--memory=2g --pids-limit=256", runner)
|
|
(payload / "runner.yaml").write_text(runner.replace("--cpu-shares=128", "--cpu-shares=1024"))
|
|
with self.assertRaisesRegex(RUNNER.DeployError, "immutable configuration"):
|
|
RUNNER.validate_gitea_ci_payload(payload, entries, patch)
|
|
|
|
def test_historical_artifact_bytes_remain_reproducible(self):
|
|
for patch, sha in (
|
|
("gitea-ci-bootstrap-v1-20260929-001", "ba942839c5815d09ac8f3c8dc23acdcf53ad9ef098a74c26b14c8b81afcf6a20"),
|
|
("gitea-ci-bootstrap-v2-20260929-002", "f9381e1ca59372f75277b6a6207ac9e35acaf29fd8ffa5dcf6afafa2daf80630"),
|
|
("gitea-ci-bootstrap-v3-20260929-003", "9df6c741e2432c0cac9fe2f2c7d90348a61d75b28098146018467050ef21ab9a"),
|
|
("gitea-ci-bootstrap-v4-20260929-004", "866771bd94c82bee6d8143574fd7cc6292b0a516a2c4f082a4565bc13ccf2fb8"),
|
|
):
|
|
self.assertEqual(hashlib.sha256(build_artifact.bundle(patch)).hexdigest(), sha)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|