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