• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2
3"""Tests for autotest_lib.client.bin.partition."""
4
5__author__ = 'gps@google.com (Gregory P. Smith)'
6
7import os, sys, unittest
8from cStringIO import StringIO
9import common
10from autotest_lib.client.common_lib.test_utils import mock
11from autotest_lib.client.bin import partition
12
13
14class 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)
25
26
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)
34
35
36# Test data used in GetPartitionTest below.
37
38SAMPLE_SWAPS = """
39Filename                                Type            Size    Used    Priority
40/dev/hdc2                               partition       9863868 0       -1
41"""
42
43SAMPLE_PARTITIONS_HDC_ONLY = """
44major minor  #blocks  name
45
46   8    16  390711384 hdc
47   8    18     530113 hdc2
48   8    19  390178687 hdc3
49"""
50
51# yes I manually added a hda1 line to this output to test parsing when the Boot
52# flag exists.
53SAMPLE_FDISK = "/sbin/fdisk -l -u '/dev/hdc'"
54SAMPLE_FDISK_OUTPUT = """
55Disk /dev/hdc: 400.0 GB, 400088457216 bytes
56255 heads, 63 sectors/track, 48641 cylinders, total 781422768 sectors
57Units = sectors of 1 * 512 = 512 bytes
58
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"""
64
65
66class get_partition_list_common(object):
67    def setUp(self):
68        self.god = mock.mock_god()
69        self.god.stub_function(os, 'popen')
70
71
72    def tearDown(self):
73        self.god.unstub_all()
74
75
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()
85
86
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)
96
97        job = 'FakeJob'
98
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()
104
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()
111
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()
119
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()
127
128
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
134class FSOptions_base_test(FsOptions_common, unittest.TestCase):
135    def setUp(self):
136        sys.modules['autotest_lib.client.bin.site_partition'] = None
137        reload(partition)
138
139
140class 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)
145
146
147class 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)
152
153
154class 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)
160
161
162if __name__ == '__main__':
163    unittest.main()
164