Skip to content

Commit

Permalink
Improving exception handling in directory_usage() util
Browse files Browse the repository at this point in the history
  • Loading branch information
pklimai committed Nov 30, 2016
1 parent d2e6459 commit 5c80bed
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
29 changes: 16 additions & 13 deletions lib/jnpr/junos/utils/fs.py
Expand Up @@ -280,22 +280,25 @@ def directory_usage(self, path=".", depth=0):

rsp = self._dev.rpc.get_directory_usage_information(path=path, depth=str(depth))

if rsp.findtext("directory/used-space") is None:
# Likely, no such directory
raise RpcError(rsp=rsp)

dirs = rsp.xpath("//directory")
result = {}

for directory in dirs:
for directory in rsp.findall(".//directory"):
dir_name = directory.findtext("directory-name").strip()
dir_size = directory.findtext("used-space").strip()
dir_blocks = int(directory.find("used-space").get("used-blocks").strip())
result[dir_name] = {
"size": dir_size,
"blocks": dir_blocks,
"bytes": dir_blocks * BLOCK_SIZE,
}
if dir_name is None:
raise RpcError(rsp=rsp)

used_space = directory.find('used-space')
if used_space is not None:
dir_size = used_space.text.strip()
dir_blocks = used_space.get('used-blocks')
if dir_blocks is not None:
dir_blocks = int(dir_blocks)
dir_bytes = dir_blocks * BLOCK_SIZE
result[dir_name] = {
"size": dir_size,
"blocks": dir_blocks,
"bytes": dir_bytes,
}

return result

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/utils/test_fs.py
Expand Up @@ -269,7 +269,7 @@ def test_storage_usage(self, mock_execute):
@patch('jnpr.junos.Device.execute')
def test_directory_usage(self, mock_execute):
mock_execute.side_effect = self._mock_manager
self.assertEqual(self.fs.directory_usage(path="/var/tmp"),
self.assertEqual(self.fs.directory_usage(path="/var/tmp", depth=1),
{'/var/tmp': {'blocks': 456076, 'bytes': 233510912, 'size': '223M'},
'/var/tmp/gres-tp': {'blocks': 68, 'bytes': 34816, 'size': '34K'},
'/var/tmp/install': {'blocks': 4, 'bytes': 2048, 'size': '2.0K'},
Expand Down

0 comments on commit 5c80bed

Please sign in to comment.