blob: b4866bcb323b575bffe4baf8ca71e6a4139ac1f3 [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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050018from tempfile import NamedTemporaryFile
19
20from oeqa.selftest.case import OESelftestTestCase
Patrick Williams45852732022-04-02 08:58:32 -050021from oeqa.core.decorator import OETestTag
Patrick Williams7784c422022-11-17 07:29:11 -060022from oeqa.core.decorator.data import skipIfNotArch
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
Andrew Geissler82c905d2020-04-13 13:39:40 -050026def extract_files(debugfs_output):
27 """
28 extract file names from the output of debugfs -R 'ls -p',
29 which looks like this:
30
31 /2/040755/0/0/.//\n
32 /2/040755/0/0/..//\n
33 /11/040700/0/0/lost+found^M//\n
34 /12/040755/1002/1002/run//\n
35 /13/040755/1002/1002/sys//\n
36 /14/040755/1002/1002/bin//\n
37 /80/040755/1002/1002/var//\n
38 /92/040755/1002/1002/tmp//\n
39 """
40 # NOTE the occasional ^M in file names
41 return [line.split('/')[5].strip() for line in \
42 debugfs_output.strip().split('/\n')]
43
44def files_own_by_root(debugfs_output):
45 for line in debugfs_output.strip().split('/\n'):
46 if line.split('/')[3:5] != ['0', '0']:
47 print(debugfs_output)
48 return False
49 return True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050050
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080051class WicTestCase(OESelftestTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050052 """Wic test class."""
53
Brad Bishopd7bf8c12018-02-25 22:55:05 -050054 image_is_ready = False
Brad Bishopd7bf8c12018-02-25 22:55:05 -050055 wicenv_cache = {}
56
57 def setUpLocal(self):
58 """This code is executed before each test method."""
Patrick Williams45852732022-04-02 08:58:32 -050059 self.resultdir = os.path.join(self.builddir, "wic-tmp")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080060 super(WicTestCase, self).setUpLocal()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050061
62 # Do this here instead of in setUpClass as the base setUp does some
63 # clean up which can result in the native tools built earlier in
64 # setUpClass being unavailable.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080065 if not WicTestCase.image_is_ready:
Patrick Williams45852732022-04-02 08:58:32 -050066 if self.td['USE_NLS'] != 'yes':
67 self.skipTest('wic-tools needs USE_NLS=yes')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050068
Patrick Williams45852732022-04-02 08:58:32 -050069 bitbake('wic-tools core-image-minimal core-image-minimal-mtdutils')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080070 WicTestCase.image_is_ready = True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050071 rmtree(self.resultdir, ignore_errors=True)
72
73 def tearDownLocal(self):
74 """Remove resultdir as it may contain images."""
75 rmtree(self.resultdir, ignore_errors=True)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080076 super(WicTestCase, self).tearDownLocal()
77
78 def _get_image_env_path(self, image):
79 """Generate and obtain the path to <image>.env"""
80 if image not in WicTestCase.wicenv_cache:
Patrick Williams45852732022-04-02 08:58:32 -050081 bitbake('%s -c do_rootfs_wicenv' % image)
82 stdir = get_bb_var('STAGING_DIR', image)
83 machine = self.td["MACHINE"]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080084 WicTestCase.wicenv_cache[image] = os.path.join(stdir, machine, 'imgdata')
85 return WicTestCase.wicenv_cache[image]
86
Patrick Williams45852732022-04-02 08:58:32 -050087class CLITests(OESelftestTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050088 def test_version(self):
89 """Test wic --version"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080090 runCmd('wic --version')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050091
Brad Bishopd7bf8c12018-02-25 22:55:05 -050092 def test_help(self):
93 """Test wic --help and wic -h"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080094 runCmd('wic --help')
95 runCmd('wic -h')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050096
Brad Bishopd7bf8c12018-02-25 22:55:05 -050097 def test_createhelp(self):
98 """Test wic create --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080099 runCmd('wic create --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500100
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500101 def test_listhelp(self):
102 """Test wic list --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800103 runCmd('wic list --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500104
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500105 def test_help_create(self):
106 """Test wic help create"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800107 runCmd('wic help create')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500108
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500109 def test_help_list(self):
110 """Test wic help list"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800111 runCmd('wic help list')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500112
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500113 def test_help_overview(self):
114 """Test wic help overview"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800115 runCmd('wic help overview')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500116
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500117 def test_help_plugins(self):
118 """Test wic help plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800119 runCmd('wic help plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500120
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500121 def test_help_kickstart(self):
122 """Test wic help kickstart"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800123 runCmd('wic help kickstart')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500124
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500125 def test_list_images(self):
126 """Test wic list images"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800127 runCmd('wic list images')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500128
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500129 def test_list_source_plugins(self):
130 """Test wic list source-plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800131 runCmd('wic list source-plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500133 def test_listed_images_help(self):
134 """Test wic listed images help"""
135 output = runCmd('wic list images').output
136 imagelist = [line.split()[0] for line in output.splitlines()]
137 for image in imagelist:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800138 runCmd('wic list %s help' % image)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500139
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500140 def test_unsupported_subcommand(self):
141 """Test unsupported subcommand"""
142 self.assertNotEqual(0, runCmd('wic unsupported', ignore_status=True).status)
143
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500144 def test_no_command(self):
145 """Test wic without command"""
146 self.assertEqual(1, runCmd('wic', ignore_status=True).status)
147
Patrick Williams45852732022-04-02 08:58:32 -0500148class Wic(WicTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500149 def test_build_image_name(self):
150 """Test wic create wictestdisk --image-name=core-image-minimal"""
151 cmd = "wic create wictestdisk --image-name=core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800152 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500153 self.assertEqual(1, len(glob(os.path.join (self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500154
Patrick Williams7784c422022-11-17 07:29:11 -0600155 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500156 def test_gpt_image(self):
157 """Test creation of core-image-minimal with gpt table and UUID boot"""
158 cmd = "wic create directdisk-gpt --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800159 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500160 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500161
Patrick Williams7784c422022-11-17 07:29:11 -0600162 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500163 def test_iso_image(self):
164 """Test creation of hybrid iso image with legacy and EFI boot"""
165 config = 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500166 'MACHINE_FEATURES:append = " efi"\n'\
167 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500168 self.append_config(config)
Brad Bishopc4ea0752018-11-15 14:30:15 -0800169 bitbake('core-image-minimal core-image-minimal-initramfs')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500170 self.remove_config(config)
171 cmd = "wic create mkhybridiso --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800172 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500173 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "HYBRID_ISO_IMG-*.direct"))))
174 self.assertEqual(1, len(glob(os.path.join (self.resultdir, "HYBRID_ISO_IMG-*.iso"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500175
Patrick Williams7784c422022-11-17 07:29:11 -0600176 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500177 def test_qemux86_directdisk(self):
178 """Test creation of qemux-86-directdisk image"""
179 cmd = "wic create qemux86-directdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800180 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500181 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "qemux86-directdisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500182
Patrick Williams7784c422022-11-17 07:29:11 -0600183 @skipIfNotArch(['i586', 'i686', 'x86_64', 'aarch64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500184 def test_mkefidisk(self):
185 """Test creation of mkefidisk image"""
186 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800187 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500188 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "mkefidisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500189
Patrick Williams7784c422022-11-17 07:29:11 -0600190 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500191 def test_bootloader_config(self):
192 """Test creation of directdisk-bootloader-config image"""
Patrick Williams213cb262021-08-07 19:21:33 -0500193 config = 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500194 self.append_config(config)
195 bitbake('core-image-minimal')
196 self.remove_config(config)
197 cmd = "wic create directdisk-bootloader-config -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800198 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500199 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-bootloader-config-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500200
Patrick Williams7784c422022-11-17 07:29:11 -0600201 @skipIfNotArch(['i586', 'i686', 'x86_64', 'aarch64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500202 def test_systemd_bootdisk(self):
203 """Test creation of systemd-bootdisk image"""
Patrick Williams213cb262021-08-07 19:21:33 -0500204 config = 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500205 self.append_config(config)
206 bitbake('core-image-minimal')
207 self.remove_config(config)
208 cmd = "wic create systemd-bootdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800209 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500210 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "systemd-bootdisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500211
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500212 def test_efi_bootpart(self):
213 """Test creation of efi-bootpart image"""
214 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
215 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
216 self.append_config('IMAGE_EFI_BOOT_FILES = "%s;kernel"\n' % kimgtype)
217 runCmd(cmd)
218 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
Patrick Williams45852732022-04-02 08:58:32 -0500219 images = glob(os.path.join(self.resultdir, "mkefidisk-*.direct"))
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500220 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
221 self.assertIn("kernel",result.output)
222
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500223 def test_sdimage_bootpart(self):
224 """Test creation of sdimage-bootpart image"""
225 cmd = "wic create sdimage-bootpart -e core-image-minimal -o %s" % self.resultdir
226 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
227 self.write_config('IMAGE_BOOT_FILES = "%s"\n' % kimgtype)
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, "sdimage-bootpart-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500230
Patrick Williams45852732022-04-02 08:58:32 -0500231 # TODO this doesn't have to be x86-specific
Patrick Williams7784c422022-11-17 07:29:11 -0600232 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500233 def test_default_output_dir(self):
234 """Test default output location"""
235 for fname in glob("directdisk-*.direct"):
236 os.remove(fname)
Patrick Williams213cb262021-08-07 19:21:33 -0500237 config = 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500238 self.append_config(config)
239 bitbake('core-image-minimal')
240 self.remove_config(config)
241 cmd = "wic create directdisk -e core-image-minimal"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800242 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500243 self.assertEqual(1, len(glob("directdisk-*.direct")))
244
Patrick Williams7784c422022-11-17 07:29:11 -0600245 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500246 def test_build_artifacts(self):
247 """Test wic create directdisk providing all artifacts."""
248 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
249 'wic-tools')
250 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
251 'core-image-minimal'))
252 bbvars = {key.lower(): value for key, value in bb_vars.items()}
253 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800254 runCmd("wic create directdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500255 "-b %(staging_datadir)s "
256 "-k %(deploy_dir_image)s "
257 "-n %(recipe_sysroot_native)s "
258 "-r %(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800259 "-o %(resultdir)s" % bbvars)
Patrick Williams45852732022-04-02 08:58:32 -0500260 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500261
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500262 def test_compress_gzip(self):
263 """Test compressing an image with gzip"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800264 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500265 "--image-name core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800266 "-c gzip -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500267 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct.gz"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500268
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500269 def test_compress_bzip2(self):
270 """Test compressing an image with bzip2"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800271 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500272 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800273 "-c bzip2 -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500274 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct.bz2"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500275
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500276 def test_compress_xz(self):
277 """Test compressing an image with xz"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800278 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500279 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800280 "--compress-with=xz -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500281 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct.xz"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500282
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500283 def test_wrong_compressor(self):
284 """Test how wic breaks if wrong compressor is provided"""
285 self.assertEqual(2, runCmd("wic create wictestdisk "
286 "--image-name=core-image-minimal "
287 "-c wrong -o %s" % self.resultdir,
288 ignore_status=True).status)
289
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500290 def test_debug_short(self):
291 """Test -D option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800292 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500293 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800294 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500295 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
296 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "tmp.wic*"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500297
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500298 def test_debug_long(self):
299 """Test --debug option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800300 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500301 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800302 "--debug -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500303 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
304 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "tmp.wic*"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500305
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500306 def test_skip_build_check_short(self):
307 """Test -s option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800308 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500309 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800310 "-s -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500311 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500312
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500313 def test_skip_build_check_long(self):
314 """Test --skip-build-check option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800315 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500316 "--image-name=core-image-minimal "
317 "--skip-build-check "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800318 "--outdir %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500319 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500320
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500321 def test_build_rootfs_short(self):
322 """Test -f option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800323 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500324 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800325 "-f -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500326 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500327
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500328 def test_build_rootfs_long(self):
329 """Test --build-rootfs option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800330 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500331 "--image-name=core-image-minimal "
332 "--build-rootfs "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800333 "--outdir %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500334 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500335
Patrick Williams45852732022-04-02 08:58:32 -0500336 # TODO this doesn't have to be x86-specific
Patrick Williams7784c422022-11-17 07:29:11 -0600337 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500338 def test_rootfs_indirect_recipes(self):
339 """Test usage of rootfs plugin with rootfs recipes"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800340 runCmd("wic create directdisk-multi-rootfs "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500341 "--image-name=core-image-minimal "
342 "--rootfs rootfs1=core-image-minimal "
343 "--rootfs rootfs2=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800344 "--outdir %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500345 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-multi-rootfs*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500346
Patrick Williams45852732022-04-02 08:58:32 -0500347 # TODO this doesn't have to be x86-specific
Patrick Williams7784c422022-11-17 07:29:11 -0600348 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500349 def test_rootfs_artifacts(self):
350 """Test usage of rootfs plugin with rootfs paths"""
351 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
352 'wic-tools')
353 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
354 'core-image-minimal'))
355 bbvars = {key.lower(): value for key, value in bb_vars.items()}
356 bbvars['wks'] = "directdisk-multi-rootfs"
357 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800358 runCmd("wic create %(wks)s "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500359 "--bootimg-dir=%(staging_datadir)s "
360 "--kernel-dir=%(deploy_dir_image)s "
361 "--native-sysroot=%(recipe_sysroot_native)s "
362 "--rootfs-dir rootfs1=%(image_rootfs)s "
363 "--rootfs-dir rootfs2=%(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800364 "--outdir %(resultdir)s" % bbvars)
Patrick Williams45852732022-04-02 08:58:32 -0500365 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "%(wks)s-*.direct" % bbvars))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500366
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500367 def test_exclude_path(self):
368 """Test --exclude-path wks option."""
369
370 oldpath = os.environ['PATH']
371 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
372
373 try:
374 wks_file = 'temp.wks'
375 with open(wks_file, 'w') as wks:
376 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
377 wks.write("""
378part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path usr
379part /usr --source rootfs --ondisk mmcblk0 --fstype=ext4 --rootfs-dir %s/usr
380part /etc --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path bin/ --rootfs-dir %s/usr"""
381 % (rootfs_dir, rootfs_dir))
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800382 runCmd("wic create %s -e core-image-minimal -o %s" \
383 % (wks_file, self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500384
385 os.remove(wks_file)
Patrick Williams45852732022-04-02 08:58:32 -0500386 wicout = glob(os.path.join(self.resultdir, "%s-*direct" % 'temp'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500387 self.assertEqual(1, len(wicout))
388
389 wicimg = wicout[0]
390
391 # verify partition size with wic
392 res = runCmd("parted -m %s unit b p 2>/dev/null" % wicimg)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500393
394 # parse parted output which looks like this:
395 # BYT;\n
396 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
397 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
398 partlns = res.output.splitlines()[2:]
399
400 self.assertEqual(3, len(partlns))
401
402 for part in [1, 2, 3]:
403 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
404 partln = partlns[part-1].split(":")
405 self.assertEqual(7, len(partln))
406 start = int(partln[1].rstrip("B")) / 512
407 length = int(partln[3].rstrip("B")) / 512
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800408 runCmd("dd if=%s of=%s skip=%d count=%d" %
409 (wicimg, part_file, start, length))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500410
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500411 # Test partition 1, should contain the normal root directories, except
412 # /usr.
413 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
414 os.path.join(self.resultdir, "selftest_img.part1"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500415 files = extract_files(res.output)
416 self.assertIn("etc", files)
417 self.assertNotIn("usr", files)
418
419 # Partition 2, should contain common directories for /usr, not root
420 # directories.
421 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
422 os.path.join(self.resultdir, "selftest_img.part2"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500423 files = extract_files(res.output)
424 self.assertNotIn("etc", files)
425 self.assertNotIn("usr", files)
426 self.assertIn("share", files)
427
428 # Partition 3, should contain the same as partition 2, including the bin
429 # directory, but not the files inside it.
430 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
431 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500432 files = extract_files(res.output)
433 self.assertNotIn("etc", files)
434 self.assertNotIn("usr", files)
435 self.assertIn("share", files)
436 self.assertIn("bin", files)
437 res = runCmd("debugfs -R 'ls -p bin' %s 2>/dev/null" % \
438 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500439 files = extract_files(res.output)
440 self.assertIn(".", files)
441 self.assertIn("..", files)
442 self.assertEqual(2, len(files))
443
444 for part in [1, 2, 3]:
445 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
446 os.remove(part_file)
447
448 finally:
449 os.environ['PATH'] = oldpath
450
Andrew Geissler82c905d2020-04-13 13:39:40 -0500451 def test_include_path(self):
452 """Test --include-path wks option."""
453
454 oldpath = os.environ['PATH']
455 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
456
457 try:
458 include_path = os.path.join(self.resultdir, 'test-include')
459 os.makedirs(include_path)
460 with open(os.path.join(include_path, 'test-file'), 'w') as t:
461 t.write("test\n")
462 wks_file = os.path.join(include_path, 'temp.wks')
463 with open(wks_file, 'w') as wks:
464 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
465 wks.write("""
466part /part1 --source rootfs --ondisk mmcblk0 --fstype=ext4
467part /part2 --source rootfs --ondisk mmcblk0 --fstype=ext4 --include-path %s"""
468 % (include_path))
469 runCmd("wic create %s -e core-image-minimal -o %s" \
470 % (wks_file, self.resultdir))
471
472 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
473 part2 = glob(os.path.join(self.resultdir, 'temp-*.direct.p2'))[0]
474
475 # Test partition 1, should not contain 'test-file'
476 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
477 files = extract_files(res.output)
478 self.assertNotIn('test-file', files)
479 self.assertEqual(True, files_own_by_root(res.output))
480
481 # Test partition 2, should contain 'test-file'
482 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part2))
483 files = extract_files(res.output)
484 self.assertIn('test-file', files)
485 self.assertEqual(True, files_own_by_root(res.output))
486
487 finally:
488 os.environ['PATH'] = oldpath
489
490 def test_include_path_embeded(self):
491 """Test --include-path wks option."""
492
493 oldpath = os.environ['PATH']
494 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
495
496 try:
497 include_path = os.path.join(self.resultdir, 'test-include')
498 os.makedirs(include_path)
499 with open(os.path.join(include_path, 'test-file'), 'w') as t:
500 t.write("test\n")
501 wks_file = os.path.join(include_path, 'temp.wks')
502 with open(wks_file, 'w') as wks:
503 wks.write("""
504part / --source rootfs --fstype=ext4 --include-path %s --include-path core-image-minimal-mtdutils export/"""
505 % (include_path))
506 runCmd("wic create %s -e core-image-minimal -o %s" \
507 % (wks_file, self.resultdir))
508
509 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
510
511 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
512 files = extract_files(res.output)
513 self.assertIn('test-file', files)
514 self.assertEqual(True, files_own_by_root(res.output))
515
516 res = runCmd("debugfs -R 'ls -p /export/etc/' %s 2>/dev/null" % (part1))
517 files = extract_files(res.output)
518 self.assertIn('passwd', files)
519 self.assertEqual(True, files_own_by_root(res.output))
520
521 finally:
522 os.environ['PATH'] = oldpath
523
524 def test_include_path_errors(self):
525 """Test --include-path wks option error handling."""
526 wks_file = 'temp.wks'
527
528 # Absolute argument.
529 with open(wks_file, 'w') as wks:
530 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils /export")
531 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
532 % (wks_file, self.resultdir), ignore_status=True).status)
533 os.remove(wks_file)
534
535 # Argument pointing to parent directory.
536 with open(wks_file, 'w') as wks:
537 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils ././..")
538 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
539 % (wks_file, self.resultdir), ignore_status=True).status)
540 os.remove(wks_file)
541
542 # 3 Argument pointing to parent directory.
543 with open(wks_file, 'w') as wks:
544 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils export/ dummy")
545 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
546 % (wks_file, self.resultdir), ignore_status=True).status)
547 os.remove(wks_file)
548
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500549 def test_exclude_path_errors(self):
550 """Test --exclude-path wks option error handling."""
551 wks_file = 'temp.wks'
552
553 # Absolute argument.
554 with open(wks_file, 'w') as wks:
555 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path /usr")
556 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
557 % (wks_file, self.resultdir), ignore_status=True).status)
558 os.remove(wks_file)
559
560 # Argument pointing to parent directory.
561 with open(wks_file, 'w') as wks:
562 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path ././..")
563 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
564 % (wks_file, self.resultdir), ignore_status=True).status)
565 os.remove(wks_file)
566
Andrew Geissler82c905d2020-04-13 13:39:40 -0500567 def test_permissions(self):
568 """Test permissions are respected"""
569
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600570 # prepare wicenv and rootfs
571 bitbake('core-image-minimal core-image-minimal-mtdutils -c do_rootfs_wicenv')
572
Andrew Geissler82c905d2020-04-13 13:39:40 -0500573 oldpath = os.environ['PATH']
574 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
575
576 t_normal = """
577part / --source rootfs --fstype=ext4
578"""
579 t_exclude = """
580part / --source rootfs --fstype=ext4 --exclude-path=home
581"""
582 t_multi = """
583part / --source rootfs --ondisk sda --fstype=ext4
584part /export --source rootfs --rootfs=core-image-minimal-mtdutils --fstype=ext4
585"""
586 t_change = """
587part / --source rootfs --ondisk sda --fstype=ext4 --exclude-path=etc/   
588part /etc --source rootfs --fstype=ext4 --change-directory=etc
589"""
590 tests = [t_normal, t_exclude, t_multi, t_change]
591
592 try:
593 for test in tests:
594 include_path = os.path.join(self.resultdir, 'test-include')
595 os.makedirs(include_path)
596 wks_file = os.path.join(include_path, 'temp.wks')
597 with open(wks_file, 'w') as wks:
598 wks.write(test)
599 runCmd("wic create %s -e core-image-minimal -o %s" \
600 % (wks_file, self.resultdir))
601
602 for part in glob(os.path.join(self.resultdir, 'temp-*.direct.p*')):
603 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part))
604 self.assertEqual(True, files_own_by_root(res.output))
605
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600606 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "%s"\n' % wks_file
607 self.append_config(config)
608 bitbake('core-image-minimal')
609 tmpdir = os.path.join(get_bb_var('WORKDIR', 'core-image-minimal'),'build-wic')
610
611 # check each partition for permission
612 for part in glob(os.path.join(tmpdir, 'temp-*.direct.p*')):
613 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part))
614 self.assertTrue(files_own_by_root(res.output)
615 ,msg='Files permission incorrect using wks set "%s"' % test)
616
617 # clean config and result directory for next cases
618 self.remove_config(config)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500619 rmtree(self.resultdir, ignore_errors=True)
620
621 finally:
622 os.environ['PATH'] = oldpath
623
624 def test_change_directory(self):
625 """Test --change-directory wks option."""
626
627 oldpath = os.environ['PATH']
628 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
629
630 try:
631 include_path = os.path.join(self.resultdir, 'test-include')
632 os.makedirs(include_path)
633 wks_file = os.path.join(include_path, 'temp.wks')
634 with open(wks_file, 'w') as wks:
635 wks.write("part /etc --source rootfs --fstype=ext4 --change-directory=etc")
636 runCmd("wic create %s -e core-image-minimal -o %s" \
637 % (wks_file, self.resultdir))
638
639 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
640
641 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
642 files = extract_files(res.output)
643 self.assertIn('passwd', files)
644
645 finally:
646 os.environ['PATH'] = oldpath
647
648 def test_change_directory_errors(self):
649 """Test --change-directory wks option error handling."""
650 wks_file = 'temp.wks'
651
652 # Absolute argument.
653 with open(wks_file, 'w') as wks:
654 wks.write("part / --source rootfs --fstype=ext4 --change-directory /usr")
655 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
656 % (wks_file, self.resultdir), ignore_status=True).status)
657 os.remove(wks_file)
658
659 # Argument pointing to parent directory.
660 with open(wks_file, 'w') as wks:
661 wks.write("part / --source rootfs --fstype=ext4 --change-directory ././..")
662 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
663 % (wks_file, self.resultdir), ignore_status=True).status)
664 os.remove(wks_file)
665
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500666 def test_no_fstab_update(self):
667 """Test --no-fstab-update wks option."""
668
669 oldpath = os.environ['PATH']
670 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
671
672 # Get stock fstab from base-files recipe
Patrick Williams45852732022-04-02 08:58:32 -0500673 bitbake('base-files -c do_install')
674 bf_fstab = os.path.join(get_bb_var('D', 'base-files'), 'etc', 'fstab')
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500675 self.assertEqual(True, os.path.exists(bf_fstab))
676 bf_fstab_md5sum = runCmd('md5sum %s 2>/dev/null' % bf_fstab).output.split(" ")[0]
677
678 try:
679 no_fstab_update_path = os.path.join(self.resultdir, 'test-no-fstab-update')
680 os.makedirs(no_fstab_update_path)
681 wks_file = os.path.join(no_fstab_update_path, 'temp.wks')
682 with open(wks_file, 'w') as wks:
683 wks.writelines(['part / --source rootfs --fstype=ext4 --label rootfs\n',
684 'part /mnt/p2 --source rootfs --rootfs-dir=core-image-minimal ',
685 '--fstype=ext4 --label p2 --no-fstab-update\n'])
686 runCmd("wic create %s -e core-image-minimal -o %s" \
687 % (wks_file, self.resultdir))
688
689 part_fstab_md5sum = []
690 for i in range(1, 3):
691 part = glob(os.path.join(self.resultdir, 'temp-*.direct.p') + str(i))[0]
692 part_fstab = runCmd("debugfs -R 'cat etc/fstab' %s 2>/dev/null" % (part))
693 part_fstab_md5sum.append(hashlib.md5((part_fstab.output + "\n\n").encode('utf-8')).hexdigest())
694
695 # '/etc/fstab' in partition 2 should contain the same stock fstab file
696 # as the one installed by the base-file recipe.
697 self.assertEqual(bf_fstab_md5sum, part_fstab_md5sum[1])
698
699 # '/etc/fstab' in partition 1 should contain an updated fstab file.
700 self.assertNotEqual(bf_fstab_md5sum, part_fstab_md5sum[0])
701
702 finally:
703 os.environ['PATH'] = oldpath
704
705 def test_no_fstab_update_errors(self):
706 """Test --no-fstab-update wks option error handling."""
707 wks_file = 'temp.wks'
708
709 # Absolute argument.
710 with open(wks_file, 'w') as wks:
711 wks.write("part / --source rootfs --fstype=ext4 --no-fstab-update /etc")
712 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
713 % (wks_file, self.resultdir), ignore_status=True).status)
714 os.remove(wks_file)
715
716 # Argument pointing to parent directory.
717 with open(wks_file, 'w') as wks:
718 wks.write("part / --source rootfs --fstype=ext4 --no-fstab-update ././..")
719 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
720 % (wks_file, self.resultdir), ignore_status=True).status)
721 os.remove(wks_file)
722
Andrew Geissler5199d832021-09-24 16:47:35 -0500723 def test_extra_space(self):
724 """Test --extra-space wks option."""
725 extraspace = 1024**3
726 runCmd("wic create wictestdisk "
727 "--image-name core-image-minimal "
728 "--extra-space %i -o %s" % (extraspace ,self.resultdir))
Patrick Williams45852732022-04-02 08:58:32 -0500729 wicout = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Andrew Geissler5199d832021-09-24 16:47:35 -0500730 self.assertEqual(1, len(wicout))
731 size = os.path.getsize(wicout[0])
Andrew Geissler220dafd2023-10-04 10:18:08 -0500732 self.assertTrue(size > extraspace, msg="Extra space not present (%s vs %s)" % (size, extraspace))
Andrew Geissler5199d832021-09-24 16:47:35 -0500733
Andrew Geissler5082cc72023-09-11 08:41:39 -0400734 def test_no_table(self):
735 """Test --no-table wks option."""
736 wks_file = 'temp.wks'
737
738 # Absolute argument.
739 with open(wks_file, 'w') as wks:
740 wks.write("part testspace --no-table --fixed-size 16k --offset 4080k")
741 runCmd("wic create %s --image-name core-image-minimal -o %s" % (wks_file, self.resultdir))
742
743 wicout = glob(os.path.join(self.resultdir, "*.*"))
744
745 self.assertEqual(1, len(wicout))
746 size = os.path.getsize(wicout[0])
747 self.assertEqual(size, 4 * 1024 * 1024)
748
749 os.remove(wks_file)
750
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800751class Wic2(WicTestCase):
752
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500753 def test_bmap_short(self):
754 """Test generation of .bmap file -m option"""
755 cmd = "wic create wictestdisk -e core-image-minimal -m -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800756 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500757 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
758 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct.bmap"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500759
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500760 def test_bmap_long(self):
761 """Test generation of .bmap file --bmap option"""
762 cmd = "wic create wictestdisk -e core-image-minimal --bmap -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800763 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500764 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
765 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct.bmap"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500766
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500767 def test_image_env(self):
768 """Test generation of <image>.env files."""
769 image = 'core-image-minimal'
770 imgdatadir = self._get_image_env_path(image)
771
772 bb_vars = get_bb_vars(['IMAGE_BASENAME', 'WICVARS'], image)
773 basename = bb_vars['IMAGE_BASENAME']
774 self.assertEqual(basename, image)
775 path = os.path.join(imgdatadir, basename) + '.env'
Andrew Geissler220dafd2023-10-04 10:18:08 -0500776 self.assertTrue(os.path.isfile(path), msg="File %s wasn't generated as expected" % path)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500777
778 wicvars = set(bb_vars['WICVARS'].split())
779 # filter out optional variables
780 wicvars = wicvars.difference(('DEPLOY_DIR_IMAGE', 'IMAGE_BOOT_FILES',
Brad Bishop96ff1982019-08-19 13:50:42 -0400781 'INITRD', 'INITRD_LIVE', 'ISODIR','INITRAMFS_IMAGE',
Andrew Geissler82c905d2020-04-13 13:39:40 -0500782 'INITRAMFS_IMAGE_BUNDLE', 'INITRAMFS_LINK_NAME',
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500783 'APPEND', 'IMAGE_EFI_BOOT_FILES'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500784 with open(path) as envfile:
785 content = dict(line.split("=", 1) for line in envfile)
786 # test if variables used by wic present in the .env file
787 for var in wicvars:
788 self.assertTrue(var in content, "%s is not in .env file" % var)
Andrew Geissler220dafd2023-10-04 10:18:08 -0500789 self.assertTrue(content[var], "%s doesn't have a value (%s)" % (var, content[var]))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500790
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500791 def test_image_vars_dir_short(self):
792 """Test image vars directory selection -v option"""
793 image = 'core-image-minimal'
794 imgenvdir = self._get_image_env_path(image)
795 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
796
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800797 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500798 "--image-name=%s -v %s -n %s -o %s"
799 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800800 self.resultdir))
Patrick Williams45852732022-04-02 08:58:32 -0500801 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500802
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500803 def test_image_vars_dir_long(self):
804 """Test image vars directory selection --vars option"""
805 image = 'core-image-minimal'
806 imgenvdir = self._get_image_env_path(image)
807 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
808
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800809 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500810 "--image-name=%s "
811 "--vars %s "
812 "--native-sysroot %s "
813 "--outdir %s"
814 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800815 self.resultdir))
Patrick Williams45852732022-04-02 08:58:32 -0500816 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500817
Patrick Williams7784c422022-11-17 07:29:11 -0600818 # TODO this test could also work on aarch64
819 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500820 def test_wic_image_type(self):
821 """Test building wic images by bitbake"""
822 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500823 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500824 self.append_config(config)
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500825 image = 'wic-image-minimal'
826 bitbake(image)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500827 self.remove_config(config)
828
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500829 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
830 prefix = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], '%s.' % bb_vars['IMAGE_LINK_NAME'])
831
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500832 # check if we have result image and manifests symlinks
833 # pointing to existing files
834 for suffix in ('wic', 'manifest'):
835 path = prefix + suffix
Andrew Geissler220dafd2023-10-04 10:18:08 -0500836 self.assertTrue(os.path.islink(path), msg="Link %s wasn't generated as expected" % path)
837 self.assertTrue(os.path.isfile(os.path.realpath(path)), msg="File linked to by %s wasn't generated as expected" % path)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500838
Patrick Williams45852732022-04-02 08:58:32 -0500839 # TODO this should work on aarch64
Patrick Williams7784c422022-11-17 07:29:11 -0600840 @skipIfNotArch(['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')
Andrew Geissler78b72792022-06-14 06:47:25 -0500856 cmd = "grep UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba /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
Patrick Williams7784c422022-11-17 07:29:11 -0600861 @skipIfNotArch(['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 Williams7784c422022-11-17 07:29:11 -06001045 # TODO this test could also work on aarch64
1046 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -05001047 @OETestTag("runqemu")
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001048 def test_rawcopy_plugin_qemu(self):
1049 """Test rawcopy plugin in qemu"""
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001050 # build ext4 and then use it for a wic image
1051 config = 'IMAGE_FSTYPES = "ext4"\n'
1052 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001053 bitbake('core-image-minimal')
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001054 image_link_name = get_bb_var('IMAGE_LINK_NAME', 'core-image-minimal')
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001055 self.remove_config(config)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001056
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001057 config = 'IMAGE_FSTYPES = "wic"\n' \
1058 'IMAGE_LINK_NAME_CORE_IMAGE_MINIMAL = "%s"\n'\
1059 'WKS_FILE = "test_rawcopy_plugin.wks.in"\n'\
1060 % image_link_name
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001061 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001062 bitbake('core-image-minimal-mtdutils')
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001063 self.remove_config(config)
1064
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001065 with runqemu('core-image-minimal-mtdutils', ssh=False,
1066 runqemuparams='nographic', image_fstype='wic') as qemu:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001067 cmd = "grep sda. /proc/partitions |wc -l"
1068 status, output = qemu.run_serial(cmd)
1069 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1070 self.assertEqual(output, '2')
1071
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001072 def _rawcopy_plugin(self, fstype):
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001073 """Test rawcopy plugin"""
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001074 image = 'core-image-minimal'
1075 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001076 params = ',unpack' if fstype.endswith('.gz') else ''
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001077 with NamedTemporaryFile("w", suffix=".wks") as wks:
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001078 wks.write('part / --source rawcopy --sourceparams="file=%s.%s%s"\n'\
1079 % (bb_vars['IMAGE_LINK_NAME'], fstype, params))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001080 wks.flush()
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001081 cmd = "wic create %s -e %s -o %s" % (wks.name, image, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001082 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001083 wksname = os.path.splitext(os.path.basename(wks.name))[0]
Patrick Williams45852732022-04-02 08:58:32 -05001084 out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001085 self.assertEqual(1, len(out))
1086
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001087 def test_rawcopy_plugin(self):
1088 self._rawcopy_plugin('ext4')
1089
1090 def test_rawcopy_plugin_unpack(self):
1091 fstype = 'ext4.gz'
1092 config = 'IMAGE_FSTYPES = "%s"\n' % fstype
1093 self.append_config(config)
1094 self.assertEqual(0, bitbake('core-image-minimal').status)
1095 self.remove_config(config)
1096 self._rawcopy_plugin(fstype)
1097
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001098 def test_empty_plugin(self):
1099 """Test empty plugin"""
1100 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_empty_plugin.wks"\n'
1101 self.append_config(config)
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001102 image = 'core-image-minimal'
1103 bitbake(image)
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001104 self.remove_config(config)
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001105 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
1106 image_path = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], '%s.wic' % bb_vars['IMAGE_LINK_NAME'])
Andrew Geissler220dafd2023-10-04 10:18:08 -05001107 self.assertTrue(os.path.exists(image_path), msg="Image file %s wasn't generated as expected" % image_path)
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001108
1109 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1110
1111 # Fstype column from 'wic ls' should be empty for the second partition
1112 # as listed in test_empty_plugin.wks
1113 result = runCmd("wic ls %s -n %s | awk -F ' ' '{print $1 \" \" $5}' | grep '^2' | wc -w" % (image_path, sysroot))
1114 self.assertEqual('1', result.output)
1115
Patrick Williams7784c422022-11-17 07:29:11 -06001116 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -05001117 @OETestTag("runqemu")
Brad Bishop96ff1982019-08-19 13:50:42 -04001118 def test_biosplusefi_plugin_qemu(self):
1119 """Test biosplusefi plugin in qemu"""
Patrick Williams213cb262021-08-07 19:21:33 -05001120 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES:append = " efi"\n'
Brad Bishop08902b02019-08-20 09:16:51 -04001121 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001122 bitbake('core-image-minimal')
Brad Bishop08902b02019-08-20 09:16:51 -04001123 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -04001124
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001125 with runqemu('core-image-minimal', ssh=False,
1126 runqemuparams='nographic', image_fstype='wic') as qemu:
Brad Bishop96ff1982019-08-19 13:50:42 -04001127 # Check that we have ONLY two /dev/sda* partitions (/boot and /)
1128 cmd = "grep sda. /proc/partitions | wc -l"
1129 status, output = qemu.run_serial(cmd)
1130 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1131 self.assertEqual(output, '2')
1132 # Check that /dev/sda1 is /boot and that either /dev/root OR /dev/sda2 is /
1133 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' -e '/dev/root /|/dev/sda2 /'"
1134 status, output = qemu.run_serial(cmd)
1135 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1136 self.assertEqual(output, '2')
1137 # Check that /boot has EFI bootx64.efi (required for EFI)
1138 cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l"
1139 status, output = qemu.run_serial(cmd)
1140 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1141 self.assertEqual(output, '1')
1142 # Check that "BOOTABLE" flag is set on boot partition (required for PC-Bios)
1143 # Trailing "cat" seems to be required; otherwise run_serial() sends back echo of the input command
1144 cmd = "fdisk -l /dev/sda | grep /dev/sda1 | awk {print'$2'} | cat"
1145 status, output = qemu.run_serial(cmd)
1146 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1147 self.assertEqual(output, '*')
1148
Patrick Williams7784c422022-11-17 07:29:11 -06001149 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishop96ff1982019-08-19 13:50:42 -04001150 def test_biosplusefi_plugin(self):
1151 """Test biosplusefi plugin"""
1152 # Wic generation below may fail depending on the order of the unittests
1153 # This is because bootimg-pcbios (that bootimg-biosplusefi uses) generate its MBR inside STAGING_DATADIR directory
1154 # which may or may not exists depending on what was built already
1155 # If an image hasn't been built yet, directory ${STAGING_DATADIR}/syslinux won't exists and _get_bootimg_dir()
1156 # will raise with "Couldn't find correct bootimg_dir"
1157 # 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 -05001158 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES:append = " efi"\n'
Brad Bishop08902b02019-08-20 09:16:51 -04001159 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001160 bitbake('core-image-minimal')
Brad Bishop08902b02019-08-20 09:16:51 -04001161 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -04001162
1163 img = 'core-image-minimal'
1164 with NamedTemporaryFile("w", suffix=".wks") as wks:
1165 wks.writelines(['part /boot --active --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\n',
1166 'part / --source rootfs --fstype=ext4 --align 1024 --use-uuid\n'\
1167 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
1168 wks.flush()
1169 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1170 runCmd(cmd)
1171 wksname = os.path.splitext(os.path.basename(wks.name))[0]
Patrick Williams45852732022-04-02 08:58:32 -05001172 out = glob(os.path.join(self.resultdir, "%s-*.direct" % wksname))
Brad Bishop96ff1982019-08-19 13:50:42 -04001173 self.assertEqual(1, len(out))
1174
Andrew Geissler6aa7eec2023-03-03 12:41:14 -06001175 @skipIfNotArch(['i586', 'i686', 'x86_64', 'aarch64'])
1176 def test_uefi_kernel(self):
1177 """ Test uefi-kernel in wic """
1178 config = 'IMAGE_EFI_BOOT_FILES="/etc/fstab;testfile"\nIMAGE_FSTYPES = "wic"\nWKS_FILE = "test_uefikernel.wks"\nMACHINE_FEATURES:append = " efi"\n'
1179 self.append_config(config)
1180 bitbake('core-image-minimal')
1181 self.remove_config(config)
1182
1183 img = 'core-image-minimal'
1184 with NamedTemporaryFile("w", suffix=".wks") as wks:
1185 wks.writelines(['part /boot --source bootimg-efi --sourceparams="loader=uefi-kernel"\n'
1186 'part / --source rootfs --fstype=ext4 --align 1024 --use-uuid\n'\
1187 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
1188 wks.flush()
1189 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1190 runCmd(cmd)
1191 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1192 out = glob(os.path.join(self.resultdir, "%s-*.direct" % wksname))
1193 self.assertEqual(1, len(out))
1194
Patrick Williams45852732022-04-02 08:58:32 -05001195 # TODO this test could also work on aarch64
Patrick Williams7784c422022-11-17 07:29:11 -06001196 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -05001197 @OETestTag("runqemu")
Patrick Williams93c203f2021-10-06 16:15:23 -05001198 def test_efi_plugin_unified_kernel_image_qemu(self):
1199 """Test efi plugin's Unified Kernel Image feature in qemu"""
1200 config = 'IMAGE_FSTYPES = "wic"\n'\
1201 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
1202 'WKS_FILE = "test_efi_plugin.wks"\n'\
1203 'MACHINE_FEATURES:append = " efi"\n'
1204 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001205 bitbake('core-image-minimal core-image-minimal-initramfs ovmf')
Patrick Williams93c203f2021-10-06 16:15:23 -05001206 self.remove_config(config)
1207
1208 with runqemu('core-image-minimal', ssh=False,
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001209 runqemuparams='nographic ovmf', image_fstype='wic') as qemu:
Patrick Williams93c203f2021-10-06 16:15:23 -05001210 # Check that /boot has EFI bootx64.efi (required for EFI)
1211 cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l"
1212 status, output = qemu.run_serial(cmd)
1213 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1214 self.assertEqual(output, '1')
1215 # Check that /boot has EFI/Linux/linux.efi (required for Unified Kernel Images auto detection)
1216 cmd = "ls /boot/EFI/Linux/linux.efi | wc -l"
1217 status, output = qemu.run_serial(cmd)
1218 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1219 self.assertEqual(output, '1')
1220 # Check that /boot doesn't have loader/entries/boot.conf (Unified Kernel Images are auto detected by the bootloader)
1221 cmd = "ls /boot/loader/entries/boot.conf 2&>/dev/null | wc -l"
1222 status, output = qemu.run_serial(cmd)
1223 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1224 self.assertEqual(output, '0')
1225
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001226 def test_fs_types(self):
1227 """Test filesystem types for empty and not empty partitions"""
1228 img = 'core-image-minimal'
1229 with NamedTemporaryFile("w", suffix=".wks") as wks:
1230 wks.writelines(['part ext2 --fstype ext2 --source rootfs\n',
1231 'part btrfs --fstype btrfs --source rootfs --size 40M\n',
1232 'part squash --fstype squashfs --source rootfs\n',
1233 'part swap --fstype swap --size 1M\n',
1234 'part emptyvfat --fstype vfat --size 1M\n',
1235 'part emptymsdos --fstype msdos --size 1M\n',
1236 'part emptyext2 --fstype ext2 --size 1M\n',
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001237 'part emptybtrfs --fstype btrfs --size 150M\n'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001238 wks.flush()
1239 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001240 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001241 wksname = os.path.splitext(os.path.basename(wks.name))[0]
Patrick Williams45852732022-04-02 08:58:32 -05001242 out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001243 self.assertEqual(1, len(out))
1244
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001245 def test_kickstart_parser(self):
1246 """Test wks parser options"""
1247 with NamedTemporaryFile("w", suffix=".wks") as wks:
1248 wks.writelines(['part / --fstype ext3 --source rootfs --system-id 0xFF '\
1249 '--overhead-factor 1.2 --size 100k\n'])
1250 wks.flush()
1251 cmd = "wic create %s -e core-image-minimal -o %s" % (wks.name, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001252 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001253 wksname = os.path.splitext(os.path.basename(wks.name))[0]
Patrick Williams45852732022-04-02 08:58:32 -05001254 out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001255 self.assertEqual(1, len(out))
1256
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001257 def test_image_bootpart_globbed(self):
1258 """Test globbed sources with image-bootpart plugin"""
1259 img = "core-image-minimal"
1260 cmd = "wic create sdimage-bootpart -e %s -o %s" % (img, self.resultdir)
1261 config = 'IMAGE_BOOT_FILES = "%s*"' % get_bb_var('KERNEL_IMAGETYPE', img)
1262 self.append_config(config)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001263 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001264 self.remove_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001265 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "sdimage-bootpart-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001266
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001267 def test_sparse_copy(self):
1268 """Test sparse_copy with FIEMAP and SEEK_HOLE filemap APIs"""
Patrick Williams45852732022-04-02 08:58:32 -05001269 libpath = os.path.join(self.td['COREBASE'], 'scripts', 'lib', 'wic')
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001270 sys.path.insert(0, libpath)
1271 from filemap import FilemapFiemap, FilemapSeek, sparse_copy, ErrorNotSupp
1272 with NamedTemporaryFile("w", suffix=".wic-sparse") as sparse:
1273 src_name = sparse.name
1274 src_size = 1024 * 10
1275 sparse.truncate(src_size)
1276 # write one byte to the file
1277 with open(src_name, 'r+b') as sfile:
1278 sfile.seek(1024 * 4)
1279 sfile.write(b'\x00')
1280 dest = sparse.name + '.out'
1281 # copy src file to dest using different filemap APIs
1282 for api in (FilemapFiemap, FilemapSeek, None):
1283 if os.path.exists(dest):
1284 os.unlink(dest)
1285 try:
1286 sparse_copy(sparse.name, dest, api=api)
1287 except ErrorNotSupp:
1288 continue # skip unsupported API
1289 dest_stat = os.stat(dest)
1290 self.assertEqual(dest_stat.st_size, src_size)
1291 # 8 blocks is 4K (physical sector size)
1292 self.assertEqual(dest_stat.st_blocks, 8)
1293 os.unlink(dest)
1294
Patrick Williams45852732022-04-02 08:58:32 -05001295 def test_mkfs_extraopts(self):
1296 """Test wks option --mkfs-extraopts for empty and not empty partitions"""
1297 img = 'core-image-minimal'
1298 with NamedTemporaryFile("w", suffix=".wks") as wks:
1299 wks.writelines(
1300 ['part ext2 --fstype ext2 --source rootfs --mkfs-extraopts "-D -F -i 8192"\n',
1301 "part btrfs --fstype btrfs --source rootfs --size 40M --mkfs-extraopts='--quiet'\n",
1302 'part squash --fstype squashfs --source rootfs --mkfs-extraopts "-no-sparse -b 4096"\n',
1303 'part emptyvfat --fstype vfat --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1304 'part emptymsdos --fstype msdos --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1305 'part emptyext2 --fstype ext2 --size 1M --mkfs-extraopts "-D -F -i 8192"\n',
1306 'part emptybtrfs --fstype btrfs --size 100M --mkfs-extraopts "--mixed -K"\n'])
1307 wks.flush()
1308 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1309 runCmd(cmd)
1310 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1311 out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
1312 self.assertEqual(1, len(out))
1313
Patrick Williams7784c422022-11-17 07:29:11 -06001314 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -05001315 @OETestTag("runqemu")
1316 def test_expand_mbr_image(self):
1317 """Test wic write --expand command for mbr image"""
1318 # build an image
1319 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n'
1320 self.append_config(config)
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001321 image = 'core-image-minimal'
1322 bitbake(image)
Patrick Williams45852732022-04-02 08:58:32 -05001323
1324 # get path to the image
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001325 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
1326 image_path = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], '%s.wic' % bb_vars['IMAGE_LINK_NAME'])
Patrick Williams45852732022-04-02 08:58:32 -05001327
1328 self.remove_config(config)
1329
1330 try:
1331 # expand image to 1G
1332 new_image_path = None
1333 with NamedTemporaryFile(mode='wb', suffix='.wic.exp',
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001334 dir=bb_vars['DEPLOY_DIR_IMAGE'], delete=False) as sparse:
Patrick Williams45852732022-04-02 08:58:32 -05001335 sparse.truncate(1024 ** 3)
1336 new_image_path = sparse.name
1337
1338 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1339 cmd = "wic write -n %s --expand 1:0 %s %s" % (sysroot, image_path, new_image_path)
1340 runCmd(cmd)
1341
1342 # check if partitions are expanded
1343 orig = runCmd("wic ls %s -n %s" % (image_path, sysroot))
1344 exp = runCmd("wic ls %s -n %s" % (new_image_path, sysroot))
1345 orig_sizes = [int(line.split()[3]) for line in orig.output.split('\n')[1:]]
1346 exp_sizes = [int(line.split()[3]) for line in exp.output.split('\n')[1:]]
1347 self.assertEqual(orig_sizes[0], exp_sizes[0]) # first partition is not resized
Andrew Geissler220dafd2023-10-04 10:18:08 -05001348 self.assertTrue(orig_sizes[1] < exp_sizes[1], msg="Parition size wasn't enlarged (%s vs %s)" % (orig_sizes[1], exp_sizes[1]))
Patrick Williams45852732022-04-02 08:58:32 -05001349
1350 # Check if all free space is partitioned
1351 result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001352 self.assertIn("0 B, 0 bytes, 0 sectors", result.output)
Patrick Williams45852732022-04-02 08:58:32 -05001353
1354 os.rename(image_path, image_path + '.bak')
1355 os.rename(new_image_path, image_path)
1356
1357 # Check if it boots in qemu
1358 with runqemu('core-image-minimal', ssh=False, runqemuparams='nographic') as qemu:
1359 cmd = "ls /etc/"
1360 status, output = qemu.run_serial('true')
1361 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1362 finally:
1363 if os.path.exists(new_image_path):
1364 os.unlink(new_image_path)
1365 if os.path.exists(image_path + '.bak'):
1366 os.rename(image_path + '.bak', image_path)
1367
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001368 def test_gpt_partition_name(self):
1369 """Test --part-name argument to set partition name in GPT table"""
1370 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "test_gpt_partition_name.wks"\n'
1371 self.append_config(config)
Patrick Williamsb542dec2023-06-09 01:26:37 -05001372 image = 'core-image-minimal'
1373 bitbake(image)
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001374 self.remove_config(config)
1375 deploy_dir = get_bb_var('DEPLOY_DIR_IMAGE')
Patrick Williamsb542dec2023-06-09 01:26:37 -05001376 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
1377 image_path = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], '%s.wic' % bb_vars['IMAGE_LINK_NAME'])
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001378
Patrick Williamsb542dec2023-06-09 01:26:37 -05001379 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1380
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001381 # Image is created
Patrick Williamsb542dec2023-06-09 01:26:37 -05001382 self.assertTrue(os.path.exists(image_path), "image file %s doesn't exist" % image_path)
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001383
1384 # Check the names of the three partitions
1385 # as listed in test_gpt_partition_name.wks
Patrick Williamsb542dec2023-06-09 01:26:37 -05001386 result = runCmd("%s/usr/sbin/sfdisk --part-label %s 1" % (sysroot, image_path))
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001387 self.assertEqual('boot-A', result.output)
Patrick Williamsb542dec2023-06-09 01:26:37 -05001388 result = runCmd("%s/usr/sbin/sfdisk --part-label %s 2" % (sysroot, image_path))
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001389 self.assertEqual('root-A', result.output)
1390 # When the --part-name is not defined, the partition name is equal to the --label
Patrick Williamsb542dec2023-06-09 01:26:37 -05001391 result = runCmd("%s/usr/sbin/sfdisk --part-label %s 3" % (sysroot, image_path))
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001392 self.assertEqual('ext-space', result.output)
1393
Patrick Williams45852732022-04-02 08:58:32 -05001394class ModifyTests(WicTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001395 def test_wic_ls(self):
1396 """Test listing image content using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001397 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001398 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001399 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001400 images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001401 self.assertEqual(1, len(images))
1402
1403 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1404
1405 # list partitions
1406 result = runCmd("wic ls %s -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001407 self.assertEqual(3, len(result.output.split('\n')))
1408
1409 # list directory content of the first partition
1410 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001411 self.assertEqual(6, len(result.output.split('\n')))
1412
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001413 def test_wic_cp(self):
1414 """Test copy files and directories to the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001415 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001416 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001417 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001418 images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001419 self.assertEqual(1, len(images))
1420
1421 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1422
1423 # list directory content of the first partition
1424 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001425 self.assertEqual(6, len(result.output.split('\n')))
1426
1427 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1428 testfile.write("test")
1429
1430 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001431 runCmd("wic cp %s %s:1/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001432
1433 # check if file is there
1434 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001435 self.assertEqual(7, len(result.output.split('\n')))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001436 self.assertIn(os.path.basename(testfile.name), result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001437
1438 # prepare directory
1439 testdir = os.path.join(self.resultdir, 'wic-test-cp-dir')
1440 testsubdir = os.path.join(testdir, 'subdir')
1441 os.makedirs(os.path.join(testsubdir))
1442 copy(testfile.name, testdir)
1443
1444 # copy directory to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001445 runCmd("wic cp %s %s:1/ -n %s" % (testdir, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001446
1447 # check if directory is there
1448 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001449 self.assertEqual(8, len(result.output.split('\n')))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001450 self.assertIn(os.path.basename(testdir), result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001451
Andrew Geissler82c905d2020-04-13 13:39:40 -05001452 # copy the file from the partition and check if it success
1453 dest = '%s-cp' % testfile.name
1454 runCmd("wic cp %s:1/%s %s -n %s" % (images[0],
1455 os.path.basename(testfile.name), dest, sysroot))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001456 self.assertTrue(os.path.exists(dest), msg="File %s wasn't generated as expected" % dest)
Andrew Geissler82c905d2020-04-13 13:39:40 -05001457
1458
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001459 def test_wic_rm(self):
1460 """Test removing files and directories from the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001461 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001462 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001463 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001464 images = glob(os.path.join(self.resultdir, "mkefidisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001465 self.assertEqual(1, len(images))
1466
1467 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
Patrick Williams45852732022-04-02 08:58:32 -05001468 # Not bulletproof but hopefully sufficient
1469 kerneltype = get_bb_var('KERNEL_IMAGETYPE', 'virtual/kernel')
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001470
1471 # list directory content of the first partition
1472 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Patrick Williams92b42cb2022-09-03 06:53:57 -05001473 self.assertIn('\n%s ' % kerneltype.upper(), result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001474 self.assertIn('\nEFI <DIR> ', result.output)
1475
Patrick Williams45852732022-04-02 08:58:32 -05001476 # remove file. EFI partitions are case-insensitive so exercise that too
1477 runCmd("wic rm %s:1/%s -n %s" % (images[0], kerneltype.lower(), sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001478
1479 # remove directory
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001480 runCmd("wic rm %s:1/efi -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001481
1482 # check if they're removed
1483 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Patrick Williams45852732022-04-02 08:58:32 -05001484 self.assertNotIn('\n%s ' % kerneltype.upper(), result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001485 self.assertNotIn('\nEFI <DIR> ', result.output)
1486
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001487 def test_wic_ls_ext(self):
1488 """Test listing content of the ext partition using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001489 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001490 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001491 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001492 images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001493 self.assertEqual(1, len(images))
1494
1495 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1496
1497 # list directory content of the second ext4 partition
1498 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001499 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(
Andrew Geissler220dafd2023-10-04 10:18:08 -05001500 set(line.split()[-1] for line in result.output.split('\n') if line)), msg="Expected directories not present %s" % result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001501
1502 def test_wic_cp_ext(self):
1503 """Test copy files and directories to the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001504 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001505 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001506 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001507 images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001508 self.assertEqual(1, len(images))
1509
1510 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1511
1512 # list directory content of the ext4 partition
1513 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001514 dirs = set(line.split()[-1] for line in result.output.split('\n') if line)
Andrew Geissler220dafd2023-10-04 10:18:08 -05001515 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(dirs), msg="Expected directories not present %s" % dirs)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001516
1517 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1518 testfile.write("test")
1519
1520 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001521 runCmd("wic cp %s %s:2/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001522
1523 # check if file is there
1524 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001525 newdirs = set(line.split()[-1] for line in result.output.split('\n') if line)
1526 self.assertEqual(newdirs.difference(dirs), set([os.path.basename(testfile.name)]))
1527
Andrew Geissler82c905d2020-04-13 13:39:40 -05001528 # check if the file to copy is in the partition
1529 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001530 self.assertIn('fstab', [line.split()[-1] for line in result.output.split('\n') if line])
Andrew Geissler82c905d2020-04-13 13:39:40 -05001531
1532 # copy file from the partition, replace the temporary file content with it and
1533 # check for the file size to validate the copy
1534 runCmd("wic cp %s:2/etc/fstab %s -n %s" % (images[0], testfile.name, sysroot))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001535 self.assertTrue(os.stat(testfile.name).st_size > 0, msg="Filesize not as expected %s" % os.stat(testfile.name).st_size)
Andrew Geissler82c905d2020-04-13 13:39:40 -05001536
1537
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001538 def test_wic_rm_ext(self):
1539 """Test removing files from the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001540 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001541 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001542 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001543 images = glob(os.path.join(self.resultdir, "mkefidisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001544 self.assertEqual(1, len(images))
1545
1546 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1547
1548 # list directory content of the /etc directory on ext4 partition
1549 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001550 self.assertIn('fstab', [line.split()[-1] for line in result.output.split('\n') if line])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001551
1552 # remove file
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001553 runCmd("wic rm %s:2/etc/fstab -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001554
1555 # check if it's removed
1556 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001557 self.assertNotIn('fstab', [line.split()[-1] for line in result.output.split('\n') if line])
Brad Bishop6dbb3162019-11-25 09:41:34 -05001558
1559 # remove non-empty directory
1560 runCmd("wic rm -r %s:2/etc/ -n %s" % (images[0], sysroot))
1561
1562 # check if it's removed
1563 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001564 self.assertNotIn('etc', [line.split()[-1] for line in result.output.split('\n') if line])