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)