xref: /aosp_15_r20/external/autotest/client/bin/partition_unittest.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1#!/usr/bin/python3
2
3"""Tests for autotest_lib.client.bin.partition."""
4
5from __future__ import absolute_import
6from __future__ import division
7from __future__ import print_function
8
9__author__ = '[email protected] (Gregory P. Smith)'
10
11import os, sys, unittest
12from six import StringIO
13from six.moves import reload_module as reload
14import common
15from autotest_lib.client.common_lib.test_utils import mock
16from autotest_lib.client.bin import partition
17from six.moves import range
18
19
20class FsOptions_common(object):
21    def test_constructor(self):
22        self.assertRaises(ValueError, partition.FsOptions, '', '', '', '')
23        self.assertRaises(ValueError, partition.FsOptions, 'ext2', '', '', '')
24        obj = partition.FsOptions('ext2', 'ext2_vanilla', '', '')
25        obj = partition.FsOptions(fstype='ext2', fs_tag='ext2_vanilla')
26        obj = partition.FsOptions('fs', 'shortie', 'mkfs opts', 'mount opts')
27        self.assertEqual('fs', obj.fstype)
28        self.assertEqual('shortie', obj.fs_tag)
29        self.assertEqual('mkfs opts', obj.mkfs_flags)
30        self.assertEqual('mount opts', obj.mount_options)
31
32
33    def test__str__(self):
34        str_obj = str(partition.FsOptions('abc', 'def', 'ghi', 'jkl'))
35        self.assert_('FsOptions' in str_obj)
36        self.assert_('abc' in str_obj)
37        self.assert_('def' in str_obj)
38        self.assert_('ghi' in str_obj)
39        self.assert_('jkl' in str_obj)
40
41
42# Test data used in GetPartitionTest below.
43
44SAMPLE_SWAPS = """
45Filename                                Type            Size    Used    Priority
46/dev/hdc2                               partition       9863868 0       -1
47"""
48
49SAMPLE_PARTITIONS_HDC_ONLY = """
50major minor  #blocks  name
51
52   8    16  390711384 hdc
53   8    18     530113 hdc2
54   8    19  390178687 hdc3
55"""
56
57# yes I manually added a hda1 line to this output to test parsing when the Boot
58# flag exists.
59SAMPLE_FDISK = "/sbin/fdisk -l -u '/dev/hdc'"
60SAMPLE_FDISK_OUTPUT = """
61Disk /dev/hdc: 400.0 GB, 400088457216 bytes
62255 heads, 63 sectors/track, 48641 cylinders, total 781422768 sectors
63Units = sectors of 1 * 512 = 512 bytes
64
65   Device Boot      Start         End      Blocks   Id  System
66/dev/hdc2              63     1060289      530113+  82  Linux swap / Solaris
67/dev/hdc3         1060290   781417664   390178687+  83  Linux
68/dev/hdc4   *    faketest    FAKETEST      232323+  83  Linux
69"""
70
71
72class get_partition_list_common(object):
73    def setUp(self):
74        self.god = mock.mock_god()
75        self.god.stub_function(os, 'popen')
76
77
78    def tearDown(self):
79        self.god.unstub_all()
80
81
82    def test_is_linux_fs_type(self):
83        for unused in range(4):
84            os.popen.expect_call(SAMPLE_FDISK).and_return(
85                    StringIO(SAMPLE_FDISK_OUTPUT))
86        self.assertFalse(partition.is_linux_fs_type('/dev/hdc1'))
87        self.assertFalse(partition.is_linux_fs_type('/dev/hdc2'))
88        self.assertTrue(partition.is_linux_fs_type('/dev/hdc3'))
89        self.assertTrue(partition.is_linux_fs_type('/dev/hdc4'))
90        self.god.check_playback()
91
92
93    def test_get_partition_list(self):
94        def fake_open(filename):
95            """Fake open() to pass to get_partition_list as __open."""
96            if filename == '/proc/swaps':
97                return StringIO(SAMPLE_SWAPS)
98            elif filename == '/proc/partitions':
99                return StringIO(SAMPLE_PARTITIONS_HDC_ONLY)
100            else:
101                self.assertFalse("Unexpected open() call: %s" % filename)
102
103        job = 'FakeJob'
104
105        # Test a filter func that denies all.
106        parts = partition.get_partition_list(job, filter_func=lambda x: False,
107                                             open_func=fake_open)
108        self.assertEqual([], parts)
109        self.god.check_playback()
110
111        # Test normal operation.
112        self.god.stub_function(partition, 'partition')
113        partition.partition.expect_call(job, '/dev/hdc3').and_return('3')
114        parts = partition.get_partition_list(job, open_func=fake_open)
115        self.assertEqual(['3'], parts)
116        self.god.check_playback()
117
118        # Test exclude_swap can be disabled.
119        partition.partition.expect_call(job, '/dev/hdc2').and_return('2')
120        partition.partition.expect_call(job, '/dev/hdc3').and_return('3')
121        parts = partition.get_partition_list(job, exclude_swap=False,
122                                             open_func=fake_open)
123        self.assertEqual(['2', '3'], parts)
124        self.god.check_playback()
125
126        # Test that min_blocks works.
127        partition.partition.expect_call(job, '/dev/hdc3').and_return('3')
128        parts = partition.get_partition_list(job, min_blocks=600000,
129                                             exclude_swap=False,
130                                             open_func=fake_open)
131        self.assertEqual(['3'], parts)
132        self.god.check_playback()
133
134
135# we want to run the unit test suite once strictly on the non site specific
136# version of partition (ie on base_partition.py) and once on the version
137# that would result after the site specific overrides take place in order
138# to check that the overrides to not break expected functionality of the
139# non site specific code
140class FSOptions_base_test(FsOptions_common, unittest.TestCase):
141    def setUp(self):
142        sys.modules['autotest_lib.client.bin.site_partition'] = None
143        reload(partition)
144
145
146class get_partition_list_base_test(get_partition_list_common, unittest.TestCase):
147    def setUp(self):
148        sys.modules['autotest_lib.client.bin.site_partition'] = None
149        reload(partition)
150        get_partition_list_common.setUp(self)
151
152
153class FSOptions_test(FsOptions_common, unittest.TestCase):
154    def setUp(self):
155        if 'autotest_lib.client.bin.site_partition' in sys.modules:
156            del sys.modules['autotest_lib.client.bin.site_partition']
157        reload(partition)
158
159
160class get_partition_list_test(get_partition_list_common, unittest.TestCase):
161    def setUp(self):
162        if 'autotest_lib.client.bin.site_partition' in sys.modules:
163            del sys.modules['autotest_lib.client.bin.site_partition']
164        reload(partition)
165        get_partition_list_common.setUp(self)
166
167
168if __name__ == '__main__':
169    unittest.main()
170