blob: 3b4143414feb75200c15aad6dc7ab7385b9674f0 [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
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050014import hashlib
Brad Bishopd7bf8c12018-02-25 22:55:05 -050015
16from glob import glob
17from shutil import rmtree, copy
18from functools import wraps, lru_cache
19from tempfile import NamedTemporaryFile
20
21from oeqa.selftest.case import OESelftestTestCase
22from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, runqemu
Brad Bishopd7bf8c12018-02-25 22:55:05 -050023
24
25@lru_cache(maxsize=32)
26def get_host_arch(recipe):
27 """A cached call to get_bb_var('HOST_ARCH', <recipe>)"""
28 return get_bb_var('HOST_ARCH', recipe)
29
30
31def only_for_arch(archs, image='core-image-minimal'):
32 """Decorator for wrapping test cases that can be run only for specific target
33 architectures. A list of compatible architectures is passed in `archs`.
34 Current architecture will be determined by parsing bitbake output for
35 `image` recipe.
36 """
37 def wrapper(func):
38 @wraps(func)
39 def wrapped_f(*args, **kwargs):
40 arch = get_host_arch(image)
41 if archs and arch not in archs:
42 raise unittest.SkipTest("Testcase arch dependency not met: %s" % arch)
43 return func(*args, **kwargs)
44 wrapped_f.__name__ = func.__name__
45 return wrapped_f
46 return wrapper
47
Andrew Geissler82c905d2020-04-13 13:39:40 -050048def extract_files(debugfs_output):
49 """
50 extract file names from the output of debugfs -R 'ls -p',
51 which looks like this:
52
53 /2/040755/0/0/.//\n
54 /2/040755/0/0/..//\n
55 /11/040700/0/0/lost+found^M//\n
56 /12/040755/1002/1002/run//\n
57 /13/040755/1002/1002/sys//\n
58 /14/040755/1002/1002/bin//\n
59 /80/040755/1002/1002/var//\n
60 /92/040755/1002/1002/tmp//\n
61 """
62 # NOTE the occasional ^M in file names
63 return [line.split('/')[5].strip() for line in \
64 debugfs_output.strip().split('/\n')]
65
66def files_own_by_root(debugfs_output):
67 for line in debugfs_output.strip().split('/\n'):
68 if line.split('/')[3:5] != ['0', '0']:
69 print(debugfs_output)
70 return False
71 return True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050072
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080073class WicTestCase(OESelftestTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050074 """Wic test class."""
75
Brad Bishopd7bf8c12018-02-25 22:55:05 -050076 image_is_ready = False
Brad Bishopd7bf8c12018-02-25 22:55:05 -050077 wicenv_cache = {}
78
79 def setUpLocal(self):
80 """This code is executed before each test method."""
Brad Bishopc4ea0752018-11-15 14:30:15 -080081 self.resultdir = self.builddir + "/wic-tmp/"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080082 super(WicTestCase, self).setUpLocal()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050083
84 # Do this here instead of in setUpClass as the base setUp does some
85 # clean up which can result in the native tools built earlier in
86 # setUpClass being unavailable.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080087 if not WicTestCase.image_is_ready:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050088 if get_bb_var('USE_NLS') == 'yes':
89 bitbake('wic-tools')
90 else:
91 self.skipTest('wic-tools cannot be built due its (intltool|gettext)-native dependency and NLS disable')
92
93 bitbake('core-image-minimal')
Andrew Geissler82c905d2020-04-13 13:39:40 -050094 bitbake('core-image-minimal-mtdutils')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080095 WicTestCase.image_is_ready = True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050096
97 rmtree(self.resultdir, ignore_errors=True)
98
99 def tearDownLocal(self):
100 """Remove resultdir as it may contain images."""
101 rmtree(self.resultdir, ignore_errors=True)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800102 super(WicTestCase, self).tearDownLocal()
103
104 def _get_image_env_path(self, image):
105 """Generate and obtain the path to <image>.env"""
106 if image not in WicTestCase.wicenv_cache:
107 self.assertEqual(0, bitbake('%s -c do_rootfs_wicenv' % image).status)
108 bb_vars = get_bb_vars(['STAGING_DIR', 'MACHINE'], image)
109 stdir = bb_vars['STAGING_DIR']
110 machine = bb_vars['MACHINE']
111 WicTestCase.wicenv_cache[image] = os.path.join(stdir, machine, 'imgdata')
112 return WicTestCase.wicenv_cache[image]
113
114class Wic(WicTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500116 def test_version(self):
117 """Test wic --version"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800118 runCmd('wic --version')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500120 def test_help(self):
121 """Test wic --help and wic -h"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800122 runCmd('wic --help')
123 runCmd('wic -h')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500124
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500125 def test_createhelp(self):
126 """Test wic create --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800127 runCmd('wic create --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500128
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500129 def test_listhelp(self):
130 """Test wic list --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800131 runCmd('wic list --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500133 def test_help_create(self):
134 """Test wic help create"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800135 runCmd('wic help create')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500136
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500137 def test_help_list(self):
138 """Test wic help list"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800139 runCmd('wic help list')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500140
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500141 def test_help_overview(self):
142 """Test wic help overview"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800143 runCmd('wic help overview')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500144
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500145 def test_help_plugins(self):
146 """Test wic help plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800147 runCmd('wic help plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500148
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500149 def test_help_kickstart(self):
150 """Test wic help kickstart"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800151 runCmd('wic help kickstart')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500152
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500153 def test_list_images(self):
154 """Test wic list images"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800155 runCmd('wic list images')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500156
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500157 def test_list_source_plugins(self):
158 """Test wic list source-plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800159 runCmd('wic list source-plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500160
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500161 def test_listed_images_help(self):
162 """Test wic listed images help"""
163 output = runCmd('wic list images').output
164 imagelist = [line.split()[0] for line in output.splitlines()]
165 for image in imagelist:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800166 runCmd('wic list %s help' % image)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500167
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500168 def test_unsupported_subcommand(self):
169 """Test unsupported subcommand"""
170 self.assertNotEqual(0, runCmd('wic unsupported', ignore_status=True).status)
171
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500172 def test_no_command(self):
173 """Test wic without command"""
174 self.assertEqual(1, runCmd('wic', ignore_status=True).status)
175
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500176 def test_build_image_name(self):
177 """Test wic create wictestdisk --image-name=core-image-minimal"""
178 cmd = "wic create wictestdisk --image-name=core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800179 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500180 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
181
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500182 @only_for_arch(['i586', 'i686', 'x86_64'])
183 def test_gpt_image(self):
184 """Test creation of core-image-minimal with gpt table and UUID boot"""
185 cmd = "wic create directdisk-gpt --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800186 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500187 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct")))
188
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500189 @only_for_arch(['i586', 'i686', 'x86_64'])
190 def test_iso_image(self):
191 """Test creation of hybrid iso image with legacy and EFI boot"""
192 config = 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500193 'MACHINE_FEATURES:append = " efi"\n'\
194 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500195 self.append_config(config)
Brad Bishopc4ea0752018-11-15 14:30:15 -0800196 bitbake('core-image-minimal core-image-minimal-initramfs')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500197 self.remove_config(config)
198 cmd = "wic create mkhybridiso --image-name 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 + "HYBRID_ISO_IMG-*.direct")))
201 self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.iso")))
202
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500203 @only_for_arch(['i586', 'i686', 'x86_64'])
204 def test_qemux86_directdisk(self):
205 """Test creation of qemux-86-directdisk image"""
206 cmd = "wic create qemux86-directdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800207 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500208 self.assertEqual(1, len(glob(self.resultdir + "qemux86-directdisk-*direct")))
209
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500210 @only_for_arch(['i586', 'i686', 'x86_64'])
211 def test_mkefidisk(self):
212 """Test creation of mkefidisk image"""
213 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800214 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500215 self.assertEqual(1, len(glob(self.resultdir + "mkefidisk-*direct")))
216
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500217 @only_for_arch(['i586', 'i686', 'x86_64'])
218 def test_bootloader_config(self):
219 """Test creation of directdisk-bootloader-config image"""
Patrick Williams213cb262021-08-07 19:21:33 -0500220 config = 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500221 self.append_config(config)
222 bitbake('core-image-minimal')
223 self.remove_config(config)
224 cmd = "wic create directdisk-bootloader-config -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800225 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500226 self.assertEqual(1, len(glob(self.resultdir + "directdisk-bootloader-config-*direct")))
227
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500228 @only_for_arch(['i586', 'i686', 'x86_64'])
229 def test_systemd_bootdisk(self):
230 """Test creation of systemd-bootdisk image"""
Patrick Williams213cb262021-08-07 19:21:33 -0500231 config = 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500232 self.append_config(config)
233 bitbake('core-image-minimal')
234 self.remove_config(config)
235 cmd = "wic create systemd-bootdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800236 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500237 self.assertEqual(1, len(glob(self.resultdir + "systemd-bootdisk-*direct")))
238
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500239 def test_efi_bootpart(self):
240 """Test creation of efi-bootpart image"""
241 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
242 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
243 self.append_config('IMAGE_EFI_BOOT_FILES = "%s;kernel"\n' % kimgtype)
244 runCmd(cmd)
245 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
246 images = glob(self.resultdir + "mkefidisk-*.direct")
247 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
248 self.assertIn("kernel",result.output)
249
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500250 def test_sdimage_bootpart(self):
251 """Test creation of sdimage-bootpart image"""
252 cmd = "wic create sdimage-bootpart -e core-image-minimal -o %s" % self.resultdir
253 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
254 self.write_config('IMAGE_BOOT_FILES = "%s"\n' % kimgtype)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800255 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500256 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct")))
257
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500258 @only_for_arch(['i586', 'i686', 'x86_64'])
259 def test_default_output_dir(self):
260 """Test default output location"""
261 for fname in glob("directdisk-*.direct"):
262 os.remove(fname)
Patrick Williams213cb262021-08-07 19:21:33 -0500263 config = 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500264 self.append_config(config)
265 bitbake('core-image-minimal')
266 self.remove_config(config)
267 cmd = "wic create directdisk -e core-image-minimal"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800268 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500269 self.assertEqual(1, len(glob("directdisk-*.direct")))
270
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500271 @only_for_arch(['i586', 'i686', 'x86_64'])
272 def test_build_artifacts(self):
273 """Test wic create directdisk providing all artifacts."""
274 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
275 'wic-tools')
276 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
277 'core-image-minimal'))
278 bbvars = {key.lower(): value for key, value in bb_vars.items()}
279 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800280 runCmd("wic create directdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500281 "-b %(staging_datadir)s "
282 "-k %(deploy_dir_image)s "
283 "-n %(recipe_sysroot_native)s "
284 "-r %(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800285 "-o %(resultdir)s" % bbvars)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500286 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct")))
287
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500288 def test_compress_gzip(self):
289 """Test compressing an image with gzip"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800290 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500291 "--image-name core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800292 "-c gzip -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500293 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.gz")))
294
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500295 def test_compress_bzip2(self):
296 """Test compressing an image with bzip2"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800297 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500298 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800299 "-c bzip2 -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500300 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.bz2")))
301
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500302 def test_compress_xz(self):
303 """Test compressing an image with xz"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800304 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500305 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800306 "--compress-with=xz -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500307 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.xz")))
308
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500309 def test_wrong_compressor(self):
310 """Test how wic breaks if wrong compressor is provided"""
311 self.assertEqual(2, runCmd("wic create wictestdisk "
312 "--image-name=core-image-minimal "
313 "-c wrong -o %s" % self.resultdir,
314 ignore_status=True).status)
315
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500316 def test_debug_short(self):
317 """Test -D option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800318 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500319 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800320 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500321 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600322 self.assertEqual(1, len(glob(self.resultdir + "tmp.wic*")))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500323
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500324 def test_debug_long(self):
325 """Test --debug option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800326 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500327 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800328 "--debug -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500329 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600330 self.assertEqual(1, len(glob(self.resultdir + "tmp.wic*")))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500331
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500332 def test_skip_build_check_short(self):
333 """Test -s option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800334 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500335 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800336 "-s -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500337 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
338
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500339 def test_skip_build_check_long(self):
340 """Test --skip-build-check option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800341 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500342 "--image-name=core-image-minimal "
343 "--skip-build-check "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800344 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500345 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
346
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500347 def test_build_rootfs_short(self):
348 """Test -f option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800349 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500350 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800351 "-f -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500352 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
353
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500354 def test_build_rootfs_long(self):
355 """Test --build-rootfs option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800356 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500357 "--image-name=core-image-minimal "
358 "--build-rootfs "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800359 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500360 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
361
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500362 @only_for_arch(['i586', 'i686', 'x86_64'])
363 def test_rootfs_indirect_recipes(self):
364 """Test usage of rootfs plugin with rootfs recipes"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800365 runCmd("wic create directdisk-multi-rootfs "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500366 "--image-name=core-image-minimal "
367 "--rootfs rootfs1=core-image-minimal "
368 "--rootfs rootfs2=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800369 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500370 self.assertEqual(1, len(glob(self.resultdir + "directdisk-multi-rootfs*.direct")))
371
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500372 @only_for_arch(['i586', 'i686', 'x86_64'])
373 def test_rootfs_artifacts(self):
374 """Test usage of rootfs plugin with rootfs paths"""
375 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
376 'wic-tools')
377 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
378 'core-image-minimal'))
379 bbvars = {key.lower(): value for key, value in bb_vars.items()}
380 bbvars['wks'] = "directdisk-multi-rootfs"
381 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800382 runCmd("wic create %(wks)s "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500383 "--bootimg-dir=%(staging_datadir)s "
384 "--kernel-dir=%(deploy_dir_image)s "
385 "--native-sysroot=%(recipe_sysroot_native)s "
386 "--rootfs-dir rootfs1=%(image_rootfs)s "
387 "--rootfs-dir rootfs2=%(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800388 "--outdir %(resultdir)s" % bbvars)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500389 self.assertEqual(1, len(glob(self.resultdir + "%(wks)s-*.direct" % bbvars)))
390
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500391 def test_exclude_path(self):
392 """Test --exclude-path wks option."""
393
394 oldpath = os.environ['PATH']
395 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
396
397 try:
398 wks_file = 'temp.wks'
399 with open(wks_file, 'w') as wks:
400 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
401 wks.write("""
402part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path usr
403part /usr --source rootfs --ondisk mmcblk0 --fstype=ext4 --rootfs-dir %s/usr
404part /etc --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path bin/ --rootfs-dir %s/usr"""
405 % (rootfs_dir, rootfs_dir))
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800406 runCmd("wic create %s -e core-image-minimal -o %s" \
407 % (wks_file, self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500408
409 os.remove(wks_file)
410 wicout = glob(self.resultdir + "%s-*direct" % 'temp')
411 self.assertEqual(1, len(wicout))
412
413 wicimg = wicout[0]
414
415 # verify partition size with wic
416 res = runCmd("parted -m %s unit b p 2>/dev/null" % wicimg)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500417
418 # parse parted output which looks like this:
419 # BYT;\n
420 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
421 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
422 partlns = res.output.splitlines()[2:]
423
424 self.assertEqual(3, len(partlns))
425
426 for part in [1, 2, 3]:
427 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
428 partln = partlns[part-1].split(":")
429 self.assertEqual(7, len(partln))
430 start = int(partln[1].rstrip("B")) / 512
431 length = int(partln[3].rstrip("B")) / 512
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800432 runCmd("dd if=%s of=%s skip=%d count=%d" %
433 (wicimg, part_file, start, length))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500434
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500435 # Test partition 1, should contain the normal root directories, except
436 # /usr.
437 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
438 os.path.join(self.resultdir, "selftest_img.part1"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500439 files = extract_files(res.output)
440 self.assertIn("etc", files)
441 self.assertNotIn("usr", files)
442
443 # Partition 2, should contain common directories for /usr, not root
444 # directories.
445 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
446 os.path.join(self.resultdir, "selftest_img.part2"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500447 files = extract_files(res.output)
448 self.assertNotIn("etc", files)
449 self.assertNotIn("usr", files)
450 self.assertIn("share", files)
451
452 # Partition 3, should contain the same as partition 2, including the bin
453 # directory, but not the files inside it.
454 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
455 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500456 files = extract_files(res.output)
457 self.assertNotIn("etc", files)
458 self.assertNotIn("usr", files)
459 self.assertIn("share", files)
460 self.assertIn("bin", files)
461 res = runCmd("debugfs -R 'ls -p bin' %s 2>/dev/null" % \
462 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500463 files = extract_files(res.output)
464 self.assertIn(".", files)
465 self.assertIn("..", files)
466 self.assertEqual(2, len(files))
467
468 for part in [1, 2, 3]:
469 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
470 os.remove(part_file)
471
472 finally:
473 os.environ['PATH'] = oldpath
474
Andrew Geissler82c905d2020-04-13 13:39:40 -0500475 def test_include_path(self):
476 """Test --include-path wks option."""
477
478 oldpath = os.environ['PATH']
479 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
480
481 try:
482 include_path = os.path.join(self.resultdir, 'test-include')
483 os.makedirs(include_path)
484 with open(os.path.join(include_path, 'test-file'), 'w') as t:
485 t.write("test\n")
486 wks_file = os.path.join(include_path, 'temp.wks')
487 with open(wks_file, 'w') as wks:
488 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
489 wks.write("""
490part /part1 --source rootfs --ondisk mmcblk0 --fstype=ext4
491part /part2 --source rootfs --ondisk mmcblk0 --fstype=ext4 --include-path %s"""
492 % (include_path))
493 runCmd("wic create %s -e core-image-minimal -o %s" \
494 % (wks_file, self.resultdir))
495
496 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
497 part2 = glob(os.path.join(self.resultdir, 'temp-*.direct.p2'))[0]
498
499 # Test partition 1, should not contain 'test-file'
500 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
501 files = extract_files(res.output)
502 self.assertNotIn('test-file', files)
503 self.assertEqual(True, files_own_by_root(res.output))
504
505 # Test partition 2, should contain 'test-file'
506 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part2))
507 files = extract_files(res.output)
508 self.assertIn('test-file', files)
509 self.assertEqual(True, files_own_by_root(res.output))
510
511 finally:
512 os.environ['PATH'] = oldpath
513
514 def test_include_path_embeded(self):
515 """Test --include-path wks option."""
516
517 oldpath = os.environ['PATH']
518 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
519
520 try:
521 include_path = os.path.join(self.resultdir, 'test-include')
522 os.makedirs(include_path)
523 with open(os.path.join(include_path, 'test-file'), 'w') as t:
524 t.write("test\n")
525 wks_file = os.path.join(include_path, 'temp.wks')
526 with open(wks_file, 'w') as wks:
527 wks.write("""
528part / --source rootfs --fstype=ext4 --include-path %s --include-path core-image-minimal-mtdutils export/"""
529 % (include_path))
530 runCmd("wic create %s -e core-image-minimal -o %s" \
531 % (wks_file, self.resultdir))
532
533 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
534
535 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
536 files = extract_files(res.output)
537 self.assertIn('test-file', files)
538 self.assertEqual(True, files_own_by_root(res.output))
539
540 res = runCmd("debugfs -R 'ls -p /export/etc/' %s 2>/dev/null" % (part1))
541 files = extract_files(res.output)
542 self.assertIn('passwd', files)
543 self.assertEqual(True, files_own_by_root(res.output))
544
545 finally:
546 os.environ['PATH'] = oldpath
547
548 def test_include_path_errors(self):
549 """Test --include-path wks option error handling."""
550 wks_file = 'temp.wks'
551
552 # Absolute argument.
553 with open(wks_file, 'w') as wks:
554 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils /export")
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 # Argument pointing to parent directory.
560 with open(wks_file, 'w') as wks:
561 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils ././..")
562 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
563 % (wks_file, self.resultdir), ignore_status=True).status)
564 os.remove(wks_file)
565
566 # 3 Argument pointing to parent directory.
567 with open(wks_file, 'w') as wks:
568 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils export/ dummy")
569 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
570 % (wks_file, self.resultdir), ignore_status=True).status)
571 os.remove(wks_file)
572
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500573 def test_exclude_path_errors(self):
574 """Test --exclude-path wks option error handling."""
575 wks_file = 'temp.wks'
576
577 # Absolute argument.
578 with open(wks_file, 'w') as wks:
579 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path /usr")
580 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
581 % (wks_file, self.resultdir), ignore_status=True).status)
582 os.remove(wks_file)
583
584 # Argument pointing to parent directory.
585 with open(wks_file, 'w') as wks:
586 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path ././..")
587 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
588 % (wks_file, self.resultdir), ignore_status=True).status)
589 os.remove(wks_file)
590
Andrew Geissler82c905d2020-04-13 13:39:40 -0500591 def test_permissions(self):
592 """Test permissions are respected"""
593
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600594 # prepare wicenv and rootfs
595 bitbake('core-image-minimal core-image-minimal-mtdutils -c do_rootfs_wicenv')
596
Andrew Geissler82c905d2020-04-13 13:39:40 -0500597 oldpath = os.environ['PATH']
598 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
599
600 t_normal = """
601part / --source rootfs --fstype=ext4
602"""
603 t_exclude = """
604part / --source rootfs --fstype=ext4 --exclude-path=home
605"""
606 t_multi = """
607part / --source rootfs --ondisk sda --fstype=ext4
608part /export --source rootfs --rootfs=core-image-minimal-mtdutils --fstype=ext4
609"""
610 t_change = """
611part / --source rootfs --ondisk sda --fstype=ext4 --exclude-path=etc/   
612part /etc --source rootfs --fstype=ext4 --change-directory=etc
613"""
614 tests = [t_normal, t_exclude, t_multi, t_change]
615
616 try:
617 for test in tests:
618 include_path = os.path.join(self.resultdir, 'test-include')
619 os.makedirs(include_path)
620 wks_file = os.path.join(include_path, 'temp.wks')
621 with open(wks_file, 'w') as wks:
622 wks.write(test)
623 runCmd("wic create %s -e core-image-minimal -o %s" \
624 % (wks_file, self.resultdir))
625
626 for part in glob(os.path.join(self.resultdir, 'temp-*.direct.p*')):
627 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part))
628 self.assertEqual(True, files_own_by_root(res.output))
629
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600630 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "%s"\n' % wks_file
631 self.append_config(config)
632 bitbake('core-image-minimal')
633 tmpdir = os.path.join(get_bb_var('WORKDIR', 'core-image-minimal'),'build-wic')
634
635 # check each partition for permission
636 for part in glob(os.path.join(tmpdir, 'temp-*.direct.p*')):
637 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part))
638 self.assertTrue(files_own_by_root(res.output)
639 ,msg='Files permission incorrect using wks set "%s"' % test)
640
641 # clean config and result directory for next cases
642 self.remove_config(config)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500643 rmtree(self.resultdir, ignore_errors=True)
644
645 finally:
646 os.environ['PATH'] = oldpath
647
648 def test_change_directory(self):
649 """Test --change-directory wks option."""
650
651 oldpath = os.environ['PATH']
652 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
653
654 try:
655 include_path = os.path.join(self.resultdir, 'test-include')
656 os.makedirs(include_path)
657 wks_file = os.path.join(include_path, 'temp.wks')
658 with open(wks_file, 'w') as wks:
659 wks.write("part /etc --source rootfs --fstype=ext4 --change-directory=etc")
660 runCmd("wic create %s -e core-image-minimal -o %s" \
661 % (wks_file, self.resultdir))
662
663 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
664
665 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
666 files = extract_files(res.output)
667 self.assertIn('passwd', files)
668
669 finally:
670 os.environ['PATH'] = oldpath
671
672 def test_change_directory_errors(self):
673 """Test --change-directory wks option error handling."""
674 wks_file = 'temp.wks'
675
676 # Absolute argument.
677 with open(wks_file, 'w') as wks:
678 wks.write("part / --source rootfs --fstype=ext4 --change-directory /usr")
679 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
680 % (wks_file, self.resultdir), ignore_status=True).status)
681 os.remove(wks_file)
682
683 # Argument pointing to parent directory.
684 with open(wks_file, 'w') as wks:
685 wks.write("part / --source rootfs --fstype=ext4 --change-directory ././..")
686 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
687 % (wks_file, self.resultdir), ignore_status=True).status)
688 os.remove(wks_file)
689
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500690 def test_no_fstab_update(self):
691 """Test --no-fstab-update wks option."""
692
693 oldpath = os.environ['PATH']
694 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
695
696 # Get stock fstab from base-files recipe
697 self.assertEqual(0, bitbake('base-files -c do_install').status)
698 bf_fstab = os.path.join(get_bb_var('D', 'base-files'), 'etc/fstab')
699 self.assertEqual(True, os.path.exists(bf_fstab))
700 bf_fstab_md5sum = runCmd('md5sum %s 2>/dev/null' % bf_fstab).output.split(" ")[0]
701
702 try:
703 no_fstab_update_path = os.path.join(self.resultdir, 'test-no-fstab-update')
704 os.makedirs(no_fstab_update_path)
705 wks_file = os.path.join(no_fstab_update_path, 'temp.wks')
706 with open(wks_file, 'w') as wks:
707 wks.writelines(['part / --source rootfs --fstype=ext4 --label rootfs\n',
708 'part /mnt/p2 --source rootfs --rootfs-dir=core-image-minimal ',
709 '--fstype=ext4 --label p2 --no-fstab-update\n'])
710 runCmd("wic create %s -e core-image-minimal -o %s" \
711 % (wks_file, self.resultdir))
712
713 part_fstab_md5sum = []
714 for i in range(1, 3):
715 part = glob(os.path.join(self.resultdir, 'temp-*.direct.p') + str(i))[0]
716 part_fstab = runCmd("debugfs -R 'cat etc/fstab' %s 2>/dev/null" % (part))
717 part_fstab_md5sum.append(hashlib.md5((part_fstab.output + "\n\n").encode('utf-8')).hexdigest())
718
719 # '/etc/fstab' in partition 2 should contain the same stock fstab file
720 # as the one installed by the base-file recipe.
721 self.assertEqual(bf_fstab_md5sum, part_fstab_md5sum[1])
722
723 # '/etc/fstab' in partition 1 should contain an updated fstab file.
724 self.assertNotEqual(bf_fstab_md5sum, part_fstab_md5sum[0])
725
726 finally:
727 os.environ['PATH'] = oldpath
728
729 def test_no_fstab_update_errors(self):
730 """Test --no-fstab-update wks option error handling."""
731 wks_file = 'temp.wks'
732
733 # Absolute argument.
734 with open(wks_file, 'w') as wks:
735 wks.write("part / --source rootfs --fstype=ext4 --no-fstab-update /etc")
736 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
737 % (wks_file, self.resultdir), ignore_status=True).status)
738 os.remove(wks_file)
739
740 # Argument pointing to parent directory.
741 with open(wks_file, 'w') as wks:
742 wks.write("part / --source rootfs --fstype=ext4 --no-fstab-update ././..")
743 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
744 % (wks_file, self.resultdir), ignore_status=True).status)
745 os.remove(wks_file)
746
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800747class Wic2(WicTestCase):
748
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500749 def test_bmap_short(self):
750 """Test generation of .bmap file -m option"""
751 cmd = "wic create wictestdisk -e core-image-minimal -m -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800752 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500753 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
754 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap")))
755
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500756 def test_bmap_long(self):
757 """Test generation of .bmap file --bmap option"""
758 cmd = "wic create wictestdisk -e core-image-minimal --bmap -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800759 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500760 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
761 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap")))
762
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500763 def test_image_env(self):
764 """Test generation of <image>.env files."""
765 image = 'core-image-minimal'
766 imgdatadir = self._get_image_env_path(image)
767
768 bb_vars = get_bb_vars(['IMAGE_BASENAME', 'WICVARS'], image)
769 basename = bb_vars['IMAGE_BASENAME']
770 self.assertEqual(basename, image)
771 path = os.path.join(imgdatadir, basename) + '.env'
772 self.assertTrue(os.path.isfile(path))
773
774 wicvars = set(bb_vars['WICVARS'].split())
775 # filter out optional variables
776 wicvars = wicvars.difference(('DEPLOY_DIR_IMAGE', 'IMAGE_BOOT_FILES',
Brad Bishop96ff1982019-08-19 13:50:42 -0400777 'INITRD', 'INITRD_LIVE', 'ISODIR','INITRAMFS_IMAGE',
Andrew Geissler82c905d2020-04-13 13:39:40 -0500778 'INITRAMFS_IMAGE_BUNDLE', 'INITRAMFS_LINK_NAME',
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500779 'APPEND', 'IMAGE_EFI_BOOT_FILES'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500780 with open(path) as envfile:
781 content = dict(line.split("=", 1) for line in envfile)
782 # test if variables used by wic present in the .env file
783 for var in wicvars:
784 self.assertTrue(var in content, "%s is not in .env file" % var)
785 self.assertTrue(content[var])
786
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500787 def test_image_vars_dir_short(self):
788 """Test image vars directory selection -v option"""
789 image = 'core-image-minimal'
790 imgenvdir = self._get_image_env_path(image)
791 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
792
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800793 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500794 "--image-name=%s -v %s -n %s -o %s"
795 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800796 self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500797 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
798
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500799 def test_image_vars_dir_long(self):
800 """Test image vars directory selection --vars option"""
801 image = 'core-image-minimal'
802 imgenvdir = self._get_image_env_path(image)
803 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
804
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800805 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500806 "--image-name=%s "
807 "--vars %s "
808 "--native-sysroot %s "
809 "--outdir %s"
810 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800811 self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500812 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
813
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500814 @only_for_arch(['i586', 'i686', 'x86_64'])
815 def test_wic_image_type(self):
816 """Test building wic images by bitbake"""
817 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500818 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500819 self.append_config(config)
820 self.assertEqual(0, bitbake('wic-image-minimal').status)
821 self.remove_config(config)
822
823 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
824 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
825 machine = bb_vars['MACHINE']
826 prefix = os.path.join(deploy_dir, 'wic-image-minimal-%s.' % machine)
827 # check if we have result image and manifests symlinks
828 # pointing to existing files
829 for suffix in ('wic', 'manifest'):
830 path = prefix + suffix
831 self.assertTrue(os.path.islink(path))
832 self.assertTrue(os.path.isfile(os.path.realpath(path)))
833
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500834 @only_for_arch(['i586', 'i686', 'x86_64'])
835 def test_qemu(self):
836 """Test wic-image-minimal under qemu"""
837 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500838 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500839 self.append_config(config)
840 self.assertEqual(0, bitbake('wic-image-minimal').status)
841 self.remove_config(config)
842
843 with runqemu('wic-image-minimal', ssh=False) as qemu:
Andrew Geissler99467da2019-02-25 18:54:23 -0600844 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' " \
845 "-e '/dev/root /|/dev/sda2 /' -e '/dev/sda3 /media' -e '/dev/sda4 /mnt'"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400846 status, output = qemu.run_serial(cmd)
Andrew Geissler99467da2019-02-25 18:54:23 -0600847 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
848 self.assertEqual(output, '4')
Brad Bishop316dfdd2018-06-25 12:45:53 -0400849 cmd = "grep UUID= /etc/fstab"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500850 status, output = qemu.run_serial(cmd)
851 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400852 self.assertEqual(output, 'UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba\t/media\text4\tdefaults\t0\t0')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500853
854 @only_for_arch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500855 def test_qemu_efi(self):
856 """Test core-image-minimal efi image under qemu"""
857 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "mkefidisk.wks"\n'
858 self.append_config(config)
859 self.assertEqual(0, bitbake('core-image-minimal ovmf').status)
860 self.remove_config(config)
861
862 with runqemu('core-image-minimal', ssh=False,
863 runqemuparams='ovmf', image_fstype='wic') as qemu:
864 cmd = "grep sda. /proc/partitions |wc -l"
865 status, output = qemu.run_serial(cmd)
866 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
867 self.assertEqual(output, '3')
868
869 @staticmethod
870 def _make_fixed_size_wks(size):
871 """
872 Create a wks of an image with a single partition. Size of the partition is set
873 using --fixed-size flag. Returns a tuple: (path to wks file, wks image name)
874 """
875 with NamedTemporaryFile("w", suffix=".wks", delete=False) as tempf:
876 wkspath = tempf.name
877 tempf.write("part " \
878 "--source rootfs --ondisk hda --align 4 --fixed-size %d "
879 "--fstype=ext4\n" % size)
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500880
881 return wkspath
882
883 def _get_wic_partitions(self, wkspath, native_sysroot=None, ignore_status=False):
884 p = runCmd("wic create %s -e core-image-minimal -o %s" % (wkspath, self.resultdir),
885 ignore_status=ignore_status)
886
887 if p.status:
888 return (p, None)
889
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500890 wksname = os.path.splitext(os.path.basename(wkspath))[0]
891
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500892 wicout = glob(self.resultdir + "%s-*direct" % wksname)
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500893
894 if not wicout:
895 return (p, None)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500896
897 wicimg = wicout[0]
898
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500899 if not native_sysroot:
900 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800901
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500902 # verify partition size with wic
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500903 res = runCmd("parted -m %s unit kib p 2>/dev/null" % wicimg,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800904 native_sysroot=native_sysroot)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500905
906 # parse parted output which looks like this:
907 # BYT;\n
908 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
909 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500910 return (p, res.output.splitlines()[2:])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500911
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500912 def test_fixed_size(self):
913 """
914 Test creation of a simple image with partition size controlled through
915 --fixed-size flag
916 """
917 wkspath = Wic2._make_fixed_size_wks(200)
918 _, partlns = self._get_wic_partitions(wkspath)
919 os.remove(wkspath)
920
921 self.assertEqual(partlns, [
922 "1:4.00kiB:204804kiB:204800kiB:ext4::;",
923 ])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500924
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500925 def test_fixed_size_error(self):
926 """
927 Test creation of a simple image with partition size controlled through
928 --fixed-size flag. The size of partition is intentionally set to 1MiB
929 in order to trigger an error in wic.
930 """
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500931 wkspath = Wic2._make_fixed_size_wks(1)
932 p, _ = self._get_wic_partitions(wkspath, ignore_status=True)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500933 os.remove(wkspath)
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500934
935 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
936
937 def test_offset(self):
938 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
939
940 with NamedTemporaryFile("w", suffix=".wks") as tempf:
941 # Test that partitions are placed at the correct offsets, default KB
942 tempf.write("bootloader --ptable gpt\n" \
943 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \
944 "part /bar --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n")
945 tempf.flush()
946
947 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
948 self.assertEqual(partlns, [
949 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
950 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
951 ])
952
953 with NamedTemporaryFile("w", suffix=".wks") as tempf:
954 # Test that partitions are placed at the correct offsets, same with explicit KB
955 tempf.write("bootloader --ptable gpt\n" \
956 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \
957 "part /bar --ondisk hda --offset 102432K --fixed-size 100M --fstype=ext4\n")
958 tempf.flush()
959
960 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
961 self.assertEqual(partlns, [
962 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
963 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
964 ])
965
966 with NamedTemporaryFile("w", suffix=".wks") as tempf:
967 # Test that partitions are placed at the correct offsets using MB
968 tempf.write("bootloader --ptable gpt\n" \
969 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \
970 "part /bar --ondisk hda --offset 101M --fixed-size 100M --fstype=ext4\n")
971 tempf.flush()
972
973 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
974 self.assertEqual(partlns, [
975 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
976 "2:103424kiB:205824kiB:102400kiB:ext4:primary:;",
977 ])
978
979 with NamedTemporaryFile("w", suffix=".wks") as tempf:
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500980 # Test that partitions can be placed on a 512 byte sector boundary
981 tempf.write("bootloader --ptable gpt\n" \
982 "part / --source rootfs --ondisk hda --offset 65s --fixed-size 99M --fstype=ext4\n" \
983 "part /bar --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n")
984 tempf.flush()
985
986 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
987 self.assertEqual(partlns, [
988 "1:32.5kiB:101408kiB:101376kiB:ext4:primary:;",
989 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
990 ])
991
992 with NamedTemporaryFile("w", suffix=".wks") as tempf:
993 # Test that a partition can be placed immediately after a MSDOS partition table
994 tempf.write("bootloader --ptable msdos\n" \
995 "part / --source rootfs --ondisk hda --offset 1s --fixed-size 100M --fstype=ext4\n")
996 tempf.flush()
997
998 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
999 self.assertEqual(partlns, [
1000 "1:0.50kiB:102400kiB:102400kiB:ext4::;",
1001 ])
1002
1003 with NamedTemporaryFile("w", suffix=".wks") as tempf:
Andrew Geissler4ed12e12020-06-05 18:00:41 -05001004 # Test that image creation fails if the partitions would overlap
1005 tempf.write("bootloader --ptable gpt\n" \
1006 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \
1007 "part /bar --ondisk hda --offset 102431 --fixed-size 100M --fstype=ext4\n")
1008 tempf.flush()
1009
1010 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True)
1011 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
1012
1013 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1014 # Test that partitions are not allowed to overlap with the booloader
1015 tempf.write("bootloader --ptable gpt\n" \
1016 "part / --source rootfs --ondisk hda --offset 8 --fixed-size 100M --fstype=ext4\n")
1017 tempf.flush()
1018
1019 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True)
1020 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001021
Andrew Geissler5a43b432020-06-13 10:46:56 -05001022 def test_extra_space(self):
1023 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
1024
1025 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1026 tempf.write("bootloader --ptable gpt\n" \
1027 "part / --source rootfs --ondisk hda --extra-space 200M --fstype=ext4\n")
1028 tempf.flush()
1029
1030 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
1031 self.assertEqual(len(partlns), 1)
1032 size = partlns[0].split(':')[3]
1033 self.assertRegex(size, r'^[0-9]+kiB$')
1034 size = int(size[:-3])
1035 self.assertGreaterEqual(size, 204800)
1036
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001037 @only_for_arch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001038 def test_rawcopy_plugin_qemu(self):
1039 """Test rawcopy plugin in qemu"""
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001040 # build ext4 and then use it for a wic image
1041 config = 'IMAGE_FSTYPES = "ext4"\n'
1042 self.append_config(config)
1043 self.assertEqual(0, bitbake('core-image-minimal').status)
1044 self.remove_config(config)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001045
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001046 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_rawcopy_plugin.wks.in"\n'
1047 self.append_config(config)
1048 self.assertEqual(0, bitbake('core-image-minimal-mtdutils').status)
1049 self.remove_config(config)
1050
1051 with runqemu('core-image-minimal-mtdutils', ssh=False, image_fstype='wic') as qemu:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001052 cmd = "grep sda. /proc/partitions |wc -l"
1053 status, output = qemu.run_serial(cmd)
1054 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1055 self.assertEqual(output, '2')
1056
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001057 def test_rawcopy_plugin(self):
1058 """Test rawcopy plugin"""
1059 img = 'core-image-minimal'
1060 machine = get_bb_var('MACHINE', img)
1061 with NamedTemporaryFile("w", suffix=".wks") as wks:
1062 wks.writelines(['part /boot --active --source bootimg-pcbios\n',
1063 'part / --source rawcopy --sourceparams="file=%s-%s.ext4" --use-uuid\n'\
1064 % (img, machine),
1065 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
1066 wks.flush()
1067 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001068 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001069 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1070 out = glob(self.resultdir + "%s-*direct" % wksname)
1071 self.assertEqual(1, len(out))
1072
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001073 def test_empty_plugin(self):
1074 """Test empty plugin"""
1075 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_empty_plugin.wks"\n'
1076 self.append_config(config)
1077 self.assertEqual(0, bitbake('core-image-minimal').status)
1078 self.remove_config(config)
1079
1080 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
1081 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
1082 machine = bb_vars['MACHINE']
1083 image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine)
1084 self.assertEqual(True, os.path.exists(image_path))
1085
1086 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1087
1088 # Fstype column from 'wic ls' should be empty for the second partition
1089 # as listed in test_empty_plugin.wks
1090 result = runCmd("wic ls %s -n %s | awk -F ' ' '{print $1 \" \" $5}' | grep '^2' | wc -w" % (image_path, sysroot))
1091 self.assertEqual('1', result.output)
1092
Brad Bishop96ff1982019-08-19 13:50:42 -04001093 @only_for_arch(['i586', 'i686', 'x86_64'])
1094 def test_biosplusefi_plugin_qemu(self):
1095 """Test biosplusefi plugin in qemu"""
Patrick Williams213cb262021-08-07 19:21:33 -05001096 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES:append = " efi"\n'
Brad Bishop08902b02019-08-20 09:16:51 -04001097 self.append_config(config)
1098 self.assertEqual(0, bitbake('core-image-minimal').status)
1099 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -04001100
1101 with runqemu('core-image-minimal', ssh=False, image_fstype='wic') as qemu:
1102 # Check that we have ONLY two /dev/sda* partitions (/boot and /)
1103 cmd = "grep sda. /proc/partitions | wc -l"
1104 status, output = qemu.run_serial(cmd)
1105 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1106 self.assertEqual(output, '2')
1107 # Check that /dev/sda1 is /boot and that either /dev/root OR /dev/sda2 is /
1108 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' -e '/dev/root /|/dev/sda2 /'"
1109 status, output = qemu.run_serial(cmd)
1110 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1111 self.assertEqual(output, '2')
1112 # Check that /boot has EFI bootx64.efi (required for EFI)
1113 cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l"
1114 status, output = qemu.run_serial(cmd)
1115 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1116 self.assertEqual(output, '1')
1117 # Check that "BOOTABLE" flag is set on boot partition (required for PC-Bios)
1118 # Trailing "cat" seems to be required; otherwise run_serial() sends back echo of the input command
1119 cmd = "fdisk -l /dev/sda | grep /dev/sda1 | awk {print'$2'} | cat"
1120 status, output = qemu.run_serial(cmd)
1121 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1122 self.assertEqual(output, '*')
1123
1124 @only_for_arch(['i586', 'i686', 'x86_64'])
1125 def test_biosplusefi_plugin(self):
1126 """Test biosplusefi plugin"""
1127 # Wic generation below may fail depending on the order of the unittests
1128 # This is because bootimg-pcbios (that bootimg-biosplusefi uses) generate its MBR inside STAGING_DATADIR directory
1129 # which may or may not exists depending on what was built already
1130 # If an image hasn't been built yet, directory ${STAGING_DATADIR}/syslinux won't exists and _get_bootimg_dir()
1131 # will raise with "Couldn't find correct bootimg_dir"
1132 # The easiest way to work-around this issue is to make sure we already built an image here, hence the bitbake call
Patrick Williams213cb262021-08-07 19:21:33 -05001133 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES:append = " efi"\n'
Brad Bishop08902b02019-08-20 09:16:51 -04001134 self.append_config(config)
1135 self.assertEqual(0, bitbake('core-image-minimal').status)
1136 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -04001137
1138 img = 'core-image-minimal'
1139 with NamedTemporaryFile("w", suffix=".wks") as wks:
1140 wks.writelines(['part /boot --active --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\n',
1141 'part / --source rootfs --fstype=ext4 --align 1024 --use-uuid\n'\
1142 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
1143 wks.flush()
1144 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1145 runCmd(cmd)
1146 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1147 out = glob(self.resultdir + "%s-*.direct" % wksname)
1148 self.assertEqual(1, len(out))
1149
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001150 def test_fs_types(self):
1151 """Test filesystem types for empty and not empty partitions"""
1152 img = 'core-image-minimal'
1153 with NamedTemporaryFile("w", suffix=".wks") as wks:
1154 wks.writelines(['part ext2 --fstype ext2 --source rootfs\n',
1155 'part btrfs --fstype btrfs --source rootfs --size 40M\n',
1156 'part squash --fstype squashfs --source rootfs\n',
1157 'part swap --fstype swap --size 1M\n',
1158 'part emptyvfat --fstype vfat --size 1M\n',
1159 'part emptymsdos --fstype msdos --size 1M\n',
1160 'part emptyext2 --fstype ext2 --size 1M\n',
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001161 'part emptybtrfs --fstype btrfs --size 150M\n'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001162 wks.flush()
1163 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001164 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001165 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1166 out = glob(self.resultdir + "%s-*direct" % wksname)
1167 self.assertEqual(1, len(out))
1168
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001169 def test_kickstart_parser(self):
1170 """Test wks parser options"""
1171 with NamedTemporaryFile("w", suffix=".wks") as wks:
1172 wks.writelines(['part / --fstype ext3 --source rootfs --system-id 0xFF '\
1173 '--overhead-factor 1.2 --size 100k\n'])
1174 wks.flush()
1175 cmd = "wic create %s -e core-image-minimal -o %s" % (wks.name, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001176 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001177 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1178 out = glob(self.resultdir + "%s-*direct" % wksname)
1179 self.assertEqual(1, len(out))
1180
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001181 def test_image_bootpart_globbed(self):
1182 """Test globbed sources with image-bootpart plugin"""
1183 img = "core-image-minimal"
1184 cmd = "wic create sdimage-bootpart -e %s -o %s" % (img, self.resultdir)
1185 config = 'IMAGE_BOOT_FILES = "%s*"' % get_bb_var('KERNEL_IMAGETYPE', img)
1186 self.append_config(config)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001187 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001188 self.remove_config(config)
1189 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct")))
1190
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001191 def test_sparse_copy(self):
1192 """Test sparse_copy with FIEMAP and SEEK_HOLE filemap APIs"""
1193 libpath = os.path.join(get_bb_var('COREBASE'), 'scripts', 'lib', 'wic')
1194 sys.path.insert(0, libpath)
1195 from filemap import FilemapFiemap, FilemapSeek, sparse_copy, ErrorNotSupp
1196 with NamedTemporaryFile("w", suffix=".wic-sparse") as sparse:
1197 src_name = sparse.name
1198 src_size = 1024 * 10
1199 sparse.truncate(src_size)
1200 # write one byte to the file
1201 with open(src_name, 'r+b') as sfile:
1202 sfile.seek(1024 * 4)
1203 sfile.write(b'\x00')
1204 dest = sparse.name + '.out'
1205 # copy src file to dest using different filemap APIs
1206 for api in (FilemapFiemap, FilemapSeek, None):
1207 if os.path.exists(dest):
1208 os.unlink(dest)
1209 try:
1210 sparse_copy(sparse.name, dest, api=api)
1211 except ErrorNotSupp:
1212 continue # skip unsupported API
1213 dest_stat = os.stat(dest)
1214 self.assertEqual(dest_stat.st_size, src_size)
1215 # 8 blocks is 4K (physical sector size)
1216 self.assertEqual(dest_stat.st_blocks, 8)
1217 os.unlink(dest)
1218
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001219 def test_wic_ls(self):
1220 """Test listing image content using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001221 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001222 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001223 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001224 images = glob(self.resultdir + "wictestdisk-*.direct")
1225 self.assertEqual(1, len(images))
1226
1227 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1228
1229 # list partitions
1230 result = runCmd("wic ls %s -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001231 self.assertEqual(3, len(result.output.split('\n')))
1232
1233 # list directory content of the first partition
1234 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001235 self.assertEqual(6, len(result.output.split('\n')))
1236
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001237 def test_wic_cp(self):
1238 """Test copy files and directories to the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001239 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001240 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001241 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001242 images = glob(self.resultdir + "wictestdisk-*.direct")
1243 self.assertEqual(1, len(images))
1244
1245 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1246
1247 # list directory content of the first partition
1248 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001249 self.assertEqual(6, len(result.output.split('\n')))
1250
1251 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1252 testfile.write("test")
1253
1254 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001255 runCmd("wic cp %s %s:1/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001256
1257 # check if file is there
1258 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001259 self.assertEqual(7, len(result.output.split('\n')))
1260 self.assertTrue(os.path.basename(testfile.name) in result.output)
1261
1262 # prepare directory
1263 testdir = os.path.join(self.resultdir, 'wic-test-cp-dir')
1264 testsubdir = os.path.join(testdir, 'subdir')
1265 os.makedirs(os.path.join(testsubdir))
1266 copy(testfile.name, testdir)
1267
1268 # copy directory to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001269 runCmd("wic cp %s %s:1/ -n %s" % (testdir, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001270
1271 # check if directory is there
1272 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001273 self.assertEqual(8, len(result.output.split('\n')))
1274 self.assertTrue(os.path.basename(testdir) in result.output)
1275
Andrew Geissler82c905d2020-04-13 13:39:40 -05001276 # copy the file from the partition and check if it success
1277 dest = '%s-cp' % testfile.name
1278 runCmd("wic cp %s:1/%s %s -n %s" % (images[0],
1279 os.path.basename(testfile.name), dest, sysroot))
1280 self.assertTrue(os.path.exists(dest))
1281
1282
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001283 def test_wic_rm(self):
1284 """Test removing files and directories from the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001285 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001286 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001287 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001288 images = glob(self.resultdir + "mkefidisk-*.direct")
1289 self.assertEqual(1, len(images))
1290
1291 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1292
1293 # list directory content of the first partition
1294 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001295 self.assertIn('\nBZIMAGE ', result.output)
1296 self.assertIn('\nEFI <DIR> ', result.output)
1297
1298 # remove file
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001299 runCmd("wic rm %s:1/bzimage -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001300
1301 # remove directory
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001302 runCmd("wic rm %s:1/efi -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001303
1304 # check if they're removed
1305 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001306 self.assertNotIn('\nBZIMAGE ', result.output)
1307 self.assertNotIn('\nEFI <DIR> ', result.output)
1308
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001309 def test_mkfs_extraopts(self):
1310 """Test wks option --mkfs-extraopts for empty and not empty partitions"""
1311 img = 'core-image-minimal'
1312 with NamedTemporaryFile("w", suffix=".wks") as wks:
1313 wks.writelines(
1314 ['part ext2 --fstype ext2 --source rootfs --mkfs-extraopts "-D -F -i 8192"\n',
1315 "part btrfs --fstype btrfs --source rootfs --size 40M --mkfs-extraopts='--quiet'\n",
1316 'part squash --fstype squashfs --source rootfs --mkfs-extraopts "-no-sparse -b 4096"\n',
1317 'part emptyvfat --fstype vfat --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1318 'part emptymsdos --fstype msdos --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1319 'part emptyext2 --fstype ext2 --size 1M --mkfs-extraopts "-D -F -i 8192"\n',
1320 'part emptybtrfs --fstype btrfs --size 100M --mkfs-extraopts "--mixed -K"\n'])
1321 wks.flush()
1322 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001323 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001324 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1325 out = glob(self.resultdir + "%s-*direct" % wksname)
1326 self.assertEqual(1, len(out))
1327
1328 def test_expand_mbr_image(self):
1329 """Test wic write --expand command for mbr image"""
1330 # build an image
1331 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n'
1332 self.append_config(config)
1333 self.assertEqual(0, bitbake('core-image-minimal').status)
1334
1335 # get path to the image
1336 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
1337 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
1338 machine = bb_vars['MACHINE']
1339 image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine)
1340
1341 self.remove_config(config)
1342
1343 try:
1344 # expand image to 1G
1345 new_image_path = None
1346 with NamedTemporaryFile(mode='wb', suffix='.wic.exp',
1347 dir=deploy_dir, delete=False) as sparse:
1348 sparse.truncate(1024 ** 3)
1349 new_image_path = sparse.name
1350
1351 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1352 cmd = "wic write -n %s --expand 1:0 %s %s" % (sysroot, image_path, new_image_path)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001353 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001354
1355 # check if partitions are expanded
1356 orig = runCmd("wic ls %s -n %s" % (image_path, sysroot))
1357 exp = runCmd("wic ls %s -n %s" % (new_image_path, sysroot))
1358 orig_sizes = [int(line.split()[3]) for line in orig.output.split('\n')[1:]]
1359 exp_sizes = [int(line.split()[3]) for line in exp.output.split('\n')[1:]]
1360 self.assertEqual(orig_sizes[0], exp_sizes[0]) # first partition is not resized
1361 self.assertTrue(orig_sizes[1] < exp_sizes[1])
1362
1363 # Check if all free space is partitioned
1364 result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path))
1365 self.assertTrue("0 B, 0 bytes, 0 sectors" in result.output)
1366
Andrew Geisslerc926e172021-05-07 16:11:35 -05001367 bb.utils.rename(image_path, image_path + '.bak')
1368 bb.utils.rename(new_image_path, image_path)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001369
1370 # Check if it boots in qemu
1371 with runqemu('core-image-minimal', ssh=False) as qemu:
1372 cmd = "ls /etc/"
1373 status, output = qemu.run_serial('true')
1374 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1375 finally:
1376 if os.path.exists(new_image_path):
1377 os.unlink(new_image_path)
1378 if os.path.exists(image_path + '.bak'):
Andrew Geisslerc926e172021-05-07 16:11:35 -05001379 bb.utils.rename(image_path + '.bak', image_path)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001380
1381 def test_wic_ls_ext(self):
1382 """Test listing content of the ext partition using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001383 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001384 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001385 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001386 images = glob(self.resultdir + "wictestdisk-*.direct")
1387 self.assertEqual(1, len(images))
1388
1389 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1390
1391 # list directory content of the second ext4 partition
1392 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001393 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(
1394 set(line.split()[-1] for line in result.output.split('\n') if line)))
1395
1396 def test_wic_cp_ext(self):
1397 """Test copy files and directories to the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001398 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001399 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001400 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001401 images = glob(self.resultdir + "wictestdisk-*.direct")
1402 self.assertEqual(1, len(images))
1403
1404 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1405
1406 # list directory content of the ext4 partition
1407 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001408 dirs = set(line.split()[-1] for line in result.output.split('\n') if line)
1409 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(dirs))
1410
1411 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1412 testfile.write("test")
1413
1414 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001415 runCmd("wic cp %s %s:2/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001416
1417 # check if file is there
1418 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001419 newdirs = set(line.split()[-1] for line in result.output.split('\n') if line)
1420 self.assertEqual(newdirs.difference(dirs), set([os.path.basename(testfile.name)]))
1421
Andrew Geissler82c905d2020-04-13 13:39:40 -05001422 # check if the file to copy is in the partition
1423 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
1424 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line])
1425
1426 # copy file from the partition, replace the temporary file content with it and
1427 # check for the file size to validate the copy
1428 runCmd("wic cp %s:2/etc/fstab %s -n %s" % (images[0], testfile.name, sysroot))
1429 self.assertTrue(os.stat(testfile.name).st_size > 0)
1430
1431
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001432 def test_wic_rm_ext(self):
1433 """Test removing files from the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001434 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001435 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001436 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001437 images = glob(self.resultdir + "mkefidisk-*.direct")
1438 self.assertEqual(1, len(images))
1439
1440 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1441
1442 # list directory content of the /etc directory on ext4 partition
1443 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001444 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line])
1445
1446 # remove file
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001447 runCmd("wic rm %s:2/etc/fstab -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001448
1449 # check if it's removed
1450 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001451 self.assertTrue('fstab' not in [line.split()[-1] for line in result.output.split('\n') if line])
Brad Bishop6dbb3162019-11-25 09:41:34 -05001452
1453 # remove non-empty directory
1454 runCmd("wic rm -r %s:2/etc/ -n %s" % (images[0], sysroot))
1455
1456 # check if it's removed
1457 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
1458 self.assertTrue('etc' not in [line.split()[-1] for line in result.output.split('\n') if line])