blob: ea75300406e045261b5de7ff058003e706b1ffd5 [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001#
2# Copyright (c) 2015, Intel Corporation.
Brad Bishopd7bf8c12018-02-25 22:55:05 -05003#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005#
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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050022
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
47
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080048class WicTestCase(OESelftestTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050049 """Wic test class."""
50
Brad Bishopd7bf8c12018-02-25 22:55:05 -050051 image_is_ready = False
Brad Bishopd7bf8c12018-02-25 22:55:05 -050052 wicenv_cache = {}
53
54 def setUpLocal(self):
55 """This code is executed before each test method."""
Brad Bishopc4ea0752018-11-15 14:30:15 -080056 self.resultdir = self.builddir + "/wic-tmp/"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080057 super(WicTestCase, self).setUpLocal()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050058
59 # Do this here instead of in setUpClass as the base setUp does some
60 # clean up which can result in the native tools built earlier in
61 # setUpClass being unavailable.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080062 if not WicTestCase.image_is_ready:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050063 if get_bb_var('USE_NLS') == 'yes':
64 bitbake('wic-tools')
65 else:
66 self.skipTest('wic-tools cannot be built due its (intltool|gettext)-native dependency and NLS disable')
67
68 bitbake('core-image-minimal')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080069 WicTestCase.image_is_ready = True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050070
71 rmtree(self.resultdir, ignore_errors=True)
72
73 def tearDownLocal(self):
74 """Remove resultdir as it may contain images."""
75 rmtree(self.resultdir, ignore_errors=True)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080076 super(WicTestCase, self).tearDownLocal()
77
78 def _get_image_env_path(self, image):
79 """Generate and obtain the path to <image>.env"""
80 if image not in WicTestCase.wicenv_cache:
81 self.assertEqual(0, bitbake('%s -c do_rootfs_wicenv' % image).status)
82 bb_vars = get_bb_vars(['STAGING_DIR', 'MACHINE'], image)
83 stdir = bb_vars['STAGING_DIR']
84 machine = bb_vars['MACHINE']
85 WicTestCase.wicenv_cache[image] = os.path.join(stdir, machine, 'imgdata')
86 return WicTestCase.wicenv_cache[image]
87
88class Wic(WicTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050089
Brad Bishopd7bf8c12018-02-25 22:55:05 -050090 def test_version(self):
91 """Test wic --version"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080092 runCmd('wic --version')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050093
Brad Bishopd7bf8c12018-02-25 22:55:05 -050094 def test_help(self):
95 """Test wic --help and wic -h"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080096 runCmd('wic --help')
97 runCmd('wic -h')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050098
Brad Bishopd7bf8c12018-02-25 22:55:05 -050099 def test_createhelp(self):
100 """Test wic create --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800101 runCmd('wic create --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500102
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500103 def test_listhelp(self):
104 """Test wic list --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800105 runCmd('wic list --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500106
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500107 def test_help_create(self):
108 """Test wic help create"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800109 runCmd('wic help create')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500110
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500111 def test_help_list(self):
112 """Test wic help list"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800113 runCmd('wic help list')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500114
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115 def test_help_overview(self):
116 """Test wic help overview"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800117 runCmd('wic help overview')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500118
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119 def test_help_plugins(self):
120 """Test wic help plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800121 runCmd('wic help plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500122
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500123 def test_help_kickstart(self):
124 """Test wic help kickstart"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800125 runCmd('wic help kickstart')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500126
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500127 def test_list_images(self):
128 """Test wic list images"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800129 runCmd('wic list images')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500130
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500131 def test_list_source_plugins(self):
132 """Test wic list source-plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800133 runCmd('wic list source-plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500134
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500135 def test_listed_images_help(self):
136 """Test wic listed images help"""
137 output = runCmd('wic list images').output
138 imagelist = [line.split()[0] for line in output.splitlines()]
139 for image in imagelist:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800140 runCmd('wic list %s help' % image)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500141
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500142 def test_unsupported_subcommand(self):
143 """Test unsupported subcommand"""
144 self.assertNotEqual(0, runCmd('wic unsupported', ignore_status=True).status)
145
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500146 def test_no_command(self):
147 """Test wic without command"""
148 self.assertEqual(1, runCmd('wic', ignore_status=True).status)
149
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500150 def test_build_image_name(self):
151 """Test wic create wictestdisk --image-name=core-image-minimal"""
152 cmd = "wic create wictestdisk --image-name=core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800153 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500154 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
155
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500156 @only_for_arch(['i586', 'i686', 'x86_64'])
157 def test_gpt_image(self):
158 """Test creation of core-image-minimal with gpt table and UUID boot"""
159 cmd = "wic create directdisk-gpt --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800160 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500161 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct")))
162
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500163 @only_for_arch(['i586', 'i686', 'x86_64'])
164 def test_iso_image(self):
165 """Test creation of hybrid iso image with legacy and EFI boot"""
166 config = 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
167 'MACHINE_FEATURES_append = " efi"\n'\
168 'DEPENDS_pn-core-image-minimal += "syslinux"\n'
169 self.append_config(config)
Brad Bishopc4ea0752018-11-15 14:30:15 -0800170 bitbake('core-image-minimal core-image-minimal-initramfs')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500171 self.remove_config(config)
172 cmd = "wic create mkhybridiso --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800173 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500174 self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.direct")))
175 self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.iso")))
176
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500177 @only_for_arch(['i586', 'i686', 'x86_64'])
178 def test_qemux86_directdisk(self):
179 """Test creation of qemux-86-directdisk image"""
180 cmd = "wic create qemux86-directdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800181 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500182 self.assertEqual(1, len(glob(self.resultdir + "qemux86-directdisk-*direct")))
183
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500184 @only_for_arch(['i586', 'i686', 'x86_64'])
185 def test_mkefidisk(self):
186 """Test creation of mkefidisk image"""
187 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800188 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500189 self.assertEqual(1, len(glob(self.resultdir + "mkefidisk-*direct")))
190
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500191 @only_for_arch(['i586', 'i686', 'x86_64'])
192 def test_bootloader_config(self):
193 """Test creation of directdisk-bootloader-config image"""
194 config = 'DEPENDS_pn-core-image-minimal += "syslinux"\n'
195 self.append_config(config)
196 bitbake('core-image-minimal')
197 self.remove_config(config)
198 cmd = "wic create directdisk-bootloader-config -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800199 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500200 self.assertEqual(1, len(glob(self.resultdir + "directdisk-bootloader-config-*direct")))
201
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500202 @only_for_arch(['i586', 'i686', 'x86_64'])
203 def test_systemd_bootdisk(self):
204 """Test creation of systemd-bootdisk image"""
205 config = 'MACHINE_FEATURES_append = " efi"\n'
206 self.append_config(config)
207 bitbake('core-image-minimal')
208 self.remove_config(config)
209 cmd = "wic create systemd-bootdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800210 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500211 self.assertEqual(1, len(glob(self.resultdir + "systemd-bootdisk-*direct")))
212
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500213 def test_sdimage_bootpart(self):
214 """Test creation of sdimage-bootpart image"""
215 cmd = "wic create sdimage-bootpart -e core-image-minimal -o %s" % self.resultdir
216 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
217 self.write_config('IMAGE_BOOT_FILES = "%s"\n' % kimgtype)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800218 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500219 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct")))
220
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500221 @only_for_arch(['i586', 'i686', 'x86_64'])
222 def test_default_output_dir(self):
223 """Test default output location"""
224 for fname in glob("directdisk-*.direct"):
225 os.remove(fname)
226 config = 'DEPENDS_pn-core-image-minimal += "syslinux"\n'
227 self.append_config(config)
228 bitbake('core-image-minimal')
229 self.remove_config(config)
230 cmd = "wic create directdisk -e core-image-minimal"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800231 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500232 self.assertEqual(1, len(glob("directdisk-*.direct")))
233
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500234 @only_for_arch(['i586', 'i686', 'x86_64'])
235 def test_build_artifacts(self):
236 """Test wic create directdisk providing all artifacts."""
237 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
238 'wic-tools')
239 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
240 'core-image-minimal'))
241 bbvars = {key.lower(): value for key, value in bb_vars.items()}
242 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800243 runCmd("wic create directdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500244 "-b %(staging_datadir)s "
245 "-k %(deploy_dir_image)s "
246 "-n %(recipe_sysroot_native)s "
247 "-r %(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800248 "-o %(resultdir)s" % bbvars)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500249 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct")))
250
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500251 def test_compress_gzip(self):
252 """Test compressing an image with gzip"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800253 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500254 "--image-name core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800255 "-c gzip -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500256 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.gz")))
257
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500258 def test_compress_bzip2(self):
259 """Test compressing an image with bzip2"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800260 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500261 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800262 "-c bzip2 -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500263 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.bz2")))
264
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500265 def test_compress_xz(self):
266 """Test compressing an image with xz"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800267 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500268 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800269 "--compress-with=xz -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500270 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.xz")))
271
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500272 def test_wrong_compressor(self):
273 """Test how wic breaks if wrong compressor is provided"""
274 self.assertEqual(2, runCmd("wic create wictestdisk "
275 "--image-name=core-image-minimal "
276 "-c wrong -o %s" % self.resultdir,
277 ignore_status=True).status)
278
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500279 def test_debug_short(self):
280 """Test -D option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800281 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500282 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800283 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500284 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
285
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500286 def test_debug_long(self):
287 """Test --debug option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800288 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500289 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800290 "--debug -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500291 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
292
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500293 def test_skip_build_check_short(self):
294 """Test -s option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800295 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500296 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800297 "-s -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500298 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
299
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500300 def test_skip_build_check_long(self):
301 """Test --skip-build-check option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800302 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500303 "--image-name=core-image-minimal "
304 "--skip-build-check "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800305 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500306 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
307
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500308 def test_build_rootfs_short(self):
309 """Test -f option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800310 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500311 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800312 "-f -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500313 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
314
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500315 def test_build_rootfs_long(self):
316 """Test --build-rootfs option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800317 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500318 "--image-name=core-image-minimal "
319 "--build-rootfs "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800320 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500321 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
322
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500323 @only_for_arch(['i586', 'i686', 'x86_64'])
324 def test_rootfs_indirect_recipes(self):
325 """Test usage of rootfs plugin with rootfs recipes"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800326 runCmd("wic create directdisk-multi-rootfs "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500327 "--image-name=core-image-minimal "
328 "--rootfs rootfs1=core-image-minimal "
329 "--rootfs rootfs2=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800330 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500331 self.assertEqual(1, len(glob(self.resultdir + "directdisk-multi-rootfs*.direct")))
332
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500333 @only_for_arch(['i586', 'i686', 'x86_64'])
334 def test_rootfs_artifacts(self):
335 """Test usage of rootfs plugin with rootfs paths"""
336 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
337 'wic-tools')
338 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
339 'core-image-minimal'))
340 bbvars = {key.lower(): value for key, value in bb_vars.items()}
341 bbvars['wks'] = "directdisk-multi-rootfs"
342 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800343 runCmd("wic create %(wks)s "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500344 "--bootimg-dir=%(staging_datadir)s "
345 "--kernel-dir=%(deploy_dir_image)s "
346 "--native-sysroot=%(recipe_sysroot_native)s "
347 "--rootfs-dir rootfs1=%(image_rootfs)s "
348 "--rootfs-dir rootfs2=%(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800349 "--outdir %(resultdir)s" % bbvars)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500350 self.assertEqual(1, len(glob(self.resultdir + "%(wks)s-*.direct" % bbvars)))
351
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500352 def test_exclude_path(self):
353 """Test --exclude-path wks option."""
354
355 oldpath = os.environ['PATH']
356 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
357
358 try:
359 wks_file = 'temp.wks'
360 with open(wks_file, 'w') as wks:
361 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
362 wks.write("""
363part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path usr
364part /usr --source rootfs --ondisk mmcblk0 --fstype=ext4 --rootfs-dir %s/usr
365part /etc --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path bin/ --rootfs-dir %s/usr"""
366 % (rootfs_dir, rootfs_dir))
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800367 runCmd("wic create %s -e core-image-minimal -o %s" \
368 % (wks_file, self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500369
370 os.remove(wks_file)
371 wicout = glob(self.resultdir + "%s-*direct" % 'temp')
372 self.assertEqual(1, len(wicout))
373
374 wicimg = wicout[0]
375
376 # verify partition size with wic
377 res = runCmd("parted -m %s unit b p 2>/dev/null" % wicimg)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500378
379 # parse parted output which looks like this:
380 # BYT;\n
381 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
382 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
383 partlns = res.output.splitlines()[2:]
384
385 self.assertEqual(3, len(partlns))
386
387 for part in [1, 2, 3]:
388 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
389 partln = partlns[part-1].split(":")
390 self.assertEqual(7, len(partln))
391 start = int(partln[1].rstrip("B")) / 512
392 length = int(partln[3].rstrip("B")) / 512
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800393 runCmd("dd if=%s of=%s skip=%d count=%d" %
394 (wicimg, part_file, start, length))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500395
396 def extract_files(debugfs_output):
397 """
398 extract file names from the output of debugfs -R 'ls -p',
399 which looks like this:
400
401 /2/040755/0/0/.//\n
402 /2/040755/0/0/..//\n
403 /11/040700/0/0/lost+found^M//\n
404 /12/040755/1002/1002/run//\n
405 /13/040755/1002/1002/sys//\n
406 /14/040755/1002/1002/bin//\n
407 /80/040755/1002/1002/var//\n
408 /92/040755/1002/1002/tmp//\n
409 """
410 # NOTE the occasional ^M in file names
411 return [line.split('/')[5].strip() for line in \
412 debugfs_output.strip().split('/\n')]
413
414 # Test partition 1, should contain the normal root directories, except
415 # /usr.
416 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
417 os.path.join(self.resultdir, "selftest_img.part1"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500418 files = extract_files(res.output)
419 self.assertIn("etc", files)
420 self.assertNotIn("usr", files)
421
422 # Partition 2, should contain common directories for /usr, not root
423 # directories.
424 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
425 os.path.join(self.resultdir, "selftest_img.part2"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500426 files = extract_files(res.output)
427 self.assertNotIn("etc", files)
428 self.assertNotIn("usr", files)
429 self.assertIn("share", files)
430
431 # Partition 3, should contain the same as partition 2, including the bin
432 # directory, but not the files inside it.
433 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
434 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500435 files = extract_files(res.output)
436 self.assertNotIn("etc", files)
437 self.assertNotIn("usr", files)
438 self.assertIn("share", files)
439 self.assertIn("bin", files)
440 res = runCmd("debugfs -R 'ls -p bin' %s 2>/dev/null" % \
441 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500442 files = extract_files(res.output)
443 self.assertIn(".", files)
444 self.assertIn("..", files)
445 self.assertEqual(2, len(files))
446
447 for part in [1, 2, 3]:
448 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
449 os.remove(part_file)
450
451 finally:
452 os.environ['PATH'] = oldpath
453
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500454 def test_exclude_path_errors(self):
455 """Test --exclude-path wks option error handling."""
456 wks_file = 'temp.wks'
457
458 # Absolute argument.
459 with open(wks_file, 'w') as wks:
460 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path /usr")
461 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
462 % (wks_file, self.resultdir), ignore_status=True).status)
463 os.remove(wks_file)
464
465 # Argument pointing to parent directory.
466 with open(wks_file, 'w') as wks:
467 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path ././..")
468 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
469 % (wks_file, self.resultdir), ignore_status=True).status)
470 os.remove(wks_file)
471
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800472class Wic2(WicTestCase):
473
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500474 def test_bmap_short(self):
475 """Test generation of .bmap file -m option"""
476 cmd = "wic create wictestdisk -e core-image-minimal -m -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800477 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500478 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
479 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap")))
480
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500481 def test_bmap_long(self):
482 """Test generation of .bmap file --bmap option"""
483 cmd = "wic create wictestdisk -e core-image-minimal --bmap -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800484 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500485 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
486 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap")))
487
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500488 def test_image_env(self):
489 """Test generation of <image>.env files."""
490 image = 'core-image-minimal'
491 imgdatadir = self._get_image_env_path(image)
492
493 bb_vars = get_bb_vars(['IMAGE_BASENAME', 'WICVARS'], image)
494 basename = bb_vars['IMAGE_BASENAME']
495 self.assertEqual(basename, image)
496 path = os.path.join(imgdatadir, basename) + '.env'
497 self.assertTrue(os.path.isfile(path))
498
499 wicvars = set(bb_vars['WICVARS'].split())
500 # filter out optional variables
501 wicvars = wicvars.difference(('DEPLOY_DIR_IMAGE', 'IMAGE_BOOT_FILES',
Brad Bishop96ff1982019-08-19 13:50:42 -0400502 'INITRD', 'INITRD_LIVE', 'ISODIR','INITRAMFS_IMAGE',
503 'INITRAMFS_IMAGE_BUNDLE', 'INITRAMFS_LINK_NAME'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500504 with open(path) as envfile:
505 content = dict(line.split("=", 1) for line in envfile)
506 # test if variables used by wic present in the .env file
507 for var in wicvars:
508 self.assertTrue(var in content, "%s is not in .env file" % var)
509 self.assertTrue(content[var])
510
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500511 def test_image_vars_dir_short(self):
512 """Test image vars directory selection -v option"""
513 image = 'core-image-minimal'
514 imgenvdir = self._get_image_env_path(image)
515 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
516
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800517 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500518 "--image-name=%s -v %s -n %s -o %s"
519 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800520 self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500521 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
522
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500523 def test_image_vars_dir_long(self):
524 """Test image vars directory selection --vars option"""
525 image = 'core-image-minimal'
526 imgenvdir = self._get_image_env_path(image)
527 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
528
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800529 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500530 "--image-name=%s "
531 "--vars %s "
532 "--native-sysroot %s "
533 "--outdir %s"
534 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800535 self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500536 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
537
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500538 @only_for_arch(['i586', 'i686', 'x86_64'])
539 def test_wic_image_type(self):
540 """Test building wic images by bitbake"""
541 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
542 'MACHINE_FEATURES_append = " efi"\n'
543 self.append_config(config)
544 self.assertEqual(0, bitbake('wic-image-minimal').status)
545 self.remove_config(config)
546
547 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
548 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
549 machine = bb_vars['MACHINE']
550 prefix = os.path.join(deploy_dir, 'wic-image-minimal-%s.' % machine)
551 # check if we have result image and manifests symlinks
552 # pointing to existing files
553 for suffix in ('wic', 'manifest'):
554 path = prefix + suffix
555 self.assertTrue(os.path.islink(path))
556 self.assertTrue(os.path.isfile(os.path.realpath(path)))
557
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500558 @only_for_arch(['i586', 'i686', 'x86_64'])
559 def test_qemu(self):
560 """Test wic-image-minimal under qemu"""
561 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
562 'MACHINE_FEATURES_append = " efi"\n'
563 self.append_config(config)
564 self.assertEqual(0, bitbake('wic-image-minimal').status)
565 self.remove_config(config)
566
567 with runqemu('wic-image-minimal', ssh=False) as qemu:
Andrew Geissler99467da2019-02-25 18:54:23 -0600568 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' " \
569 "-e '/dev/root /|/dev/sda2 /' -e '/dev/sda3 /media' -e '/dev/sda4 /mnt'"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400570 status, output = qemu.run_serial(cmd)
Andrew Geissler99467da2019-02-25 18:54:23 -0600571 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
572 self.assertEqual(output, '4')
Brad Bishop316dfdd2018-06-25 12:45:53 -0400573 cmd = "grep UUID= /etc/fstab"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500574 status, output = qemu.run_serial(cmd)
575 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400576 self.assertEqual(output, 'UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba\t/media\text4\tdefaults\t0\t0')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500577
578 @only_for_arch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500579 def test_qemu_efi(self):
580 """Test core-image-minimal efi image under qemu"""
581 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "mkefidisk.wks"\n'
582 self.append_config(config)
583 self.assertEqual(0, bitbake('core-image-minimal ovmf').status)
584 self.remove_config(config)
585
586 with runqemu('core-image-minimal', ssh=False,
587 runqemuparams='ovmf', image_fstype='wic') as qemu:
588 cmd = "grep sda. /proc/partitions |wc -l"
589 status, output = qemu.run_serial(cmd)
590 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
591 self.assertEqual(output, '3')
592
593 @staticmethod
594 def _make_fixed_size_wks(size):
595 """
596 Create a wks of an image with a single partition. Size of the partition is set
597 using --fixed-size flag. Returns a tuple: (path to wks file, wks image name)
598 """
599 with NamedTemporaryFile("w", suffix=".wks", delete=False) as tempf:
600 wkspath = tempf.name
601 tempf.write("part " \
602 "--source rootfs --ondisk hda --align 4 --fixed-size %d "
603 "--fstype=ext4\n" % size)
604 wksname = os.path.splitext(os.path.basename(wkspath))[0]
605
606 return wkspath, wksname
607
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500608 def test_fixed_size(self):
609 """
610 Test creation of a simple image with partition size controlled through
611 --fixed-size flag
612 """
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800613 wkspath, wksname = Wic2._make_fixed_size_wks(200)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500614
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800615 runCmd("wic create %s -e core-image-minimal -o %s" \
616 % (wkspath, self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500617 os.remove(wkspath)
618 wicout = glob(self.resultdir + "%s-*direct" % wksname)
619 self.assertEqual(1, len(wicout))
620
621 wicimg = wicout[0]
622
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800623 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
624
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500625 # verify partition size with wic
626 res = runCmd("parted -m %s unit mib p 2>/dev/null" % wicimg,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800627 native_sysroot=native_sysroot)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500628
629 # parse parted output which looks like this:
630 # BYT;\n
631 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
632 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
633 partlns = res.output.splitlines()[2:]
634
Brad Bishopa34c0302019-09-23 22:34:48 -0400635 self.assertEqual(1, len(partlns),
636 msg="Partition list '%s'" % res.output)
637 self.assertEqual("1:0.00MiB:200MiB:200MiB:ext4::;", partlns[0],
638 msg="Partition list '%s'" % res.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500639
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500640 def test_fixed_size_error(self):
641 """
642 Test creation of a simple image with partition size controlled through
643 --fixed-size flag. The size of partition is intentionally set to 1MiB
644 in order to trigger an error in wic.
645 """
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800646 wkspath, wksname = Wic2._make_fixed_size_wks(1)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500647
648 self.assertEqual(1, runCmd("wic create %s -e core-image-minimal -o %s" \
649 % (wkspath, self.resultdir), ignore_status=True).status)
650 os.remove(wkspath)
651 wicout = glob(self.resultdir + "%s-*direct" % wksname)
652 self.assertEqual(0, len(wicout))
653
654 @only_for_arch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500655 def test_rawcopy_plugin_qemu(self):
656 """Test rawcopy plugin in qemu"""
657 # build ext4 and wic images
658 for fstype in ("ext4", "wic"):
659 config = 'IMAGE_FSTYPES = "%s"\nWKS_FILE = "test_rawcopy_plugin.wks.in"\n' % fstype
660 self.append_config(config)
661 self.assertEqual(0, bitbake('core-image-minimal').status)
662 self.remove_config(config)
663
664 with runqemu('core-image-minimal', ssh=False, image_fstype='wic') as qemu:
665 cmd = "grep sda. /proc/partitions |wc -l"
666 status, output = qemu.run_serial(cmd)
667 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
668 self.assertEqual(output, '2')
669
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500670 def test_rawcopy_plugin(self):
671 """Test rawcopy plugin"""
672 img = 'core-image-minimal'
673 machine = get_bb_var('MACHINE', img)
674 with NamedTemporaryFile("w", suffix=".wks") as wks:
675 wks.writelines(['part /boot --active --source bootimg-pcbios\n',
676 'part / --source rawcopy --sourceparams="file=%s-%s.ext4" --use-uuid\n'\
677 % (img, machine),
678 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
679 wks.flush()
680 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800681 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500682 wksname = os.path.splitext(os.path.basename(wks.name))[0]
683 out = glob(self.resultdir + "%s-*direct" % wksname)
684 self.assertEqual(1, len(out))
685
Brad Bishop96ff1982019-08-19 13:50:42 -0400686 @only_for_arch(['i586', 'i686', 'x86_64'])
687 def test_biosplusefi_plugin_qemu(self):
688 """Test biosplusefi plugin in qemu"""
Brad Bishop08902b02019-08-20 09:16:51 -0400689 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES_append = " efi"\n'
690 self.append_config(config)
691 self.assertEqual(0, bitbake('core-image-minimal').status)
692 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -0400693
694 with runqemu('core-image-minimal', ssh=False, image_fstype='wic') as qemu:
695 # Check that we have ONLY two /dev/sda* partitions (/boot and /)
696 cmd = "grep sda. /proc/partitions | wc -l"
697 status, output = qemu.run_serial(cmd)
698 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
699 self.assertEqual(output, '2')
700 # Check that /dev/sda1 is /boot and that either /dev/root OR /dev/sda2 is /
701 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' -e '/dev/root /|/dev/sda2 /'"
702 status, output = qemu.run_serial(cmd)
703 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
704 self.assertEqual(output, '2')
705 # Check that /boot has EFI bootx64.efi (required for EFI)
706 cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l"
707 status, output = qemu.run_serial(cmd)
708 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
709 self.assertEqual(output, '1')
710 # Check that "BOOTABLE" flag is set on boot partition (required for PC-Bios)
711 # Trailing "cat" seems to be required; otherwise run_serial() sends back echo of the input command
712 cmd = "fdisk -l /dev/sda | grep /dev/sda1 | awk {print'$2'} | cat"
713 status, output = qemu.run_serial(cmd)
714 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
715 self.assertEqual(output, '*')
716
717 @only_for_arch(['i586', 'i686', 'x86_64'])
718 def test_biosplusefi_plugin(self):
719 """Test biosplusefi plugin"""
720 # Wic generation below may fail depending on the order of the unittests
721 # This is because bootimg-pcbios (that bootimg-biosplusefi uses) generate its MBR inside STAGING_DATADIR directory
722 # which may or may not exists depending on what was built already
723 # If an image hasn't been built yet, directory ${STAGING_DATADIR}/syslinux won't exists and _get_bootimg_dir()
724 # will raise with "Couldn't find correct bootimg_dir"
725 # The easiest way to work-around this issue is to make sure we already built an image here, hence the bitbake call
Brad Bishop08902b02019-08-20 09:16:51 -0400726 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES_append = " efi"\n'
727 self.append_config(config)
728 self.assertEqual(0, bitbake('core-image-minimal').status)
729 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -0400730
731 img = 'core-image-minimal'
732 with NamedTemporaryFile("w", suffix=".wks") as wks:
733 wks.writelines(['part /boot --active --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\n',
734 'part / --source rootfs --fstype=ext4 --align 1024 --use-uuid\n'\
735 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
736 wks.flush()
737 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
738 runCmd(cmd)
739 wksname = os.path.splitext(os.path.basename(wks.name))[0]
740 out = glob(self.resultdir + "%s-*.direct" % wksname)
741 self.assertEqual(1, len(out))
742
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500743 def test_fs_types(self):
744 """Test filesystem types for empty and not empty partitions"""
745 img = 'core-image-minimal'
746 with NamedTemporaryFile("w", suffix=".wks") as wks:
747 wks.writelines(['part ext2 --fstype ext2 --source rootfs\n',
748 'part btrfs --fstype btrfs --source rootfs --size 40M\n',
749 'part squash --fstype squashfs --source rootfs\n',
750 'part swap --fstype swap --size 1M\n',
751 'part emptyvfat --fstype vfat --size 1M\n',
752 'part emptymsdos --fstype msdos --size 1M\n',
753 'part emptyext2 --fstype ext2 --size 1M\n',
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800754 'part emptybtrfs --fstype btrfs --size 150M\n'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500755 wks.flush()
756 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800757 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500758 wksname = os.path.splitext(os.path.basename(wks.name))[0]
759 out = glob(self.resultdir + "%s-*direct" % wksname)
760 self.assertEqual(1, len(out))
761
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500762 def test_kickstart_parser(self):
763 """Test wks parser options"""
764 with NamedTemporaryFile("w", suffix=".wks") as wks:
765 wks.writelines(['part / --fstype ext3 --source rootfs --system-id 0xFF '\
766 '--overhead-factor 1.2 --size 100k\n'])
767 wks.flush()
768 cmd = "wic create %s -e core-image-minimal -o %s" % (wks.name, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800769 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500770 wksname = os.path.splitext(os.path.basename(wks.name))[0]
771 out = glob(self.resultdir + "%s-*direct" % wksname)
772 self.assertEqual(1, len(out))
773
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500774 def test_image_bootpart_globbed(self):
775 """Test globbed sources with image-bootpart plugin"""
776 img = "core-image-minimal"
777 cmd = "wic create sdimage-bootpart -e %s -o %s" % (img, self.resultdir)
778 config = 'IMAGE_BOOT_FILES = "%s*"' % get_bb_var('KERNEL_IMAGETYPE', img)
779 self.append_config(config)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800780 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500781 self.remove_config(config)
782 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct")))
783
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500784 def test_sparse_copy(self):
785 """Test sparse_copy with FIEMAP and SEEK_HOLE filemap APIs"""
786 libpath = os.path.join(get_bb_var('COREBASE'), 'scripts', 'lib', 'wic')
787 sys.path.insert(0, libpath)
788 from filemap import FilemapFiemap, FilemapSeek, sparse_copy, ErrorNotSupp
789 with NamedTemporaryFile("w", suffix=".wic-sparse") as sparse:
790 src_name = sparse.name
791 src_size = 1024 * 10
792 sparse.truncate(src_size)
793 # write one byte to the file
794 with open(src_name, 'r+b') as sfile:
795 sfile.seek(1024 * 4)
796 sfile.write(b'\x00')
797 dest = sparse.name + '.out'
798 # copy src file to dest using different filemap APIs
799 for api in (FilemapFiemap, FilemapSeek, None):
800 if os.path.exists(dest):
801 os.unlink(dest)
802 try:
803 sparse_copy(sparse.name, dest, api=api)
804 except ErrorNotSupp:
805 continue # skip unsupported API
806 dest_stat = os.stat(dest)
807 self.assertEqual(dest_stat.st_size, src_size)
808 # 8 blocks is 4K (physical sector size)
809 self.assertEqual(dest_stat.st_blocks, 8)
810 os.unlink(dest)
811
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500812 def test_wic_ls(self):
813 """Test listing image content using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800814 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500815 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800816 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500817 images = glob(self.resultdir + "wictestdisk-*.direct")
818 self.assertEqual(1, len(images))
819
820 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
821
822 # list partitions
823 result = runCmd("wic ls %s -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500824 self.assertEqual(3, len(result.output.split('\n')))
825
826 # list directory content of the first partition
827 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500828 self.assertEqual(6, len(result.output.split('\n')))
829
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500830 def test_wic_cp(self):
831 """Test copy files and directories to the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800832 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500833 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800834 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500835 images = glob(self.resultdir + "wictestdisk-*.direct")
836 self.assertEqual(1, len(images))
837
838 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
839
840 # list directory content of the first partition
841 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500842 self.assertEqual(6, len(result.output.split('\n')))
843
844 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
845 testfile.write("test")
846
847 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800848 runCmd("wic cp %s %s:1/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500849
850 # check if file is there
851 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500852 self.assertEqual(7, len(result.output.split('\n')))
853 self.assertTrue(os.path.basename(testfile.name) in result.output)
854
855 # prepare directory
856 testdir = os.path.join(self.resultdir, 'wic-test-cp-dir')
857 testsubdir = os.path.join(testdir, 'subdir')
858 os.makedirs(os.path.join(testsubdir))
859 copy(testfile.name, testdir)
860
861 # copy directory to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800862 runCmd("wic cp %s %s:1/ -n %s" % (testdir, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500863
864 # check if directory is there
865 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500866 self.assertEqual(8, len(result.output.split('\n')))
867 self.assertTrue(os.path.basename(testdir) in result.output)
868
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500869 def test_wic_rm(self):
870 """Test removing files and directories from the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800871 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500872 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800873 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500874 images = glob(self.resultdir + "mkefidisk-*.direct")
875 self.assertEqual(1, len(images))
876
877 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
878
879 # list directory content of the first partition
880 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500881 self.assertIn('\nBZIMAGE ', result.output)
882 self.assertIn('\nEFI <DIR> ', result.output)
883
884 # remove file
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800885 runCmd("wic rm %s:1/bzimage -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500886
887 # remove directory
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800888 runCmd("wic rm %s:1/efi -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500889
890 # check if they're removed
891 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500892 self.assertNotIn('\nBZIMAGE ', result.output)
893 self.assertNotIn('\nEFI <DIR> ', result.output)
894
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500895 def test_mkfs_extraopts(self):
896 """Test wks option --mkfs-extraopts for empty and not empty partitions"""
897 img = 'core-image-minimal'
898 with NamedTemporaryFile("w", suffix=".wks") as wks:
899 wks.writelines(
900 ['part ext2 --fstype ext2 --source rootfs --mkfs-extraopts "-D -F -i 8192"\n',
901 "part btrfs --fstype btrfs --source rootfs --size 40M --mkfs-extraopts='--quiet'\n",
902 'part squash --fstype squashfs --source rootfs --mkfs-extraopts "-no-sparse -b 4096"\n',
903 'part emptyvfat --fstype vfat --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
904 'part emptymsdos --fstype msdos --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
905 'part emptyext2 --fstype ext2 --size 1M --mkfs-extraopts "-D -F -i 8192"\n',
906 'part emptybtrfs --fstype btrfs --size 100M --mkfs-extraopts "--mixed -K"\n'])
907 wks.flush()
908 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800909 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500910 wksname = os.path.splitext(os.path.basename(wks.name))[0]
911 out = glob(self.resultdir + "%s-*direct" % wksname)
912 self.assertEqual(1, len(out))
913
914 def test_expand_mbr_image(self):
915 """Test wic write --expand command for mbr image"""
916 # build an image
917 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n'
918 self.append_config(config)
919 self.assertEqual(0, bitbake('core-image-minimal').status)
920
921 # get path to the image
922 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
923 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
924 machine = bb_vars['MACHINE']
925 image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine)
926
927 self.remove_config(config)
928
929 try:
930 # expand image to 1G
931 new_image_path = None
932 with NamedTemporaryFile(mode='wb', suffix='.wic.exp',
933 dir=deploy_dir, delete=False) as sparse:
934 sparse.truncate(1024 ** 3)
935 new_image_path = sparse.name
936
937 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
938 cmd = "wic write -n %s --expand 1:0 %s %s" % (sysroot, image_path, new_image_path)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800939 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500940
941 # check if partitions are expanded
942 orig = runCmd("wic ls %s -n %s" % (image_path, sysroot))
943 exp = runCmd("wic ls %s -n %s" % (new_image_path, sysroot))
944 orig_sizes = [int(line.split()[3]) for line in orig.output.split('\n')[1:]]
945 exp_sizes = [int(line.split()[3]) for line in exp.output.split('\n')[1:]]
946 self.assertEqual(orig_sizes[0], exp_sizes[0]) # first partition is not resized
947 self.assertTrue(orig_sizes[1] < exp_sizes[1])
948
949 # Check if all free space is partitioned
950 result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path))
951 self.assertTrue("0 B, 0 bytes, 0 sectors" in result.output)
952
953 os.rename(image_path, image_path + '.bak')
954 os.rename(new_image_path, image_path)
955
956 # Check if it boots in qemu
957 with runqemu('core-image-minimal', ssh=False) as qemu:
958 cmd = "ls /etc/"
959 status, output = qemu.run_serial('true')
960 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
961 finally:
962 if os.path.exists(new_image_path):
963 os.unlink(new_image_path)
964 if os.path.exists(image_path + '.bak'):
965 os.rename(image_path + '.bak', image_path)
966
967 def test_wic_ls_ext(self):
968 """Test listing content of the ext partition using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800969 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500970 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800971 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500972 images = glob(self.resultdir + "wictestdisk-*.direct")
973 self.assertEqual(1, len(images))
974
975 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
976
977 # list directory content of the second ext4 partition
978 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500979 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(
980 set(line.split()[-1] for line in result.output.split('\n') if line)))
981
982 def test_wic_cp_ext(self):
983 """Test copy files and directories to the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800984 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500985 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800986 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500987 images = glob(self.resultdir + "wictestdisk-*.direct")
988 self.assertEqual(1, len(images))
989
990 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
991
992 # list directory content of the ext4 partition
993 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500994 dirs = set(line.split()[-1] for line in result.output.split('\n') if line)
995 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(dirs))
996
997 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
998 testfile.write("test")
999
1000 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001001 runCmd("wic cp %s %s:2/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001002
1003 # check if file is there
1004 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001005 newdirs = set(line.split()[-1] for line in result.output.split('\n') if line)
1006 self.assertEqual(newdirs.difference(dirs), set([os.path.basename(testfile.name)]))
1007
1008 def test_wic_rm_ext(self):
1009 """Test removing files from the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001010 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001011 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001012 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001013 images = glob(self.resultdir + "mkefidisk-*.direct")
1014 self.assertEqual(1, len(images))
1015
1016 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1017
1018 # list directory content of the /etc directory on ext4 partition
1019 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001020 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line])
1021
1022 # remove file
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001023 runCmd("wic rm %s:2/etc/fstab -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001024
1025 # check if it's removed
1026 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001027 self.assertTrue('fstab' not in [line.split()[-1] for line in result.output.split('\n') if line])