89 lines
2.4 KiB
Python
89 lines
2.4 KiB
Python
import os
|
|
import sys
|
|
import unittest
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
# Make ca_core importable as the module root (so `import crypto...` works)
|
|
code_path = Path(__file__).parents[1] / "ca_core"
|
|
sys.path.insert(0, str(code_path))
|
|
|
|
from crypto.zenroom_client import ZenroomDockerClient, ZenroomError
|
|
|
|
|
|
def _docker_ok():
|
|
"""Return (ok, reason)."""
|
|
try:
|
|
p = subprocess.run(
|
|
["docker", "version"],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
text=True,
|
|
timeout=10,
|
|
check=False,
|
|
)
|
|
except FileNotFoundError:
|
|
return False, "docker CLI not found"
|
|
except Exception as e:
|
|
return False, f"docker check failed: {e}"
|
|
|
|
if p.returncode != 0:
|
|
out = (p.stdout or "").strip()
|
|
return False, f"docker not usable: {out}"
|
|
return True, ""
|
|
|
|
|
|
def _image_exists(image: str):
|
|
"""Return (ok, reason)."""
|
|
try:
|
|
p = subprocess.run(
|
|
["docker", "image", "inspect", image],
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.STDOUT,
|
|
text=True,
|
|
timeout=10,
|
|
check=False,
|
|
)
|
|
except Exception as e:
|
|
return False, f"docker image inspect failed: {e}"
|
|
|
|
if p.returncode != 0:
|
|
return False, f"docker image not found locally: {image}"
|
|
return True, ""
|
|
|
|
|
|
class TestZenroomDockerIntegration(unittest.TestCase):
|
|
"""Integration tests for ZenroomDockerClient.
|
|
|
|
Enable by setting:
|
|
ZENROOM_DOCKER_INTEGRATION=1
|
|
|
|
Image selection:
|
|
ZENROOM_IMAGE (default: zenroom)
|
|
"""
|
|
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
if not os.getenv("ZENROOM_DOCKER_INTEGRATION"):
|
|
raise unittest.SkipTest("Docker integration disabled (set ZENROOM_DOCKER_INTEGRATION=1)")
|
|
|
|
ok, reason = _docker_ok()
|
|
if not ok:
|
|
raise unittest.SkipTest(reason)
|
|
|
|
cls.image = os.getenv("ZENROOM_IMAGE", "zenroom")
|
|
|
|
ok, reason = _image_exists(cls.image)
|
|
if not ok:
|
|
raise unittest.SkipTest(reason + " (build it or set ZENROOM_IMAGE)")
|
|
|
|
def test_basic_execution(self):
|
|
client = ZenroomDockerClient(image=self.image)
|
|
out = client.run("print('hello')")
|
|
self.assertIn("hello", str(out))
|
|
|
|
def test_nonzero_exit_raises(self):
|
|
client = ZenroomDockerClient(image=self.image)
|
|
with self.assertRaises(ZenroomError):
|
|
client.run("THIS IS NOT VALID ZENCODE")
|