diff --git a/avocado/utils/disk.py b/avocado/utils/disk.py index 4034addcc2d9c5513f5c504b1820065abdef3f3e..2cb9b787ff489976383cca113240c06f9604709a 100644 --- a/avocado/utils/disk.py +++ b/avocado/utils/disk.py @@ -24,6 +24,7 @@ Disk utilities import os import json + from . import process @@ -39,6 +40,18 @@ def get_disk_blocksize(path): def get_disks(): + """ + Returns the physical "hard drives" available on this system + + This is a simple wrapper around `lsblk` and will return all the + top level physical (non-virtual) devices return by it. + + TODO: this is currently Linux specific. Support for other + platforms is desirable and may be implemented in the future. + + :returns: a list of paths to the physical disks on the system + :rtype: list of str + """ json_result = process.run('lsblk --json') json_data = json.loads(json_result.stdout_text) return ['/dev/%s' % str(disk['name']) for disk in json_data['blockdevices']] diff --git a/selftests/unit/test_utils_disk.py b/selftests/unit/test_utils_disk.py new file mode 100644 index 0000000000000000000000000000000000000000..919eb739a59bcf0d928881758cc26e12e02e50b6 --- /dev/null +++ b/selftests/unit/test_utils_disk.py @@ -0,0 +1,47 @@ +import unittest + +try: + from unittest import mock +except ImportError: + import mock + + +from avocado.utils import disk +from avocado.utils import process + + +class Disk(unittest.TestCase): + + LSBLK_OUTPUT = b''' +{ + "blockdevices": [ + {"name": "vda", "maj:min": "252:0", "rm": "0", "size": "6G", "ro": "0", "type": "disk", "mountpoint": null, + "children": [ + {"name": "vda1", "maj:min": "252:1", "rm": "0", "size": "1M", "ro": "0", "type": "part", "mountpoint": null}, + {"name": "vda2", "maj:min": "252:2", "rm": "0", "size": "1G", "ro": "0", "type": "part", "mountpoint": "/boot"}, + {"name": "vda3", "maj:min": "252:3", "rm": "0", "size": "615M", "ro": "0", "type": "part", "mountpoint": "[SWAP]"}, + {"name": "vda4", "maj:min": "252:4", "rm": "0", "size": "4.4G", "ro": "0", "type": "part", "mountpoint": "/"} + ] + } + ] +}''' + + def test_empty(self): + mock_result = process.CmdResult( + command='lsblk --json', + stdout=b'{"blockdevices": []}') + with mock.patch('avocado.utils.disk.process.run', + return_value=mock_result): + self.assertEqual(disk.get_disks(), []) + + def test_disks(self): + mock_result = process.CmdResult( + command='lsblk --json', + stdout=self.LSBLK_OUTPUT) + with mock.patch('avocado.utils.disk.process.run', + return_value=mock_result): + self.assertEqual(disk.get_disks(), ['/dev/vda']) + + +if __name__ == '__main__': + unittest.main()