xref: /openbmc/u-boot/test/py/tests/test_fs/conftest.py (revision a370e429)
1# SPDX-License-Identifier:      GPL-2.0+
2# Copyright (c) 2018, Linaro Limited
3# Author: Takahiro Akashi <takahiro.akashi@linaro.org>
4
5import os
6import os.path
7import pytest
8import re
9from subprocess import call, check_call, check_output, CalledProcessError
10from fstest_defs import *
11
12supported_fs_basic = ['fat16', 'fat32', 'ext4']
13supported_fs_ext = ['fat16', 'fat32']
14supported_fs_mkdir = ['fat16', 'fat32']
15supported_fs_unlink = ['fat16', 'fat32']
16
17#
18# Filesystem test specific setup
19#
20def pytest_addoption(parser):
21    parser.addoption('--fs-type', action='append', default=None,
22        help='Targeting Filesystem Types')
23
24def pytest_configure(config):
25    global supported_fs_basic
26    global supported_fs_ext
27    global supported_fs_mkdir
28    global supported_fs_unlink
29
30    def intersect(listA, listB):
31        return  [x for x in listA if x in listB]
32
33    supported_fs = config.getoption('fs_type')
34    if supported_fs:
35        print("*** FS TYPE modified: %s" % supported_fs)
36        supported_fs_basic =  intersect(supported_fs, supported_fs_basic)
37        supported_fs_ext =  intersect(supported_fs, supported_fs_ext)
38        supported_fs_mkdir =  intersect(supported_fs, supported_fs_mkdir)
39        supported_fs_unlink =  intersect(supported_fs, supported_fs_unlink)
40
41def pytest_generate_tests(metafunc):
42    if 'fs_obj_basic' in metafunc.fixturenames:
43        metafunc.parametrize('fs_obj_basic', supported_fs_basic,
44            indirect=True, scope='module')
45    if 'fs_obj_ext' in metafunc.fixturenames:
46        metafunc.parametrize('fs_obj_ext', supported_fs_ext,
47            indirect=True, scope='module')
48    if 'fs_obj_mkdir' in metafunc.fixturenames:
49        metafunc.parametrize('fs_obj_mkdir', supported_fs_mkdir,
50            indirect=True, scope='module')
51    if 'fs_obj_unlink' in metafunc.fixturenames:
52        metafunc.parametrize('fs_obj_unlink', supported_fs_unlink,
53            indirect=True, scope='module')
54
55#
56# Helper functions
57#
58def fstype_to_ubname(fs_type):
59    if re.match('fat', fs_type):
60        return 'fat'
61    else:
62        return fs_type
63
64def check_ubconfig(config, fs_type):
65    if not config.buildconfig.get('config_cmd_%s' % fs_type, None):
66        pytest.skip('.config feature "CMD_%s" not enabled' % fs_type.upper())
67    if not config.buildconfig.get('config_%s_write' % fs_type, None):
68        pytest.skip('.config feature "%s_WRITE" not enabled'
69        % fs_type.upper())
70
71def mk_fs(config, fs_type, size, id):
72    fs_img = '%s.%s.img' % (id, fs_type)
73    fs_img = config.persistent_data_dir + '/' + fs_img
74
75    if fs_type == 'fat16':
76        mkfs_opt = '-F 16'
77    elif fs_type == 'fat32':
78        mkfs_opt = '-F 32'
79    else:
80        mkfs_opt = ''
81
82    if re.match('fat', fs_type):
83        fs_lnxtype = 'vfat'
84    else:
85        fs_lnxtype = fs_type
86
87    count = (size + 1048576 - 1) / 1048576
88
89    try:
90        check_call('rm -f %s' % fs_img, shell=True)
91        check_call('dd if=/dev/zero of=%s bs=1M count=%d'
92            % (fs_img, count), shell=True)
93        check_call('mkfs.%s %s %s'
94            % (fs_lnxtype, mkfs_opt, fs_img), shell=True)
95        return fs_img
96    except CalledProcessError:
97        call('rm -f %s' % fs_img, shell=True)
98        raise
99
100# from test/py/conftest.py
101def tool_is_in_path(tool):
102    for path in os.environ["PATH"].split(os.pathsep):
103        fn = os.path.join(path, tool)
104        if os.path.isfile(fn) and os.access(fn, os.X_OK):
105            return True
106    return False
107
108fuse_mounted = False
109
110def mount_fs(fs_type, device, mount_point):
111    global fuse_mounted
112
113    fuse_mounted = False
114    try:
115        if tool_is_in_path('guestmount'):
116            fuse_mounted = True
117            check_call('guestmount -a %s -m /dev/sda %s'
118                % (device, mount_point), shell=True)
119        else:
120            mount_opt = "loop,rw"
121            if re.match('fat', fs_type):
122                mount_opt += ",umask=0000"
123
124            check_call('sudo mount -o %s %s %s'
125                % (mount_opt, device, mount_point), shell=True)
126
127            # may not be effective for some file systems
128            check_call('sudo chmod a+rw %s' % mount_point, shell=True)
129    except CalledProcessError:
130        raise
131
132def umount_fs(fs_type, mount_point):
133    if fuse_mounted:
134        call('sync')
135        call('guestunmount %s' % mount_point, shell=True)
136    else:
137        call('sudo umount %s' % mount_point, shell=True)
138
139#
140# Fixture for basic fs test
141#     derived from test/fs/fs-test.sh
142#
143# NOTE: yield_fixture was deprecated since pytest-3.0
144@pytest.yield_fixture()
145def fs_obj_basic(request, u_boot_config):
146    fs_type = request.param
147    fs_img = ''
148
149    fs_ubtype = fstype_to_ubname(fs_type)
150    check_ubconfig(u_boot_config, fs_ubtype)
151
152    mount_dir = u_boot_config.persistent_data_dir + '/mnt'
153
154    small_file = mount_dir + '/' + SMALL_FILE
155    big_file = mount_dir + '/' + BIG_FILE
156
157    try:
158
159        # 3GiB volume
160        fs_img = mk_fs(u_boot_config, fs_type, 0xc0000000, '3GB')
161
162        # Mount the image so we can populate it.
163        check_call('mkdir -p %s' % mount_dir, shell=True)
164        mount_fs(fs_type, fs_img, mount_dir)
165
166        # Create a subdirectory.
167        check_call('mkdir %s/SUBDIR' % mount_dir, shell=True)
168
169        # Create big file in this image.
170        # Note that we work only on the start 1MB, couple MBs in the 2GB range
171        # and the last 1 MB of the huge 2.5GB file.
172        # So, just put random values only in those areas.
173        check_call('dd if=/dev/urandom of=%s bs=1M count=1'
174	    % big_file, shell=True)
175        check_call('dd if=/dev/urandom of=%s bs=1M count=2 seek=2047'
176            % big_file, shell=True)
177        check_call('dd if=/dev/urandom of=%s bs=1M count=1 seek=2499'
178            % big_file, shell=True)
179
180        # Create a small file in this image.
181        check_call('dd if=/dev/urandom of=%s bs=1M count=1'
182	    % small_file, shell=True)
183
184        # Delete the small file copies which possibly are written as part of a
185        # previous test.
186        # check_call('rm -f "%s.w"' % MB1, shell=True)
187        # check_call('rm -f "%s.w2"' % MB1, shell=True)
188
189        # Generate the md5sums of reads that we will test against small file
190        out = check_output(
191            'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum'
192	    % small_file, shell=True)
193        md5val = [ out.split()[0] ]
194
195        # Generate the md5sums of reads that we will test against big file
196        # One from beginning of file.
197        out = check_output(
198            'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum'
199	    % big_file, shell=True)
200        md5val.append(out.split()[0])
201
202        # One from end of file.
203        out = check_output(
204            'dd if=%s bs=1M skip=2499 count=1 2> /dev/null | md5sum'
205	    % big_file, shell=True)
206        md5val.append(out.split()[0])
207
208        # One from the last 1MB chunk of 2GB
209        out = check_output(
210            'dd if=%s bs=1M skip=2047 count=1 2> /dev/null | md5sum'
211	    % big_file, shell=True)
212        md5val.append(out.split()[0])
213
214        # One from the start 1MB chunk from 2GB
215        out = check_output(
216            'dd if=%s bs=1M skip=2048 count=1 2> /dev/null | md5sum'
217	    % big_file, shell=True)
218        md5val.append(out.split()[0])
219
220        # One 1MB chunk crossing the 2GB boundary
221        out = check_output(
222            'dd if=%s bs=512K skip=4095 count=2 2> /dev/null | md5sum'
223	    % big_file, shell=True)
224        md5val.append(out.split()[0])
225
226        umount_fs(fs_type, mount_dir)
227    except CalledProcessError:
228        pytest.skip('Setup failed for filesystem: ' + fs_type)
229        return
230    else:
231        yield [fs_ubtype, fs_img, md5val]
232    finally:
233        umount_fs(fs_type, mount_dir)
234        call('rmdir %s' % mount_dir, shell=True)
235        if fs_img:
236            call('rm -f %s' % fs_img, shell=True)
237
238#
239# Fixture for extended fs test
240#
241# NOTE: yield_fixture was deprecated since pytest-3.0
242@pytest.yield_fixture()
243def fs_obj_ext(request, u_boot_config):
244    fs_type = request.param
245    fs_img = ''
246
247    fs_ubtype = fstype_to_ubname(fs_type)
248    check_ubconfig(u_boot_config, fs_ubtype)
249
250    mount_dir = u_boot_config.persistent_data_dir + '/mnt'
251
252    min_file = mount_dir + '/' + MIN_FILE
253    tmp_file = mount_dir + '/tmpfile'
254
255    try:
256
257        # 128MiB volume
258        fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
259
260        # Mount the image so we can populate it.
261        check_call('mkdir -p %s' % mount_dir, shell=True)
262        mount_fs(fs_type, fs_img, mount_dir)
263
264        # Create a test directory
265        check_call('mkdir %s/dir1' % mount_dir, shell=True)
266
267        # Create a small file and calculate md5
268        check_call('dd if=/dev/urandom of=%s bs=1K count=20'
269            % min_file, shell=True)
270        out = check_output(
271            'dd if=%s bs=1K 2> /dev/null | md5sum'
272            % min_file, shell=True)
273        md5val = [ out.split()[0] ]
274
275        # Calculate md5sum of Test Case 4
276        check_call('dd if=%s of=%s bs=1K count=20'
277            % (min_file, tmp_file), shell=True)
278        check_call('dd if=%s of=%s bs=1K seek=5 count=20'
279            % (min_file, tmp_file), shell=True)
280        out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
281            % tmp_file, shell=True)
282        md5val.append(out.split()[0])
283
284        # Calculate md5sum of Test Case 5
285        check_call('dd if=%s of=%s bs=1K count=20'
286            % (min_file, tmp_file), shell=True)
287        check_call('dd if=%s of=%s bs=1K seek=5 count=5'
288            % (min_file, tmp_file), shell=True)
289        out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
290            % tmp_file, shell=True)
291        md5val.append(out.split()[0])
292
293        # Calculate md5sum of Test Case 7
294        check_call('dd if=%s of=%s bs=1K count=20'
295            % (min_file, tmp_file), shell=True)
296        check_call('dd if=%s of=%s bs=1K seek=20 count=20'
297            % (min_file, tmp_file), shell=True)
298        out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
299            % tmp_file, shell=True)
300        md5val.append(out.split()[0])
301
302        check_call('rm %s' % tmp_file, shell=True)
303        umount_fs(fs_type, mount_dir)
304    except CalledProcessError:
305        pytest.skip('Setup failed for filesystem: ' + fs_type)
306        return
307    else:
308        yield [fs_ubtype, fs_img, md5val]
309    finally:
310        umount_fs(fs_type, mount_dir)
311        call('rmdir %s' % mount_dir, shell=True)
312        if fs_img:
313            call('rm -f %s' % fs_img, shell=True)
314
315#
316# Fixture for mkdir test
317#
318# NOTE: yield_fixture was deprecated since pytest-3.0
319@pytest.yield_fixture()
320def fs_obj_mkdir(request, u_boot_config):
321    fs_type = request.param
322    fs_img = ''
323
324    fs_ubtype = fstype_to_ubname(fs_type)
325    check_ubconfig(u_boot_config, fs_ubtype)
326
327    try:
328        # 128MiB volume
329        fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
330    except:
331        pytest.skip('Setup failed for filesystem: ' + fs_type)
332    else:
333        yield [fs_ubtype, fs_img]
334    finally:
335        if fs_img:
336            call('rm -f %s' % fs_img, shell=True)
337
338#
339# Fixture for unlink test
340#
341# NOTE: yield_fixture was deprecated since pytest-3.0
342@pytest.yield_fixture()
343def fs_obj_unlink(request, u_boot_config):
344    fs_type = request.param
345    fs_img = ''
346
347    fs_ubtype = fstype_to_ubname(fs_type)
348    check_ubconfig(u_boot_config, fs_ubtype)
349
350    mount_dir = u_boot_config.persistent_data_dir + '/mnt'
351
352    try:
353
354        # 128MiB volume
355        fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
356
357        # Mount the image so we can populate it.
358        check_call('mkdir -p %s' % mount_dir, shell=True)
359        mount_fs(fs_type, fs_img, mount_dir)
360
361        # Test Case 1 & 3
362        check_call('mkdir %s/dir1' % mount_dir, shell=True)
363        check_call('dd if=/dev/urandom of=%s/dir1/file1 bs=1K count=1'
364                                    % mount_dir, shell=True)
365        check_call('dd if=/dev/urandom of=%s/dir1/file2 bs=1K count=1'
366                                    % mount_dir, shell=True)
367
368        # Test Case 2
369        check_call('mkdir %s/dir2' % mount_dir, shell=True)
370	for i in range(0, 20):
371	    check_call('mkdir %s/dir2/0123456789abcdef%02x'
372                                    % (mount_dir, i), shell=True)
373
374        # Test Case 4
375        check_call('mkdir %s/dir4' % mount_dir, shell=True)
376
377        # Test Case 5, 6 & 7
378        check_call('mkdir %s/dir5' % mount_dir, shell=True)
379        check_call('dd if=/dev/urandom of=%s/dir5/file1 bs=1K count=1'
380                                    % mount_dir, shell=True)
381
382        umount_fs(fs_type, mount_dir)
383    except CalledProcessError:
384        pytest.skip('Setup failed for filesystem: ' + fs_type)
385        return
386    else:
387        yield [fs_ubtype, fs_img]
388    finally:
389        umount_fs(fs_type, mount_dir)
390        call('rmdir %s' % mount_dir, shell=True)
391        if fs_img:
392            call('rm -f %s' % fs_img, shell=True)
393