~mabac/linaro-image-tools/bug-923650-snowball-android-emmc

« back to all changes in this revision

Viewing changes to linaro_image_tools/media_create/tests/test_media_create.py

  • Committer: Georgy Redkozubov
  • Date: 2011-12-26 12:05:41 UTC
  • mfrom: (472.1.5 718665)
  • Revision ID: georgy.redkozubov@linaro.org-20111226120541-ygb3d6o3qdoigbfp
[r=mabac] Run sfdisk -l in loop to wait for partition to settle after parted in function create_partitions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
93
93
    setup_partitions,
94
94
    get_uuid,
95
95
    _parse_blkid_output,
 
96
    wait_partition_to_settle,
96
97
    _get_device_file_for_partition_number,
97
98
    )
98
99
from linaro_image_tools.media_create.rootfs import (
258
259
        with hp:
259
260
            test_file = hp.get_file(metadata_file)
260
261
            self.assertEquals(data, open(test_file, 'r').read())
261
 
        
 
262
 
262
263
 
263
264
class TestSetMetadata(TestCaseWithFixtures):
264
265
 
1186
1187
            '1318912,-,E\n1318912,524288,L\n1843200,1048576,L\n2891776,,,-',
1187
1188
            android_boards.AndroidMx53LoCoConfig.get_sfdisk_cmd())
1188
1189
 
 
1190
 
1189
1191
class TestGetSfdiskCmdV2(TestCase):
1190
1192
 
1191
1193
    def test_mx5(self):
1328
1330
                       'bootm 0x00100000 0x08000000'}
1329
1331
        self.assertEqual(expected, boot_commands)
1330
1332
 
1331
 
 
1332
1333
    def test_panda(self):
1333
1334
        # XXX: To fix bug 697824 we have to change class attributes of our
1334
1335
        # OMAP board configs, and some tests do that so to make sure they
1826
1827
 
1827
1828
        self.assertEqual(
1828
1829
            ['%s parted -s %s mklabel msdos' % (sudo_args, self.media.path),
1829
 
             'sync'],
 
1830
             '%s sfdisk -l %s' % (sudo_args, self.media.path),
 
1831
             'sync',
 
1832
             '%s sfdisk -l %s' % (sudo_args, self.media.path)],
1830
1833
            popen_fixture.mock.commands_executed)
1831
1834
        # Notice that we create all partitions in a single sfdisk run because
1832
1835
        # every time we run sfdisk it actually repartitions the device,
1846
1849
 
1847
1850
        self.assertEqual(
1848
1851
            ['%s parted -s %s mklabel msdos' % (sudo_args, self.media.path),
1849
 
             'sync'],
 
1852
             '%s sfdisk -l %s' % (sudo_args, self.media.path),
 
1853
             'sync',
 
1854
             '%s sfdisk -l %s' % (sudo_args, self.media.path)],
1850
1855
            popen_fixture.mock.commands_executed)
1851
1856
        # Notice that we create all partitions in a single sfdisk run because
1852
1857
        # every time we run sfdisk it actually repartitions the device,
1865
1870
 
1866
1871
        self.assertEqual(
1867
1872
            ['%s parted -s %s mklabel msdos' % (sudo_args, self.media.path),
1868
 
             'sync'],
 
1873
             '%s sfdisk -l %s' % (sudo_args, self.media.path),
 
1874
             'sync',
 
1875
             '%s sfdisk -l %s' % (sudo_args, self.media.path)],
1869
1876
            popen_fixture.mock.commands_executed)
1870
1877
        # Notice that we create all partitions in a single sfdisk run because
1871
1878
        # every time we run sfdisk it actually repartitions the device,
1883
1890
 
1884
1891
        self.assertEqual(
1885
1892
            ['%s parted -s %s mklabel msdos' % (sudo_args, self.media.path),
1886
 
             'sync'],
 
1893
             '%s sfdisk -l %s' % (sudo_args, self.media.path),
 
1894
             'sync',
 
1895
             '%s sfdisk -l %s' % (sudo_args, self.media.path)],
1887
1896
            popen_fixture.mock.commands_executed)
1888
1897
        self.assertEqual(
1889
1898
            [('63,106432,0x0C,*\n106496,,,-', HEADS, SECTORS, '',
1901
1910
        # Unlike the test for partitioning of a regular block device, in this
1902
1911
        # case parted was not called as there's no existing partition table
1903
1912
        # for us to overwrite on the image file.
1904
 
        self.assertEqual(['sync'], popen_fixture.mock.commands_executed)
 
1913
        self.assertEqual(
 
1914
            ['%s sfdisk -l %s' % (sudo_args, tmpfile),
 
1915
             'sync',
 
1916
             '%s sfdisk -l %s' % (sudo_args, tmpfile)],
 
1917
            popen_fixture.mock.commands_executed)
1905
1918
 
1906
1919
        self.assertEqual(
1907
1920
            [('63,106432,0x0C,*\n106496,,,-', HEADS, SECTORS, '', tmpfile)],
1926
1939
            ',1,0xDA', HEADS, SECTORS, '', tmpfile, as_root=False,
1927
1940
            stderr=subprocess.PIPE)
1928
1941
 
 
1942
    def test_wait_partitions_to_settle(self):
 
1943
        class Namespace:
 
1944
            pass
 
1945
 
 
1946
        ns = Namespace()
 
1947
        ns.count = 0
 
1948
 
 
1949
        class MockCmdRunnerPopen(object):
 
1950
            def __call__(self, cmd, *args, **kwargs):
 
1951
                ns.count += 1
 
1952
                self.returncode = 0
 
1953
                if ns.count < 5:
 
1954
                    raise cmd_runner.SubcommandNonZeroReturnValue(args, 1)
 
1955
                else:
 
1956
                    return self
 
1957
 
 
1958
            def communicate(self, input=None):
 
1959
                self.wait()
 
1960
                return '', ''
 
1961
 
 
1962
            def wait(self):
 
1963
                return self.returncode
 
1964
 
 
1965
        fixture = self.useFixture(MockCmdRunnerPopenFixture())
 
1966
 
 
1967
        tmpfile = self.createTempFileAsFixture()
 
1968
        media = Media(tmpfile)
 
1969
        media.is_block_device = True
 
1970
 
 
1971
        self.assertEqual(0, wait_partition_to_settle(media))
 
1972
 
 
1973
    def test_wait_partitions_to_settle_raises_SubcommandNonZeroReturnValue(self):
 
1974
        def mock_run(args, as_root=False, chroot=None, stdin=None, stdout=None,
 
1975
            stderr=None, cwd=None):
 
1976
            raise cmd_runner.SubcommandNonZeroReturnValue(args, 1)
 
1977
 
 
1978
        self.useFixture(MockSomethingFixture(
 
1979
            cmd_runner, 'run',
 
1980
            mock_run))
 
1981
 
 
1982
        tmpfile = self.createTempFileAsFixture()
 
1983
        media = Media(tmpfile)
 
1984
        media.is_block_device = True
 
1985
 
 
1986
        self.assertRaises(cmd_runner.SubcommandNonZeroReturnValue,
 
1987
            wait_partition_to_settle,
 
1988
            media)
 
1989
 
1929
1990
 
1930
1991
class TestPartitionSetup(TestCaseWithFixtures):
1931
1992
 
1937
1998
        self.linux_image_size = 30 * 1024**2
1938
1999
        self.linux_offsets_and_sizes = [
1939
2000
            (16384 * SECTOR_SIZE, 15746 * SECTOR_SIZE),
1940
 
            (32768 * SECTOR_SIZE, (self.linux_image_size - 
 
2001
            (32768 * SECTOR_SIZE, (self.linux_image_size -
1941
2002
                                        32768 * SECTOR_SIZE))
1942
2003
            ]
1943
2004
        self.android_image_size = 256 * 1024**2
1949
2010
            (98367 * SECTOR_SIZE, 65536 * SECTOR_SIZE),
1950
2011
            ((294975 + ext_part_size) * SECTOR_SIZE,
1951
2012
             (131072 - ext_part_size) * SECTOR_SIZE),
1952
 
            ((426047 + ext_part_size) * SECTOR_SIZE, 
 
2013
            ((426047 + ext_part_size) * SECTOR_SIZE,
1953
2014
             self.android_image_size - (426047 + ext_part_size) * SECTOR_SIZE)
1954
2015
            ]
1955
 
        
 
2016
 
1956
2017
        self.android_snowball_offsets_and_sizes = [
1957
2018
            (8192 * SECTOR_SIZE, 24639 * SECTOR_SIZE),
1958
2019
            (32831 * SECTOR_SIZE, 65536 * SECTOR_SIZE),
1959
 
            ((98367  + ext_part_size)* SECTOR_SIZE, 
 
2020
            ((98367 + ext_part_size)* SECTOR_SIZE,
1960
2021
             (65536 - ext_part_size) * SECTOR_SIZE),
1961
2022
            (294975 * SECTOR_SIZE, 131072 * SECTOR_SIZE),
1962
 
            ((426047 + ext_part_size) * SECTOR_SIZE, 
 
2023
            ((426047 + ext_part_size) * SECTOR_SIZE,
1963
2024
             self.android_image_size - (426047 + ext_part_size) * SECTOR_SIZE)
1964
2025
            ]
1965
2026
 
2098
2159
        get_boot_and_root_loopback_devices(tmpfile)
2099
2160
        self.assertEqual(
2100
2161
            ['%s losetup -f --show %s --offset %s --sizelimit %s'
2101
 
                % (sudo_args, tmpfile, offset, size) for (offset, size) in 
 
2162
                % (sudo_args, tmpfile, offset, size) for (offset, size) in
2102
2163
             self.linux_offsets_and_sizes],
2103
2164
            popen_fixture.mock.commands_executed)
2104
2165
 
2126
2187
        get_android_loopback_devices(tmpfile)
2127
2188
        self.assertEqual(
2128
2189
            ['%s losetup -f --show %s --offset %s --sizelimit %s'
2129
 
                % (sudo_args, tmpfile, offset, size) for (offset, size) in 
 
2190
                % (sudo_args, tmpfile, offset, size) for (offset, size) in
2130
2191
             self.android_offsets_and_sizes],
2131
2192
            popen_fixture.mock.commands_executed)
2132
2193
 
2173
2234
        self.assertEqual(
2174
2235
             # This is the call that would create a 2 GiB image file.
2175
2236
            ['dd of=%s bs=1 seek=2147483648 count=0' % tmpfile,
 
2237
             '%s sfdisk -l %s' % (sudo_args, tmpfile),
2176
2238
             # This call would partition the image file.
2177
2239
             '%s sfdisk --force -D -uS -H %s -S %s -C 1024 %s' % (
2178
2240
                 sudo_args, HEADS, SECTORS, tmpfile),
2179
2241
             # Make sure changes are written to disk.
2180
2242
             'sync',
 
2243
             '%s sfdisk -l %s' % (sudo_args, tmpfile),
2181
2244
             '%s mkfs.vfat -F 32 %s -n boot' % (sudo_args, bootfs_dev),
2182
2245
             '%s mkfs.ext3 %s -L root' % (sudo_args, rootfs_dev)],
2183
2246
            popen_fixture.mock.commands_executed)
2201
2264
            True, True, True)
2202
2265
        self.assertEqual(
2203
2266
            ['%s parted -s %s mklabel msdos' % (sudo_args, tmpfile),
 
2267
             '%s sfdisk -l %s' % (sudo_args, tmpfile),
2204
2268
             '%s sfdisk --force -D -uS -H %s -S %s %s' % (
2205
2269
                 sudo_args, HEADS, SECTORS, tmpfile),
2206
2270
             'sync',
 
2271
             '%s sfdisk -l %s' % (sudo_args, tmpfile),
2207
2272
             # Since the partitions are mounted, setup_partitions will umount
2208
2273
             # them before running mkfs.
2209
2274
             '%s umount %s' % (sudo_args, bootfs_dev),
2213
2278
            popen_fixture.mock.commands_executed)
2214
2279
 
2215
2280
    def test_get_device_file_for_partition_number_raises_DBusException(self):
2216
 
        def mock_get_udisks_device_path(d):
2217
 
            raise dbus.exceptions.DBusException
 
2281
        def mock_get_udisks_device_path(d):
 
2282
            raise dbus.exceptions.DBusException
2218
2283
 
2219
2284
        self.useFixture(MockSomethingFixture(
2220
2285
            partitions, '_get_udisks_device_path',
2221
2286
            mock_get_udisks_device_path))
2222
2287
 
2223
2288
        tmpfile = self.createTempFileAsFixture()
2224
 
        partition = board_configs['beagle'].mmc_part_offset
 
2289
        partition = board_configs['beagle'].mmc_part_offset
2225
2290
 
2226
2291
        self.useFixture(MockSomethingFixture(
2227
2292
            glob, 'glob',
2233
2298
        media = Media(tmpfile)
2234
2299
        media.is_block_device = True
2235
2300
        self.assertRaises(dbus.exceptions.DBusException,
2236
 
            _get_device_file_for_partition_number,
 
2301
            _get_device_file_for_partition_number,
2237
2302
            media.path, partition)
2238
2303
 
2239
2304
    def test_get_device_file_for_partition_number(self):
2240
 
        class Namespace: pass
2241
 
        ns = Namespace()
2242
 
        ns.count = 0
2243
 
 
2244
 
        def mock_get_udisks_device_path(dev):
2245
 
            ns.count += 1
2246
 
            if ns.count < 5:
2247
 
                raise dbus.exceptions.DBusException
2248
 
            else:
2249
 
                return '/abc/123'
2250
 
 
2251
 
        def mock_get_udisks_device_file(dev, part):
2252
 
            if ns.count < 5:
2253
 
                raise dbus.exceptions.DBusException
2254
 
            else:
2255
 
                return '/abc/123'
 
2305
        class Namespace:
 
2306
            pass
 
2307
        ns = Namespace()
 
2308
        ns.count = 0
 
2309
 
 
2310
        def mock_get_udisks_device_path(dev):
 
2311
            ns.count += 1
 
2312
            if ns.count < 5:
 
2313
                raise dbus.exceptions.DBusException
 
2314
            else:
 
2315
                return '/abc/123'
 
2316
 
 
2317
        def mock_get_udisks_device_file(dev, part):
 
2318
            if ns.count < 5:
 
2319
                raise dbus.exceptions.DBusException
 
2320
            else:
 
2321
                return '/abc/123'
2256
2322
 
2257
2323
        self.useFixture(MockSomethingFixture(
2258
2324
            partitions, '_get_udisks_device_path',
2263
2329
            mock_get_udisks_device_file))
2264
2330
 
2265
2331
        tmpfile = self.createTempFileAsFixture()
2266
 
        partition = board_configs['beagle'].mmc_part_offset
 
2332
        partition = board_configs['beagle'].mmc_part_offset
2267
2333
 
2268
2334
        self.useFixture(MockSomethingFixture(
2269
2335
            glob, 'glob',