Browse Source

handle that subprocess_exec takes/returns bytes...

main
John-Mark Gurney 5 years ago
parent
commit
f185c3a39e
1 changed files with 18 additions and 12 deletions
  1. +18
    -12
      bitelab/__init__.py

+ 18
- 12
bitelab/__init__.py View File

@@ -68,14 +68,15 @@ def new_parse_socket_addr(domain, addr):
tcp_server.parse_socket_addr = new_parse_socket_addr tcp_server.parse_socket_addr = new_parse_socket_addr


async def snmpget(host, oid, type): 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() res = (await p.communicate())[0].strip()


if type == 'bool': if type == 'bool':
if res == 'true':
if res == b'true':
return True return True
elif res == 'false':
elif res == b'false':
return False return False


raise RuntimeError('unknown results for bool: %s' % repr(res)) raise RuntimeError('unknown results for bool: %s' % repr(res))
@@ -557,7 +558,9 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase):
'arch': 'arm-armv7', 'clsname': 'cora-z7s', }) }) 'arch': 'arm-armv7', 'clsname': 'cora-z7s', }) })


@staticmethod @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 = Mock()
proc.communicate = AsyncMock() proc.communicate = AsyncMock()
proc.communicate.return_value = (stdout, stderr) proc.communicate.return_value = (stdout, stderr)
@@ -568,21 +571,22 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase):


@patch('asyncio.create_subprocess_exec') @patch('asyncio.create_subprocess_exec')
async def test_snmpwrapper(self, cse): 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') r = await snmpget('somehost', 'snmpoid', 'bool')


self.assertEqual(r, False) 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') r = await snmpget('somehost', 'snmpoid', 'bool')


self.assertEqual(r, True) self.assertEqual(r, True)


# that a bogus return value # that a bogus return value
self._wrap_subprocess_exec(cse, 'bogus\n')
self._wrap_subprocess_exec(cse, b'bogus\n')


# raises an error # raises an error
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
@@ -606,7 +610,7 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase):
sg.return_value = False sg.return_value = False


# that when the setup script will fail # 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 # that reserving the board
res = await self.client.post('/board/cora-1/reserve', 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) self.assertEqual(res.status_code, HTTP_500_INTERNAL_SERVER_ERROR)


# and returns the correct data # 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', board=Board(name='cora-1',
brdclass='cora-z7s', brdclass='cora-z7s',
reserved=False, reserved=False,
@@ -626,10 +630,12 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase):


# and that it called the start script # and that it called the start script
cse.assert_called_with(self.settings.setup_script, 'reserve', 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 # 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 # that reserving the board
res = await self.client.post('/board/cora-1/reserve', res = await self.client.post('/board/cora-1/reserve',


Loading…
Cancel
Save