提交 dfe3a7d5 编写于 作者: C Caio Carrara

utils.disk: Adds utility functions related to filesystems

This change introduces two new utility functions related with
filesystems. One function returns all filesystem types supported by the
running kernel and the other returns the filesystem type of a given
mount point.
Signed-off-by: NPraveen K Pandey <praveen@linux.vnet.ibm.com>
Signed-off-by: NCaio Carrara <ccarrara@redhat.com>
上级 d6c38093
......@@ -24,6 +24,7 @@ Disk utilities
import os
import json
import re
from . import process
......@@ -55,3 +56,34 @@ def get_disks():
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']]
def get_available_filesystems():
"""
Return a list of all available filesystem types
:returns: a list of filesystem types
:rtype: list of str
"""
filesystems = set()
with open('/proc/filesystems') as proc_fs:
for proc_fs_line in proc_fs.readlines():
filesystems.add(re.sub(r'(nodev)?\s*', '', proc_fs_line))
return list(filesystems)
def get_filesystem_type(mount_point='/'):
"""
Returns the type of the filesystem of mount point informed.
The default mount point considered when none is informed
is the root "/" mount point.
:param str mount_point: mount point to asses the filesystem type, default "/"
:returns: filesystem type
:rtype: str
"""
with open('/proc/mounts') as mounts:
for mount_line in mounts.readlines():
_, fs_file, fs_vfstype, _, _, _ = mount_line.split()
if fs_file == mount_point:
return fs_vfstype
import sys
import unittest
try:
......@@ -10,9 +11,7 @@ from avocado.utils import disk
from avocado.utils import process
class Disk(unittest.TestCase):
LSBLK_OUTPUT = b'''
LSBLK_OUTPUT = b'''
{
"blockdevices": [
{"name": "vda", "maj:min": "252:0", "rm": "0", "size": "6G", "ro": "0", "type": "disk", "mountpoint": null,
......@@ -26,6 +25,31 @@ class Disk(unittest.TestCase):
]
}'''
PROC_FILESYSTEMS = (
'nodev dax\n' +
'nodev bpf\n' +
'nodev pipefs\n' +
'nodev hugetlbfs\n' +
'nodev devpts\n' +
' ext3'
)
PROC_MOUNTS = (
"/dev/mapper/fedora-root / ext4 rw,seclabel,relatime 0 0\n" +
"/dev/mapper/fedora-home /home ext2 rw,seclabel,relatime 0 0\n" +
"sysfs /sys sysfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0\n" +
"proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0"
)
class Disk(unittest.TestCase):
@property
def builtin_open(self):
py_version = sys.version_info[0]
return 'builtins.open' if py_version == 3 else '__builtin__.open'
def test_empty(self):
mock_result = process.CmdResult(
command='lsblk --json',
......@@ -37,11 +61,28 @@ class Disk(unittest.TestCase):
def test_disks(self):
mock_result = process.CmdResult(
command='lsblk --json',
stdout=self.LSBLK_OUTPUT)
stdout=LSBLK_OUTPUT)
with mock.patch('avocado.utils.disk.process.run',
return_value=mock_result):
self.assertEqual(disk.get_disks(), ['/dev/vda'])
def test_get_filesystems(self):
expected_fs = ['dax', 'bpf', 'pipefs', 'hugetlbfs', 'devpts', 'ext3']
open_mocked = mock.mock_open(read_data=PROC_FILESYSTEMS)
with mock.patch(self.builtin_open, open_mocked):
self.assertEqual(sorted(expected_fs),
sorted(disk.get_available_filesystems()))
def test_get_filesystem_type_default_root(self):
open_mocked = mock.mock_open(read_data=PROC_MOUNTS)
with mock.patch(self.builtin_open, open_mocked):
self.assertEqual('ext4', disk.get_filesystem_type())
def test_get_filesystem_type(self):
open_mocked = mock.mock_open(read_data=PROC_MOUNTS)
with mock.patch(self.builtin_open, open_mocked):
self.assertEqual('ext2', disk.get_filesystem_type(mount_point='/home'))
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册