blob: de74c07a039634d1cfbb8e49f1c14218c3fa5b89 [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
Patrick Williams45852732022-04-02 08:58:32 -050022from oeqa.core.decorator import OETestTag
Brad Bishopd7bf8c12018-02-25 22:55:05 -050023from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, runqemu
Brad Bishopd7bf8c12018-02-25 22:55:05 -050024
25
Patrick Williams45852732022-04-02 08:58:32 -050026@lru_cache()
27def get_host_arch():
28 return get_bb_var('HOST_ARCH')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050029
30
Patrick Williams45852732022-04-02 08:58:32 -050031def only_for_arch(archs):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050032 """Decorator for wrapping test cases that can be run only for specific target
33 architectures. A list of compatible architectures is passed in `archs`.
Brad Bishopd7bf8c12018-02-25 22:55:05 -050034 """
35 def wrapper(func):
36 @wraps(func)
37 def wrapped_f(*args, **kwargs):
Patrick Williams45852732022-04-02 08:58:32 -050038 arch = get_host_arch()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050039 if archs and arch not in archs:
40 raise unittest.SkipTest("Testcase arch dependency not met: %s" % arch)
41 return func(*args, **kwargs)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050042 return wrapped_f
43 return wrapper
44
Andrew Geissler82c905d2020-04-13 13:39:40 -050045def extract_files(debugfs_output):
46 """
47 extract file names from the output of debugfs -R 'ls -p',
48 which looks like this:
49
50 /2/040755/0/0/.//\n
51 /2/040755/0/0/..//\n
52 /11/040700/0/0/lost+found^M//\n
53 /12/040755/1002/1002/run//\n
54 /13/040755/1002/1002/sys//\n
55 /14/040755/1002/1002/bin//\n
56 /80/040755/1002/1002/var//\n
57 /92/040755/1002/1002/tmp//\n
58 """
59 # NOTE the occasional ^M in file names
60 return [line.split('/')[5].strip() for line in \
61 debugfs_output.strip().split('/\n')]
62
63def files_own_by_root(debugfs_output):
64 for line in debugfs_output.strip().split('/\n'):
65 if line.split('/')[3:5] != ['0', '0']:
66 print(debugfs_output)
67 return False
68 return True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050069
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080070class WicTestCase(OESelftestTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050071 """Wic test class."""
72
Brad Bishopd7bf8c12018-02-25 22:55:05 -050073 image_is_ready = False
Brad Bishopd7bf8c12018-02-25 22:55:05 -050074 wicenv_cache = {}
75
76 def setUpLocal(self):
77 """This code is executed before each test method."""
Patrick Williams45852732022-04-02 08:58:32 -050078 self.resultdir = os.path.join(self.builddir, "wic-tmp")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080079 super(WicTestCase, self).setUpLocal()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050080
81 # Do this here instead of in setUpClass as the base setUp does some
82 # clean up which can result in the native tools built earlier in
83 # setUpClass being unavailable.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080084 if not WicTestCase.image_is_ready:
Patrick Williams45852732022-04-02 08:58:32 -050085 if self.td['USE_NLS'] != 'yes':
86 self.skipTest('wic-tools needs USE_NLS=yes')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050087
Patrick Williams45852732022-04-02 08:58:32 -050088 bitbake('wic-tools core-image-minimal core-image-minimal-mtdutils')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080089 WicTestCase.image_is_ready = True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050090 rmtree(self.resultdir, ignore_errors=True)
91
92 def tearDownLocal(self):
93 """Remove resultdir as it may contain images."""
94 rmtree(self.resultdir, ignore_errors=True)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080095 super(WicTestCase, self).tearDownLocal()
96
97 def _get_image_env_path(self, image):
98 """Generate and obtain the path to <image>.env"""
99 if image not in WicTestCase.wicenv_cache:
Patrick Williams45852732022-04-02 08:58:32 -0500100 bitbake('%s -c do_rootfs_wicenv' % image)
101 stdir = get_bb_var('STAGING_DIR', image)
102 machine = self.td["MACHINE"]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800103 WicTestCase.wicenv_cache[image] = os.path.join(stdir, machine, 'imgdata')
104 return WicTestCase.wicenv_cache[image]
105
Patrick Williams45852732022-04-02 08:58:32 -0500106class CLITests(OESelftestTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500107 def test_version(self):
108 """Test wic --version"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800109 runCmd('wic --version')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500110
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500111 def test_help(self):
112 """Test wic --help and wic -h"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800113 runCmd('wic --help')
114 runCmd('wic -h')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500116 def test_createhelp(self):
117 """Test wic create --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800118 runCmd('wic create --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500120 def test_listhelp(self):
121 """Test wic list --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800122 runCmd('wic list --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500123
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500124 def test_help_create(self):
125 """Test wic help create"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800126 runCmd('wic help create')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500127
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500128 def test_help_list(self):
129 """Test wic help list"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800130 runCmd('wic help list')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500131
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132 def test_help_overview(self):
133 """Test wic help overview"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800134 runCmd('wic help overview')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500135
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500136 def test_help_plugins(self):
137 """Test wic help plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800138 runCmd('wic help plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500139
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500140 def test_help_kickstart(self):
141 """Test wic help kickstart"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800142 runCmd('wic help kickstart')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500143
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500144 def test_list_images(self):
145 """Test wic list images"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800146 runCmd('wic list images')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500147
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500148 def test_list_source_plugins(self):
149 """Test wic list source-plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800150 runCmd('wic list source-plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500151
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500152 def test_listed_images_help(self):
153 """Test wic listed images help"""
154 output = runCmd('wic list images').output
155 imagelist = [line.split()[0] for line in output.splitlines()]
156 for image in imagelist:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800157 runCmd('wic list %s help' % image)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500158
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500159 def test_unsupported_subcommand(self):
160 """Test unsupported subcommand"""
161 self.assertNotEqual(0, runCmd('wic unsupported', ignore_status=True).status)
162
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500163 def test_no_command(self):
164 """Test wic without command"""
165 self.assertEqual(1, runCmd('wic', ignore_status=True).status)
166
Patrick Williams45852732022-04-02 08:58:32 -0500167class Wic(WicTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500168 def test_build_image_name(self):
169 """Test wic create wictestdisk --image-name=core-image-minimal"""
170 cmd = "wic create wictestdisk --image-name=core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800171 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500172 self.assertEqual(1, len(glob(os.path.join (self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500173
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500174 @only_for_arch(['i586', 'i686', 'x86_64'])
175 def test_gpt_image(self):
176 """Test creation of core-image-minimal with gpt table and UUID boot"""
177 cmd = "wic create directdisk-gpt --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800178 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500179 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500180
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500181 @only_for_arch(['i586', 'i686', 'x86_64'])
182 def test_iso_image(self):
183 """Test creation of hybrid iso image with legacy and EFI boot"""
184 config = 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500185 'MACHINE_FEATURES:append = " efi"\n'\
186 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500187 self.append_config(config)
Brad Bishopc4ea0752018-11-15 14:30:15 -0800188 bitbake('core-image-minimal core-image-minimal-initramfs')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500189 self.remove_config(config)
190 cmd = "wic create mkhybridiso --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800191 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500192 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "HYBRID_ISO_IMG-*.direct"))))
193 self.assertEqual(1, len(glob(os.path.join (self.resultdir, "HYBRID_ISO_IMG-*.iso"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500194
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500195 @only_for_arch(['i586', 'i686', 'x86_64'])
196 def test_qemux86_directdisk(self):
197 """Test creation of qemux-86-directdisk image"""
198 cmd = "wic create qemux86-directdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800199 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500200 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "qemux86-directdisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500201
Patrick Williams45852732022-04-02 08:58:32 -0500202 @only_for_arch(['i586', 'i686', 'x86_64', 'aarch64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500203 def test_mkefidisk(self):
204 """Test creation of mkefidisk image"""
205 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800206 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500207 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "mkefidisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500208
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500209 @only_for_arch(['i586', 'i686', 'x86_64'])
210 def test_bootloader_config(self):
211 """Test creation of directdisk-bootloader-config image"""
Patrick Williams213cb262021-08-07 19:21:33 -0500212 config = 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500213 self.append_config(config)
214 bitbake('core-image-minimal')
215 self.remove_config(config)
216 cmd = "wic create directdisk-bootloader-config -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800217 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500218 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-bootloader-config-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500219
Patrick Williams45852732022-04-02 08:58:32 -0500220 @only_for_arch(['i586', 'i686', 'x86_64', 'aarch64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500221 def test_systemd_bootdisk(self):
222 """Test creation of systemd-bootdisk image"""
Patrick Williams213cb262021-08-07 19:21:33 -0500223 config = 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500224 self.append_config(config)
225 bitbake('core-image-minimal')
226 self.remove_config(config)
227 cmd = "wic create systemd-bootdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800228 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500229 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "systemd-bootdisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500230
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500231 def test_efi_bootpart(self):
232 """Test creation of efi-bootpart image"""
233 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
234 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
235 self.append_config('IMAGE_EFI_BOOT_FILES = "%s;kernel"\n' % kimgtype)
236 runCmd(cmd)
237 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
Patrick Williams45852732022-04-02 08:58:32 -0500238 images = glob(os.path.join(self.resultdir, "mkefidisk-*.direct"))
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500239 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
240 self.assertIn("kernel",result.output)
241
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500242 def test_sdimage_bootpart(self):
243 """Test creation of sdimage-bootpart image"""
244 cmd = "wic create sdimage-bootpart -e core-image-minimal -o %s" % self.resultdir
245 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
246 self.write_config('IMAGE_BOOT_FILES = "%s"\n' % kimgtype)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800247 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500248 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "sdimage-bootpart-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500249
Patrick Williams45852732022-04-02 08:58:32 -0500250 # TODO this doesn't have to be x86-specific
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500251 @only_for_arch(['i586', 'i686', 'x86_64'])
252 def test_default_output_dir(self):
253 """Test default output location"""
254 for fname in glob("directdisk-*.direct"):
255 os.remove(fname)
Patrick Williams213cb262021-08-07 19:21:33 -0500256 config = 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500257 self.append_config(config)
258 bitbake('core-image-minimal')
259 self.remove_config(config)
260 cmd = "wic create directdisk -e core-image-minimal"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800261 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500262 self.assertEqual(1, len(glob("directdisk-*.direct")))
263
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500264 @only_for_arch(['i586', 'i686', 'x86_64'])
265 def test_build_artifacts(self):
266 """Test wic create directdisk providing all artifacts."""
267 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
268 'wic-tools')
269 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
270 'core-image-minimal'))
271 bbvars = {key.lower(): value for key, value in bb_vars.items()}
272 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800273 runCmd("wic create directdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500274 "-b %(staging_datadir)s "
275 "-k %(deploy_dir_image)s "
276 "-n %(recipe_sysroot_native)s "
277 "-r %(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800278 "-o %(resultdir)s" % bbvars)
Patrick Williams45852732022-04-02 08:58:32 -0500279 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500280
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500281 def test_compress_gzip(self):
282 """Test compressing an image with gzip"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800283 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500284 "--image-name core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800285 "-c gzip -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500286 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct.gz"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500287
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500288 def test_compress_bzip2(self):
289 """Test compressing an image with bzip2"""
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 bzip2 -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500293 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct.bz2"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500294
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500295 def test_compress_xz(self):
296 """Test compressing an image with xz"""
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 "--compress-with=xz -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500300 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct.xz"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500301
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500302 def test_wrong_compressor(self):
303 """Test how wic breaks if wrong compressor is provided"""
304 self.assertEqual(2, runCmd("wic create wictestdisk "
305 "--image-name=core-image-minimal "
306 "-c wrong -o %s" % self.resultdir,
307 ignore_status=True).status)
308
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500309 def test_debug_short(self):
310 """Test -D option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800311 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500312 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800313 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500314 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
315 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "tmp.wic*"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500316
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500317 def test_debug_long(self):
318 """Test --debug option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800319 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500320 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800321 "--debug -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500322 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
323 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "tmp.wic*"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500324
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500325 def test_skip_build_check_short(self):
326 """Test -s option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800327 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500328 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800329 "-s -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500330 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500331
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500332 def test_skip_build_check_long(self):
333 """Test --skip-build-check 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 "
336 "--skip-build-check "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800337 "--outdir %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500338 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500339
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500340 def test_build_rootfs_short(self):
341 """Test -f option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800342 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500343 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800344 "-f -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500345 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500346
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500347 def test_build_rootfs_long(self):
348 """Test --build-rootfs 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 "
351 "--build-rootfs "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800352 "--outdir %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500353 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500354
Patrick Williams45852732022-04-02 08:58:32 -0500355 # TODO this doesn't have to be x86-specific
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500356 @only_for_arch(['i586', 'i686', 'x86_64'])
357 def test_rootfs_indirect_recipes(self):
358 """Test usage of rootfs plugin with rootfs recipes"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800359 runCmd("wic create directdisk-multi-rootfs "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500360 "--image-name=core-image-minimal "
361 "--rootfs rootfs1=core-image-minimal "
362 "--rootfs rootfs2=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800363 "--outdir %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500364 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-multi-rootfs*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500365
Patrick Williams45852732022-04-02 08:58:32 -0500366 # TODO this doesn't have to be x86-specific
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500367 @only_for_arch(['i586', 'i686', 'x86_64'])
368 def test_rootfs_artifacts(self):
369 """Test usage of rootfs plugin with rootfs paths"""
370 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
371 'wic-tools')
372 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
373 'core-image-minimal'))
374 bbvars = {key.lower(): value for key, value in bb_vars.items()}
375 bbvars['wks'] = "directdisk-multi-rootfs"
376 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800377 runCmd("wic create %(wks)s "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500378 "--bootimg-dir=%(staging_datadir)s "
379 "--kernel-dir=%(deploy_dir_image)s "
380 "--native-sysroot=%(recipe_sysroot_native)s "
381 "--rootfs-dir rootfs1=%(image_rootfs)s "
382 "--rootfs-dir rootfs2=%(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800383 "--outdir %(resultdir)s" % bbvars)
Patrick Williams45852732022-04-02 08:58:32 -0500384 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "%(wks)s-*.direct" % bbvars))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500385
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500386 def test_exclude_path(self):
387 """Test --exclude-path wks option."""
388
389 oldpath = os.environ['PATH']
390 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
391
392 try:
393 wks_file = 'temp.wks'
394 with open(wks_file, 'w') as wks:
395 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
396 wks.write("""
397part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path usr
398part /usr --source rootfs --ondisk mmcblk0 --fstype=ext4 --rootfs-dir %s/usr
399part /etc --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path bin/ --rootfs-dir %s/usr"""
400 % (rootfs_dir, rootfs_dir))
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800401 runCmd("wic create %s -e core-image-minimal -o %s" \
402 % (wks_file, self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500403
404 os.remove(wks_file)
Patrick Williams45852732022-04-02 08:58:32 -0500405 wicout = glob(os.path.join(self.resultdir, "%s-*direct" % 'temp'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500406 self.assertEqual(1, len(wicout))
407
408 wicimg = wicout[0]
409
410 # verify partition size with wic
411 res = runCmd("parted -m %s unit b p 2>/dev/null" % wicimg)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500412
413 # parse parted output which looks like this:
414 # BYT;\n
415 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
416 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
417 partlns = res.output.splitlines()[2:]
418
419 self.assertEqual(3, len(partlns))
420
421 for part in [1, 2, 3]:
422 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
423 partln = partlns[part-1].split(":")
424 self.assertEqual(7, len(partln))
425 start = int(partln[1].rstrip("B")) / 512
426 length = int(partln[3].rstrip("B")) / 512
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800427 runCmd("dd if=%s of=%s skip=%d count=%d" %
428 (wicimg, part_file, start, length))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500429
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500430 # Test partition 1, should contain the normal root directories, except
431 # /usr.
432 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
433 os.path.join(self.resultdir, "selftest_img.part1"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500434 files = extract_files(res.output)
435 self.assertIn("etc", files)
436 self.assertNotIn("usr", files)
437
438 # Partition 2, should contain common directories for /usr, not root
439 # directories.
440 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
441 os.path.join(self.resultdir, "selftest_img.part2"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500442 files = extract_files(res.output)
443 self.assertNotIn("etc", files)
444 self.assertNotIn("usr", files)
445 self.assertIn("share", files)
446
447 # Partition 3, should contain the same as partition 2, including the bin
448 # directory, but not the files inside it.
449 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
450 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500451 files = extract_files(res.output)
452 self.assertNotIn("etc", files)
453 self.assertNotIn("usr", files)
454 self.assertIn("share", files)
455 self.assertIn("bin", files)
456 res = runCmd("debugfs -R 'ls -p bin' %s 2>/dev/null" % \
457 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500458 files = extract_files(res.output)
459 self.assertIn(".", files)
460 self.assertIn("..", files)
461 self.assertEqual(2, len(files))
462
463 for part in [1, 2, 3]:
464 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
465 os.remove(part_file)
466
467 finally:
468 os.environ['PATH'] = oldpath
469
Andrew Geissler82c905d2020-04-13 13:39:40 -0500470 def test_include_path(self):
471 """Test --include-path wks option."""
472
473 oldpath = os.environ['PATH']
474 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
475
476 try:
477 include_path = os.path.join(self.resultdir, 'test-include')
478 os.makedirs(include_path)
479 with open(os.path.join(include_path, 'test-file'), 'w') as t:
480 t.write("test\n")
481 wks_file = os.path.join(include_path, 'temp.wks')
482 with open(wks_file, 'w') as wks:
483 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
484 wks.write("""
485part /part1 --source rootfs --ondisk mmcblk0 --fstype=ext4
486part /part2 --source rootfs --ondisk mmcblk0 --fstype=ext4 --include-path %s"""
487 % (include_path))
488 runCmd("wic create %s -e core-image-minimal -o %s" \
489 % (wks_file, self.resultdir))
490
491 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
492 part2 = glob(os.path.join(self.resultdir, 'temp-*.direct.p2'))[0]
493
494 # Test partition 1, should not contain 'test-file'
495 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
496 files = extract_files(res.output)
497 self.assertNotIn('test-file', files)
498 self.assertEqual(True, files_own_by_root(res.output))
499
500 # Test partition 2, should contain 'test-file'
501 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part2))
502 files = extract_files(res.output)
503 self.assertIn('test-file', files)
504 self.assertEqual(True, files_own_by_root(res.output))
505
506 finally:
507 os.environ['PATH'] = oldpath
508
509 def test_include_path_embeded(self):
510 """Test --include-path wks option."""
511
512 oldpath = os.environ['PATH']
513 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
514
515 try:
516 include_path = os.path.join(self.resultdir, 'test-include')
517 os.makedirs(include_path)
518 with open(os.path.join(include_path, 'test-file'), 'w') as t:
519 t.write("test\n")
520 wks_file = os.path.join(include_path, 'temp.wks')
521 with open(wks_file, 'w') as wks:
522 wks.write("""
523part / --source rootfs --fstype=ext4 --include-path %s --include-path core-image-minimal-mtdutils export/"""
524 % (include_path))
525 runCmd("wic create %s -e core-image-minimal -o %s" \
526 % (wks_file, self.resultdir))
527
528 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
529
530 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
531 files = extract_files(res.output)
532 self.assertIn('test-file', files)
533 self.assertEqual(True, files_own_by_root(res.output))
534
535 res = runCmd("debugfs -R 'ls -p /export/etc/' %s 2>/dev/null" % (part1))
536 files = extract_files(res.output)
537 self.assertIn('passwd', files)
538 self.assertEqual(True, files_own_by_root(res.output))
539
540 finally:
541 os.environ['PATH'] = oldpath
542
543 def test_include_path_errors(self):
544 """Test --include-path wks option error handling."""
545 wks_file = 'temp.wks'
546
547 # Absolute argument.
548 with open(wks_file, 'w') as wks:
549 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils /export")
550 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
551 % (wks_file, self.resultdir), ignore_status=True).status)
552 os.remove(wks_file)
553
554 # Argument pointing to parent directory.
555 with open(wks_file, 'w') as wks:
556 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils ././..")
557 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
558 % (wks_file, self.resultdir), ignore_status=True).status)
559 os.remove(wks_file)
560
561 # 3 Argument pointing to parent directory.
562 with open(wks_file, 'w') as wks:
563 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils export/ dummy")
564 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
565 % (wks_file, self.resultdir), ignore_status=True).status)
566 os.remove(wks_file)
567
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500568 def test_exclude_path_errors(self):
569 """Test --exclude-path wks option error handling."""
570 wks_file = 'temp.wks'
571
572 # Absolute argument.
573 with open(wks_file, 'w') as wks:
574 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path /usr")
575 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
576 % (wks_file, self.resultdir), ignore_status=True).status)
577 os.remove(wks_file)
578
579 # Argument pointing to parent directory.
580 with open(wks_file, 'w') as wks:
581 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path ././..")
582 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
583 % (wks_file, self.resultdir), ignore_status=True).status)
584 os.remove(wks_file)
585
Andrew Geissler82c905d2020-04-13 13:39:40 -0500586 def test_permissions(self):
587 """Test permissions are respected"""
588
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600589 # prepare wicenv and rootfs
590 bitbake('core-image-minimal core-image-minimal-mtdutils -c do_rootfs_wicenv')
591
Andrew Geissler82c905d2020-04-13 13:39:40 -0500592 oldpath = os.environ['PATH']
593 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
594
595 t_normal = """
596part / --source rootfs --fstype=ext4
597"""
598 t_exclude = """
599part / --source rootfs --fstype=ext4 --exclude-path=home
600"""
601 t_multi = """
602part / --source rootfs --ondisk sda --fstype=ext4
603part /export --source rootfs --rootfs=core-image-minimal-mtdutils --fstype=ext4
604"""
605 t_change = """
606part / --source rootfs --ondisk sda --fstype=ext4 --exclude-path=etc/   
607part /etc --source rootfs --fstype=ext4 --change-directory=etc
608"""
609 tests = [t_normal, t_exclude, t_multi, t_change]
610
611 try:
612 for test in tests:
613 include_path = os.path.join(self.resultdir, 'test-include')
614 os.makedirs(include_path)
615 wks_file = os.path.join(include_path, 'temp.wks')
616 with open(wks_file, 'w') as wks:
617 wks.write(test)
618 runCmd("wic create %s -e core-image-minimal -o %s" \
619 % (wks_file, self.resultdir))
620
621 for part in glob(os.path.join(self.resultdir, 'temp-*.direct.p*')):
622 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part))
623 self.assertEqual(True, files_own_by_root(res.output))
624
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600625 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "%s"\n' % wks_file
626 self.append_config(config)
627 bitbake('core-image-minimal')
628 tmpdir = os.path.join(get_bb_var('WORKDIR', 'core-image-minimal'),'build-wic')
629
630 # check each partition for permission
631 for part in glob(os.path.join(tmpdir, 'temp-*.direct.p*')):
632 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part))
633 self.assertTrue(files_own_by_root(res.output)
634 ,msg='Files permission incorrect using wks set "%s"' % test)
635
636 # clean config and result directory for next cases
637 self.remove_config(config)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500638 rmtree(self.resultdir, ignore_errors=True)
639
640 finally:
641 os.environ['PATH'] = oldpath
642
643 def test_change_directory(self):
644 """Test --change-directory wks option."""
645
646 oldpath = os.environ['PATH']
647 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
648
649 try:
650 include_path = os.path.join(self.resultdir, 'test-include')
651 os.makedirs(include_path)
652 wks_file = os.path.join(include_path, 'temp.wks')
653 with open(wks_file, 'w') as wks:
654 wks.write("part /etc --source rootfs --fstype=ext4 --change-directory=etc")
655 runCmd("wic create %s -e core-image-minimal -o %s" \
656 % (wks_file, self.resultdir))
657
658 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
659
660 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
661 files = extract_files(res.output)
662 self.assertIn('passwd', files)
663
664 finally:
665 os.environ['PATH'] = oldpath
666
667 def test_change_directory_errors(self):
668 """Test --change-directory wks option error handling."""
669 wks_file = 'temp.wks'
670
671 # Absolute argument.
672 with open(wks_file, 'w') as wks:
673 wks.write("part / --source rootfs --fstype=ext4 --change-directory /usr")
674 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
675 % (wks_file, self.resultdir), ignore_status=True).status)
676 os.remove(wks_file)
677
678 # Argument pointing to parent directory.
679 with open(wks_file, 'w') as wks:
680 wks.write("part / --source rootfs --fstype=ext4 --change-directory ././..")
681 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
682 % (wks_file, self.resultdir), ignore_status=True).status)
683 os.remove(wks_file)
684
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500685 def test_no_fstab_update(self):
686 """Test --no-fstab-update wks option."""
687
688 oldpath = os.environ['PATH']
689 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
690
691 # Get stock fstab from base-files recipe
Patrick Williams45852732022-04-02 08:58:32 -0500692 bitbake('base-files -c do_install')
693 bf_fstab = os.path.join(get_bb_var('D', 'base-files'), 'etc', 'fstab')
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500694 self.assertEqual(True, os.path.exists(bf_fstab))
695 bf_fstab_md5sum = runCmd('md5sum %s 2>/dev/null' % bf_fstab).output.split(" ")[0]
696
697 try:
698 no_fstab_update_path = os.path.join(self.resultdir, 'test-no-fstab-update')
699 os.makedirs(no_fstab_update_path)
700 wks_file = os.path.join(no_fstab_update_path, 'temp.wks')
701 with open(wks_file, 'w') as wks:
702 wks.writelines(['part / --source rootfs --fstype=ext4 --label rootfs\n',
703 'part /mnt/p2 --source rootfs --rootfs-dir=core-image-minimal ',
704 '--fstype=ext4 --label p2 --no-fstab-update\n'])
705 runCmd("wic create %s -e core-image-minimal -o %s" \
706 % (wks_file, self.resultdir))
707
708 part_fstab_md5sum = []
709 for i in range(1, 3):
710 part = glob(os.path.join(self.resultdir, 'temp-*.direct.p') + str(i))[0]
711 part_fstab = runCmd("debugfs -R 'cat etc/fstab' %s 2>/dev/null" % (part))
712 part_fstab_md5sum.append(hashlib.md5((part_fstab.output + "\n\n").encode('utf-8')).hexdigest())
713
714 # '/etc/fstab' in partition 2 should contain the same stock fstab file
715 # as the one installed by the base-file recipe.
716 self.assertEqual(bf_fstab_md5sum, part_fstab_md5sum[1])
717
718 # '/etc/fstab' in partition 1 should contain an updated fstab file.
719 self.assertNotEqual(bf_fstab_md5sum, part_fstab_md5sum[0])
720
721 finally:
722 os.environ['PATH'] = oldpath
723
724 def test_no_fstab_update_errors(self):
725 """Test --no-fstab-update wks option error handling."""
726 wks_file = 'temp.wks'
727
728 # Absolute argument.
729 with open(wks_file, 'w') as wks:
730 wks.write("part / --source rootfs --fstype=ext4 --no-fstab-update /etc")
731 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
732 % (wks_file, self.resultdir), ignore_status=True).status)
733 os.remove(wks_file)
734
735 # Argument pointing to parent directory.
736 with open(wks_file, 'w') as wks:
737 wks.write("part / --source rootfs --fstype=ext4 --no-fstab-update ././..")
738 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
739 % (wks_file, self.resultdir), ignore_status=True).status)
740 os.remove(wks_file)
741
Andrew Geissler5199d832021-09-24 16:47:35 -0500742 def test_extra_space(self):
743 """Test --extra-space wks option."""
744 extraspace = 1024**3
745 runCmd("wic create wictestdisk "
746 "--image-name core-image-minimal "
747 "--extra-space %i -o %s" % (extraspace ,self.resultdir))
Patrick Williams45852732022-04-02 08:58:32 -0500748 wicout = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Andrew Geissler5199d832021-09-24 16:47:35 -0500749 self.assertEqual(1, len(wicout))
750 size = os.path.getsize(wicout[0])
751 self.assertTrue(size > extraspace)
752
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800753class Wic2(WicTestCase):
754
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500755 def test_bmap_short(self):
756 """Test generation of .bmap file -m option"""
757 cmd = "wic create wictestdisk -e core-image-minimal -m -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800758 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500759 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
760 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct.bmap"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500761
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500762 def test_bmap_long(self):
763 """Test generation of .bmap file --bmap option"""
764 cmd = "wic create wictestdisk -e core-image-minimal --bmap -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800765 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500766 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
767 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct.bmap"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500768
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500769 def test_image_env(self):
770 """Test generation of <image>.env files."""
771 image = 'core-image-minimal'
772 imgdatadir = self._get_image_env_path(image)
773
774 bb_vars = get_bb_vars(['IMAGE_BASENAME', 'WICVARS'], image)
775 basename = bb_vars['IMAGE_BASENAME']
776 self.assertEqual(basename, image)
777 path = os.path.join(imgdatadir, basename) + '.env'
778 self.assertTrue(os.path.isfile(path))
779
780 wicvars = set(bb_vars['WICVARS'].split())
781 # filter out optional variables
782 wicvars = wicvars.difference(('DEPLOY_DIR_IMAGE', 'IMAGE_BOOT_FILES',
Brad Bishop96ff1982019-08-19 13:50:42 -0400783 'INITRD', 'INITRD_LIVE', 'ISODIR','INITRAMFS_IMAGE',
Andrew Geissler82c905d2020-04-13 13:39:40 -0500784 'INITRAMFS_IMAGE_BUNDLE', 'INITRAMFS_LINK_NAME',
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500785 'APPEND', 'IMAGE_EFI_BOOT_FILES'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500786 with open(path) as envfile:
787 content = dict(line.split("=", 1) for line in envfile)
788 # test if variables used by wic present in the .env file
789 for var in wicvars:
790 self.assertTrue(var in content, "%s is not in .env file" % var)
791 self.assertTrue(content[var])
792
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500793 def test_image_vars_dir_short(self):
794 """Test image vars directory selection -v option"""
795 image = 'core-image-minimal'
796 imgenvdir = self._get_image_env_path(image)
797 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
798
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800799 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500800 "--image-name=%s -v %s -n %s -o %s"
801 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800802 self.resultdir))
Patrick Williams45852732022-04-02 08:58:32 -0500803 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500804
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500805 def test_image_vars_dir_long(self):
806 """Test image vars directory selection --vars option"""
807 image = 'core-image-minimal'
808 imgenvdir = self._get_image_env_path(image)
809 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
810
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800811 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500812 "--image-name=%s "
813 "--vars %s "
814 "--native-sysroot %s "
815 "--outdir %s"
816 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800817 self.resultdir))
Patrick Williams45852732022-04-02 08:58:32 -0500818 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500819
Patrick Williams45852732022-04-02 08:58:32 -0500820 @only_for_arch(['i586', 'i686', 'x86_64', 'aarch64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500821 def test_wic_image_type(self):
822 """Test building wic images by bitbake"""
823 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500824 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500825 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -0500826 bitbake('wic-image-minimal')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500827 self.remove_config(config)
828
Patrick Williams45852732022-04-02 08:58:32 -0500829 deploy_dir = get_bb_var('DEPLOY_DIR_IMAGE')
830 machine = self.td['MACHINE']
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500831 prefix = os.path.join(deploy_dir, 'wic-image-minimal-%s.' % machine)
832 # check if we have result image and manifests symlinks
833 # pointing to existing files
834 for suffix in ('wic', 'manifest'):
835 path = prefix + suffix
836 self.assertTrue(os.path.islink(path))
837 self.assertTrue(os.path.isfile(os.path.realpath(path)))
838
Patrick Williams45852732022-04-02 08:58:32 -0500839 # TODO this should work on aarch64
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500840 @only_for_arch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -0500841 @OETestTag("runqemu")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500842 def test_qemu(self):
843 """Test wic-image-minimal under qemu"""
844 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500845 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500846 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -0500847 bitbake('wic-image-minimal')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500848 self.remove_config(config)
849
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000850 with runqemu('wic-image-minimal', ssh=False, runqemuparams='nographic') as qemu:
Andrew Geissler99467da2019-02-25 18:54:23 -0600851 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' " \
852 "-e '/dev/root /|/dev/sda2 /' -e '/dev/sda3 /media' -e '/dev/sda4 /mnt'"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400853 status, output = qemu.run_serial(cmd)
Andrew Geissler99467da2019-02-25 18:54:23 -0600854 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
855 self.assertEqual(output, '4')
Brad Bishop316dfdd2018-06-25 12:45:53 -0400856 cmd = "grep UUID= /etc/fstab"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500857 status, output = qemu.run_serial(cmd)
858 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400859 self.assertEqual(output, 'UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba\t/media\text4\tdefaults\t0\t0')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500860
861 @only_for_arch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -0500862 @OETestTag("runqemu")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500863 def test_qemu_efi(self):
864 """Test core-image-minimal efi image under qemu"""
865 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "mkefidisk.wks"\n'
866 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -0500867 bitbake('core-image-minimal ovmf')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500868 self.remove_config(config)
869
870 with runqemu('core-image-minimal', ssh=False,
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000871 runqemuparams='nographic ovmf', image_fstype='wic') as qemu:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500872 cmd = "grep sda. /proc/partitions |wc -l"
873 status, output = qemu.run_serial(cmd)
874 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
875 self.assertEqual(output, '3')
876
877 @staticmethod
878 def _make_fixed_size_wks(size):
879 """
880 Create a wks of an image with a single partition. Size of the partition is set
881 using --fixed-size flag. Returns a tuple: (path to wks file, wks image name)
882 """
883 with NamedTemporaryFile("w", suffix=".wks", delete=False) as tempf:
884 wkspath = tempf.name
885 tempf.write("part " \
886 "--source rootfs --ondisk hda --align 4 --fixed-size %d "
887 "--fstype=ext4\n" % size)
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500888
889 return wkspath
890
891 def _get_wic_partitions(self, wkspath, native_sysroot=None, ignore_status=False):
892 p = runCmd("wic create %s -e core-image-minimal -o %s" % (wkspath, self.resultdir),
893 ignore_status=ignore_status)
894
895 if p.status:
896 return (p, None)
897
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500898 wksname = os.path.splitext(os.path.basename(wkspath))[0]
899
Patrick Williams45852732022-04-02 08:58:32 -0500900 wicout = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500901
902 if not wicout:
903 return (p, None)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500904
905 wicimg = wicout[0]
906
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500907 if not native_sysroot:
908 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800909
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500910 # verify partition size with wic
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500911 res = runCmd("parted -m %s unit kib p 2>/dev/null" % wicimg,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800912 native_sysroot=native_sysroot)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500913
914 # parse parted output which looks like this:
915 # BYT;\n
916 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
917 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500918 return (p, res.output.splitlines()[2:])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500919
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500920 def test_fixed_size(self):
921 """
922 Test creation of a simple image with partition size controlled through
923 --fixed-size flag
924 """
925 wkspath = Wic2._make_fixed_size_wks(200)
926 _, partlns = self._get_wic_partitions(wkspath)
927 os.remove(wkspath)
928
929 self.assertEqual(partlns, [
930 "1:4.00kiB:204804kiB:204800kiB:ext4::;",
931 ])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500932
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500933 def test_fixed_size_error(self):
934 """
935 Test creation of a simple image with partition size controlled through
936 --fixed-size flag. The size of partition is intentionally set to 1MiB
937 in order to trigger an error in wic.
938 """
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500939 wkspath = Wic2._make_fixed_size_wks(1)
940 p, _ = self._get_wic_partitions(wkspath, ignore_status=True)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500941 os.remove(wkspath)
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500942
943 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
944
945 def test_offset(self):
946 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
947
948 with NamedTemporaryFile("w", suffix=".wks") as tempf:
949 # Test that partitions are placed at the correct offsets, default KB
950 tempf.write("bootloader --ptable gpt\n" \
951 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \
952 "part /bar --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n")
953 tempf.flush()
954
955 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
956 self.assertEqual(partlns, [
957 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
958 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
959 ])
960
961 with NamedTemporaryFile("w", suffix=".wks") as tempf:
962 # Test that partitions are placed at the correct offsets, same with explicit KB
963 tempf.write("bootloader --ptable gpt\n" \
964 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \
965 "part /bar --ondisk hda --offset 102432K --fixed-size 100M --fstype=ext4\n")
966 tempf.flush()
967
968 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
969 self.assertEqual(partlns, [
970 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
971 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
972 ])
973
974 with NamedTemporaryFile("w", suffix=".wks") as tempf:
975 # Test that partitions are placed at the correct offsets using MB
976 tempf.write("bootloader --ptable gpt\n" \
977 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \
978 "part /bar --ondisk hda --offset 101M --fixed-size 100M --fstype=ext4\n")
979 tempf.flush()
980
981 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
982 self.assertEqual(partlns, [
983 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
984 "2:103424kiB:205824kiB:102400kiB:ext4:primary:;",
985 ])
986
987 with NamedTemporaryFile("w", suffix=".wks") as tempf:
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500988 # Test that partitions can be placed on a 512 byte sector boundary
989 tempf.write("bootloader --ptable gpt\n" \
990 "part / --source rootfs --ondisk hda --offset 65s --fixed-size 99M --fstype=ext4\n" \
991 "part /bar --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n")
992 tempf.flush()
993
994 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
995 self.assertEqual(partlns, [
996 "1:32.5kiB:101408kiB:101376kiB:ext4:primary:;",
997 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
998 ])
999
1000 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1001 # Test that a partition can be placed immediately after a MSDOS partition table
1002 tempf.write("bootloader --ptable msdos\n" \
1003 "part / --source rootfs --ondisk hda --offset 1s --fixed-size 100M --fstype=ext4\n")
1004 tempf.flush()
1005
1006 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
1007 self.assertEqual(partlns, [
1008 "1:0.50kiB:102400kiB:102400kiB:ext4::;",
1009 ])
1010
1011 with NamedTemporaryFile("w", suffix=".wks") as tempf:
Andrew Geissler4ed12e12020-06-05 18:00:41 -05001012 # Test that image creation fails if the partitions would overlap
1013 tempf.write("bootloader --ptable gpt\n" \
1014 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \
1015 "part /bar --ondisk hda --offset 102431 --fixed-size 100M --fstype=ext4\n")
1016 tempf.flush()
1017
1018 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True)
1019 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
1020
1021 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1022 # Test that partitions are not allowed to overlap with the booloader
1023 tempf.write("bootloader --ptable gpt\n" \
1024 "part / --source rootfs --ondisk hda --offset 8 --fixed-size 100M --fstype=ext4\n")
1025 tempf.flush()
1026
1027 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True)
1028 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001029
Andrew Geissler5a43b432020-06-13 10:46:56 -05001030 def test_extra_space(self):
1031 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
1032
1033 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1034 tempf.write("bootloader --ptable gpt\n" \
1035 "part / --source rootfs --ondisk hda --extra-space 200M --fstype=ext4\n")
1036 tempf.flush()
1037
1038 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
1039 self.assertEqual(len(partlns), 1)
1040 size = partlns[0].split(':')[3]
1041 self.assertRegex(size, r'^[0-9]+kiB$')
1042 size = int(size[:-3])
1043 self.assertGreaterEqual(size, 204800)
1044
Patrick Williams45852732022-04-02 08:58:32 -05001045 @only_for_arch(['i586', 'i686', 'x86_64', 'aarch64'])
1046 @OETestTag("runqemu")
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001047 def test_rawcopy_plugin_qemu(self):
1048 """Test rawcopy plugin in qemu"""
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001049 # build ext4 and then use it for a wic image
1050 config = 'IMAGE_FSTYPES = "ext4"\n'
1051 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001052 bitbake('core-image-minimal')
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001053 self.remove_config(config)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001054
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001055 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_rawcopy_plugin.wks.in"\n'
1056 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001057 bitbake('core-image-minimal-mtdutils')
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001058 self.remove_config(config)
1059
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001060 with runqemu('core-image-minimal-mtdutils', ssh=False,
1061 runqemuparams='nographic', image_fstype='wic') as qemu:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001062 cmd = "grep sda. /proc/partitions |wc -l"
1063 status, output = qemu.run_serial(cmd)
1064 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1065 self.assertEqual(output, '2')
1066
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001067 def _rawcopy_plugin(self, fstype):
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001068 """Test rawcopy plugin"""
1069 img = 'core-image-minimal'
Patrick Williams45852732022-04-02 08:58:32 -05001070 machine = self.td["MACHINE"]
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001071 params = ',unpack' if fstype.endswith('.gz') else ''
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001072 with NamedTemporaryFile("w", suffix=".wks") as wks:
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001073 wks.write('part / --source rawcopy --sourceparams="file=%s-%s.%s%s"\n'\
1074 % (img, machine, fstype, params))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001075 wks.flush()
1076 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001077 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001078 wksname = os.path.splitext(os.path.basename(wks.name))[0]
Patrick Williams45852732022-04-02 08:58:32 -05001079 out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001080 self.assertEqual(1, len(out))
1081
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001082 def test_rawcopy_plugin(self):
1083 self._rawcopy_plugin('ext4')
1084
1085 def test_rawcopy_plugin_unpack(self):
1086 fstype = 'ext4.gz'
1087 config = 'IMAGE_FSTYPES = "%s"\n' % fstype
1088 self.append_config(config)
1089 self.assertEqual(0, bitbake('core-image-minimal').status)
1090 self.remove_config(config)
1091 self._rawcopy_plugin(fstype)
1092
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001093 def test_empty_plugin(self):
1094 """Test empty plugin"""
1095 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_empty_plugin.wks"\n'
1096 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001097 bitbake('core-image-minimal')
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001098 self.remove_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001099 deploy_dir = get_bb_var('DEPLOY_DIR_IMAGE')
1100 machine = self.td['MACHINE']
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001101
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001102 image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine)
Patrick Williams45852732022-04-02 08:58:32 -05001103 self.assertTrue(os.path.exists(image_path))
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001104
1105 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1106
1107 # Fstype column from 'wic ls' should be empty for the second partition
1108 # as listed in test_empty_plugin.wks
1109 result = runCmd("wic ls %s -n %s | awk -F ' ' '{print $1 \" \" $5}' | grep '^2' | wc -w" % (image_path, sysroot))
1110 self.assertEqual('1', result.output)
1111
Brad Bishop96ff1982019-08-19 13:50:42 -04001112 @only_for_arch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -05001113 @OETestTag("runqemu")
Brad Bishop96ff1982019-08-19 13:50:42 -04001114 def test_biosplusefi_plugin_qemu(self):
1115 """Test biosplusefi plugin in qemu"""
Patrick Williams213cb262021-08-07 19:21:33 -05001116 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES:append = " efi"\n'
Brad Bishop08902b02019-08-20 09:16:51 -04001117 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001118 bitbake('core-image-minimal')
Brad Bishop08902b02019-08-20 09:16:51 -04001119 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -04001120
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001121 with runqemu('core-image-minimal', ssh=False,
1122 runqemuparams='nographic', image_fstype='wic') as qemu:
Brad Bishop96ff1982019-08-19 13:50:42 -04001123 # Check that we have ONLY two /dev/sda* partitions (/boot and /)
1124 cmd = "grep sda. /proc/partitions | wc -l"
1125 status, output = qemu.run_serial(cmd)
1126 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1127 self.assertEqual(output, '2')
1128 # Check that /dev/sda1 is /boot and that either /dev/root OR /dev/sda2 is /
1129 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' -e '/dev/root /|/dev/sda2 /'"
1130 status, output = qemu.run_serial(cmd)
1131 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1132 self.assertEqual(output, '2')
1133 # Check that /boot has EFI bootx64.efi (required for EFI)
1134 cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l"
1135 status, output = qemu.run_serial(cmd)
1136 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1137 self.assertEqual(output, '1')
1138 # Check that "BOOTABLE" flag is set on boot partition (required for PC-Bios)
1139 # Trailing "cat" seems to be required; otherwise run_serial() sends back echo of the input command
1140 cmd = "fdisk -l /dev/sda | grep /dev/sda1 | awk {print'$2'} | cat"
1141 status, output = qemu.run_serial(cmd)
1142 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1143 self.assertEqual(output, '*')
1144
1145 @only_for_arch(['i586', 'i686', 'x86_64'])
1146 def test_biosplusefi_plugin(self):
1147 """Test biosplusefi plugin"""
1148 # Wic generation below may fail depending on the order of the unittests
1149 # This is because bootimg-pcbios (that bootimg-biosplusefi uses) generate its MBR inside STAGING_DATADIR directory
1150 # which may or may not exists depending on what was built already
1151 # If an image hasn't been built yet, directory ${STAGING_DATADIR}/syslinux won't exists and _get_bootimg_dir()
1152 # will raise with "Couldn't find correct bootimg_dir"
1153 # 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 -05001154 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES:append = " efi"\n'
Brad Bishop08902b02019-08-20 09:16:51 -04001155 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001156 bitbake('core-image-minimal')
Brad Bishop08902b02019-08-20 09:16:51 -04001157 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -04001158
1159 img = 'core-image-minimal'
1160 with NamedTemporaryFile("w", suffix=".wks") as wks:
1161 wks.writelines(['part /boot --active --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\n',
1162 'part / --source rootfs --fstype=ext4 --align 1024 --use-uuid\n'\
1163 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
1164 wks.flush()
1165 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1166 runCmd(cmd)
1167 wksname = os.path.splitext(os.path.basename(wks.name))[0]
Patrick Williams45852732022-04-02 08:58:32 -05001168 out = glob(os.path.join(self.resultdir, "%s-*.direct" % wksname))
Brad Bishop96ff1982019-08-19 13:50:42 -04001169 self.assertEqual(1, len(out))
1170
Patrick Williams45852732022-04-02 08:58:32 -05001171 # TODO this test could also work on aarch64
Patrick Williams93c203f2021-10-06 16:15:23 -05001172 @only_for_arch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -05001173 @OETestTag("runqemu")
Patrick Williams93c203f2021-10-06 16:15:23 -05001174 def test_efi_plugin_unified_kernel_image_qemu(self):
1175 """Test efi plugin's Unified Kernel Image feature in qemu"""
1176 config = 'IMAGE_FSTYPES = "wic"\n'\
1177 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
1178 'WKS_FILE = "test_efi_plugin.wks"\n'\
1179 'MACHINE_FEATURES:append = " efi"\n'
1180 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001181 bitbake('core-image-minimal core-image-minimal-initramfs ovmf')
Patrick Williams93c203f2021-10-06 16:15:23 -05001182 self.remove_config(config)
1183
1184 with runqemu('core-image-minimal', ssh=False,
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001185 runqemuparams='nographic ovmf', image_fstype='wic') as qemu:
Patrick Williams93c203f2021-10-06 16:15:23 -05001186 # Check that /boot has EFI bootx64.efi (required for EFI)
1187 cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l"
1188 status, output = qemu.run_serial(cmd)
1189 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1190 self.assertEqual(output, '1')
1191 # Check that /boot has EFI/Linux/linux.efi (required for Unified Kernel Images auto detection)
1192 cmd = "ls /boot/EFI/Linux/linux.efi | wc -l"
1193 status, output = qemu.run_serial(cmd)
1194 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1195 self.assertEqual(output, '1')
1196 # Check that /boot doesn't have loader/entries/boot.conf (Unified Kernel Images are auto detected by the bootloader)
1197 cmd = "ls /boot/loader/entries/boot.conf 2&>/dev/null | wc -l"
1198 status, output = qemu.run_serial(cmd)
1199 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1200 self.assertEqual(output, '0')
1201
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001202 def test_fs_types(self):
1203 """Test filesystem types for empty and not empty partitions"""
1204 img = 'core-image-minimal'
1205 with NamedTemporaryFile("w", suffix=".wks") as wks:
1206 wks.writelines(['part ext2 --fstype ext2 --source rootfs\n',
1207 'part btrfs --fstype btrfs --source rootfs --size 40M\n',
1208 'part squash --fstype squashfs --source rootfs\n',
1209 'part swap --fstype swap --size 1M\n',
1210 'part emptyvfat --fstype vfat --size 1M\n',
1211 'part emptymsdos --fstype msdos --size 1M\n',
1212 'part emptyext2 --fstype ext2 --size 1M\n',
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001213 'part emptybtrfs --fstype btrfs --size 150M\n'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001214 wks.flush()
1215 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001216 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001217 wksname = os.path.splitext(os.path.basename(wks.name))[0]
Patrick Williams45852732022-04-02 08:58:32 -05001218 out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001219 self.assertEqual(1, len(out))
1220
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001221 def test_kickstart_parser(self):
1222 """Test wks parser options"""
1223 with NamedTemporaryFile("w", suffix=".wks") as wks:
1224 wks.writelines(['part / --fstype ext3 --source rootfs --system-id 0xFF '\
1225 '--overhead-factor 1.2 --size 100k\n'])
1226 wks.flush()
1227 cmd = "wic create %s -e core-image-minimal -o %s" % (wks.name, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001228 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001229 wksname = os.path.splitext(os.path.basename(wks.name))[0]
Patrick Williams45852732022-04-02 08:58:32 -05001230 out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001231 self.assertEqual(1, len(out))
1232
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001233 def test_image_bootpart_globbed(self):
1234 """Test globbed sources with image-bootpart plugin"""
1235 img = "core-image-minimal"
1236 cmd = "wic create sdimage-bootpart -e %s -o %s" % (img, self.resultdir)
1237 config = 'IMAGE_BOOT_FILES = "%s*"' % get_bb_var('KERNEL_IMAGETYPE', img)
1238 self.append_config(config)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001239 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001240 self.remove_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001241 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "sdimage-bootpart-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001242
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001243 def test_sparse_copy(self):
1244 """Test sparse_copy with FIEMAP and SEEK_HOLE filemap APIs"""
Patrick Williams45852732022-04-02 08:58:32 -05001245 libpath = os.path.join(self.td['COREBASE'], 'scripts', 'lib', 'wic')
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001246 sys.path.insert(0, libpath)
1247 from filemap import FilemapFiemap, FilemapSeek, sparse_copy, ErrorNotSupp
1248 with NamedTemporaryFile("w", suffix=".wic-sparse") as sparse:
1249 src_name = sparse.name
1250 src_size = 1024 * 10
1251 sparse.truncate(src_size)
1252 # write one byte to the file
1253 with open(src_name, 'r+b') as sfile:
1254 sfile.seek(1024 * 4)
1255 sfile.write(b'\x00')
1256 dest = sparse.name + '.out'
1257 # copy src file to dest using different filemap APIs
1258 for api in (FilemapFiemap, FilemapSeek, None):
1259 if os.path.exists(dest):
1260 os.unlink(dest)
1261 try:
1262 sparse_copy(sparse.name, dest, api=api)
1263 except ErrorNotSupp:
1264 continue # skip unsupported API
1265 dest_stat = os.stat(dest)
1266 self.assertEqual(dest_stat.st_size, src_size)
1267 # 8 blocks is 4K (physical sector size)
1268 self.assertEqual(dest_stat.st_blocks, 8)
1269 os.unlink(dest)
1270
Patrick Williams45852732022-04-02 08:58:32 -05001271 def test_mkfs_extraopts(self):
1272 """Test wks option --mkfs-extraopts for empty and not empty partitions"""
1273 img = 'core-image-minimal'
1274 with NamedTemporaryFile("w", suffix=".wks") as wks:
1275 wks.writelines(
1276 ['part ext2 --fstype ext2 --source rootfs --mkfs-extraopts "-D -F -i 8192"\n',
1277 "part btrfs --fstype btrfs --source rootfs --size 40M --mkfs-extraopts='--quiet'\n",
1278 'part squash --fstype squashfs --source rootfs --mkfs-extraopts "-no-sparse -b 4096"\n',
1279 'part emptyvfat --fstype vfat --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1280 'part emptymsdos --fstype msdos --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1281 'part emptyext2 --fstype ext2 --size 1M --mkfs-extraopts "-D -F -i 8192"\n',
1282 'part emptybtrfs --fstype btrfs --size 100M --mkfs-extraopts "--mixed -K"\n'])
1283 wks.flush()
1284 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1285 runCmd(cmd)
1286 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1287 out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
1288 self.assertEqual(1, len(out))
1289
1290 @only_for_arch(['i586', 'i686', 'x86_64'])
1291 @OETestTag("runqemu")
1292 def test_expand_mbr_image(self):
1293 """Test wic write --expand command for mbr image"""
1294 # build an image
1295 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n'
1296 self.append_config(config)
1297 bitbake('core-image-minimal')
1298
1299 # get path to the image
1300 deploy_dir = get_bb_var('DEPLOY_DIR_IMAGE')
1301 machine = self.td['MACHINE']
1302 image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine)
1303
1304 self.remove_config(config)
1305
1306 try:
1307 # expand image to 1G
1308 new_image_path = None
1309 with NamedTemporaryFile(mode='wb', suffix='.wic.exp',
1310 dir=deploy_dir, delete=False) as sparse:
1311 sparse.truncate(1024 ** 3)
1312 new_image_path = sparse.name
1313
1314 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1315 cmd = "wic write -n %s --expand 1:0 %s %s" % (sysroot, image_path, new_image_path)
1316 runCmd(cmd)
1317
1318 # check if partitions are expanded
1319 orig = runCmd("wic ls %s -n %s" % (image_path, sysroot))
1320 exp = runCmd("wic ls %s -n %s" % (new_image_path, sysroot))
1321 orig_sizes = [int(line.split()[3]) for line in orig.output.split('\n')[1:]]
1322 exp_sizes = [int(line.split()[3]) for line in exp.output.split('\n')[1:]]
1323 self.assertEqual(orig_sizes[0], exp_sizes[0]) # first partition is not resized
1324 self.assertTrue(orig_sizes[1] < exp_sizes[1])
1325
1326 # Check if all free space is partitioned
1327 result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path))
1328 self.assertTrue("0 B, 0 bytes, 0 sectors" in result.output)
1329
1330 os.rename(image_path, image_path + '.bak')
1331 os.rename(new_image_path, image_path)
1332
1333 # Check if it boots in qemu
1334 with runqemu('core-image-minimal', ssh=False, runqemuparams='nographic') as qemu:
1335 cmd = "ls /etc/"
1336 status, output = qemu.run_serial('true')
1337 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1338 finally:
1339 if os.path.exists(new_image_path):
1340 os.unlink(new_image_path)
1341 if os.path.exists(image_path + '.bak'):
1342 os.rename(image_path + '.bak', image_path)
1343
1344class ModifyTests(WicTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001345 def test_wic_ls(self):
1346 """Test listing image content using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001347 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001348 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001349 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001350 images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001351 self.assertEqual(1, len(images))
1352
1353 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1354
1355 # list partitions
1356 result = runCmd("wic ls %s -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001357 self.assertEqual(3, len(result.output.split('\n')))
1358
1359 # list directory content of the first partition
1360 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001361 self.assertEqual(6, len(result.output.split('\n')))
1362
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001363 def test_wic_cp(self):
1364 """Test copy files and directories to the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001365 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001366 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001367 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001368 images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001369 self.assertEqual(1, len(images))
1370
1371 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1372
1373 # list directory content of the first partition
1374 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001375 self.assertEqual(6, len(result.output.split('\n')))
1376
1377 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1378 testfile.write("test")
1379
1380 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001381 runCmd("wic cp %s %s:1/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001382
1383 # check if file is there
1384 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001385 self.assertEqual(7, len(result.output.split('\n')))
1386 self.assertTrue(os.path.basename(testfile.name) in result.output)
1387
1388 # prepare directory
1389 testdir = os.path.join(self.resultdir, 'wic-test-cp-dir')
1390 testsubdir = os.path.join(testdir, 'subdir')
1391 os.makedirs(os.path.join(testsubdir))
1392 copy(testfile.name, testdir)
1393
1394 # copy directory to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001395 runCmd("wic cp %s %s:1/ -n %s" % (testdir, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001396
1397 # check if directory is there
1398 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001399 self.assertEqual(8, len(result.output.split('\n')))
1400 self.assertTrue(os.path.basename(testdir) in result.output)
1401
Andrew Geissler82c905d2020-04-13 13:39:40 -05001402 # copy the file from the partition and check if it success
1403 dest = '%s-cp' % testfile.name
1404 runCmd("wic cp %s:1/%s %s -n %s" % (images[0],
1405 os.path.basename(testfile.name), dest, sysroot))
1406 self.assertTrue(os.path.exists(dest))
1407
1408
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001409 def test_wic_rm(self):
1410 """Test removing files and directories from the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001411 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001412 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001413 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001414 images = glob(os.path.join(self.resultdir, "mkefidisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001415 self.assertEqual(1, len(images))
1416
1417 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
Patrick Williams45852732022-04-02 08:58:32 -05001418 # Not bulletproof but hopefully sufficient
1419 kerneltype = get_bb_var('KERNEL_IMAGETYPE', 'virtual/kernel')
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001420
1421 # list directory content of the first partition
1422 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Patrick Williams45852732022-04-02 08:58:32 -05001423 self.assertIn('\n%s ' % kerneltype.upper(), result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001424 self.assertIn('\nEFI <DIR> ', result.output)
1425
Patrick Williams45852732022-04-02 08:58:32 -05001426 # remove file. EFI partitions are case-insensitive so exercise that too
1427 runCmd("wic rm %s:1/%s -n %s" % (images[0], kerneltype.lower(), sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001428
1429 # remove directory
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001430 runCmd("wic rm %s:1/efi -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001431
1432 # check if they're removed
1433 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Patrick Williams45852732022-04-02 08:58:32 -05001434 self.assertNotIn('\n%s ' % kerneltype.upper(), result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001435 self.assertNotIn('\nEFI <DIR> ', result.output)
1436
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001437 def test_wic_ls_ext(self):
1438 """Test listing content of the ext partition using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001439 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001440 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001441 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001442 images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001443 self.assertEqual(1, len(images))
1444
1445 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1446
1447 # list directory content of the second ext4 partition
1448 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001449 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(
1450 set(line.split()[-1] for line in result.output.split('\n') if line)))
1451
1452 def test_wic_cp_ext(self):
1453 """Test copy files and directories to the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001454 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001455 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001456 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001457 images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001458 self.assertEqual(1, len(images))
1459
1460 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1461
1462 # list directory content of the ext4 partition
1463 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001464 dirs = set(line.split()[-1] for line in result.output.split('\n') if line)
1465 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(dirs))
1466
1467 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1468 testfile.write("test")
1469
1470 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001471 runCmd("wic cp %s %s:2/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001472
1473 # check if file is there
1474 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001475 newdirs = set(line.split()[-1] for line in result.output.split('\n') if line)
1476 self.assertEqual(newdirs.difference(dirs), set([os.path.basename(testfile.name)]))
1477
Andrew Geissler82c905d2020-04-13 13:39:40 -05001478 # check if the file to copy is in the partition
1479 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
1480 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line])
1481
1482 # copy file from the partition, replace the temporary file content with it and
1483 # check for the file size to validate the copy
1484 runCmd("wic cp %s:2/etc/fstab %s -n %s" % (images[0], testfile.name, sysroot))
1485 self.assertTrue(os.stat(testfile.name).st_size > 0)
1486
1487
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001488 def test_wic_rm_ext(self):
1489 """Test removing files from the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001490 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001491 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001492 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001493 images = glob(os.path.join(self.resultdir, "mkefidisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001494 self.assertEqual(1, len(images))
1495
1496 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1497
1498 # list directory content of the /etc directory on ext4 partition
1499 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001500 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line])
1501
1502 # remove file
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001503 runCmd("wic rm %s:2/etc/fstab -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001504
1505 # check if it's removed
1506 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001507 self.assertTrue('fstab' not in [line.split()[-1] for line in result.output.split('\n') if line])
Brad Bishop6dbb3162019-11-25 09:41:34 -05001508
1509 # remove non-empty directory
1510 runCmd("wic rm -r %s:2/etc/ -n %s" % (images[0], sysroot))
1511
1512 # check if it's removed
1513 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
1514 self.assertTrue('etc' not in [line.split()[-1] for line in result.output.split('\n') if line])