1#!/usr/bin/env python
2#
3# Copyright (c) 2015, Intel Corporation.
4#
5# SPDX-License-Identifier: GPL-2.0-only
6#
7# AUTHORS
8# Ed Bartosh <ed.bartosh@linux.intel.com>
9
10"""Test cases for wic."""
11
12import os
13import sys
14import unittest
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.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, runqemu
23
24
25@lru_cache(maxsize=32)
26def get_host_arch(recipe):
27    """A cached call to get_bb_var('HOST_ARCH', <recipe>)"""
28    return get_bb_var('HOST_ARCH', recipe)
29
30
31def only_for_arch(archs, image='core-image-minimal'):
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    Current architecture will be determined by parsing bitbake output for
35    `image` recipe.
36    """
37    def wrapper(func):
38        @wraps(func)
39        def wrapped_f(*args, **kwargs):
40            arch = get_host_arch(image)
41            if archs and arch not in archs:
42                raise unittest.SkipTest("Testcase arch dependency not met: %s" % arch)
43            return func(*args, **kwargs)
44        wrapped_f.__name__ = func.__name__
45        return wrapped_f
46    return wrapper
47
48
49class WicTestCase(OESelftestTestCase):
50    """Wic test class."""
51
52    image_is_ready = False
53    wicenv_cache = {}
54
55    def setUpLocal(self):
56        """This code is executed before each test method."""
57        self.resultdir = self.builddir + "/wic-tmp/"
58        super(WicTestCase, self).setUpLocal()
59
60        # Do this here instead of in setUpClass as the base setUp does some
61        # clean up which can result in the native tools built earlier in
62        # setUpClass being unavailable.
63        if not WicTestCase.image_is_ready:
64            if get_bb_var('USE_NLS') == 'yes':
65                bitbake('wic-tools')
66            else:
67                self.skipTest('wic-tools cannot be built due its (intltool|gettext)-native dependency and NLS disable')
68
69            bitbake('core-image-minimal')
70            WicTestCase.image_is_ready = True
71
72        rmtree(self.resultdir, ignore_errors=True)
73
74    def tearDownLocal(self):
75        """Remove resultdir as it may contain images."""
76        rmtree(self.resultdir, ignore_errors=True)
77        super(WicTestCase, self).tearDownLocal()
78
79    def _get_image_env_path(self, image):
80        """Generate and obtain the path to <image>.env"""
81        if image not in WicTestCase.wicenv_cache:
82            self.assertEqual(0, bitbake('%s -c do_rootfs_wicenv' % image).status)
83            bb_vars = get_bb_vars(['STAGING_DIR', 'MACHINE'], image)
84            stdir = bb_vars['STAGING_DIR']
85            machine = bb_vars['MACHINE']
86            WicTestCase.wicenv_cache[image] = os.path.join(stdir, machine, 'imgdata')
87        return WicTestCase.wicenv_cache[image]
88
89class Wic(WicTestCase):
90
91    def test_version(self):
92        """Test wic --version"""
93        runCmd('wic --version')
94
95    def test_help(self):
96        """Test wic --help and wic -h"""
97        runCmd('wic --help')
98        runCmd('wic -h')
99
100    def test_createhelp(self):
101        """Test wic create --help"""
102        runCmd('wic create --help')
103
104    def test_listhelp(self):
105        """Test wic list --help"""
106        runCmd('wic list --help')
107
108    def test_help_create(self):
109        """Test wic help create"""
110        runCmd('wic help create')
111
112    def test_help_list(self):
113        """Test wic help list"""
114        runCmd('wic help list')
115
116    def test_help_overview(self):
117        """Test wic help overview"""
118        runCmd('wic help overview')
119
120    def test_help_plugins(self):
121        """Test wic help plugins"""
122        runCmd('wic help plugins')
123
124    def test_help_kickstart(self):
125        """Test wic help kickstart"""
126        runCmd('wic help kickstart')
127
128    def test_list_images(self):
129        """Test wic list images"""
130        runCmd('wic list images')
131
132    def test_list_source_plugins(self):
133        """Test wic list source-plugins"""
134        runCmd('wic list source-plugins')
135
136    def test_listed_images_help(self):
137        """Test wic listed images help"""
138        output = runCmd('wic list images').output
139        imagelist = [line.split()[0] for line in output.splitlines()]
140        for image in imagelist:
141            runCmd('wic list %s help' % image)
142
143    def test_unsupported_subcommand(self):
144        """Test unsupported subcommand"""
145        self.assertNotEqual(0, runCmd('wic unsupported', ignore_status=True).status)
146
147    def test_no_command(self):
148        """Test wic without command"""
149        self.assertEqual(1, runCmd('wic', ignore_status=True).status)
150
151    def test_build_image_name(self):
152        """Test wic create wictestdisk --image-name=core-image-minimal"""
153        cmd = "wic create wictestdisk --image-name=core-image-minimal -o %s" % self.resultdir
154        runCmd(cmd)
155        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
156
157    @only_for_arch(['i586', 'i686', 'x86_64'])
158    def test_gpt_image(self):
159        """Test creation of core-image-minimal with gpt table and UUID boot"""
160        cmd = "wic create directdisk-gpt --image-name core-image-minimal -o %s" % self.resultdir
161        runCmd(cmd)
162        self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct")))
163
164    @only_for_arch(['i586', 'i686', 'x86_64'])
165    def test_iso_image(self):
166        """Test creation of hybrid iso image with legacy and EFI boot"""
167        config = 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
168                 'MACHINE_FEATURES_append = " efi"\n'\
169                 'DEPENDS_pn-core-image-minimal += "syslinux"\n'
170        self.append_config(config)
171        bitbake('core-image-minimal core-image-minimal-initramfs')
172        self.remove_config(config)
173        cmd = "wic create mkhybridiso --image-name core-image-minimal -o %s" % self.resultdir
174        runCmd(cmd)
175        self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.direct")))
176        self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.iso")))
177
178    @only_for_arch(['i586', 'i686', 'x86_64'])
179    def test_qemux86_directdisk(self):
180        """Test creation of qemux-86-directdisk image"""
181        cmd = "wic create qemux86-directdisk -e core-image-minimal -o %s" % self.resultdir
182        runCmd(cmd)
183        self.assertEqual(1, len(glob(self.resultdir + "qemux86-directdisk-*direct")))
184
185    @only_for_arch(['i586', 'i686', 'x86_64'])
186    def test_mkefidisk(self):
187        """Test creation of mkefidisk image"""
188        cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
189        runCmd(cmd)
190        self.assertEqual(1, len(glob(self.resultdir + "mkefidisk-*direct")))
191
192    @only_for_arch(['i586', 'i686', 'x86_64'])
193    def test_bootloader_config(self):
194        """Test creation of directdisk-bootloader-config image"""
195        config = 'DEPENDS_pn-core-image-minimal += "syslinux"\n'
196        self.append_config(config)
197        bitbake('core-image-minimal')
198        self.remove_config(config)
199        cmd = "wic create directdisk-bootloader-config -e core-image-minimal -o %s" % self.resultdir
200        runCmd(cmd)
201        self.assertEqual(1, len(glob(self.resultdir + "directdisk-bootloader-config-*direct")))
202
203    @only_for_arch(['i586', 'i686', 'x86_64'])
204    def test_systemd_bootdisk(self):
205        """Test creation of systemd-bootdisk image"""
206        config = 'MACHINE_FEATURES_append = " efi"\n'
207        self.append_config(config)
208        bitbake('core-image-minimal')
209        self.remove_config(config)
210        cmd = "wic create systemd-bootdisk -e core-image-minimal -o %s" % self.resultdir
211        runCmd(cmd)
212        self.assertEqual(1, len(glob(self.resultdir + "systemd-bootdisk-*direct")))
213
214    def test_sdimage_bootpart(self):
215        """Test creation of sdimage-bootpart image"""
216        cmd = "wic create sdimage-bootpart -e core-image-minimal -o %s" % self.resultdir
217        kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
218        self.write_config('IMAGE_BOOT_FILES = "%s"\n' % kimgtype)
219        runCmd(cmd)
220        self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct")))
221
222    @only_for_arch(['i586', 'i686', 'x86_64'])
223    def test_default_output_dir(self):
224        """Test default output location"""
225        for fname in glob("directdisk-*.direct"):
226            os.remove(fname)
227        config = 'DEPENDS_pn-core-image-minimal += "syslinux"\n'
228        self.append_config(config)
229        bitbake('core-image-minimal')
230        self.remove_config(config)
231        cmd = "wic create directdisk -e core-image-minimal"
232        runCmd(cmd)
233        self.assertEqual(1, len(glob("directdisk-*.direct")))
234
235    @only_for_arch(['i586', 'i686', 'x86_64'])
236    def test_build_artifacts(self):
237        """Test wic create directdisk providing all artifacts."""
238        bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
239                              'wic-tools')
240        bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
241                                   'core-image-minimal'))
242        bbvars = {key.lower(): value for key, value in bb_vars.items()}
243        bbvars['resultdir'] = self.resultdir
244        runCmd("wic create directdisk "
245                        "-b %(staging_datadir)s "
246                        "-k %(deploy_dir_image)s "
247                        "-n %(recipe_sysroot_native)s "
248                        "-r %(image_rootfs)s "
249                        "-o %(resultdir)s" % bbvars)
250        self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct")))
251
252    def test_compress_gzip(self):
253        """Test compressing an image with gzip"""
254        runCmd("wic create wictestdisk "
255                                   "--image-name core-image-minimal "
256                                   "-c gzip -o %s" % self.resultdir)
257        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.gz")))
258
259    def test_compress_bzip2(self):
260        """Test compressing an image with bzip2"""
261        runCmd("wic create wictestdisk "
262                                   "--image-name=core-image-minimal "
263                                   "-c bzip2 -o %s" % self.resultdir)
264        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.bz2")))
265
266    def test_compress_xz(self):
267        """Test compressing an image with xz"""
268        runCmd("wic create wictestdisk "
269                                   "--image-name=core-image-minimal "
270                                   "--compress-with=xz -o %s" % self.resultdir)
271        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.xz")))
272
273    def test_wrong_compressor(self):
274        """Test how wic breaks if wrong compressor is provided"""
275        self.assertEqual(2, runCmd("wic create wictestdisk "
276                                   "--image-name=core-image-minimal "
277                                   "-c wrong -o %s" % self.resultdir,
278                                   ignore_status=True).status)
279
280    def test_debug_short(self):
281        """Test -D option"""
282        runCmd("wic create wictestdisk "
283                                   "--image-name=core-image-minimal "
284                                   "-D -o %s" % self.resultdir)
285        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
286
287    def test_debug_long(self):
288        """Test --debug option"""
289        runCmd("wic create wictestdisk "
290                                   "--image-name=core-image-minimal "
291                                   "--debug -o %s" % self.resultdir)
292        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
293
294    def test_skip_build_check_short(self):
295        """Test -s option"""
296        runCmd("wic create wictestdisk "
297                                   "--image-name=core-image-minimal "
298                                   "-s -o %s" % self.resultdir)
299        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
300
301    def test_skip_build_check_long(self):
302        """Test --skip-build-check option"""
303        runCmd("wic create wictestdisk "
304                                   "--image-name=core-image-minimal "
305                                   "--skip-build-check "
306                                   "--outdir %s" % self.resultdir)
307        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
308
309    def test_build_rootfs_short(self):
310        """Test -f option"""
311        runCmd("wic create wictestdisk "
312                                   "--image-name=core-image-minimal "
313                                   "-f -o %s" % self.resultdir)
314        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
315
316    def test_build_rootfs_long(self):
317        """Test --build-rootfs option"""
318        runCmd("wic create wictestdisk "
319                                   "--image-name=core-image-minimal "
320                                   "--build-rootfs "
321                                   "--outdir %s" % self.resultdir)
322        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
323
324    @only_for_arch(['i586', 'i686', 'x86_64'])
325    def test_rootfs_indirect_recipes(self):
326        """Test usage of rootfs plugin with rootfs recipes"""
327        runCmd("wic create directdisk-multi-rootfs "
328                        "--image-name=core-image-minimal "
329                        "--rootfs rootfs1=core-image-minimal "
330                        "--rootfs rootfs2=core-image-minimal "
331                        "--outdir %s" % self.resultdir)
332        self.assertEqual(1, len(glob(self.resultdir + "directdisk-multi-rootfs*.direct")))
333
334    @only_for_arch(['i586', 'i686', 'x86_64'])
335    def test_rootfs_artifacts(self):
336        """Test usage of rootfs plugin with rootfs paths"""
337        bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
338                              'wic-tools')
339        bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
340                                   'core-image-minimal'))
341        bbvars = {key.lower(): value for key, value in bb_vars.items()}
342        bbvars['wks'] = "directdisk-multi-rootfs"
343        bbvars['resultdir'] = self.resultdir
344        runCmd("wic create %(wks)s "
345                        "--bootimg-dir=%(staging_datadir)s "
346                        "--kernel-dir=%(deploy_dir_image)s "
347                        "--native-sysroot=%(recipe_sysroot_native)s "
348                        "--rootfs-dir rootfs1=%(image_rootfs)s "
349                        "--rootfs-dir rootfs2=%(image_rootfs)s "
350                        "--outdir %(resultdir)s" % bbvars)
351        self.assertEqual(1, len(glob(self.resultdir + "%(wks)s-*.direct" % bbvars)))
352
353    def test_exclude_path(self):
354        """Test --exclude-path wks option."""
355
356        oldpath = os.environ['PATH']
357        os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
358
359        try:
360            wks_file = 'temp.wks'
361            with open(wks_file, 'w') as wks:
362                rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
363                wks.write("""
364part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path usr
365part /usr --source rootfs --ondisk mmcblk0 --fstype=ext4 --rootfs-dir %s/usr
366part /etc --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path bin/ --rootfs-dir %s/usr"""
367                          % (rootfs_dir, rootfs_dir))
368            runCmd("wic create %s -e core-image-minimal -o %s" \
369                                       % (wks_file, self.resultdir))
370
371            os.remove(wks_file)
372            wicout = glob(self.resultdir + "%s-*direct" % 'temp')
373            self.assertEqual(1, len(wicout))
374
375            wicimg = wicout[0]
376
377            # verify partition size with wic
378            res = runCmd("parted -m %s unit b p 2>/dev/null" % wicimg)
379
380            # parse parted output which looks like this:
381            # BYT;\n
382            # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
383            # 1:0.00MiB:200MiB:200MiB:ext4::;\n
384            partlns = res.output.splitlines()[2:]
385
386            self.assertEqual(3, len(partlns))
387
388            for part in [1, 2, 3]:
389                part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
390                partln = partlns[part-1].split(":")
391                self.assertEqual(7, len(partln))
392                start = int(partln[1].rstrip("B")) / 512
393                length = int(partln[3].rstrip("B")) / 512
394                runCmd("dd if=%s of=%s skip=%d count=%d" %
395                                           (wicimg, part_file, start, length))
396
397            def extract_files(debugfs_output):
398                """
399                extract file names from the output of debugfs -R 'ls -p',
400                which looks like this:
401
402                 /2/040755/0/0/.//\n
403                 /2/040755/0/0/..//\n
404                 /11/040700/0/0/lost+found^M//\n
405                 /12/040755/1002/1002/run//\n
406                 /13/040755/1002/1002/sys//\n
407                 /14/040755/1002/1002/bin//\n
408                 /80/040755/1002/1002/var//\n
409                 /92/040755/1002/1002/tmp//\n
410                """
411                # NOTE the occasional ^M in file names
412                return [line.split('/')[5].strip() for line in \
413                        debugfs_output.strip().split('/\n')]
414
415            # Test partition 1, should contain the normal root directories, except
416            # /usr.
417            res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
418                             os.path.join(self.resultdir, "selftest_img.part1"))
419            files = extract_files(res.output)
420            self.assertIn("etc", files)
421            self.assertNotIn("usr", files)
422
423            # Partition 2, should contain common directories for /usr, not root
424            # directories.
425            res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
426                             os.path.join(self.resultdir, "selftest_img.part2"))
427            files = extract_files(res.output)
428            self.assertNotIn("etc", files)
429            self.assertNotIn("usr", files)
430            self.assertIn("share", files)
431
432            # Partition 3, should contain the same as partition 2, including the bin
433            # directory, but not the files inside it.
434            res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
435                             os.path.join(self.resultdir, "selftest_img.part3"))
436            files = extract_files(res.output)
437            self.assertNotIn("etc", files)
438            self.assertNotIn("usr", files)
439            self.assertIn("share", files)
440            self.assertIn("bin", files)
441            res = runCmd("debugfs -R 'ls -p bin' %s 2>/dev/null" % \
442                             os.path.join(self.resultdir, "selftest_img.part3"))
443            files = extract_files(res.output)
444            self.assertIn(".", files)
445            self.assertIn("..", files)
446            self.assertEqual(2, len(files))
447
448            for part in [1, 2, 3]:
449                part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
450                os.remove(part_file)
451
452        finally:
453            os.environ['PATH'] = oldpath
454
455    def test_exclude_path_errors(self):
456        """Test --exclude-path wks option error handling."""
457        wks_file = 'temp.wks'
458
459        # Absolute argument.
460        with open(wks_file, 'w') as wks:
461            wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path /usr")
462        self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
463                                      % (wks_file, self.resultdir), ignore_status=True).status)
464        os.remove(wks_file)
465
466        # Argument pointing to parent directory.
467        with open(wks_file, 'w') as wks:
468            wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path ././..")
469        self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
470                                      % (wks_file, self.resultdir), ignore_status=True).status)
471        os.remove(wks_file)
472
473class Wic2(WicTestCase):
474
475    def test_bmap_short(self):
476        """Test generation of .bmap file -m option"""
477        cmd = "wic create wictestdisk -e core-image-minimal -m -o %s" % self.resultdir
478        runCmd(cmd)
479        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
480        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap")))
481
482    def test_bmap_long(self):
483        """Test generation of .bmap file --bmap option"""
484        cmd = "wic create wictestdisk -e core-image-minimal --bmap -o %s" % self.resultdir
485        runCmd(cmd)
486        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
487        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap")))
488
489    def test_image_env(self):
490        """Test generation of <image>.env files."""
491        image = 'core-image-minimal'
492        imgdatadir = self._get_image_env_path(image)
493
494        bb_vars = get_bb_vars(['IMAGE_BASENAME', 'WICVARS'], image)
495        basename = bb_vars['IMAGE_BASENAME']
496        self.assertEqual(basename, image)
497        path = os.path.join(imgdatadir, basename) + '.env'
498        self.assertTrue(os.path.isfile(path))
499
500        wicvars = set(bb_vars['WICVARS'].split())
501        # filter out optional variables
502        wicvars = wicvars.difference(('DEPLOY_DIR_IMAGE', 'IMAGE_BOOT_FILES',
503                                      'INITRD', 'INITRD_LIVE', 'ISODIR'))
504        with open(path) as envfile:
505            content = dict(line.split("=", 1) for line in envfile)
506            # test if variables used by wic present in the .env file
507            for var in wicvars:
508                self.assertTrue(var in content, "%s is not in .env file" % var)
509                self.assertTrue(content[var])
510
511    def test_image_vars_dir_short(self):
512        """Test image vars directory selection -v option"""
513        image = 'core-image-minimal'
514        imgenvdir = self._get_image_env_path(image)
515        native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
516
517        runCmd("wic create wictestdisk "
518                                   "--image-name=%s -v %s -n %s -o %s"
519                                   % (image, imgenvdir, native_sysroot,
520                                      self.resultdir))
521        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
522
523    def test_image_vars_dir_long(self):
524        """Test image vars directory selection --vars option"""
525        image = 'core-image-minimal'
526        imgenvdir = self._get_image_env_path(image)
527        native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
528
529        runCmd("wic create wictestdisk "
530                                   "--image-name=%s "
531                                   "--vars %s "
532                                   "--native-sysroot %s "
533                                   "--outdir %s"
534                                   % (image, imgenvdir, native_sysroot,
535                                      self.resultdir))
536        self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
537
538    @only_for_arch(['i586', 'i686', 'x86_64'])
539    def test_wic_image_type(self):
540        """Test building wic images by bitbake"""
541        config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
542                 'MACHINE_FEATURES_append = " efi"\n'
543        self.append_config(config)
544        self.assertEqual(0, bitbake('wic-image-minimal').status)
545        self.remove_config(config)
546
547        bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
548        deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
549        machine = bb_vars['MACHINE']
550        prefix = os.path.join(deploy_dir, 'wic-image-minimal-%s.' % machine)
551        # check if we have result image and manifests symlinks
552        # pointing to existing files
553        for suffix in ('wic', 'manifest'):
554            path = prefix + suffix
555            self.assertTrue(os.path.islink(path))
556            self.assertTrue(os.path.isfile(os.path.realpath(path)))
557
558    @only_for_arch(['i586', 'i686', 'x86_64'])
559    def test_qemu(self):
560        """Test wic-image-minimal under qemu"""
561        config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
562                 'MACHINE_FEATURES_append = " efi"\n'
563        self.append_config(config)
564        self.assertEqual(0, bitbake('wic-image-minimal').status)
565        self.remove_config(config)
566
567        with runqemu('wic-image-minimal', ssh=False) as qemu:
568            cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' " \
569                  "-e '/dev/root /|/dev/sda2 /' -e '/dev/sda3 /media' -e '/dev/sda4 /mnt'"
570            status, output = qemu.run_serial(cmd)
571            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
572            self.assertEqual(output, '4')
573            cmd = "grep UUID= /etc/fstab"
574            status, output = qemu.run_serial(cmd)
575            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
576            self.assertEqual(output, 'UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba\t/media\text4\tdefaults\t0\t0')
577
578    @only_for_arch(['i586', 'i686', 'x86_64'])
579    def test_qemu_efi(self):
580        """Test core-image-minimal efi image under qemu"""
581        config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "mkefidisk.wks"\n'
582        self.append_config(config)
583        self.assertEqual(0, bitbake('core-image-minimal ovmf').status)
584        self.remove_config(config)
585
586        with runqemu('core-image-minimal', ssh=False,
587                     runqemuparams='ovmf', image_fstype='wic') as qemu:
588            cmd = "grep sda. /proc/partitions  |wc -l"
589            status, output = qemu.run_serial(cmd)
590            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
591            self.assertEqual(output, '3')
592
593    @staticmethod
594    def _make_fixed_size_wks(size):
595        """
596        Create a wks of an image with a single partition. Size of the partition is set
597        using --fixed-size flag. Returns a tuple: (path to wks file, wks image name)
598        """
599        with NamedTemporaryFile("w", suffix=".wks", delete=False) as tempf:
600            wkspath = tempf.name
601            tempf.write("part " \
602                     "--source rootfs --ondisk hda --align 4 --fixed-size %d "
603                     "--fstype=ext4\n" % size)
604        wksname = os.path.splitext(os.path.basename(wkspath))[0]
605
606        return wkspath, wksname
607
608    def test_fixed_size(self):
609        """
610        Test creation of a simple image with partition size controlled through
611        --fixed-size flag
612        """
613        wkspath, wksname = Wic2._make_fixed_size_wks(200)
614
615        runCmd("wic create %s -e core-image-minimal -o %s" \
616                                   % (wkspath, self.resultdir))
617        os.remove(wkspath)
618        wicout = glob(self.resultdir + "%s-*direct" % wksname)
619        self.assertEqual(1, len(wicout))
620
621        wicimg = wicout[0]
622
623        native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
624
625        # verify partition size with wic
626        res = runCmd("parted -m %s unit mib p 2>/dev/null" % wicimg,
627                     native_sysroot=native_sysroot)
628
629        # parse parted output which looks like this:
630        # BYT;\n
631        # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
632        # 1:0.00MiB:200MiB:200MiB:ext4::;\n
633        partlns = res.output.splitlines()[2:]
634
635        self.assertEqual(1, len(partlns))
636        self.assertEqual("1:0.00MiB:200MiB:200MiB:ext4::;", partlns[0])
637
638    def test_fixed_size_error(self):
639        """
640        Test creation of a simple image with partition size controlled through
641        --fixed-size flag. The size of partition is intentionally set to 1MiB
642        in order to trigger an error in wic.
643        """
644        wkspath, wksname = Wic2._make_fixed_size_wks(1)
645
646        self.assertEqual(1, runCmd("wic create %s -e core-image-minimal -o %s" \
647                                   % (wkspath, self.resultdir), ignore_status=True).status)
648        os.remove(wkspath)
649        wicout = glob(self.resultdir + "%s-*direct" % wksname)
650        self.assertEqual(0, len(wicout))
651
652    @only_for_arch(['i586', 'i686', 'x86_64'])
653    def test_rawcopy_plugin_qemu(self):
654        """Test rawcopy plugin in qemu"""
655        # build ext4 and wic images
656        for fstype in ("ext4", "wic"):
657            config = 'IMAGE_FSTYPES = "%s"\nWKS_FILE = "test_rawcopy_plugin.wks.in"\n' % fstype
658            self.append_config(config)
659            self.assertEqual(0, bitbake('core-image-minimal').status)
660            self.remove_config(config)
661
662        with runqemu('core-image-minimal', ssh=False, image_fstype='wic') as qemu:
663            cmd = "grep sda. /proc/partitions  |wc -l"
664            status, output = qemu.run_serial(cmd)
665            self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
666            self.assertEqual(output, '2')
667
668    def test_rawcopy_plugin(self):
669        """Test rawcopy plugin"""
670        img = 'core-image-minimal'
671        machine = get_bb_var('MACHINE', img)
672        with NamedTemporaryFile("w", suffix=".wks") as wks:
673            wks.writelines(['part /boot --active --source bootimg-pcbios\n',
674                            'part / --source rawcopy --sourceparams="file=%s-%s.ext4" --use-uuid\n'\
675                             % (img, machine),
676                            'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
677            wks.flush()
678            cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
679            runCmd(cmd)
680            wksname = os.path.splitext(os.path.basename(wks.name))[0]
681            out = glob(self.resultdir + "%s-*direct" % wksname)
682            self.assertEqual(1, len(out))
683
684    def test_fs_types(self):
685        """Test filesystem types for empty and not empty partitions"""
686        img = 'core-image-minimal'
687        with NamedTemporaryFile("w", suffix=".wks") as wks:
688            wks.writelines(['part ext2   --fstype ext2     --source rootfs\n',
689                            'part btrfs  --fstype btrfs    --source rootfs --size 40M\n',
690                            'part squash --fstype squashfs --source rootfs\n',
691                            'part swap   --fstype swap --size 1M\n',
692                            'part emptyvfat   --fstype vfat   --size 1M\n',
693                            'part emptymsdos  --fstype msdos  --size 1M\n',
694                            'part emptyext2   --fstype ext2   --size 1M\n',
695                            'part emptybtrfs  --fstype btrfs  --size 150M\n'])
696            wks.flush()
697            cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
698            runCmd(cmd)
699            wksname = os.path.splitext(os.path.basename(wks.name))[0]
700            out = glob(self.resultdir + "%s-*direct" % wksname)
701            self.assertEqual(1, len(out))
702
703    def test_kickstart_parser(self):
704        """Test wks parser options"""
705        with NamedTemporaryFile("w", suffix=".wks") as wks:
706            wks.writelines(['part / --fstype ext3 --source rootfs --system-id 0xFF '\
707                            '--overhead-factor 1.2 --size 100k\n'])
708            wks.flush()
709            cmd = "wic create %s -e core-image-minimal -o %s" % (wks.name, self.resultdir)
710            runCmd(cmd)
711            wksname = os.path.splitext(os.path.basename(wks.name))[0]
712            out = glob(self.resultdir + "%s-*direct" % wksname)
713            self.assertEqual(1, len(out))
714
715    def test_image_bootpart_globbed(self):
716        """Test globbed sources with image-bootpart plugin"""
717        img = "core-image-minimal"
718        cmd = "wic create sdimage-bootpart -e %s -o %s" % (img, self.resultdir)
719        config = 'IMAGE_BOOT_FILES = "%s*"' % get_bb_var('KERNEL_IMAGETYPE', img)
720        self.append_config(config)
721        runCmd(cmd)
722        self.remove_config(config)
723        self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct")))
724
725    def test_sparse_copy(self):
726        """Test sparse_copy with FIEMAP and SEEK_HOLE filemap APIs"""
727        libpath = os.path.join(get_bb_var('COREBASE'), 'scripts', 'lib', 'wic')
728        sys.path.insert(0, libpath)
729        from  filemap import FilemapFiemap, FilemapSeek, sparse_copy, ErrorNotSupp
730        with NamedTemporaryFile("w", suffix=".wic-sparse") as sparse:
731            src_name = sparse.name
732            src_size = 1024 * 10
733            sparse.truncate(src_size)
734            # write one byte to the file
735            with open(src_name, 'r+b') as sfile:
736                sfile.seek(1024 * 4)
737                sfile.write(b'\x00')
738            dest = sparse.name + '.out'
739            # copy src file to dest using different filemap APIs
740            for api in (FilemapFiemap, FilemapSeek, None):
741                if os.path.exists(dest):
742                    os.unlink(dest)
743                try:
744                    sparse_copy(sparse.name, dest, api=api)
745                except ErrorNotSupp:
746                    continue # skip unsupported API
747                dest_stat = os.stat(dest)
748                self.assertEqual(dest_stat.st_size, src_size)
749                # 8 blocks is 4K (physical sector size)
750                self.assertEqual(dest_stat.st_blocks, 8)
751            os.unlink(dest)
752
753    def test_wic_ls(self):
754        """Test listing image content using 'wic ls'"""
755        runCmd("wic create wictestdisk "
756                                   "--image-name=core-image-minimal "
757                                   "-D -o %s" % self.resultdir)
758        images = glob(self.resultdir + "wictestdisk-*.direct")
759        self.assertEqual(1, len(images))
760
761        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
762
763        # list partitions
764        result = runCmd("wic ls %s -n %s" % (images[0], sysroot))
765        self.assertEqual(3, len(result.output.split('\n')))
766
767        # list directory content of the first partition
768        result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
769        self.assertEqual(6, len(result.output.split('\n')))
770
771    def test_wic_cp(self):
772        """Test copy files and directories to the the wic image."""
773        runCmd("wic create wictestdisk "
774                                   "--image-name=core-image-minimal "
775                                   "-D -o %s" % self.resultdir)
776        images = glob(self.resultdir + "wictestdisk-*.direct")
777        self.assertEqual(1, len(images))
778
779        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
780
781        # list directory content of the first partition
782        result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
783        self.assertEqual(6, len(result.output.split('\n')))
784
785        with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
786            testfile.write("test")
787
788            # copy file to the partition
789            runCmd("wic cp %s %s:1/ -n %s" % (testfile.name, images[0], sysroot))
790
791            # check if file is there
792            result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
793            self.assertEqual(7, len(result.output.split('\n')))
794            self.assertTrue(os.path.basename(testfile.name) in result.output)
795
796            # prepare directory
797            testdir = os.path.join(self.resultdir, 'wic-test-cp-dir')
798            testsubdir = os.path.join(testdir, 'subdir')
799            os.makedirs(os.path.join(testsubdir))
800            copy(testfile.name, testdir)
801
802            # copy directory to the partition
803            runCmd("wic cp %s %s:1/ -n %s" % (testdir, images[0], sysroot))
804
805            # check if directory is there
806            result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
807            self.assertEqual(8, len(result.output.split('\n')))
808            self.assertTrue(os.path.basename(testdir) in result.output)
809
810    def test_wic_rm(self):
811        """Test removing files and directories from the the wic image."""
812        runCmd("wic create mkefidisk "
813                                   "--image-name=core-image-minimal "
814                                   "-D -o %s" % self.resultdir)
815        images = glob(self.resultdir + "mkefidisk-*.direct")
816        self.assertEqual(1, len(images))
817
818        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
819
820        # list directory content of the first partition
821        result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
822        self.assertIn('\nBZIMAGE        ', result.output)
823        self.assertIn('\nEFI          <DIR>     ', result.output)
824
825        # remove file
826        runCmd("wic rm %s:1/bzimage -n %s" % (images[0], sysroot))
827
828        # remove directory
829        runCmd("wic rm %s:1/efi -n %s" % (images[0], sysroot))
830
831        # check if they're removed
832        result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
833        self.assertNotIn('\nBZIMAGE        ', result.output)
834        self.assertNotIn('\nEFI          <DIR>     ', result.output)
835
836    def test_mkfs_extraopts(self):
837        """Test wks option --mkfs-extraopts for empty and not empty partitions"""
838        img = 'core-image-minimal'
839        with NamedTemporaryFile("w", suffix=".wks") as wks:
840            wks.writelines(
841                ['part ext2   --fstype ext2     --source rootfs --mkfs-extraopts "-D -F -i 8192"\n',
842                 "part btrfs  --fstype btrfs    --source rootfs --size 40M --mkfs-extraopts='--quiet'\n",
843                 'part squash --fstype squashfs --source rootfs --mkfs-extraopts "-no-sparse -b 4096"\n',
844                 'part emptyvfat   --fstype vfat   --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
845                 'part emptymsdos  --fstype msdos  --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
846                 'part emptyext2   --fstype ext2   --size 1M --mkfs-extraopts "-D -F -i 8192"\n',
847                 'part emptybtrfs  --fstype btrfs  --size 100M --mkfs-extraopts "--mixed -K"\n'])
848            wks.flush()
849            cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
850            runCmd(cmd)
851            wksname = os.path.splitext(os.path.basename(wks.name))[0]
852            out = glob(self.resultdir + "%s-*direct" % wksname)
853            self.assertEqual(1, len(out))
854
855    def test_expand_mbr_image(self):
856        """Test wic write --expand command for mbr image"""
857        # build an image
858        config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n'
859        self.append_config(config)
860        self.assertEqual(0, bitbake('core-image-minimal').status)
861
862        # get path to the image
863        bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
864        deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
865        machine = bb_vars['MACHINE']
866        image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine)
867
868        self.remove_config(config)
869
870        try:
871            # expand image to 1G
872            new_image_path = None
873            with NamedTemporaryFile(mode='wb', suffix='.wic.exp',
874                                    dir=deploy_dir, delete=False) as sparse:
875                sparse.truncate(1024 ** 3)
876                new_image_path = sparse.name
877
878            sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
879            cmd = "wic write -n %s --expand 1:0 %s %s" % (sysroot, image_path, new_image_path)
880            runCmd(cmd)
881
882            # check if partitions are expanded
883            orig = runCmd("wic ls %s -n %s" % (image_path, sysroot))
884            exp = runCmd("wic ls %s -n %s" % (new_image_path, sysroot))
885            orig_sizes = [int(line.split()[3]) for line in orig.output.split('\n')[1:]]
886            exp_sizes = [int(line.split()[3]) for line in exp.output.split('\n')[1:]]
887            self.assertEqual(orig_sizes[0], exp_sizes[0]) # first partition is not resized
888            self.assertTrue(orig_sizes[1] < exp_sizes[1])
889
890            # Check if all free space is partitioned
891            result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path))
892            self.assertTrue("0 B, 0 bytes, 0 sectors" in result.output)
893
894            os.rename(image_path, image_path + '.bak')
895            os.rename(new_image_path, image_path)
896
897            # Check if it boots in qemu
898            with runqemu('core-image-minimal', ssh=False) as qemu:
899                cmd = "ls /etc/"
900                status, output = qemu.run_serial('true')
901                self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
902        finally:
903            if os.path.exists(new_image_path):
904                os.unlink(new_image_path)
905            if os.path.exists(image_path + '.bak'):
906                os.rename(image_path + '.bak', image_path)
907
908    def test_wic_ls_ext(self):
909        """Test listing content of the ext partition using 'wic ls'"""
910        runCmd("wic create wictestdisk "
911                                   "--image-name=core-image-minimal "
912                                   "-D -o %s" % self.resultdir)
913        images = glob(self.resultdir + "wictestdisk-*.direct")
914        self.assertEqual(1, len(images))
915
916        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
917
918        # list directory content of the second ext4 partition
919        result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
920        self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(
921                            set(line.split()[-1] for line in result.output.split('\n') if line)))
922
923    def test_wic_cp_ext(self):
924        """Test copy files and directories to the ext partition."""
925        runCmd("wic create wictestdisk "
926                                   "--image-name=core-image-minimal "
927                                   "-D -o %s" % self.resultdir)
928        images = glob(self.resultdir + "wictestdisk-*.direct")
929        self.assertEqual(1, len(images))
930
931        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
932
933        # list directory content of the ext4 partition
934        result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
935        dirs = set(line.split()[-1] for line in result.output.split('\n') if line)
936        self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(dirs))
937
938        with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
939            testfile.write("test")
940
941            # copy file to the partition
942            runCmd("wic cp %s %s:2/ -n %s" % (testfile.name, images[0], sysroot))
943
944            # check if file is there
945            result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
946            newdirs = set(line.split()[-1] for line in result.output.split('\n') if line)
947            self.assertEqual(newdirs.difference(dirs), set([os.path.basename(testfile.name)]))
948
949    def test_wic_rm_ext(self):
950        """Test removing files from the ext partition."""
951        runCmd("wic create mkefidisk "
952                                   "--image-name=core-image-minimal "
953                                   "-D -o %s" % self.resultdir)
954        images = glob(self.resultdir + "mkefidisk-*.direct")
955        self.assertEqual(1, len(images))
956
957        sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
958
959        # list directory content of the /etc directory on ext4 partition
960        result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
961        self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line])
962
963        # remove file
964        runCmd("wic rm %s:2/etc/fstab -n %s" % (images[0], sysroot))
965
966        # check if it's removed
967        result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
968        self.assertTrue('fstab' not in [line.split()[-1] for line in result.output.split('\n') if line])
969