blob: d16eae58895918bfd46165d016add54fe16bea9a [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001#!/usr/bin/env python
Brad Bishopd7bf8c12018-02-25 22:55:05 -05002#
3# Copyright (c) 2015, Intel Corporation.
Brad Bishopd7bf8c12018-02-25 22:55:05 -05004#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-only
Brad Bishopd7bf8c12018-02-25 22:55:05 -05006#
7# AUTHORS
8# Ed Bartosh <ed.bartosh@linux.intel.com>
9
10"""Test cases for wic."""
11
12import os
13import sys
14import unittest
15
16from glob import glob
17from shutil import rmtree, copy
18from functools import wraps, lru_cache
19from tempfile import NamedTemporaryFile
20
21from oeqa.selftest.case import OESelftestTestCase
22from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, runqemu
Brad Bishopd7bf8c12018-02-25 22:55:05 -050023
24
25@lru_cache(maxsize=32)
26def get_host_arch(recipe):
27 """A cached call to get_bb_var('HOST_ARCH', <recipe>)"""
28 return get_bb_var('HOST_ARCH', recipe)
29
30
31def only_for_arch(archs, image='core-image-minimal'):
32 """Decorator for wrapping test cases that can be run only for specific target
33 architectures. A list of compatible architectures is passed in `archs`.
34 Current architecture will be determined by parsing bitbake output for
35 `image` recipe.
36 """
37 def wrapper(func):
38 @wraps(func)
39 def wrapped_f(*args, **kwargs):
40 arch = get_host_arch(image)
41 if archs and arch not in archs:
42 raise unittest.SkipTest("Testcase arch dependency not met: %s" % arch)
43 return func(*args, **kwargs)
44 wrapped_f.__name__ = func.__name__
45 return wrapped_f
46 return wrapper
47
48
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080049class WicTestCase(OESelftestTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050050 """Wic test class."""
51
Brad Bishopd7bf8c12018-02-25 22:55:05 -050052 image_is_ready = False
Brad Bishopd7bf8c12018-02-25 22:55:05 -050053 wicenv_cache = {}
54
55 def setUpLocal(self):
56 """This code is executed before each test method."""
Brad Bishopc4ea0752018-11-15 14:30:15 -080057 self.resultdir = self.builddir + "/wic-tmp/"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080058 super(WicTestCase, self).setUpLocal()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050059
60 # Do this here instead of in setUpClass as the base setUp does some
61 # clean up which can result in the native tools built earlier in
62 # setUpClass being unavailable.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080063 if not WicTestCase.image_is_ready:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050064 if get_bb_var('USE_NLS') == 'yes':
65 bitbake('wic-tools')
66 else:
67 self.skipTest('wic-tools cannot be built due its (intltool|gettext)-native dependency and NLS disable')
68
69 bitbake('core-image-minimal')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080070 WicTestCase.image_is_ready = True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050071
72 rmtree(self.resultdir, ignore_errors=True)
73
74 def tearDownLocal(self):
75 """Remove resultdir as it may contain images."""
76 rmtree(self.resultdir, ignore_errors=True)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080077 super(WicTestCase, self).tearDownLocal()
78
79 def _get_image_env_path(self, image):
80 """Generate and obtain the path to <image>.env"""
81 if image not in WicTestCase.wicenv_cache:
82 self.assertEqual(0, bitbake('%s -c do_rootfs_wicenv' % image).status)
83 bb_vars = get_bb_vars(['STAGING_DIR', 'MACHINE'], image)
84 stdir = bb_vars['STAGING_DIR']
85 machine = bb_vars['MACHINE']
86 WicTestCase.wicenv_cache[image] = os.path.join(stdir, machine, 'imgdata')
87 return WicTestCase.wicenv_cache[image]
88
89class Wic(WicTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050090
Brad Bishopd7bf8c12018-02-25 22:55:05 -050091 def test_version(self):
92 """Test wic --version"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080093 runCmd('wic --version')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050094
Brad Bishopd7bf8c12018-02-25 22:55:05 -050095 def test_help(self):
96 """Test wic --help and wic -h"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080097 runCmd('wic --help')
98 runCmd('wic -h')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050099
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500100 def test_createhelp(self):
101 """Test wic create --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800102 runCmd('wic create --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500103
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500104 def test_listhelp(self):
105 """Test wic list --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800106 runCmd('wic list --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500107
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500108 def test_help_create(self):
109 """Test wic help create"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800110 runCmd('wic help create')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500111
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500112 def test_help_list(self):
113 """Test wic help list"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800114 runCmd('wic help list')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500116 def test_help_overview(self):
117 """Test wic help overview"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800118 runCmd('wic help overview')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500120 def test_help_plugins(self):
121 """Test wic help plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800122 runCmd('wic help plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500123
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500124 def test_help_kickstart(self):
125 """Test wic help kickstart"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800126 runCmd('wic help kickstart')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500127
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500128 def test_list_images(self):
129 """Test wic list images"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800130 runCmd('wic list images')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500131
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132 def test_list_source_plugins(self):
133 """Test wic list source-plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800134 runCmd('wic list source-plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500135
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500136 def test_listed_images_help(self):
137 """Test wic listed images help"""
138 output = runCmd('wic list images').output
139 imagelist = [line.split()[0] for line in output.splitlines()]
140 for image in imagelist:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800141 runCmd('wic list %s help' % image)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500142
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500143 def test_unsupported_subcommand(self):
144 """Test unsupported subcommand"""
145 self.assertNotEqual(0, runCmd('wic unsupported', ignore_status=True).status)
146
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500147 def test_no_command(self):
148 """Test wic without command"""
149 self.assertEqual(1, runCmd('wic', ignore_status=True).status)
150
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500151 def test_build_image_name(self):
152 """Test wic create wictestdisk --image-name=core-image-minimal"""
153 cmd = "wic create wictestdisk --image-name=core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800154 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500155 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
156
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500157 @only_for_arch(['i586', 'i686', 'x86_64'])
158 def test_gpt_image(self):
159 """Test creation of core-image-minimal with gpt table and UUID boot"""
160 cmd = "wic create directdisk-gpt --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800161 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500162 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct")))
163
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500164 @only_for_arch(['i586', 'i686', 'x86_64'])
165 def test_iso_image(self):
166 """Test creation of hybrid iso image with legacy and EFI boot"""
167 config = 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
168 'MACHINE_FEATURES_append = " efi"\n'\
169 'DEPENDS_pn-core-image-minimal += "syslinux"\n'
170 self.append_config(config)
Brad Bishopc4ea0752018-11-15 14:30:15 -0800171 bitbake('core-image-minimal core-image-minimal-initramfs')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500172 self.remove_config(config)
173 cmd = "wic create mkhybridiso --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800174 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500175 self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.direct")))
176 self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.iso")))
177
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500178 @only_for_arch(['i586', 'i686', 'x86_64'])
179 def test_qemux86_directdisk(self):
180 """Test creation of qemux-86-directdisk image"""
181 cmd = "wic create qemux86-directdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800182 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500183 self.assertEqual(1, len(glob(self.resultdir + "qemux86-directdisk-*direct")))
184
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500185 @only_for_arch(['i586', 'i686', 'x86_64'])
186 def test_mkefidisk(self):
187 """Test creation of mkefidisk image"""
188 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800189 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500190 self.assertEqual(1, len(glob(self.resultdir + "mkefidisk-*direct")))
191
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500192 @only_for_arch(['i586', 'i686', 'x86_64'])
193 def test_bootloader_config(self):
194 """Test creation of directdisk-bootloader-config image"""
195 config = 'DEPENDS_pn-core-image-minimal += "syslinux"\n'
196 self.append_config(config)
197 bitbake('core-image-minimal')
198 self.remove_config(config)
199 cmd = "wic create directdisk-bootloader-config -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800200 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500201 self.assertEqual(1, len(glob(self.resultdir + "directdisk-bootloader-config-*direct")))
202
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500203 @only_for_arch(['i586', 'i686', 'x86_64'])
204 def test_systemd_bootdisk(self):
205 """Test creation of systemd-bootdisk image"""
206 config = 'MACHINE_FEATURES_append = " efi"\n'
207 self.append_config(config)
208 bitbake('core-image-minimal')
209 self.remove_config(config)
210 cmd = "wic create systemd-bootdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800211 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500212 self.assertEqual(1, len(glob(self.resultdir + "systemd-bootdisk-*direct")))
213
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500214 def test_sdimage_bootpart(self):
215 """Test creation of sdimage-bootpart image"""
216 cmd = "wic create sdimage-bootpart -e core-image-minimal -o %s" % self.resultdir
217 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
218 self.write_config('IMAGE_BOOT_FILES = "%s"\n' % kimgtype)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800219 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500220 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct")))
221
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500222 @only_for_arch(['i586', 'i686', 'x86_64'])
223 def test_default_output_dir(self):
224 """Test default output location"""
225 for fname in glob("directdisk-*.direct"):
226 os.remove(fname)
227 config = 'DEPENDS_pn-core-image-minimal += "syslinux"\n'
228 self.append_config(config)
229 bitbake('core-image-minimal')
230 self.remove_config(config)
231 cmd = "wic create directdisk -e core-image-minimal"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800232 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500233 self.assertEqual(1, len(glob("directdisk-*.direct")))
234
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500235 @only_for_arch(['i586', 'i686', 'x86_64'])
236 def test_build_artifacts(self):
237 """Test wic create directdisk providing all artifacts."""
238 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
239 'wic-tools')
240 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
241 'core-image-minimal'))
242 bbvars = {key.lower(): value for key, value in bb_vars.items()}
243 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800244 runCmd("wic create directdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500245 "-b %(staging_datadir)s "
246 "-k %(deploy_dir_image)s "
247 "-n %(recipe_sysroot_native)s "
248 "-r %(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800249 "-o %(resultdir)s" % bbvars)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500250 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct")))
251
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500252 def test_compress_gzip(self):
253 """Test compressing an image with gzip"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800254 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500255 "--image-name core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800256 "-c gzip -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500257 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.gz")))
258
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500259 def test_compress_bzip2(self):
260 """Test compressing an image with bzip2"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800261 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500262 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800263 "-c bzip2 -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500264 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.bz2")))
265
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500266 def test_compress_xz(self):
267 """Test compressing an image with xz"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800268 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500269 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800270 "--compress-with=xz -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500271 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.xz")))
272
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500273 def test_wrong_compressor(self):
274 """Test how wic breaks if wrong compressor is provided"""
275 self.assertEqual(2, runCmd("wic create wictestdisk "
276 "--image-name=core-image-minimal "
277 "-c wrong -o %s" % self.resultdir,
278 ignore_status=True).status)
279
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500280 def test_debug_short(self):
281 """Test -D option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800282 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500283 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800284 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500285 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
286
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500287 def test_debug_long(self):
288 """Test --debug option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800289 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500290 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800291 "--debug -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500292 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
293
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500294 def test_skip_build_check_short(self):
295 """Test -s option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800296 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500297 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800298 "-s -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500299 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
300
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500301 def test_skip_build_check_long(self):
302 """Test --skip-build-check option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800303 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500304 "--image-name=core-image-minimal "
305 "--skip-build-check "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800306 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500307 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
308
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500309 def test_build_rootfs_short(self):
310 """Test -f 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 "-f -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500314 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
315
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500316 def test_build_rootfs_long(self):
317 """Test --build-rootfs option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800318 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500319 "--image-name=core-image-minimal "
320 "--build-rootfs "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800321 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500322 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
323
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500324 @only_for_arch(['i586', 'i686', 'x86_64'])
325 def test_rootfs_indirect_recipes(self):
326 """Test usage of rootfs plugin with rootfs recipes"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800327 runCmd("wic create directdisk-multi-rootfs "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500328 "--image-name=core-image-minimal "
329 "--rootfs rootfs1=core-image-minimal "
330 "--rootfs rootfs2=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800331 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500332 self.assertEqual(1, len(glob(self.resultdir + "directdisk-multi-rootfs*.direct")))
333
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500334 @only_for_arch(['i586', 'i686', 'x86_64'])
335 def test_rootfs_artifacts(self):
336 """Test usage of rootfs plugin with rootfs paths"""
337 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
338 'wic-tools')
339 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
340 'core-image-minimal'))
341 bbvars = {key.lower(): value for key, value in bb_vars.items()}
342 bbvars['wks'] = "directdisk-multi-rootfs"
343 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800344 runCmd("wic create %(wks)s "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500345 "--bootimg-dir=%(staging_datadir)s "
346 "--kernel-dir=%(deploy_dir_image)s "
347 "--native-sysroot=%(recipe_sysroot_native)s "
348 "--rootfs-dir rootfs1=%(image_rootfs)s "
349 "--rootfs-dir rootfs2=%(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800350 "--outdir %(resultdir)s" % bbvars)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500351 self.assertEqual(1, len(glob(self.resultdir + "%(wks)s-*.direct" % bbvars)))
352
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500353 def test_exclude_path(self):
354 """Test --exclude-path wks option."""
355
356 oldpath = os.environ['PATH']
357 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
358
359 try:
360 wks_file = 'temp.wks'
361 with open(wks_file, 'w') as wks:
362 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
363 wks.write("""
364part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path usr
365part /usr --source rootfs --ondisk mmcblk0 --fstype=ext4 --rootfs-dir %s/usr
366part /etc --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path bin/ --rootfs-dir %s/usr"""
367 % (rootfs_dir, rootfs_dir))
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800368 runCmd("wic create %s -e core-image-minimal -o %s" \
369 % (wks_file, self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500370
371 os.remove(wks_file)
372 wicout = glob(self.resultdir + "%s-*direct" % 'temp')
373 self.assertEqual(1, len(wicout))
374
375 wicimg = wicout[0]
376
377 # verify partition size with wic
378 res = runCmd("parted -m %s unit b p 2>/dev/null" % wicimg)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500379
380 # parse parted output which looks like this:
381 # BYT;\n
382 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
383 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
384 partlns = res.output.splitlines()[2:]
385
386 self.assertEqual(3, len(partlns))
387
388 for part in [1, 2, 3]:
389 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
390 partln = partlns[part-1].split(":")
391 self.assertEqual(7, len(partln))
392 start = int(partln[1].rstrip("B")) / 512
393 length = int(partln[3].rstrip("B")) / 512
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800394 runCmd("dd if=%s of=%s skip=%d count=%d" %
395 (wicimg, part_file, start, length))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500396
397 def extract_files(debugfs_output):
398 """
399 extract file names from the output of debugfs -R 'ls -p',
400 which looks like this:
401
402 /2/040755/0/0/.//\n
403 /2/040755/0/0/..//\n
404 /11/040700/0/0/lost+found^M//\n
405 /12/040755/1002/1002/run//\n
406 /13/040755/1002/1002/sys//\n
407 /14/040755/1002/1002/bin//\n
408 /80/040755/1002/1002/var//\n
409 /92/040755/1002/1002/tmp//\n
410 """
411 # NOTE the occasional ^M in file names
412 return [line.split('/')[5].strip() for line in \
413 debugfs_output.strip().split('/\n')]
414
415 # Test partition 1, should contain the normal root directories, except
416 # /usr.
417 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
418 os.path.join(self.resultdir, "selftest_img.part1"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500419 files = extract_files(res.output)
420 self.assertIn("etc", files)
421 self.assertNotIn("usr", files)
422
423 # Partition 2, should contain common directories for /usr, not root
424 # directories.
425 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
426 os.path.join(self.resultdir, "selftest_img.part2"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500427 files = extract_files(res.output)
428 self.assertNotIn("etc", files)
429 self.assertNotIn("usr", files)
430 self.assertIn("share", files)
431
432 # Partition 3, should contain the same as partition 2, including the bin
433 # directory, but not the files inside it.
434 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
435 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500436 files = extract_files(res.output)
437 self.assertNotIn("etc", files)
438 self.assertNotIn("usr", files)
439 self.assertIn("share", files)
440 self.assertIn("bin", files)
441 res = runCmd("debugfs -R 'ls -p bin' %s 2>/dev/null" % \
442 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500443 files = extract_files(res.output)
444 self.assertIn(".", files)
445 self.assertIn("..", files)
446 self.assertEqual(2, len(files))
447
448 for part in [1, 2, 3]:
449 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
450 os.remove(part_file)
451
452 finally:
453 os.environ['PATH'] = oldpath
454
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500455 def test_exclude_path_errors(self):
456 """Test --exclude-path wks option error handling."""
457 wks_file = 'temp.wks'
458
459 # Absolute argument.
460 with open(wks_file, 'w') as wks:
461 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path /usr")
462 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
463 % (wks_file, self.resultdir), ignore_status=True).status)
464 os.remove(wks_file)
465
466 # Argument pointing to parent directory.
467 with open(wks_file, 'w') as wks:
468 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path ././..")
469 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
470 % (wks_file, self.resultdir), ignore_status=True).status)
471 os.remove(wks_file)
472
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800473class Wic2(WicTestCase):
474
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500475 def test_bmap_short(self):
476 """Test generation of .bmap file -m option"""
477 cmd = "wic create wictestdisk -e core-image-minimal -m -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800478 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500479 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
480 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap")))
481
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500482 def test_bmap_long(self):
483 """Test generation of .bmap file --bmap option"""
484 cmd = "wic create wictestdisk -e core-image-minimal --bmap -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800485 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500486 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
487 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap")))
488
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500489 def test_image_env(self):
490 """Test generation of <image>.env files."""
491 image = 'core-image-minimal'
492 imgdatadir = self._get_image_env_path(image)
493
494 bb_vars = get_bb_vars(['IMAGE_BASENAME', 'WICVARS'], image)
495 basename = bb_vars['IMAGE_BASENAME']
496 self.assertEqual(basename, image)
497 path = os.path.join(imgdatadir, basename) + '.env'
498 self.assertTrue(os.path.isfile(path))
499
500 wicvars = set(bb_vars['WICVARS'].split())
501 # filter out optional variables
502 wicvars = wicvars.difference(('DEPLOY_DIR_IMAGE', 'IMAGE_BOOT_FILES',
503 'INITRD', 'INITRD_LIVE', 'ISODIR'))
504 with open(path) as envfile:
505 content = dict(line.split("=", 1) for line in envfile)
506 # test if variables used by wic present in the .env file
507 for var in wicvars:
508 self.assertTrue(var in content, "%s is not in .env file" % var)
509 self.assertTrue(content[var])
510
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500511 def test_image_vars_dir_short(self):
512 """Test image vars directory selection -v option"""
513 image = 'core-image-minimal'
514 imgenvdir = self._get_image_env_path(image)
515 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
516
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800517 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500518 "--image-name=%s -v %s -n %s -o %s"
519 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800520 self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500521 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
522
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500523 def test_image_vars_dir_long(self):
524 """Test image vars directory selection --vars option"""
525 image = 'core-image-minimal'
526 imgenvdir = self._get_image_env_path(image)
527 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
528
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800529 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500530 "--image-name=%s "
531 "--vars %s "
532 "--native-sysroot %s "
533 "--outdir %s"
534 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800535 self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500536 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
537
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500538 @only_for_arch(['i586', 'i686', 'x86_64'])
539 def test_wic_image_type(self):
540 """Test building wic images by bitbake"""
541 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
542 'MACHINE_FEATURES_append = " efi"\n'
543 self.append_config(config)
544 self.assertEqual(0, bitbake('wic-image-minimal').status)
545 self.remove_config(config)
546
547 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
548 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
549 machine = bb_vars['MACHINE']
550 prefix = os.path.join(deploy_dir, 'wic-image-minimal-%s.' % machine)
551 # check if we have result image and manifests symlinks
552 # pointing to existing files
553 for suffix in ('wic', 'manifest'):
554 path = prefix + suffix
555 self.assertTrue(os.path.islink(path))
556 self.assertTrue(os.path.isfile(os.path.realpath(path)))
557
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500558 @only_for_arch(['i586', 'i686', 'x86_64'])
559 def test_qemu(self):
560 """Test wic-image-minimal under qemu"""
561 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
562 'MACHINE_FEATURES_append = " efi"\n'
563 self.append_config(config)
564 self.assertEqual(0, bitbake('wic-image-minimal').status)
565 self.remove_config(config)
566
567 with runqemu('wic-image-minimal', ssh=False) as qemu:
Andrew Geissler99467da2019-02-25 18:54:23 -0600568 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' " \
569 "-e '/dev/root /|/dev/sda2 /' -e '/dev/sda3 /media' -e '/dev/sda4 /mnt'"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400570 status, output = qemu.run_serial(cmd)
Andrew Geissler99467da2019-02-25 18:54:23 -0600571 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
572 self.assertEqual(output, '4')
Brad Bishop316dfdd2018-06-25 12:45:53 -0400573 cmd = "grep UUID= /etc/fstab"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500574 status, output = qemu.run_serial(cmd)
575 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400576 self.assertEqual(output, 'UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba\t/media\text4\tdefaults\t0\t0')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500577
578 @only_for_arch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500579 def test_qemu_efi(self):
580 """Test core-image-minimal efi image under qemu"""
581 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "mkefidisk.wks"\n'
582 self.append_config(config)
583 self.assertEqual(0, bitbake('core-image-minimal ovmf').status)
584 self.remove_config(config)
585
586 with runqemu('core-image-minimal', ssh=False,
587 runqemuparams='ovmf', image_fstype='wic') as qemu:
588 cmd = "grep sda. /proc/partitions |wc -l"
589 status, output = qemu.run_serial(cmd)
590 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
591 self.assertEqual(output, '3')
592
593 @staticmethod
594 def _make_fixed_size_wks(size):
595 """
596 Create a wks of an image with a single partition. Size of the partition is set
597 using --fixed-size flag. Returns a tuple: (path to wks file, wks image name)
598 """
599 with NamedTemporaryFile("w", suffix=".wks", delete=False) as tempf:
600 wkspath = tempf.name
601 tempf.write("part " \
602 "--source rootfs --ondisk hda --align 4 --fixed-size %d "
603 "--fstype=ext4\n" % size)
604 wksname = os.path.splitext(os.path.basename(wkspath))[0]
605
606 return wkspath, wksname
607
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500608 def test_fixed_size(self):
609 """
610 Test creation of a simple image with partition size controlled through
611 --fixed-size flag
612 """
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800613 wkspath, wksname = Wic2._make_fixed_size_wks(200)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500614
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800615 runCmd("wic create %s -e core-image-minimal -o %s" \
616 % (wkspath, self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500617 os.remove(wkspath)
618 wicout = glob(self.resultdir + "%s-*direct" % wksname)
619 self.assertEqual(1, len(wicout))
620
621 wicimg = wicout[0]
622
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800623 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
624
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500625 # verify partition size with wic
626 res = runCmd("parted -m %s unit mib p 2>/dev/null" % wicimg,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800627 native_sysroot=native_sysroot)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500628
629 # parse parted output which looks like this:
630 # BYT;\n
631 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
632 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
633 partlns = res.output.splitlines()[2:]
634
635 self.assertEqual(1, len(partlns))
636 self.assertEqual("1:0.00MiB:200MiB:200MiB:ext4::;", partlns[0])
637
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500638 def test_fixed_size_error(self):
639 """
640 Test creation of a simple image with partition size controlled through
641 --fixed-size flag. The size of partition is intentionally set to 1MiB
642 in order to trigger an error in wic.
643 """
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800644 wkspath, wksname = Wic2._make_fixed_size_wks(1)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500645
646 self.assertEqual(1, runCmd("wic create %s -e core-image-minimal -o %s" \
647 % (wkspath, self.resultdir), ignore_status=True).status)
648 os.remove(wkspath)
649 wicout = glob(self.resultdir + "%s-*direct" % wksname)
650 self.assertEqual(0, len(wicout))
651
652 @only_for_arch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500653 def test_rawcopy_plugin_qemu(self):
654 """Test rawcopy plugin in qemu"""
655 # build ext4 and wic images
656 for fstype in ("ext4", "wic"):
657 config = 'IMAGE_FSTYPES = "%s"\nWKS_FILE = "test_rawcopy_plugin.wks.in"\n' % fstype
658 self.append_config(config)
659 self.assertEqual(0, bitbake('core-image-minimal').status)
660 self.remove_config(config)
661
662 with runqemu('core-image-minimal', ssh=False, image_fstype='wic') as qemu:
663 cmd = "grep sda. /proc/partitions |wc -l"
664 status, output = qemu.run_serial(cmd)
665 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
666 self.assertEqual(output, '2')
667
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500668 def test_rawcopy_plugin(self):
669 """Test rawcopy plugin"""
670 img = 'core-image-minimal'
671 machine = get_bb_var('MACHINE', img)
672 with NamedTemporaryFile("w", suffix=".wks") as wks:
673 wks.writelines(['part /boot --active --source bootimg-pcbios\n',
674 'part / --source rawcopy --sourceparams="file=%s-%s.ext4" --use-uuid\n'\
675 % (img, machine),
676 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
677 wks.flush()
678 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800679 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500680 wksname = os.path.splitext(os.path.basename(wks.name))[0]
681 out = glob(self.resultdir + "%s-*direct" % wksname)
682 self.assertEqual(1, len(out))
683
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500684 def test_fs_types(self):
685 """Test filesystem types for empty and not empty partitions"""
686 img = 'core-image-minimal'
687 with NamedTemporaryFile("w", suffix=".wks") as wks:
688 wks.writelines(['part ext2 --fstype ext2 --source rootfs\n',
689 'part btrfs --fstype btrfs --source rootfs --size 40M\n',
690 'part squash --fstype squashfs --source rootfs\n',
691 'part swap --fstype swap --size 1M\n',
692 'part emptyvfat --fstype vfat --size 1M\n',
693 'part emptymsdos --fstype msdos --size 1M\n',
694 'part emptyext2 --fstype ext2 --size 1M\n',
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800695 'part emptybtrfs --fstype btrfs --size 150M\n'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500696 wks.flush()
697 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800698 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500699 wksname = os.path.splitext(os.path.basename(wks.name))[0]
700 out = glob(self.resultdir + "%s-*direct" % wksname)
701 self.assertEqual(1, len(out))
702
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500703 def test_kickstart_parser(self):
704 """Test wks parser options"""
705 with NamedTemporaryFile("w", suffix=".wks") as wks:
706 wks.writelines(['part / --fstype ext3 --source rootfs --system-id 0xFF '\
707 '--overhead-factor 1.2 --size 100k\n'])
708 wks.flush()
709 cmd = "wic create %s -e core-image-minimal -o %s" % (wks.name, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800710 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500711 wksname = os.path.splitext(os.path.basename(wks.name))[0]
712 out = glob(self.resultdir + "%s-*direct" % wksname)
713 self.assertEqual(1, len(out))
714
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500715 def test_image_bootpart_globbed(self):
716 """Test globbed sources with image-bootpart plugin"""
717 img = "core-image-minimal"
718 cmd = "wic create sdimage-bootpart -e %s -o %s" % (img, self.resultdir)
719 config = 'IMAGE_BOOT_FILES = "%s*"' % get_bb_var('KERNEL_IMAGETYPE', img)
720 self.append_config(config)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800721 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500722 self.remove_config(config)
723 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct")))
724
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500725 def test_sparse_copy(self):
726 """Test sparse_copy with FIEMAP and SEEK_HOLE filemap APIs"""
727 libpath = os.path.join(get_bb_var('COREBASE'), 'scripts', 'lib', 'wic')
728 sys.path.insert(0, libpath)
729 from filemap import FilemapFiemap, FilemapSeek, sparse_copy, ErrorNotSupp
730 with NamedTemporaryFile("w", suffix=".wic-sparse") as sparse:
731 src_name = sparse.name
732 src_size = 1024 * 10
733 sparse.truncate(src_size)
734 # write one byte to the file
735 with open(src_name, 'r+b') as sfile:
736 sfile.seek(1024 * 4)
737 sfile.write(b'\x00')
738 dest = sparse.name + '.out'
739 # copy src file to dest using different filemap APIs
740 for api in (FilemapFiemap, FilemapSeek, None):
741 if os.path.exists(dest):
742 os.unlink(dest)
743 try:
744 sparse_copy(sparse.name, dest, api=api)
745 except ErrorNotSupp:
746 continue # skip unsupported API
747 dest_stat = os.stat(dest)
748 self.assertEqual(dest_stat.st_size, src_size)
749 # 8 blocks is 4K (physical sector size)
750 self.assertEqual(dest_stat.st_blocks, 8)
751 os.unlink(dest)
752
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500753 def test_wic_ls(self):
754 """Test listing image content using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800755 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500756 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800757 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500758 images = glob(self.resultdir + "wictestdisk-*.direct")
759 self.assertEqual(1, len(images))
760
761 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
762
763 # list partitions
764 result = runCmd("wic ls %s -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500765 self.assertEqual(3, len(result.output.split('\n')))
766
767 # list directory content of the first partition
768 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500769 self.assertEqual(6, len(result.output.split('\n')))
770
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500771 def test_wic_cp(self):
772 """Test copy files and directories to the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800773 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500774 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800775 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500776 images = glob(self.resultdir + "wictestdisk-*.direct")
777 self.assertEqual(1, len(images))
778
779 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
780
781 # list directory content of the first partition
782 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500783 self.assertEqual(6, len(result.output.split('\n')))
784
785 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
786 testfile.write("test")
787
788 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800789 runCmd("wic cp %s %s:1/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500790
791 # check if file is there
792 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500793 self.assertEqual(7, len(result.output.split('\n')))
794 self.assertTrue(os.path.basename(testfile.name) in result.output)
795
796 # prepare directory
797 testdir = os.path.join(self.resultdir, 'wic-test-cp-dir')
798 testsubdir = os.path.join(testdir, 'subdir')
799 os.makedirs(os.path.join(testsubdir))
800 copy(testfile.name, testdir)
801
802 # copy directory to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800803 runCmd("wic cp %s %s:1/ -n %s" % (testdir, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500804
805 # check if directory is there
806 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500807 self.assertEqual(8, len(result.output.split('\n')))
808 self.assertTrue(os.path.basename(testdir) in result.output)
809
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500810 def test_wic_rm(self):
811 """Test removing files and directories from the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800812 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500813 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800814 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500815 images = glob(self.resultdir + "mkefidisk-*.direct")
816 self.assertEqual(1, len(images))
817
818 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
819
820 # list directory content of the first partition
821 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500822 self.assertIn('\nBZIMAGE ', result.output)
823 self.assertIn('\nEFI <DIR> ', result.output)
824
825 # remove file
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800826 runCmd("wic rm %s:1/bzimage -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500827
828 # remove directory
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800829 runCmd("wic rm %s:1/efi -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500830
831 # check if they're removed
832 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500833 self.assertNotIn('\nBZIMAGE ', result.output)
834 self.assertNotIn('\nEFI <DIR> ', result.output)
835
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500836 def test_mkfs_extraopts(self):
837 """Test wks option --mkfs-extraopts for empty and not empty partitions"""
838 img = 'core-image-minimal'
839 with NamedTemporaryFile("w", suffix=".wks") as wks:
840 wks.writelines(
841 ['part ext2 --fstype ext2 --source rootfs --mkfs-extraopts "-D -F -i 8192"\n',
842 "part btrfs --fstype btrfs --source rootfs --size 40M --mkfs-extraopts='--quiet'\n",
843 'part squash --fstype squashfs --source rootfs --mkfs-extraopts "-no-sparse -b 4096"\n',
844 'part emptyvfat --fstype vfat --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
845 'part emptymsdos --fstype msdos --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
846 'part emptyext2 --fstype ext2 --size 1M --mkfs-extraopts "-D -F -i 8192"\n',
847 'part emptybtrfs --fstype btrfs --size 100M --mkfs-extraopts "--mixed -K"\n'])
848 wks.flush()
849 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800850 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500851 wksname = os.path.splitext(os.path.basename(wks.name))[0]
852 out = glob(self.resultdir + "%s-*direct" % wksname)
853 self.assertEqual(1, len(out))
854
855 def test_expand_mbr_image(self):
856 """Test wic write --expand command for mbr image"""
857 # build an image
858 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n'
859 self.append_config(config)
860 self.assertEqual(0, bitbake('core-image-minimal').status)
861
862 # get path to the image
863 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
864 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
865 machine = bb_vars['MACHINE']
866 image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine)
867
868 self.remove_config(config)
869
870 try:
871 # expand image to 1G
872 new_image_path = None
873 with NamedTemporaryFile(mode='wb', suffix='.wic.exp',
874 dir=deploy_dir, delete=False) as sparse:
875 sparse.truncate(1024 ** 3)
876 new_image_path = sparse.name
877
878 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
879 cmd = "wic write -n %s --expand 1:0 %s %s" % (sysroot, image_path, new_image_path)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800880 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500881
882 # check if partitions are expanded
883 orig = runCmd("wic ls %s -n %s" % (image_path, sysroot))
884 exp = runCmd("wic ls %s -n %s" % (new_image_path, sysroot))
885 orig_sizes = [int(line.split()[3]) for line in orig.output.split('\n')[1:]]
886 exp_sizes = [int(line.split()[3]) for line in exp.output.split('\n')[1:]]
887 self.assertEqual(orig_sizes[0], exp_sizes[0]) # first partition is not resized
888 self.assertTrue(orig_sizes[1] < exp_sizes[1])
889
890 # Check if all free space is partitioned
891 result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path))
892 self.assertTrue("0 B, 0 bytes, 0 sectors" in result.output)
893
894 os.rename(image_path, image_path + '.bak')
895 os.rename(new_image_path, image_path)
896
897 # Check if it boots in qemu
898 with runqemu('core-image-minimal', ssh=False) as qemu:
899 cmd = "ls /etc/"
900 status, output = qemu.run_serial('true')
901 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
902 finally:
903 if os.path.exists(new_image_path):
904 os.unlink(new_image_path)
905 if os.path.exists(image_path + '.bak'):
906 os.rename(image_path + '.bak', image_path)
907
908 def test_wic_ls_ext(self):
909 """Test listing content of the ext partition using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800910 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500911 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800912 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500913 images = glob(self.resultdir + "wictestdisk-*.direct")
914 self.assertEqual(1, len(images))
915
916 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
917
918 # list directory content of the second ext4 partition
919 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500920 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(
921 set(line.split()[-1] for line in result.output.split('\n') if line)))
922
923 def test_wic_cp_ext(self):
924 """Test copy files and directories to the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800925 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500926 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800927 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500928 images = glob(self.resultdir + "wictestdisk-*.direct")
929 self.assertEqual(1, len(images))
930
931 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
932
933 # list directory content of the ext4 partition
934 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500935 dirs = set(line.split()[-1] for line in result.output.split('\n') if line)
936 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(dirs))
937
938 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
939 testfile.write("test")
940
941 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800942 runCmd("wic cp %s %s:2/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500943
944 # check if file is there
945 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500946 newdirs = set(line.split()[-1] for line in result.output.split('\n') if line)
947 self.assertEqual(newdirs.difference(dirs), set([os.path.basename(testfile.name)]))
948
949 def test_wic_rm_ext(self):
950 """Test removing files from the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800951 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500952 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800953 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500954 images = glob(self.resultdir + "mkefidisk-*.direct")
955 self.assertEqual(1, len(images))
956
957 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
958
959 # list directory content of the /etc directory on ext4 partition
960 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500961 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line])
962
963 # remove file
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800964 runCmd("wic rm %s:2/etc/fstab -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500965
966 # check if it's removed
967 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500968 self.assertTrue('fstab' not in [line.split()[-1] for line in result.output.split('\n') if line])