未验证 提交 1a0530e9 编写于 作者: C Caio Carrara

Merge remote-tracking branch 'clebergnu/get_disks_tests'

Signed-off-by: NCaio Carrara <ccarrara@redhat.com>
......@@ -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']]
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()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册