Offline project checks / check (push) Canceled after 0s
- Add platina-mesh CLI (scripts/phone-mesh.py) for status inspection, control, and JSON telemetry. - Add OpenRC service (scripts/phone-mesh.openrc) for managing easytier-core daemon. - Add NetworkManager dispatcher hook (scripts/phone-mesh-dispatcher.sh) to automatically start mesh when external default route is active and stop when disconnected. - Add default mesh configuration (scripts/easytier.conf) with node identity Platina (192.168.20.10). - Integrate mesh runtime into image overlay builder (scripts/image-runtime.py). - Add unit tests for mesh control and image runtime integration (142 tests passing).
137 lines
4.7 KiB
Python
137 lines
4.7 KiB
Python
"""Verify Platina Linux MeshPort / EasyTier auto-connect and control logic."""
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
spec = importlib.util.spec_from_file_location('mesh_setup', ROOT / 'scripts/phone-mesh.py')
|
|
mesh = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(mesh)
|
|
|
|
SAMPLE_NODE_JSON = {
|
|
"peer_id": 1697707457,
|
|
"ipv4_addr": "192.168.20.10/24",
|
|
"hostname": "Platina"
|
|
}
|
|
|
|
SAMPLE_PEER_JSON = [
|
|
{
|
|
"ipv4": "192.168.20.1",
|
|
"hostname": "DIG-US",
|
|
"cost": "p2p",
|
|
"tunnel": "tcp"
|
|
},
|
|
{
|
|
"ipv4": "192.168.20.2",
|
|
"hostname": "ORA-JP",
|
|
"cost": "p2p",
|
|
"tunnel": "tcp"
|
|
}
|
|
]
|
|
|
|
|
|
class MeshSetupTests(unittest.TestCase):
|
|
def test_is_service_running(self):
|
|
def mock_running(cmd, **kwargs):
|
|
return subprocess.CompletedProcess(cmd, 0, stdout='started\n', stderr='')
|
|
|
|
def mock_stopped(cmd, **kwargs):
|
|
return subprocess.CompletedProcess(cmd, 1, stdout='stopped\n', stderr='')
|
|
|
|
self.assertTrue(mesh.is_service_running(executor=mock_running))
|
|
self.assertFalse(mesh.is_service_running(executor=mock_stopped))
|
|
|
|
def test_get_mesh_status_stopped(self):
|
|
def mock_stopped(cmd, **kwargs):
|
|
return subprocess.CompletedProcess(cmd, 1)
|
|
|
|
status = mesh.get_mesh_status(executor=mock_stopped)
|
|
self.assertFalse(status['service_running'])
|
|
self.assertIsNone(status['node'])
|
|
self.assertEqual(status['peers'], [])
|
|
|
|
def test_get_mesh_status_running(self):
|
|
def mock_executor(cmd, **kwargs):
|
|
cmd_str = ' '.join(cmd)
|
|
if 'status' in cmd_str:
|
|
return subprocess.CompletedProcess(cmd, 0)
|
|
if 'node' in cmd_str:
|
|
return subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(SAMPLE_NODE_JSON), stderr='')
|
|
if 'peer' in cmd_str:
|
|
return subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(SAMPLE_PEER_JSON), stderr='')
|
|
return subprocess.CompletedProcess(cmd, 0)
|
|
|
|
with tempfile.NamedTemporaryFile() as tmp_conf:
|
|
status = mesh.get_mesh_status(executor=mock_executor, config_file=tmp_conf.name)
|
|
self.assertTrue(status['service_running'])
|
|
self.assertTrue(status['config_present'])
|
|
self.assertEqual(status['node']['hostname'], 'Platina')
|
|
self.assertEqual(status['node']['ipv4_addr'], '192.168.20.10/24')
|
|
self.assertEqual(len(status['peers']), 2)
|
|
|
|
def test_toggle_service(self):
|
|
recorded = []
|
|
|
|
def mock_executor(cmd, **kwargs):
|
|
recorded.append(cmd)
|
|
return subprocess.CompletedProcess(cmd, 0)
|
|
|
|
self.assertTrue(mesh.toggle_service('start', executor=mock_executor))
|
|
self.assertEqual(recorded[-1], ['rc-service', 'easytier', 'start'])
|
|
|
|
self.assertTrue(mesh.toggle_service('stop', executor=mock_executor))
|
|
self.assertEqual(recorded[-1], ['rc-service', 'easytier', 'stop'])
|
|
|
|
def test_auto_up_starts_service_when_stopped(self):
|
|
recorded = []
|
|
|
|
def mock_executor(cmd, **kwargs):
|
|
recorded.append(cmd)
|
|
return subprocess.CompletedProcess(cmd, 0)
|
|
|
|
with tempfile.NamedTemporaryFile() as tmp_conf:
|
|
res = mesh.auto_up(executor=mock_executor, checker=lambda **kw: False, config_file=tmp_conf.name)
|
|
self.assertTrue(res)
|
|
self.assertIn(['rc-service', 'easytier', 'start'], recorded)
|
|
|
|
def test_auto_up_skips_when_already_running(self):
|
|
recorded = []
|
|
|
|
def mock_executor(cmd, **kwargs):
|
|
recorded.append(cmd)
|
|
return subprocess.CompletedProcess(cmd, 0)
|
|
|
|
with tempfile.NamedTemporaryFile() as tmp_conf:
|
|
res = mesh.auto_up(executor=mock_executor, checker=lambda **kw: True, config_file=tmp_conf.name)
|
|
self.assertTrue(res)
|
|
self.assertEqual(recorded, [])
|
|
|
|
def test_auto_down_stops_service_when_running(self):
|
|
recorded = []
|
|
|
|
def mock_executor(cmd, **kwargs):
|
|
recorded.append(cmd)
|
|
return subprocess.CompletedProcess(cmd, 0)
|
|
|
|
res = mesh.auto_down(executor=mock_executor, checker=lambda **kw: True)
|
|
self.assertTrue(res)
|
|
self.assertIn(['rc-service', 'easytier', 'stop'], recorded)
|
|
|
|
def test_auto_down_skips_when_already_stopped(self):
|
|
recorded = []
|
|
|
|
def mock_executor(cmd, **kwargs):
|
|
recorded.append(cmd)
|
|
return subprocess.CompletedProcess(cmd, 0)
|
|
|
|
res = mesh.auto_down(executor=mock_executor, checker=lambda **kw: False)
|
|
self.assertTrue(res)
|
|
self.assertEqual(recorded, [])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|