From f185c3a39eadc574bf385291e675318ffb3360e1 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Tue, 24 Nov 2020 18:57:06 -0800 Subject: [PATCH] handle that subprocess_exec takes/returns bytes... --- bitelab/__init__.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/bitelab/__init__.py b/bitelab/__init__.py index 572819b..5440d67 100644 --- a/bitelab/__init__.py +++ b/bitelab/__init__.py @@ -68,14 +68,15 @@ def new_parse_socket_addr(domain, addr): tcp_server.parse_socket_addr = new_parse_socket_addr async def snmpget(host, oid, type): - p = await asyncio.create_subprocess_exec('snmpget', '-Oqv', host, oid) + p = await asyncio.create_subprocess_exec('snmpget', '-Oqv', host, oid, + stdout=subprocess.PIPE) res = (await p.communicate())[0].strip() if type == 'bool': - if res == 'true': + if res == b'true': return True - elif res == 'false': + elif res == b'false': return False raise RuntimeError('unknown results for bool: %s' % repr(res)) @@ -557,7 +558,9 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase): 'arch': 'arm-armv7', 'clsname': 'cora-z7s', }) }) @staticmethod - def _wrap_subprocess_exec(mockobj, stdout='', stderr='', retcode=0): + def _wrap_subprocess_exec(mockobj, stdout=b'', stderr=b'', retcode=0): + assert isinstance(stdout, bytes) + assert isinstance(stderr, bytes) proc = Mock() proc.communicate = AsyncMock() proc.communicate.return_value = (stdout, stderr) @@ -568,21 +571,22 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase): @patch('asyncio.create_subprocess_exec') async def test_snmpwrapper(self, cse): - self._wrap_subprocess_exec(cse, 'false\n') + self._wrap_subprocess_exec(cse, b'false\n') r = await snmpget('somehost', 'snmpoid', 'bool') self.assertEqual(r, False) - cse.assert_called_with('snmpget', '-Oqv', 'somehost', 'snmpoid') + cse.assert_called_with('snmpget', '-Oqv', 'somehost', + 'snmpoid', stdout=subprocess.PIPE) - self._wrap_subprocess_exec(cse, 'true\n') + self._wrap_subprocess_exec(cse, b'true\n') r = await snmpget('somehost', 'snmpoid', 'bool') self.assertEqual(r, True) # that a bogus return value - self._wrap_subprocess_exec(cse, 'bogus\n') + self._wrap_subprocess_exec(cse, b'bogus\n') # raises an error with self.assertRaises(RuntimeError): @@ -606,7 +610,7 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase): sg.return_value = False # that when the setup script will fail - self._wrap_subprocess_exec(cse, stderr='error', retcode=1) + self._wrap_subprocess_exec(cse, stderr=b'error', retcode=1) # that reserving the board res = await self.client.post('/board/cora-1/reserve', @@ -616,7 +620,7 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase): self.assertEqual(res.status_code, HTTP_500_INTERNAL_SERVER_ERROR) # and returns the correct data - info = Error(error='Failed to init board, ret: 1, stderr: \'error\'', + info = Error(error='Failed to init board, ret: 1, stderr: b\'error\'', board=Board(name='cora-1', brdclass='cora-z7s', reserved=False, @@ -626,10 +630,12 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase): # and that it called the start script cse.assert_called_with(self.settings.setup_script, 'reserve', - 'cora-1', 'foo', stdout=subprocess.PIPE, stderr=subprocess.PIPE) + 'cora-1', 'foo', stdout=subprocess.PIPE, + stderr=subprocess.PIPE) # that when the setup script returns - self._wrap_subprocess_exec(cse, json.dumps(dict(ip='192.0.2.10'))) + self._wrap_subprocess_exec(cse, + json.dumps(dict(ip='192.0.2.10')).encode('utf-8')) # that reserving the board res = await self.client.post('/board/cora-1/reserve',