Browse Source

sort attributes... properly post for reserve, and add release support...

genericize error handling for the CLI...
main
John-Mark Gurney 4 years ago
parent
commit
ccfc232d67
1 changed files with 52 additions and 13 deletions
  1. +52
    -13
      bitelab/__init__.py

+ 52
- 13
bitelab/__init__.py View File

@@ -405,6 +405,14 @@ def getApp():
# uvicorn can't call the above function, while hypercorn can
#app = getApp()

def check_res_code(res):
if res.status_code == HTTP_401_UNAUTHORIZED:
print('Invalid authentication credentials.')
sys.exit(1)
elif res.status_code != HTTP_200_OK:
print('Got status: %d, json: %s' % (res.status_code, res.json()))
sys.exit(1)

async def real_main():
baseurl = os.environ['BITELAB_URL']
authkey = os.environ['BITELAB_AUTH']
@@ -415,25 +423,26 @@ async def real_main():
if sys.argv[1] == 'list':
res = await client.get('board/classes', auth=BiteAuth(authkey))

if res.status_code == HTTP_401_UNAUTHORIZED:
print('Invalid authentication credentials.')
sys.exit(1)
check_res_code(res)

print('Classes:')
for i in res.json():
print('\t' + i)

res.close()
elif sys.argv[1] == 'reserve':
res = await client.get('board/%s/reserve' %
urllib.parse.quote(sys.argv[2], safe=''),
elif sys.argv[1] in ('reserve', 'release'):
res = await client.post('board/%s/%s' %
(urllib.parse.quote(sys.argv[2], safe=''),
sys.argv[1]),
auth=BiteAuth(authkey))

check_res_code(res)

brd = Board.parse_obj(res.json())
print('Name:\t%s' % brd.name)
print('Class:\t%s' % brd.brdclass)
print('Attributes:')
for i in brd.attrs:
for i in sorted(brd.attrs):
print('\t%s\t%s' % (i, brd.attrs[i]))
finally:
await client.aclose()
@@ -782,6 +791,9 @@ class TestClient(unittest.TestCase):
self.acaclose = self.ac.return_value.aclose = AsyncMock()
self.acgr = self.acg.return_value = Mock()

self.acp = self.ac.return_value.post = AsyncMock()
self.acpr = self.acp.return_value = Mock()

def runMain(self):
try:
stdout = StringIO()
@@ -809,6 +821,7 @@ class TestClient(unittest.TestCase):
'''

self.assertEqual(ret, 1)
# XXX -- really should go to stderr
self.assertEqual(stdout, output)

ac.assert_called_with(base_url='http://someserver/')
@@ -843,13 +856,13 @@ class TestClient(unittest.TestCase):
@patch.dict(sys.__dict__, dict(argv=[ '', 'reserve', 'cora-z7s' ]))
def test_reserve(self):
ac = self.ac
acg = self.acg
acg.return_value.status_code = HTTP_200_OK
acg.return_value.json.return_value = Board(name='cora-1',
acp = self.acp
acp.return_value.status_code = HTTP_200_OK
acp.return_value.json.return_value = Board(name='cora-1',
brdclass='cora-z7s', reserved=True,
attrs={
'power': False,
'ip': '172.20.20.5',
'power': False,
}).dict()

ret, stdout = self.runMain()
@@ -857,8 +870,34 @@ class TestClient(unittest.TestCase):
output = '''Name:\tcora-1
Class:\tcora-z7s
Attributes:
\tpower\tFalse
\tip\t172.20.20.5
\tpower\tFalse
'''

self.assertEqual(ret, 0)
self.assertEqual(stdout, output)

ac.assert_called_with(base_url='http://someserver/')

acp.assert_called_with('board/cora-z7s/reserve', auth=BiteAuth('thisisanapikey'))

@patch.dict(sys.__dict__, dict(argv=[ '', 'release', 'cora-z7s' ]))
def test_release(self):
ac = self.ac
acp = self.acp
acp.return_value.status_code = HTTP_200_OK
acp.return_value.json.return_value = Board(name='cora-1',
brdclass='cora-z7s', reserved=False,
attrs={
'power': False,
}).dict()

ret, stdout = self.runMain()

output = '''Name:\tcora-1
Class:\tcora-z7s
Attributes:
\tpower\tFalse
'''

self.assertEqual(ret, 0)
@@ -866,4 +905,4 @@ Attributes:

ac.assert_called_with(base_url='http://someserver/')

acg.assert_called_with('board/cora-z7s/reserve', auth=BiteAuth('thisisanapikey'))
acp.assert_called_with('board/cora-z7s/release', auth=BiteAuth('thisisanapikey'))

Loading…
Cancel
Save