partition_unittest.py revision 87529d121ba0db95743098e016ad934094f816cf
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, unittest
8from cStringIO import StringIO
9import common
10from autotest_lib.client.bin import partition
11from autotest_lib.client.common_lib.test_utils import mock
12
13
14class FsOptions_test(unittest.TestCase):
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('fs', 'mkfs opts', 'mount opts', 'shortie')
20        self.assertEqual('fs', obj.fstype)
21        self.assertEqual('mkfs opts', obj.mkfs_flags)
22        self.assertEqual('mount opts', obj.mount_options)
23        self.assertEqual('shortie', obj.fs_tag)
24
25
26    def test__str__(self):
27        str_obj = str(partition.FsOptions('abc', 'def', 'ghi', 'jkl'))
28        self.assert_('FsOptions' in str_obj)
29        self.assert_('abc' in str_obj)
30        self.assert_('def' in str_obj)
31        self.assert_('ghi' in str_obj)
32        self.assert_('jkl' in str_obj)
33
34
35# Test data used in GetPartitionTest below.
36
37SAMPLE_SWAPS = """
38Filename                                Type            Size    Used    Priority
39/dev/hdc2                               partition       9863868 0       -1
40"""
41
42SAMPLE_PARTITIONS_HDC_ONLY = """
43major minor  #blocks  name
44
45   8    16  390711384 hdc
46   8    18     530113 hdc2
47   8    19  390178687 hdc3
48"""
49
50# yes I manually added a hda1 line to this output to test parsing when the Boot
51# flag exists.
52SAMPLE_FDISK = "/sbin/fdisk -l -u '/dev/hdc'"
53SAMPLE_FDISK_OUTPUT = """
54Disk /dev/hdc: 400.0 GB, 400088457216 bytes
55255 heads, 63 sectors/track, 48641 cylinders, total 781422768 sectors
56Units = sectors of 1 * 512 = 512 bytes
57
58   Device Boot      Start         End      Blocks   Id  System
59/dev/hdc2              63     1060289      530113+  82  Linux swap / Solaris
60/dev/hdc3         1060290   781417664   390178687+  83  Linux
61/dev/hdc4   *    faketest    FAKETEST      232323+  83  Linux
62"""
63
64
65class GetPartitionsTest(unittest.TestCase):
66    def setUp(self):
67        self.god = mock.mock_god()
68        self.god.stub_function(os, 'popen')
69
70
71    def tearDown(self):
72        self.god.unstub_all()
73
74
75    def test_filter_non_linux(self):
76        for unused in xrange(4):
77            os.popen.expect_call(SAMPLE_FDISK).and_return(
78                    StringIO(SAMPLE_FDISK_OUTPUT))
79        self.assertFalse(partition.filter_non_linux('hdc1'))
80        self.assertFalse(partition.filter_non_linux('hdc2'))
81        self.assertTrue(partition.filter_non_linux('hdc3'))
82        self.assertTrue(partition.filter_non_linux('hdc4'))
83        self.god.check_playback()
84
85
86    def test_get_partition_list(self):
87        def fake_open(filename):
88            """Fake open() to pass to get_partition_list as __open."""
89            if filename == '/proc/swaps':
90                return StringIO(SAMPLE_SWAPS)
91            elif filename == '/proc/partitions':
92                return StringIO(SAMPLE_PARTITIONS_HDC_ONLY)
93            else:
94                self.assertFalse("Unexpected open() call: %s" % filename)
95
96        job = 'FakeJob'
97
98        # Test a filter func that denies all.
99        parts = partition.get_partition_list(job, filter_func=lambda x: False,
100                                             __open=fake_open)
101        self.assertEqual([], parts)
102        self.god.check_playback()
103
104        # Test normal operation.
105        self.god.stub_function(partition, 'partition')
106        partition.partition.expect_call(job, '/dev/hdc3').and_return('3')
107        parts = partition.get_partition_list(job, __open=fake_open)
108        self.assertEqual(['3'], parts)
109        self.god.check_playback()
110
111        # Test exclude_swap can be disabled.
112        partition.partition.expect_call(job, '/dev/hdc2').and_return('2')
113        partition.partition.expect_call(job, '/dev/hdc3').and_return('3')
114        parts = partition.get_partition_list(job, exclude_swap=False,
115                                             __open=fake_open)
116        self.assertEqual(['2', '3'], parts)
117        self.god.check_playback()
118
119        # Test that min_blocks works.
120        partition.partition.expect_call(job, '/dev/hdc3').and_return('3')
121        parts = partition.get_partition_list(job, min_blocks=600000,
122                                             exclude_swap=False,
123                                             __open=fake_open)
124        self.assertEqual(['3'], parts)
125        self.god.check_playback()
126
127
128
129if __name__ == '__main__':
130    unittest.main()
131