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