| 1 | #!/usr/bin/env python3 |
| 2 | import sys |
| 3 | import unittest |
| 4 | from unittest.mock import patch |
| 5 | import vms |
| 6 | |
| 7 | |
| 8 | class ChannelTests(unittest.TestCase): |
| 9 | def request(self, source, state="running", channel=None): |
| 10 | channel = vms.GUEST_CHANNEL if channel is None else channel |
| 11 | xml = f'<domain id="9"><name>fixture</name><devices><channel type="unix"><source mode="bind" path="{source}"/><target type="virtio" name="{channel}"/></channel></devices></domain>' |
| 12 | with patch.object(sys, "argv", ["vms.py", "guest", '{"name":"fixture"}']), patch.object(vms, "virsh", side_effect=[xml, state]): |
| 13 | return vms.main() |
| 14 | |
| 15 | def test_only_the_running_domains_owned_channel_is_opened(self): |
| 16 | path = f"/run/libvirt/qemu/channel/9-fixture/{vms.GUEST_CHANNEL}" |
| 17 | self.assertEqual(self.request(path), {"path": path}) |
| 18 | for source, state, channel in [("/run/studio-host/host.sock", "running", None), |
| 19 | (path.replace("9-fixture", "8-other"), "running", None), |
| 20 | (path, "paused", None), (path, "running", "org.qemu.guest_agent.0")]: |
| 21 | with self.subTest(source=source, state=state, channel=channel), self.assertRaises(ValueError): |
| 22 | self.request(source, state, channel) |
| 23 | with self.assertRaises(ValueError): |
| 24 | vms.validate("guest", {"name": "fixture", "path": "/run/studio-host/host.sock"}) |
| 25 | with self.assertRaises(ValueError): |
| 26 | vms.validate("guest", {"name": "../other"}) |
| 27 | |
| 28 | |
| 29 | if __name__ == "__main__": |
| 30 | unittest.main() |