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