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