summaryrefslogtreecommitdiffstats
path: root/testing/mozharness/test/test_base_diskutils.py
blob: 79d36692fa1b3a43e478c447a68fced6b39851a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import mock
import unittest
from mozharness.base.diskutils import convert_to, DiskutilsError, DiskSize, DiskInfo


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'))