From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- testing/mozharness/test/test_base_diskutils.py | 89 ++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 testing/mozharness/test/test_base_diskutils.py (limited to 'testing/mozharness/test/test_base_diskutils.py') diff --git a/testing/mozharness/test/test_base_diskutils.py b/testing/mozharness/test/test_base_diskutils.py new file mode 100644 index 0000000000..9c5ac59083 --- /dev/null +++ b/testing/mozharness/test/test_base_diskutils.py @@ -0,0 +1,89 @@ +import unittest +from unittest import mock + +from mozharness.base.diskutils import DiskInfo, DiskSize, DiskutilsError, convert_to + + +class TestDiskutils(unittest.TestCase): + def test_convert_to(self): + # 0 is 0 regardless from_unit/to_unit + self.assertTrue(convert_to(size=0, from_unit="GB", to_unit="MB") == 0) + size = 524288 # 512 * 1024 + # converting from/to same unit + self.assertTrue(convert_to(size=size, from_unit="MB", to_unit="MB") == size) + + self.assertTrue(convert_to(size=size, from_unit="MB", to_unit="GB") == 512) + + self.assertRaises( + DiskutilsError, + lambda: convert_to(size="a string", from_unit="MB", to_unit="MB"), + ) + self.assertRaises( + DiskutilsError, lambda: convert_to(size=0, from_unit="foo", to_unit="MB") + ) + self.assertRaises( + DiskutilsError, lambda: convert_to(size=0, from_unit="MB", to_unit="foo") + ) + + +class TestDiskInfo(unittest.TestCase): + def testDiskinfo_to(self): + di = DiskInfo() + self.assertTrue(di.unit == "bytes") + self.assertTrue(di.free == 0) + self.assertTrue(di.used == 0) + self.assertTrue(di.total == 0) + # convert to GB + di._to("GB") + self.assertTrue(di.unit == "GB") + self.assertTrue(di.free == 0) + self.assertTrue(di.used == 0) + self.assertTrue(di.total == 0) + + +class MockStatvfs(object): + def __init__(self): + self.f_bsize = 0 + self.f_frsize = 0 + self.f_blocks = 0 + self.f_bfree = 0 + self.f_bavail = 0 + self.f_files = 0 + self.f_ffree = 0 + self.f_favail = 0 + self.f_flag = 0 + self.f_namemax = 0 + + +class TestDiskSpace(unittest.TestCase): + @mock.patch("mozharness.base.diskutils.os") + def testDiskSpacePosix(self, mock_os): + ds = MockStatvfs() + mock_os.statvfs.return_value = ds + di = DiskSize()._posix_size("/") + self.assertTrue(di.unit == "bytes") + self.assertTrue(di.free == 0) + self.assertTrue(di.used == 0) + self.assertTrue(di.total == 0) + + @mock.patch("mozharness.base.diskutils.ctypes") + def testDiskSpaceWindows(self, mock_ctypes): + mock_ctypes.windll.kernel32.GetDiskFreeSpaceExA.return_value = 0 + mock_ctypes.windll.kernel32.GetDiskFreeSpaceExW.return_value = 0 + di = DiskSize()._windows_size("/c/") + self.assertTrue(di.unit == "bytes") + self.assertTrue(di.free == 0) + self.assertTrue(di.used == 0) + self.assertTrue(di.total == 0) + + @mock.patch("mozharness.base.diskutils.os") + @mock.patch("mozharness.base.diskutils.ctypes") + def testUnspportedPlafrom(self, mock_ctypes, mock_os): + mock_os.statvfs.side_effect = AttributeError("") + self.assertRaises(AttributeError, lambda: DiskSize()._posix_size("/")) + mock_ctypes.windll.kernel32.GetDiskFreeSpaceExW.side_effect = AttributeError("") + mock_ctypes.windll.kernel32.GetDiskFreeSpaceExA.side_effect = AttributeError("") + self.assertRaises(AttributeError, lambda: DiskSize()._windows_size("/")) + self.assertRaises( + DiskutilsError, lambda: DiskSize().get_size(path="/", unit="GB") + ) -- cgit v1.2.3