From d691bade318bc183db1f6cf2cc6f85297fef29ca Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Wed, 9 Dec 2020 17:21:15 -0800 Subject: [PATCH] add attribute to add the board's ethernet interface to the jail.. --- bitelab/__init__.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/bitelab/__init__.py b/bitelab/__init__.py index 3798adc..7c0e5ad 100644 --- a/bitelab/__init__.py +++ b/bitelab/__init__.py @@ -73,6 +73,20 @@ def new_parse_socket_addr(domain, addr): tcp_server.parse_socket_addr = new_parse_socket_addr +class EtherIface(DefROAttribute): + defattrname = 'eiface' + + async def activate(self, brd): + cmd = ('ifconfig', self._value, 'vnet', brd.name,) + sub = await asyncio.create_subprocess_exec(*cmd, + stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) + + ret = await sub.wait() + + if ret: + raise RuntimeError('activate failed: %d' % ret) + class SerialConsole(DefROAttribute): defattrname = 'console' @@ -963,3 +977,30 @@ class TestAttrs(unittest.IsolatedAsyncioTestCase): wrap_subprocess_exec(cse, retcode=1) with self.assertRaises(RuntimeError): await sc.activate(brd) + + @patch('asyncio.create_subprocess_exec') + async def test_etheriface(self, cse): + eiface = 'aneiface' + + ei = EtherIface(eiface) + + self.assertEqual(ei.defattrname, 'eiface') + + self.assertEqual(eiface, await ei.getvalue()) + + with self.assertRaises(TypeError): + await ei.setvalue('randomdata') + + brd = BoardImpl('foo', 'bar', [ ei ]) + + wrap_subprocess_exec(cse, retcode=0) + + await ei.activate(brd) + + cse.assert_called_with('ifconfig', eiface, 'vnet', 'foo', + stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) + + wrap_subprocess_exec(cse, retcode=1) + with self.assertRaises(RuntimeError): + await ei.activate(brd)