1#
2# Copyright (c) 2015, Intel Corporation.
3#
4# SPDX-License-Identifier: GPL-2.0-only
5#
6# AUTHORS
7# Ed Bartosh <ed.bartosh@linux.intel.com>
8
9"""Test cases for wic."""
10
11import os
12import sys
13import unittest
14import hashlib
15
16from glob import glob
17from shutil import rmtree, copy
18from functools import wraps, lru_cache
19from tempfile import NamedTemporaryFile
20
21from oeqa.selftest.case import OESelftestTestCase
22from oeqa.core.decorator import OETestTag
23from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, runqemu
24
25
26@lru_cache()
27def get_host_arch():
28    return get_bb_var('HOST_ARCH')
29
30
31def only_for_arch(archs):
32    """Decorator for wrapping test cases that can be run only for specific target
33    architectures. A list of compatible architectures is passed in `archs`.
34    """
35    def wrapper(func):
36        @wraps(func)
37        def wrapped_f(*args, **kwargs):
38            arch = get_host_arch()
39            if archs and arch not in archs:
40                raise unittest.SkipTest("Testcase arch dependency not met: %s" % arch)
41            return func(*args, **kwargs)
42        return wrapped_f
43    return wrapper
44
45def extract_files(debugfs_output):
46    """
47    extract file names from the output of debugfs -R 'ls -p',
48    which looks like this:
49
50     /2/040755/0/0/.//\n
51     /2/040755/0/0/..//\n
52     /11/040700/0/0/lost+found^M//\n
53     /12/040755/1002/1002/run//\n
54     /13/040755/1002/1002/sys//\n
55     /14/040755/1002/1002/bin//\n
56     /80/040755/1002/1002/var//\n
57     /92/040755/1002/1002/tmp//\n
58    """
59    # NOTE the occasional ^M in file names
60    return [line.split('/')[5].strip() for line in \
61            debugfs_output.strip().split('/\n')]
62
63def files_own_by_root(debugfs_output):
64    for line in debugfs_output.strip().split('/\n'):
65        if line.split('/')[3:5] != ['0', '0']:
66            print(debugfs_output)
67            return False
68    return True
69
70class WicTestCase(OESelftestTestCase):
71    """Wic test class."""
72
73    image_is_ready = False
74    wicenv_cache = {}
75
76    def setUpLocal(self):
77        """This code is executed before each test method."""
78        self.resultdir = os.path.join(self.builddir, "wic-tmp")
79        super(WicTestCase, self).setUpLocal()
80
81        # Do this here instead of in setUpClass as the base setUp does some
82        # clean up which can result in the native tools built earlier in
83        # setUpClass being unavailable.
84        if not WicTestCase.image_is_ready:
85            if self.td['USE_NLS'] != 'yes':
86                self.skipTest('wic-tools needs USE_NLS=yes')
87
88            bitbake('wic-tools core-image-minimal core-image-minimal-mtdutils')
89            WicTestCase.image_is_ready = True
90        rmtree(self.resultdir, ignore_errors=True)
91
92    def tearDownLocal(self):
93        """Remove resultdir as it may contain images."""
94        rmtree(self.resultdir, ignore_errors=True)
95        super(WicTestCase, self).tearDownLocal()
96
97    def _get_image_env_path(self, image):
98        """Generate and obtain the path to <image>.env"""
99        if image not in WicTestCase.wicenv_cache:
100            bitbake('%s -c do_rootfs_wicenv' % image)
101            stdir = get_bb_var('STAGING_DIR', image)
102            machine = self.td["MACHINE"]
103            WicTestCase.wicenv_cache[image] = os.path.join(stdir, machine, 'imgdata')
104        return WicTestCase.wicenv_cache[image]
105
106class CLITests(OESelftestTestCase):
107    def test_version(self):
108        """Test wic --version"""
109        runCmd('wic --version')
110
111    def test_help(self):
112        """Test wic --help and wic -h"""
113        runCmd('wic --help')
114        runCmd('wic -h')
115
116    def test_createhelp(self):
117        """Test wic create --help"""
118        runCmd('wic create --help')
119
120    def test_listhelp(self):
121        """Test wic list --help"""
122        runCmd('wic list --help')
123
124    def test_help_create(self):
125        """Test wic help create"""
126        runCmd('wic help create')
127
128    def test_help_list(self):
129        """Test wic help list"""
130        runCmd('wic help list')
131
132    def test_help_overview(self):
133        """Test wic help overview"""
134        runCmd('wic help overview')
135
136    def test_help_plugins(self):
137        """Test wic help plugins"""
138        runCmd('wic help plugins')
139
140    def test_help_kickstart(self):
141        """Test wic help kickstart"""
142        runCmd('wic help kickstart')
143
144    def test_list_images(self):
145        """Test wic list images"""
146        runCmd('wic list images')
147
148    def test_list_source_plugins(self):
149        """Test wic list source-plugins"""
150        runCmd('wic list source-plugins')
151
152    def test_listed_images_help(self):
153        """Test wic listed images help"""
154        output = runCmd('wic list images').output
155        imagelist = [line.split()[0] for line in output.splitlines()]
156        for image in imagelist:
157            runCmd('wic list %s help' % image)
158
159    def test_unsupported_subcommand(self):
160        """Test unsupported subcommand"""
161        self.assertNotEqual(0, runCmd('wic unsupported', ignore_status=True).status)
162
163    def test_no_command(self):
164        """Test wic without command"""
165        self.assertEqual(1, runCmd('wic', ignore_status=True).status)
166
167class Wic(WicTestCase):
168    def test_build_image_name(self):
169        """Test wic create wictestdisk --image-name=core-image-minimal"""
170        cmd = "wic create wictestdisk --image-name=core-image-minimal -o %s" % self.resultdir
171        runCmd(cmd)
172        self.assertEqual(1, len(glob(os.path.join (self.resultdir, "wictestdisk-*.direct"))))
173
174    @only_for_arch(['i586', 'i686', 'x86_64'])
175    def test_gpt_image(self):
176        """Test creation of core-image-minimal with gpt table and UUID boot"""
177        cmd = "wic create directdisk-gpt --image-name core-image-minimal -o %s" % self.resultdir
178        runCmd(cmd)
179        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-*.direct"))))
180
181    @only_for_arch(['i586', 'i686', 'x86_64'])
182    def test_iso_image(self):
183        """Test creation of hybrid iso image with legacy and EFI boot"""
184        config = 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
185                 'MACHINE_FEATURES:append = " efi"\n'\
186                 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
187        self.append_config(config)
188        bitbake('core-image-minimal core-image-minimal-initramfs')
189        self.remove_config(config)
190        cmd = "wic create mkhybridiso --image-name core-image-minimal -o %s" % self.resultdir
191        runCmd(cmd)
192        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "HYBRID_ISO_IMG-*.direct"))))
193        self.assertEqual(1, len(glob(os.path.join (self.resultdir, "HYBRID_ISO_IMG-*.iso"))))
194
195    @only_for_arch(['i586', 'i686', 'x86_64'])
196    def test_qemux86_directdisk(self):
197        """Test creation of qemux-86-directdisk image"""
198        cmd = "wic create qemux86-directdisk -e core-image-minimal -o %s" % self.resultdir
199        runCmd(cmd)
200        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "qemux86-directdisk-*direct"))))
201
202    @only_for_arch(['i586', 'i686', 'x86_64', 'aarch64'])
203    def test_mkefidisk(self):
204        """Test creation of mkefidisk image"""
205        cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
206        runCmd(cmd)
207        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "mkefidisk-*direct"))))
208
209    @only_for_arch(['i586', 'i686', 'x86_64'])
210    def test_bootloader_config(self):
211        """Test creation of directdisk-bootloader-config image"""
212        config = 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
213        self.append_config(config)
214        bitbake('core-image-minimal')
215        self.remove_config(config)
216        cmd = "wic create directdisk-bootloader-config -e core-image-minimal -o %s" % self.resultdir
217        runCmd(cmd)
218        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-bootloader-config-*direct"))))
219
220    @only_for_arch(['i586', 'i686', 'x86_64', 'aarch64'])
221    def test_systemd_bootdisk(self):
222        """Test creation of systemd-bootdisk image"""
223        config = 'MACHINE_FEATURES:append = " efi"\n'
224        self.append_config(config)
225        bitbake('core-image-minimal')
226        self.remove_config(config)
227        cmd = "wic create systemd-bootdisk -e core-image-minimal -o %s" % self.resultdir
228        runCmd(cmd)
229        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "systemd-bootdisk-*direct"))))
230
231    def test_efi_bootpart(self):
232        """Test creation of efi-bootpart image"""
233        cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
234        kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
235        self.append_config('IMAGE_EFI_BOOT_FILES = "%s;kernel"\n' % kimgtype)
236        runCmd(cmd)
237        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
238        images = glob(os.path.join(self.resultdir, "mkefidisk-*.direct"))
239        result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
240        self.assertIn("kernel",result.output)
241
242    def test_sdimage_bootpart(self):
243        """Test creation of sdimage-bootpart image"""
244        cmd = "wic create sdimage-bootpart -e core-image-minimal -o %s" % self.resultdir
245        kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
246        self.write_config('IMAGE_BOOT_FILES = "%s"\n' % kimgtype)
247        runCmd(cmd)
248        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "sdimage-bootpart-*direct"))))
249
250    # TODO this doesn't have to be x86-specific
251    @only_for_arch(['i586', 'i686', 'x86_64'])
252    def test_default_output_dir(self):
253        """Test default output location"""
254        for fname in glob("directdisk-*.direct"):
255            os.remove(fname)
256        config = 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
257        self.append_config(config)
258        bitbake('core-image-minimal')
259        self.remove_config(config)
260        cmd = "wic create directdisk -e core-image-minimal"
261        runCmd(cmd)
262        self.assertEqual(1, len(glob("directdisk-*.direct")))
263
264    @only_for_arch(['i586', 'i686', 'x86_64'])
265    def test_build_artifacts(self):
266        """Test wic create directdisk providing all artifacts."""
267        bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
268                              'wic-tools')
269        bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
270                                   'core-image-minimal'))
271        bbvars = {key.lower(): value for key, value in bb_vars.items()}
272        bbvars['resultdir'] = self.resultdir
273        runCmd("wic create directdisk "
274                        "-b %(staging_datadir)s "
275                        "-k %(deploy_dir_image)s "
276                        "-n %(recipe_sysroot_native)s "
277                        "-r %(image_rootfs)s "
278                        "-o %(resultdir)s" % bbvars)
279        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-*.direct"))))
280
281    def test_compress_gzip(self):
282        """Test compressing an image with gzip"""
283        runCmd("wic create wictestdisk "
284                                   "--image-name core-image-minimal "
285                                   "-c gzip -o %s" % self.resultdir)
286        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct.gz"))))
287
288    def test_compress_bzip2(self):
289        """Test compressing an image with bzip2"""
290        runCmd("wic create wictestdisk "
291                                   "--image-name=core-image-minimal "
292                                   "-c bzip2 -o %s" % self.resultdir)
293        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct.bz2"))))
294
295    def test_compress_xz(self):
296        """Test compressing an image with xz"""
297        runCmd("wic create wictestdisk "
298                                   "--image-name=core-image-minimal "
299                                   "--compress-with=xz -o %s" % self.resultdir)
300        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct.xz"))))
301
302    def test_wrong_compressor(self):
303        """Test how wic breaks if wrong compressor is provided"""
304        self.assertEqual(2, runCmd("wic create wictestdisk "
305                                   "--image-name=core-image-minimal "
306                                   "-c wrong -o %s" % self.resultdir,
307                                   ignore_status=True).status)
308
309    def test_debug_short(self):
310        """Test -D option"""
311        runCmd("wic create wictestdisk "
312                                   "--image-name=core-image-minimal "
313                                   "-D -o %s" % self.resultdir)
314        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
315        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "tmp.wic*"))))
316
317    def test_debug_long(self):
318        """Test --debug option"""
319        runCmd("wic create wictestdisk "
320                                   "--image-name=core-image-minimal "
321                                   "--debug -o %s" % self.resultdir)
322        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
323        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "tmp.wic*"))))
324
325    def test_skip_build_check_short(self):
326        """Test -s option"""
327        runCmd("wic create wictestdisk "
328                                   "--image-name=core-image-minimal "
329                                   "-s -o %s" % self.resultdir)
330        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
331
332    def test_skip_build_check_long(self):
333        """Test --skip-build-check option"""
334        runCmd("wic create wictestdisk "
335                                   "--image-name=core-image-minimal "
336                                   "--skip-build-check "
337                                   "--outdir %s" % self.resultdir)
338        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
339
340    def test_build_rootfs_short(self):
341        """Test -f option"""
342        runCmd("wic create wictestdisk "
343                                   "--image-name=core-image-minimal "
344                                   "-f -o %s" % self.resultdir)
345        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
346
347    def test_build_rootfs_long(self):
348        """Test --build-rootfs option"""
349        runCmd("wic create wictestdisk "
350                                   "--image-name=core-image-minimal "
351                                   "--build-rootfs "
352                                   "--outdir %s" % self.resultdir)
353        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
354
355    # TODO this doesn't have to be x86-specific
356    @only_for_arch(['i586', 'i686', 'x86_64'])
357    def test_rootfs_indirect_recipes(self):
358        """Test usage of rootfs plugin with rootfs recipes"""
359        runCmd("wic create directdisk-multi-rootfs "
360                        "--image-name=core-image-minimal "
361                        "--rootfs rootfs1=core-image-minimal "
362                        "--rootfs rootfs2=core-image-minimal "
363                        "--outdir %s" % self.resultdir)
364        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-multi-rootfs*.direct"))))
365
366    # TODO this doesn't have to be x86-specific
367    @only_for_arch(['i586', 'i686', 'x86_64'])
368    def test_rootfs_artifacts(self):
369        """Test usage of rootfs plugin with rootfs paths"""
370        bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
371                              'wic-tools')
372        bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
373                                   'core-image-minimal'))
374        bbvars = {key.lower(): value for key, value in bb_vars.items()}
375        bbvars['wks'] = "directdisk-multi-rootfs"
376        bbvars['resultdir'] = self.resultdir
377        runCmd("wic create %(wks)s "
378                        "--bootimg-dir=%(staging_datadir)s "
379                        "--kernel-dir=%(deploy_dir_image)s "
380                        "--native-sysroot=%(recipe_sysroot_native)s "
381                        "--rootfs-dir rootfs1=%(image_rootfs)s "
382                        "--rootfs-dir rootfs2=%(image_rootfs)s "
383                        "--outdir %(resultdir)s" % bbvars)
384        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "%(wks)s-*.direct" % bbvars))))
385
386    def test_exclude_path(self):
387        """Test --exclude-path wks option."""
388
389        oldpath = os.environ['PATH']
390        os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
391
392        try:
393            wks_file = 'temp.wks'
394            with open(wks_file, 'w') as wks:
395                rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
396                wks.write("""
397part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path usr
398part /usr --source rootfs --ondisk mmcblk0 --fstype=ext4 --rootfs-dir %s/usr
399part /etc --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path bin/ --rootfs-dir %s/usr"""
400                          % (rootfs_dir, rootfs_dir))
401            runCmd("wic create %s -e core-image-minimal -o %s" \
402                                       % (wks_file, self.resultdir))
403
404            os.remove(wks_file)
405            wicout = glob(os.path.join(self.resultdir, "%s-*direct" % 'temp'))
406            self.assertEqual(1, len(wicout))
407
408            wicimg = wicout[0]
409
410            # verify partition size with wic
411            res = runCmd("parted -m %s unit b p 2>/dev/null" % wicimg)
412
413            # parse parted output which looks like this:
414            # BYT;\n
415            # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
416            # 1:0.00MiB:200MiB:200MiB:ext4::;\n
417            partlns = res.output.splitlines()[2:]
418
419            self.assertEqual(3, len(partlns))
420
421            for part in [1, 2, 3]:
422                part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
423                partln = partlns[part-1].split(":")
424                self.assertEqual(7, len(partln))
425                start = int(partln[1].rstrip("B")) / 512
426                length = int(partln[3].rstrip("B")) / 512
427                runCmd("dd if=%s of=%s skip=%d count=%d" %
428                                           (wicimg, part_file, start, length))
429
430            # Test partition 1, should contain the normal root directories, except
431            # /usr.
432            res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
433                             os.path.join(self.resultdir, "selftest_img.part1"))
434            files = extract_files(res.output)
435            self.assertIn("etc", files)
436            self.assertNotIn("usr", files)
437
438            # Partition 2, should contain common directories for /usr, not root
439            # directories.
440            res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
441                             os.path.join(self.resultdir, "selftest_img.part2"))
442            files = extract_files(res.output)
443            self.assertNotIn("etc", files)
444            self.assertNotIn("usr", files)
445            self.assertIn("share", files)
446
447            # Partition 3, should contain the same as partition 2, including the bin
448            # directory, but not the files inside it.
449            res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
450                             os.path.join(self.resultdir, "selftest_img.part3"))
451            files = extract_files(res.output)
452            self.assertNotIn("etc", files)
453            self.assertNotIn("usr", files)
454            self.assertIn("share", files)
455            self.assertIn("bin", files)
456            res = runCmd("debugfs -R 'ls -p bin' %s 2>/dev/null" % \
457                             os.path.join(self.resultdir, "selftest_img.part3"))
458            files = extract_files(res.output)
459            self.assertIn(".", files)
460            self.assertIn("..", files)
461            self.assertEqual(2, len(files))
462
463            for part in [1, 2, 3]:
464                part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
465                os.remove(part_file)
466
467        finally:
468            os.environ['PATH'] = oldpath
469
470    def test_include_path(self):
471        """Test --include-path wks option."""
472
473        oldpath = os.environ['PATH']
474        os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
475
476        try:
477            include_path = os.path.join(self.resultdir, 'test-include')
478            os.makedirs(include_path)
479            with open(os.path.join(include_path, 'test-file'), 'w') as t:
480                t.write("test\n")
481            wks_file = os.path.join(include_path, 'temp.wks')
482            with open(wks_file, 'w') as wks:
483                rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
484                wks.write("""
485part /part1 --source rootfs --ondisk mmcblk0 --fstype=ext4
486part /part2 --source rootfs --ondisk mmcblk0 --fstype=ext4 --include-path %s"""
487                          % (include_path))
488            runCmd("wic create %s -e core-image-minimal -o %s" \
489                                       % (wks_file, self.resultdir))
490
491            part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
492            part2 = glob(os.path.join(self.resultdir, 'temp-*.direct.p2'))[0]
493
494            # Test partition 1, should not contain 'test-file'
495            res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
496            files = extract_files(res.output)
497            self.assertNotIn('test-file', files)
498            self.assertEqual(True, files_own_by_root(res.output))
499
500            # Test partition 2, should contain 'test-file'
501            res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part2))
502            files = extract_files(res.output)
503            self.assertIn('test-file', files)
504            self.assertEqual(True, files_own_by_root(res.output))
505
506        finally:
507            os.environ['PATH'] = oldpath
508
509    def test_include_path_embeded(self):
510        """Test --include-path wks option."""
511
512        oldpath = os.environ['PATH']
513        os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
514
515        try:
516            include_path = os.path.join(self.resultdir, 'test-include')
517            os.makedirs(include_path)
518            with open(os.path.join(include_path, 'test-file'), 'w') as t:
519                t.write("test\n")
520            wks_file = os.path.join(include_path, 'temp.wks')
521            with open(wks_file, 'w') as wks:
522                wks.write("""
523part / --source rootfs  --fstype=ext4 --include-path %s --include-path core-image-minimal-mtdutils export/"""
524                          % (include_path))
525            runCmd("wic create %s -e core-image-minimal -o %s" \
526                                       % (wks_file, self.resultdir))
527
528            part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
529
530            res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
531            files = extract_files(res.output)
532            self.assertIn('test-file', files)
533            self.assertEqual(True, files_own_by_root(res.output))
534
535            res = runCmd("debugfs -R 'ls -p /export/etc/' %s 2>/dev/null" % (part1))
536            files = extract_files(res.output)
537            self.assertIn('passwd', files)
538            self.assertEqual(True, files_own_by_root(res.output))
539
540        finally:
541            os.environ['PATH'] = oldpath
542
543    def test_include_path_errors(self):
544        """Test --include-path wks option error handling."""
545        wks_file = 'temp.wks'
546
547        # Absolute argument.
548        with open(wks_file, 'w') as wks:
549            wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils /export")
550        self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
551                                      % (wks_file, self.resultdir), ignore_status=True).status)
552        os.remove(wks_file)
553
554        # Argument pointing to parent directory.
555        with open(wks_file, 'w') as wks:
556            wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils ././..")
557        self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
558                                      % (wks_file, self.resultdir), ignore_status=True).status)
559        os.remove(wks_file)
560
561        # 3 Argument pointing to parent directory.
562        with open(wks_file, 'w') as wks:
563            wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils export/ dummy")
564        self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
565                                      % (wks_file, self.resultdir), ignore_status=True).status)
566        os.remove(wks_file)
567
568    def test_exclude_path_errors(self):
569        """Test --exclude-path wks option error handling."""
570        wks_file = 'temp.wks'
571
572        # Absolute argument.
573        with open(wks_file, 'w') as wks:
574            wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path /usr")
575        self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
576                                      % (wks_file, self.resultdir), ignore_status=True).status)
577        os.remove(wks_file)
578
579        # Argument pointing to parent directory.
580        with open(wks_file, 'w') as wks:
581            wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path ././..")
582        self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
583                                      % (wks_file, self.resultdir), ignore_status=True).status)
584        os.remove(wks_file)
585
586    def test_permissions(self):
587        """Test permissions are respected"""
588
589        # prepare wicenv and rootfs
590        bitbake('core-image-minimal core-image-minimal-mtdutils -c do_rootfs_wicenv')
591
592        oldpath = os.environ['PATH']
593        os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
594
595        t_normal = """
596part / --source rootfs --fstype=ext4
597"""
598        t_exclude = """
599part / --source rootfs --fstype=ext4 --exclude-path=home
600"""
601        t_multi = """
602part / --source rootfs --ondisk sda --fstype=ext4
603part /export --source rootfs --rootfs=core-image-minimal-mtdutils --fstype=ext4
604"""
605        t_change = """
606part / --source rootfs --ondisk sda --fstype=ext4 --exclude-path=etc/   
607part /etc --source rootfs --fstype=ext4 --change-directory=etc
608"""
609        tests = [t_normal, t_exclude, t_multi, t_change]
610
611        try:
612            for test in tests:
613                include_path = os.path.join(self.resultdir, 'test-include')
614                os.makedirs(include_path)
615                wks_file = os.path.join(include_path, 'temp.wks')
616                with open(wks_file, 'w') as wks:
617                    wks.write(test)
618                runCmd("wic create %s -e core-image-minimal -o %s" \
619                                       % (wks_file, self.resultdir))
620
621                for part in glob(os.path.join(self.resultdir, 'temp-*.direct.p*')):
622                    res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part))
623                    self.assertEqual(True, files_own_by_root(res.output))
624
625                config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "%s"\n' % wks_file
626                self.append_config(config)
627                bitbake('core-image-minimal')
628                tmpdir = os.path.join(get_bb_var('WORKDIR', 'core-image-minimal'),'build-wic')
629
630                # check each partition for permission
631                for part in glob(os.path.join(tmpdir, 'temp-*.direct.p*')):
632                    res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part))
633                    self.assertTrue(files_own_by_root(res.output)
634                        ,msg='Files permission incorrect using wks set "%s"' % test)
635
636                # clean config and result directory for next cases
637                self.remove_config(config)
638                rmtree(self.resultdir, ignore_errors=True)
639
640        finally:
641            os.environ['PATH'] = oldpath
642
643    def test_change_directory(self):
644        """Test --change-directory wks option."""
645
646        oldpath = os.environ['PATH']
647        os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
648
649        try:
650            include_path = os.path.join(self.resultdir, 'test-include')
651            os.makedirs(include_path)
652            wks_file = os.path.join(include_path, 'temp.wks')
653            with open(wks_file, 'w') as wks:
654                wks.write("part /etc --source rootfs --fstype=ext4 --change-directory=etc")
655            runCmd("wic create %s -e core-image-minimal -o %s" \
656                                       % (wks_file, self.resultdir))
657
658            part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
659
660            res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
661            files = extract_files(res.output)
662            self.assertIn('passwd', files)
663
664        finally:
665            os.environ['PATH'] = oldpath
666
667    def test_change_directory_errors(self):
668        """Test --change-directory wks option error handling."""
669        wks_file = 'temp.wks'
670
671        # Absolute argument.
672        with open(wks_file, 'w') as wks:
673            wks.write("part / --source rootfs --fstype=ext4 --change-directory /usr")
674        self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
675                                      % (wks_file, self.resultdir), ignore_status=True).status)
676        os.remove(wks_file)
677
678        # Argument pointing to parent directory.
679        with open(wks_file, 'w') as wks:
680            wks.write("part / --source rootfs --fstype=ext4 --change-directory ././..")
681        self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
682                                      % (wks_file, self.resultdir), ignore_status=True).status)
683        os.remove(wks_file)
684
685    def test_no_fstab_update(self):
686        """Test --no-fstab-update wks option."""
687
688        oldpath = os.environ['PATH']
689        os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
690
691        # Get stock fstab from base-files recipe
692        bitbake('base-files -c do_install')
693        bf_fstab = os.path.join(get_bb_var('D', 'base-files'), 'etc', 'fstab')
694        self.assertEqual(True, os.path.exists(bf_fstab))
695        bf_fstab_md5sum = runCmd('md5sum %s 2>/dev/null' % bf_fstab).output.split(" ")[0]
696
697        try:
698            no_fstab_update_path = os.path.join(self.resultdir, 'test-no-fstab-update')
699            os.makedirs(no_fstab_update_path)
700            wks_file = os.path.join(no_fstab_update_path, 'temp.wks')
701            with open(wks_file, 'w') as wks:
702                wks.writelines(['part / --source rootfs --fstype=ext4 --label rootfs\n',
703                                'part /mnt/p2 --source rootfs --rootfs-dir=core-image-minimal ',
704                                '--fstype=ext4 --label p2 --no-fstab-update\n'])
705            runCmd("wic create %s -e core-image-minimal -o %s" \
706                                       % (wks_file, self.resultdir))
707
708            part_fstab_md5sum = []
709            for i in range(1, 3):
710                part = glob(os.path.join(self.resultdir, 'temp-*.direct.p') + str(i))[0]
711                part_fstab = runCmd("debugfs -R 'cat etc/fstab' %s 2>/dev/null" % (part))
712                part_fstab_md5sum.append(hashlib.md5((part_fstab.output + "\n\n").encode('utf-8')).hexdigest())
713
714            # '/etc/fstab' in partition 2 should contain the same stock fstab file
715            # as the one installed by the base-file recipe.
716            self.assertEqual(bf_fstab_md5sum, part_fstab_md5sum[1])
717
718            # '/etc/fstab' in partition 1 should contain an updated fstab file.
719            self.assertNotEqual(bf_fstab_md5sum, part_fstab_md5sum[0])
720
721        finally:
722            os.environ['PATH'] = oldpath
723
724    def test_no_fstab_update_errors(self):
725        """Test --no-fstab-update wks option error handling."""
726        wks_file = 'temp.wks'
727
728        # Absolute argument.
729        with open(wks_file, 'w') as wks:
730            wks.write("part / --source rootfs --fstype=ext4 --no-fstab-update /etc")
731        self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
732                                      % (wks_file, self.resultdir), ignore_status=True).status)
733        os.remove(wks_file)
734
735        # Argument pointing to parent directory.
736        with open(wks_file, 'w') as wks:
737            wks.write("part / --source rootfs --fstype=ext4 --no-fstab-update ././..")
738        self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
739                                      % (wks_file, self.resultdir), ignore_status=True).status)
740        os.remove(wks_file)
741
742    def test_extra_space(self):
743        """Test --extra-space wks option."""
744        extraspace = 1024**3
745        runCmd("wic create wictestdisk "
746                                   "--image-name core-image-minimal "
747                                   "--extra-space %i -o %s" % (extraspace ,self.resultdir))
748        wicout = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
749        self.assertEqual(1, len(wicout))
750        size = os.path.getsize(wicout[0])
751        self.assertTrue(size > extraspace)
752
753class Wic2(WicTestCase):
754
755    def test_bmap_short(self):
756        """Test generation of .bmap file -m option"""
757        cmd = "wic create wictestdisk -e core-image-minimal -m -o %s" % self.resultdir
758        runCmd(cmd)
759        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
760        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct.bmap"))))
761
762    def test_bmap_long(self):
763        """Test generation of .bmap file --bmap option"""
764        cmd = "wic create wictestdisk -e core-image-minimal --bmap -o %s" % self.resultdir
765        runCmd(cmd)
766        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
767        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct.bmap"))))
768
769    def test_image_env(self):
770        """Test generation of <image>.env files."""
771        image = 'core-image-minimal'
772        imgdatadir = self._get_image_env_path(image)
773
774        bb_vars = get_bb_vars(['IMAGE_BASENAME', 'WICVARS'], image)
775        basename = bb_vars['IMAGE_BASENAME']
776        self.assertEqual(basename, image)
777        path = os.path.join(imgdatadir, basename) + '.env'
778        self.assertTrue(os.path.isfile(path))
779
780        wicvars = set(bb_vars['WICVARS'].split())
781        # filter out optional variables
782        wicvars = wicvars.difference(('DEPLOY_DIR_IMAGE', 'IMAGE_BOOT_FILES',
783                                      'INITRD', 'INITRD_LIVE', 'ISODIR','INITRAMFS_IMAGE',
784                                      'INITRAMFS_IMAGE_BUNDLE', 'INITRAMFS_LINK_NAME',
785                                      'APPEND', 'IMAGE_EFI_BOOT_FILES'))
786        with open(path) as envfile:
787            content = dict(line.split("=", 1) for line in envfile)
788            # test if variables used by wic present in the .env file
789            for var in wicvars:
790                self.assertTrue(var in content, "%s is not in .env file" % var)
791                self.assertTrue(content[var])
792
793    def test_image_vars_dir_short(self):
794        """Test image vars directory selection -v option"""
795        image = 'core-image-minimal'
796        imgenvdir = self._get_image_env_path(image)
797        native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
798
799        runCmd("wic create wictestdisk "
800                                   "--image-name=%s -v %s -n %s -o %s"
801                                   % (image, imgenvdir, native_sysroot,
802                                      self.resultdir))
803        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
804
805    def test_image_vars_dir_long(self):
806        """Test image vars directory selection --vars option"""
807        image = 'core-image-minimal'
808        imgenvdir = self._get_image_env_path(image)
809        native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
810
811        runCmd("wic create wictestdisk "
812                                   "--image-name=%s "
813                                   "--vars %s "
814                                   "--native-sysroot %s "
815                                   "--outdir %s"
816                                   % (image, imgenvdir, native_sysroot,
817                                      self.resultdir))
818        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
819
820    @only_for_arch(['i586', 'i686', 'x86_64', 'aarch64'])
821    def test_wic_image_type(self):
822        """Test building wic images by bitbake"""
823        config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
824                 'MACHINE_FEATURES:append = " efi"\n'
825        self.append_config(config)
826        bitbake('wic-image-minimal')
827        self.remove_config(config)
828
829        deploy_dir = get_bb_var('DEPLOY_DIR_IMAGE')
830        machine = self.td['MACHINE']
831        prefix = os.path.join(deploy_dir, 'wic-image-minimal-%s.' % machine)
832        # check if we have result image and manifests symlinks
833        # pointing to existing files
834        for suffix in ('wic', 'manifest'):
835            path = prefix + suffix
836            self.assertTrue(os.path.islink(path))
837            self.assertTrue(os.path.isfile(os.path.realpath(path)))
838
839    # TODO this should work on aarch64
840    @only_for_arch(['i586', 'i686', 'x86_64'])
841    @OETestTag("runqemu")
842    def test_qemu(self):
843        """Test wic-image-minimal under qemu"""
844        config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
845                 'MACHINE_FEATURES:append = " efi"\n'
846        self.append_config(config)
847        bitbake('wic-image-minimal')
848        self.remove_config(config)
849
850        with runqemu('wic-image-minimal', ssh=False, runqemuparams='nographic') as qemu:
851            cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' " \
852                  "-e '/dev/root /|/dev/sda2 /' -e '/dev/sda3 /media' -e '/dev/sda4 /mnt'"
853            status, output = qemu.run_serial(cmd)
854            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
855            self.assertEqual(output, '4')
856            cmd = "grep UUID= /etc/fstab"
857            status, output = qemu.run_serial(cmd)
858            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
859            self.assertEqual(output, 'UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba\t/media\text4\tdefaults\t0\t0')
860
861    @only_for_arch(['i586', 'i686', 'x86_64'])
862    @OETestTag("runqemu")
863    def test_qemu_efi(self):
864        """Test core-image-minimal efi image under qemu"""
865        config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "mkefidisk.wks"\n'
866        self.append_config(config)
867        bitbake('core-image-minimal ovmf')
868        self.remove_config(config)
869
870        with runqemu('core-image-minimal', ssh=False,
871                     runqemuparams='nographic ovmf', image_fstype='wic') as qemu:
872            cmd = "grep sda. /proc/partitions  |wc -l"
873            status, output = qemu.run_serial(cmd)
874            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
875            self.assertEqual(output, '3')
876
877    @staticmethod
878    def _make_fixed_size_wks(size):
879        """
880        Create a wks of an image with a single partition. Size of the partition is set
881        using --fixed-size flag. Returns a tuple: (path to wks file, wks image name)
882        """
883        with NamedTemporaryFile("w", suffix=".wks", delete=False) as tempf:
884            wkspath = tempf.name
885            tempf.write("part " \
886                     "--source rootfs --ondisk hda --align 4 --fixed-size %d "
887                     "--fstype=ext4\n" % size)
888
889        return wkspath
890
891    def _get_wic_partitions(self, wkspath, native_sysroot=None, ignore_status=False):
892        p = runCmd("wic create %s -e core-image-minimal -o %s" % (wkspath, self.resultdir),
893                   ignore_status=ignore_status)
894
895        if p.status:
896            return (p, None)
897
898        wksname = os.path.splitext(os.path.basename(wkspath))[0]
899
900        wicout = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
901
902        if not wicout:
903            return (p, None)
904
905        wicimg = wicout[0]
906
907        if not native_sysroot:
908            native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
909
910        # verify partition size with wic
911        res = runCmd("parted -m %s unit kib p 2>/dev/null" % wicimg,
912                     native_sysroot=native_sysroot)
913
914        # parse parted output which looks like this:
915        # BYT;\n
916        # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
917        # 1:0.00MiB:200MiB:200MiB:ext4::;\n
918        return (p, res.output.splitlines()[2:])
919
920    def test_fixed_size(self):
921        """
922        Test creation of a simple image with partition size controlled through
923        --fixed-size flag
924        """
925        wkspath = Wic2._make_fixed_size_wks(200)
926        _, partlns = self._get_wic_partitions(wkspath)
927        os.remove(wkspath)
928
929        self.assertEqual(partlns, [
930                        "1:4.00kiB:204804kiB:204800kiB:ext4::;",
931                        ])
932
933    def test_fixed_size_error(self):
934        """
935        Test creation of a simple image with partition size controlled through
936        --fixed-size flag. The size of partition is intentionally set to 1MiB
937        in order to trigger an error in wic.
938        """
939        wkspath = Wic2._make_fixed_size_wks(1)
940        p, _ = self._get_wic_partitions(wkspath, ignore_status=True)
941        os.remove(wkspath)
942
943        self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
944
945    def test_offset(self):
946        native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
947
948        with NamedTemporaryFile("w", suffix=".wks") as tempf:
949            # Test that partitions are placed at the correct offsets, default KB
950            tempf.write("bootloader --ptable gpt\n" \
951                        "part /    --source rootfs --ondisk hda --offset 32     --fixed-size 100M --fstype=ext4\n" \
952                        "part /bar                 --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n")
953            tempf.flush()
954
955            _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
956            self.assertEqual(partlns, [
957                "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
958                "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
959                ])
960
961        with NamedTemporaryFile("w", suffix=".wks") as tempf:
962            # Test that partitions are placed at the correct offsets, same with explicit KB
963            tempf.write("bootloader --ptable gpt\n" \
964                        "part /    --source rootfs --ondisk hda --offset 32K     --fixed-size 100M --fstype=ext4\n" \
965                        "part /bar                 --ondisk hda --offset 102432K --fixed-size 100M --fstype=ext4\n")
966            tempf.flush()
967
968            _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
969            self.assertEqual(partlns, [
970                "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
971                "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
972                ])
973
974        with NamedTemporaryFile("w", suffix=".wks") as tempf:
975            # Test that partitions are placed at the correct offsets using MB
976            tempf.write("bootloader --ptable gpt\n" \
977                        "part /    --source rootfs --ondisk hda --offset 32K  --fixed-size 100M --fstype=ext4\n" \
978                        "part /bar                 --ondisk hda --offset 101M --fixed-size 100M --fstype=ext4\n")
979            tempf.flush()
980
981            _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
982            self.assertEqual(partlns, [
983                "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
984                "2:103424kiB:205824kiB:102400kiB:ext4:primary:;",
985                ])
986
987        with NamedTemporaryFile("w", suffix=".wks") as tempf:
988            # Test that partitions can be placed on a 512 byte sector boundary
989            tempf.write("bootloader --ptable gpt\n" \
990                        "part /    --source rootfs --ondisk hda --offset 65s --fixed-size 99M --fstype=ext4\n" \
991                        "part /bar                 --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n")
992            tempf.flush()
993
994            _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
995            self.assertEqual(partlns, [
996                "1:32.5kiB:101408kiB:101376kiB:ext4:primary:;",
997                "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
998                ])
999
1000        with NamedTemporaryFile("w", suffix=".wks") as tempf:
1001            # Test that a partition can be placed immediately after a MSDOS partition table
1002            tempf.write("bootloader --ptable msdos\n" \
1003                        "part /    --source rootfs --ondisk hda --offset 1s --fixed-size 100M --fstype=ext4\n")
1004            tempf.flush()
1005
1006            _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
1007            self.assertEqual(partlns, [
1008                "1:0.50kiB:102400kiB:102400kiB:ext4::;",
1009                ])
1010
1011        with NamedTemporaryFile("w", suffix=".wks") as tempf:
1012            # Test that image creation fails if the partitions would overlap
1013            tempf.write("bootloader --ptable gpt\n" \
1014                        "part /    --source rootfs --ondisk hda --offset 32     --fixed-size 100M --fstype=ext4\n" \
1015                        "part /bar                 --ondisk hda --offset 102431 --fixed-size 100M --fstype=ext4\n")
1016            tempf.flush()
1017
1018            p, _ = self._get_wic_partitions(tempf.name, ignore_status=True)
1019            self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
1020
1021        with NamedTemporaryFile("w", suffix=".wks") as tempf:
1022            # Test that partitions are not allowed to overlap with the booloader
1023            tempf.write("bootloader --ptable gpt\n" \
1024                        "part /    --source rootfs --ondisk hda --offset 8 --fixed-size 100M --fstype=ext4\n")
1025            tempf.flush()
1026
1027            p, _ = self._get_wic_partitions(tempf.name, ignore_status=True)
1028            self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
1029
1030    def test_extra_space(self):
1031        native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
1032
1033        with NamedTemporaryFile("w", suffix=".wks") as tempf:
1034            tempf.write("bootloader --ptable gpt\n" \
1035                        "part /     --source rootfs --ondisk hda --extra-space 200M --fstype=ext4\n")
1036            tempf.flush()
1037
1038            _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
1039            self.assertEqual(len(partlns), 1)
1040            size = partlns[0].split(':')[3]
1041            self.assertRegex(size, r'^[0-9]+kiB$')
1042            size = int(size[:-3])
1043            self.assertGreaterEqual(size, 204800)
1044
1045    @only_for_arch(['i586', 'i686', 'x86_64', 'aarch64'])
1046    @OETestTag("runqemu")
1047    def test_rawcopy_plugin_qemu(self):
1048        """Test rawcopy plugin in qemu"""
1049        # build ext4 and then use it for a wic image
1050        config = 'IMAGE_FSTYPES = "ext4"\n'
1051        self.append_config(config)
1052        bitbake('core-image-minimal')
1053        self.remove_config(config)
1054
1055        config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_rawcopy_plugin.wks.in"\n'
1056        self.append_config(config)
1057        bitbake('core-image-minimal-mtdutils')
1058        self.remove_config(config)
1059
1060        with runqemu('core-image-minimal-mtdutils', ssh=False,
1061                     runqemuparams='nographic', image_fstype='wic') as qemu:
1062            cmd = "grep sda. /proc/partitions  |wc -l"
1063            status, output = qemu.run_serial(cmd)
1064            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1065            self.assertEqual(output, '2')
1066
1067    def _rawcopy_plugin(self, fstype):
1068        """Test rawcopy plugin"""
1069        img = 'core-image-minimal'
1070        machine = self.td["MACHINE"]
1071        params = ',unpack' if fstype.endswith('.gz') else ''
1072        with NamedTemporaryFile("w", suffix=".wks") as wks:
1073            wks.write('part / --source rawcopy --sourceparams="file=%s-%s.%s%s"\n'\
1074                      % (img, machine, fstype, params))
1075            wks.flush()
1076            cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1077            runCmd(cmd)
1078            wksname = os.path.splitext(os.path.basename(wks.name))[0]
1079            out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
1080            self.assertEqual(1, len(out))
1081
1082    def test_rawcopy_plugin(self):
1083        self._rawcopy_plugin('ext4')
1084
1085    def test_rawcopy_plugin_unpack(self):
1086        fstype = 'ext4.gz'
1087        config = 'IMAGE_FSTYPES = "%s"\n' % fstype
1088        self.append_config(config)
1089        self.assertEqual(0, bitbake('core-image-minimal').status)
1090        self.remove_config(config)
1091        self._rawcopy_plugin(fstype)
1092
1093    def test_empty_plugin(self):
1094        """Test empty plugin"""
1095        config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_empty_plugin.wks"\n'
1096        self.append_config(config)
1097        bitbake('core-image-minimal')
1098        self.remove_config(config)
1099        deploy_dir = get_bb_var('DEPLOY_DIR_IMAGE')
1100        machine = self.td['MACHINE']
1101
1102        image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine)
1103        self.assertTrue(os.path.exists(image_path))
1104
1105        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1106
1107        # Fstype column from 'wic ls' should be empty for the second partition
1108        # as listed in test_empty_plugin.wks
1109        result = runCmd("wic ls %s -n %s | awk -F ' ' '{print $1 \" \" $5}' | grep '^2' | wc -w" % (image_path, sysroot))
1110        self.assertEqual('1', result.output)
1111
1112    @only_for_arch(['i586', 'i686', 'x86_64'])
1113    @OETestTag("runqemu")
1114    def test_biosplusefi_plugin_qemu(self):
1115        """Test biosplusefi plugin in qemu"""
1116        config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES:append = " efi"\n'
1117        self.append_config(config)
1118        bitbake('core-image-minimal')
1119        self.remove_config(config)
1120
1121        with runqemu('core-image-minimal', ssh=False,
1122                     runqemuparams='nographic', image_fstype='wic') as qemu:
1123            # Check that we have ONLY two /dev/sda* partitions (/boot and /)
1124            cmd = "grep sda. /proc/partitions | wc -l"
1125            status, output = qemu.run_serial(cmd)
1126            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1127            self.assertEqual(output, '2')
1128            # Check that /dev/sda1 is /boot and that either /dev/root OR /dev/sda2 is /
1129            cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' -e '/dev/root /|/dev/sda2 /'"
1130            status, output = qemu.run_serial(cmd)
1131            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1132            self.assertEqual(output, '2')
1133            # Check that /boot has EFI bootx64.efi (required for EFI)
1134            cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l"
1135            status, output = qemu.run_serial(cmd)
1136            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1137            self.assertEqual(output, '1')
1138            # Check that "BOOTABLE" flag is set on boot partition (required for PC-Bios)
1139            # Trailing "cat" seems to be required; otherwise run_serial() sends back echo of the input command
1140            cmd = "fdisk -l /dev/sda | grep /dev/sda1 | awk {print'$2'} | cat"
1141            status, output = qemu.run_serial(cmd)
1142            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1143            self.assertEqual(output, '*')
1144
1145    @only_for_arch(['i586', 'i686', 'x86_64'])
1146    def test_biosplusefi_plugin(self):
1147        """Test biosplusefi plugin"""
1148        # Wic generation below may fail depending on the order of the unittests
1149        # This is because bootimg-pcbios (that bootimg-biosplusefi uses) generate its MBR inside STAGING_DATADIR directory
1150        #    which may or may not exists depending on what was built already
1151        # If an image hasn't been built yet, directory ${STAGING_DATADIR}/syslinux won't exists and _get_bootimg_dir()
1152        #   will raise with "Couldn't find correct bootimg_dir"
1153        # The easiest way to work-around this issue is to make sure we already built an image here, hence the bitbake call
1154        config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES:append = " efi"\n'
1155        self.append_config(config)
1156        bitbake('core-image-minimal')
1157        self.remove_config(config)
1158
1159        img = 'core-image-minimal'
1160        with NamedTemporaryFile("w", suffix=".wks") as wks:
1161            wks.writelines(['part /boot --active --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\n',
1162                            'part / --source rootfs --fstype=ext4 --align 1024 --use-uuid\n'\
1163                            'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
1164            wks.flush()
1165            cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1166            runCmd(cmd)
1167            wksname = os.path.splitext(os.path.basename(wks.name))[0]
1168            out = glob(os.path.join(self.resultdir, "%s-*.direct" % wksname))
1169            self.assertEqual(1, len(out))
1170
1171    # TODO this test could also work on aarch64
1172    @only_for_arch(['i586', 'i686', 'x86_64'])
1173    @OETestTag("runqemu")
1174    def test_efi_plugin_unified_kernel_image_qemu(self):
1175        """Test efi plugin's Unified Kernel Image feature in qemu"""
1176        config = 'IMAGE_FSTYPES = "wic"\n'\
1177                 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
1178                 'WKS_FILE = "test_efi_plugin.wks"\n'\
1179                 'MACHINE_FEATURES:append = " efi"\n'
1180        self.append_config(config)
1181        bitbake('core-image-minimal core-image-minimal-initramfs ovmf')
1182        self.remove_config(config)
1183
1184        with runqemu('core-image-minimal', ssh=False,
1185                     runqemuparams='nographic ovmf', image_fstype='wic') as qemu:
1186            # Check that /boot has EFI bootx64.efi (required for EFI)
1187            cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l"
1188            status, output = qemu.run_serial(cmd)
1189            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1190            self.assertEqual(output, '1')
1191            # Check that /boot has EFI/Linux/linux.efi (required for Unified Kernel Images auto detection)
1192            cmd = "ls /boot/EFI/Linux/linux.efi | wc -l"
1193            status, output = qemu.run_serial(cmd)
1194            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1195            self.assertEqual(output, '1')
1196            # Check that /boot doesn't have loader/entries/boot.conf (Unified Kernel Images are auto detected by the bootloader)
1197            cmd = "ls /boot/loader/entries/boot.conf 2&>/dev/null | wc -l"
1198            status, output = qemu.run_serial(cmd)
1199            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1200            self.assertEqual(output, '0')
1201
1202    def test_fs_types(self):
1203        """Test filesystem types for empty and not empty partitions"""
1204        img = 'core-image-minimal'
1205        with NamedTemporaryFile("w", suffix=".wks") as wks:
1206            wks.writelines(['part ext2   --fstype ext2     --source rootfs\n',
1207                            'part btrfs  --fstype btrfs    --source rootfs --size 40M\n',
1208                            'part squash --fstype squashfs --source rootfs\n',
1209                            'part swap   --fstype swap --size 1M\n',
1210                            'part emptyvfat   --fstype vfat   --size 1M\n',
1211                            'part emptymsdos  --fstype msdos  --size 1M\n',
1212                            'part emptyext2   --fstype ext2   --size 1M\n',
1213                            'part emptybtrfs  --fstype btrfs  --size 150M\n'])
1214            wks.flush()
1215            cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1216            runCmd(cmd)
1217            wksname = os.path.splitext(os.path.basename(wks.name))[0]
1218            out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
1219            self.assertEqual(1, len(out))
1220
1221    def test_kickstart_parser(self):
1222        """Test wks parser options"""
1223        with NamedTemporaryFile("w", suffix=".wks") as wks:
1224            wks.writelines(['part / --fstype ext3 --source rootfs --system-id 0xFF '\
1225                            '--overhead-factor 1.2 --size 100k\n'])
1226            wks.flush()
1227            cmd = "wic create %s -e core-image-minimal -o %s" % (wks.name, self.resultdir)
1228            runCmd(cmd)
1229            wksname = os.path.splitext(os.path.basename(wks.name))[0]
1230            out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
1231            self.assertEqual(1, len(out))
1232
1233    def test_image_bootpart_globbed(self):
1234        """Test globbed sources with image-bootpart plugin"""
1235        img = "core-image-minimal"
1236        cmd = "wic create sdimage-bootpart -e %s -o %s" % (img, self.resultdir)
1237        config = 'IMAGE_BOOT_FILES = "%s*"' % get_bb_var('KERNEL_IMAGETYPE', img)
1238        self.append_config(config)
1239        runCmd(cmd)
1240        self.remove_config(config)
1241        self.assertEqual(1, len(glob(os.path.join(self.resultdir, "sdimage-bootpart-*direct"))))
1242
1243    def test_sparse_copy(self):
1244        """Test sparse_copy with FIEMAP and SEEK_HOLE filemap APIs"""
1245        libpath = os.path.join(self.td['COREBASE'], 'scripts', 'lib', 'wic')
1246        sys.path.insert(0, libpath)
1247        from  filemap import FilemapFiemap, FilemapSeek, sparse_copy, ErrorNotSupp
1248        with NamedTemporaryFile("w", suffix=".wic-sparse") as sparse:
1249            src_name = sparse.name
1250            src_size = 1024 * 10
1251            sparse.truncate(src_size)
1252            # write one byte to the file
1253            with open(src_name, 'r+b') as sfile:
1254                sfile.seek(1024 * 4)
1255                sfile.write(b'\x00')
1256            dest = sparse.name + '.out'
1257            # copy src file to dest using different filemap APIs
1258            for api in (FilemapFiemap, FilemapSeek, None):
1259                if os.path.exists(dest):
1260                    os.unlink(dest)
1261                try:
1262                    sparse_copy(sparse.name, dest, api=api)
1263                except ErrorNotSupp:
1264                    continue # skip unsupported API
1265                dest_stat = os.stat(dest)
1266                self.assertEqual(dest_stat.st_size, src_size)
1267                # 8 blocks is 4K (physical sector size)
1268                self.assertEqual(dest_stat.st_blocks, 8)
1269            os.unlink(dest)
1270
1271    def test_mkfs_extraopts(self):
1272        """Test wks option --mkfs-extraopts for empty and not empty partitions"""
1273        img = 'core-image-minimal'
1274        with NamedTemporaryFile("w", suffix=".wks") as wks:
1275            wks.writelines(
1276                ['part ext2   --fstype ext2     --source rootfs --mkfs-extraopts "-D -F -i 8192"\n',
1277                 "part btrfs  --fstype btrfs    --source rootfs --size 40M --mkfs-extraopts='--quiet'\n",
1278                 'part squash --fstype squashfs --source rootfs --mkfs-extraopts "-no-sparse -b 4096"\n',
1279                 'part emptyvfat   --fstype vfat   --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1280                 'part emptymsdos  --fstype msdos  --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1281                 'part emptyext2   --fstype ext2   --size 1M --mkfs-extraopts "-D -F -i 8192"\n',
1282                 'part emptybtrfs  --fstype btrfs  --size 100M --mkfs-extraopts "--mixed -K"\n'])
1283            wks.flush()
1284            cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1285            runCmd(cmd)
1286            wksname = os.path.splitext(os.path.basename(wks.name))[0]
1287            out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
1288            self.assertEqual(1, len(out))
1289
1290    @only_for_arch(['i586', 'i686', 'x86_64'])
1291    @OETestTag("runqemu")
1292    def test_expand_mbr_image(self):
1293        """Test wic write --expand command for mbr image"""
1294        # build an image
1295        config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n'
1296        self.append_config(config)
1297        bitbake('core-image-minimal')
1298
1299        # get path to the image
1300        deploy_dir = get_bb_var('DEPLOY_DIR_IMAGE')
1301        machine = self.td['MACHINE']
1302        image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine)
1303
1304        self.remove_config(config)
1305
1306        try:
1307            # expand image to 1G
1308            new_image_path = None
1309            with NamedTemporaryFile(mode='wb', suffix='.wic.exp',
1310                                    dir=deploy_dir, delete=False) as sparse:
1311                sparse.truncate(1024 ** 3)
1312                new_image_path = sparse.name
1313
1314            sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1315            cmd = "wic write -n %s --expand 1:0 %s %s" % (sysroot, image_path, new_image_path)
1316            runCmd(cmd)
1317
1318            # check if partitions are expanded
1319            orig = runCmd("wic ls %s -n %s" % (image_path, sysroot))
1320            exp = runCmd("wic ls %s -n %s" % (new_image_path, sysroot))
1321            orig_sizes = [int(line.split()[3]) for line in orig.output.split('\n')[1:]]
1322            exp_sizes = [int(line.split()[3]) for line in exp.output.split('\n')[1:]]
1323            self.assertEqual(orig_sizes[0], exp_sizes[0]) # first partition is not resized
1324            self.assertTrue(orig_sizes[1] < exp_sizes[1])
1325
1326            # Check if all free space is partitioned
1327            result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path))
1328            self.assertTrue("0 B, 0 bytes, 0 sectors" in result.output)
1329
1330            os.rename(image_path, image_path + '.bak')
1331            os.rename(new_image_path, image_path)
1332
1333            # Check if it boots in qemu
1334            with runqemu('core-image-minimal', ssh=False, runqemuparams='nographic') as qemu:
1335                cmd = "ls /etc/"
1336                status, output = qemu.run_serial('true')
1337                self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1338        finally:
1339            if os.path.exists(new_image_path):
1340                os.unlink(new_image_path)
1341            if os.path.exists(image_path + '.bak'):
1342                os.rename(image_path + '.bak', image_path)
1343
1344class ModifyTests(WicTestCase):
1345    def test_wic_ls(self):
1346        """Test listing image content using 'wic ls'"""
1347        runCmd("wic create wictestdisk "
1348                                   "--image-name=core-image-minimal "
1349                                   "-D -o %s" % self.resultdir)
1350        images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
1351        self.assertEqual(1, len(images))
1352
1353        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1354
1355        # list partitions
1356        result = runCmd("wic ls %s -n %s" % (images[0], sysroot))
1357        self.assertEqual(3, len(result.output.split('\n')))
1358
1359        # list directory content of the first partition
1360        result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
1361        self.assertEqual(6, len(result.output.split('\n')))
1362
1363    def test_wic_cp(self):
1364        """Test copy files and directories to the the wic image."""
1365        runCmd("wic create wictestdisk "
1366                                   "--image-name=core-image-minimal "
1367                                   "-D -o %s" % self.resultdir)
1368        images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
1369        self.assertEqual(1, len(images))
1370
1371        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1372
1373        # list directory content of the first partition
1374        result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
1375        self.assertEqual(6, len(result.output.split('\n')))
1376
1377        with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1378            testfile.write("test")
1379
1380            # copy file to the partition
1381            runCmd("wic cp %s %s:1/ -n %s" % (testfile.name, images[0], sysroot))
1382
1383            # check if file is there
1384            result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
1385            self.assertEqual(7, len(result.output.split('\n')))
1386            self.assertTrue(os.path.basename(testfile.name) in result.output)
1387
1388            # prepare directory
1389            testdir = os.path.join(self.resultdir, 'wic-test-cp-dir')
1390            testsubdir = os.path.join(testdir, 'subdir')
1391            os.makedirs(os.path.join(testsubdir))
1392            copy(testfile.name, testdir)
1393
1394            # copy directory to the partition
1395            runCmd("wic cp %s %s:1/ -n %s" % (testdir, images[0], sysroot))
1396
1397            # check if directory is there
1398            result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
1399            self.assertEqual(8, len(result.output.split('\n')))
1400            self.assertTrue(os.path.basename(testdir) in result.output)
1401
1402            # copy the file from the partition and check if it success
1403            dest = '%s-cp' % testfile.name
1404            runCmd("wic cp %s:1/%s %s -n %s" % (images[0],
1405                    os.path.basename(testfile.name), dest, sysroot))
1406            self.assertTrue(os.path.exists(dest))
1407
1408
1409    def test_wic_rm(self):
1410        """Test removing files and directories from the the wic image."""
1411        runCmd("wic create mkefidisk "
1412                                   "--image-name=core-image-minimal "
1413                                   "-D -o %s" % self.resultdir)
1414        images = glob(os.path.join(self.resultdir, "mkefidisk-*.direct"))
1415        self.assertEqual(1, len(images))
1416
1417        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1418        # Not bulletproof but hopefully sufficient
1419        kerneltype = get_bb_var('KERNEL_IMAGETYPE', 'virtual/kernel')
1420
1421        # list directory content of the first partition
1422        result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
1423        self.assertIn('\n%s        ' % kerneltype.upper(), result.output)
1424        self.assertIn('\nEFI          <DIR>     ', result.output)
1425
1426        # remove file. EFI partitions are case-insensitive so exercise that too
1427        runCmd("wic rm %s:1/%s -n %s" % (images[0], kerneltype.lower(), sysroot))
1428
1429        # remove directory
1430        runCmd("wic rm %s:1/efi -n %s" % (images[0], sysroot))
1431
1432        # check if they're removed
1433        result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
1434        self.assertNotIn('\n%s        ' % kerneltype.upper(), result.output)
1435        self.assertNotIn('\nEFI          <DIR>     ', result.output)
1436
1437    def test_wic_ls_ext(self):
1438        """Test listing content of the ext partition using 'wic ls'"""
1439        runCmd("wic create wictestdisk "
1440                                   "--image-name=core-image-minimal "
1441                                   "-D -o %s" % self.resultdir)
1442        images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
1443        self.assertEqual(1, len(images))
1444
1445        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1446
1447        # list directory content of the second ext4 partition
1448        result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
1449        self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(
1450                            set(line.split()[-1] for line in result.output.split('\n') if line)))
1451
1452    def test_wic_cp_ext(self):
1453        """Test copy files and directories to the ext partition."""
1454        runCmd("wic create wictestdisk "
1455                                   "--image-name=core-image-minimal "
1456                                   "-D -o %s" % self.resultdir)
1457        images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
1458        self.assertEqual(1, len(images))
1459
1460        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1461
1462        # list directory content of the ext4 partition
1463        result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
1464        dirs = set(line.split()[-1] for line in result.output.split('\n') if line)
1465        self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(dirs))
1466
1467        with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1468            testfile.write("test")
1469
1470            # copy file to the partition
1471            runCmd("wic cp %s %s:2/ -n %s" % (testfile.name, images[0], sysroot))
1472
1473            # check if file is there
1474            result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
1475            newdirs = set(line.split()[-1] for line in result.output.split('\n') if line)
1476            self.assertEqual(newdirs.difference(dirs), set([os.path.basename(testfile.name)]))
1477
1478            # check if the file to copy is in the partition
1479            result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
1480            self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line])
1481
1482            # copy file from the partition, replace the temporary file content with it and
1483            # check for the file size to validate the copy
1484            runCmd("wic cp %s:2/etc/fstab %s -n %s" % (images[0], testfile.name, sysroot))
1485            self.assertTrue(os.stat(testfile.name).st_size > 0)
1486
1487
1488    def test_wic_rm_ext(self):
1489        """Test removing files from the ext partition."""
1490        runCmd("wic create mkefidisk "
1491                                   "--image-name=core-image-minimal "
1492                                   "-D -o %s" % self.resultdir)
1493        images = glob(os.path.join(self.resultdir, "mkefidisk-*.direct"))
1494        self.assertEqual(1, len(images))
1495
1496        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1497
1498        # list directory content of the /etc directory on ext4 partition
1499        result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
1500        self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line])
1501
1502        # remove file
1503        runCmd("wic rm %s:2/etc/fstab -n %s" % (images[0], sysroot))
1504
1505        # check if it's removed
1506        result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
1507        self.assertTrue('fstab' not in [line.split()[-1] for line in result.output.split('\n') if line])
1508
1509        # remove non-empty directory
1510        runCmd("wic rm -r %s:2/etc/ -n %s" % (images[0], sysroot))
1511
1512        # check if it's removed
1513        result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
1514        self.assertTrue('etc' not in [line.split()[-1] for line in result.output.split('\n') if line])
1515