1#!/usr/bin/env python 2# ex:ts=4:sw=4:sts=4:et 3# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- 4# 5# Copyright (c) 2015, Intel Corporation. 6# All rights reserved. 7# 8# This program is free software; you can redistribute it and/or modify 9# it under the terms of the GNU General Public License version 2 as 10# published by the Free Software Foundation. 11# 12# This program is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License along 18# with this program; if not, write to the Free Software Foundation, Inc., 19# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20# 21# AUTHORS 22# Ed Bartosh <ed.bartosh@linux.intel.com> 23 24"""Test cases for wic.""" 25 26import os 27import sys 28import unittest 29 30from glob import glob 31from shutil import rmtree, copy 32from functools import wraps, lru_cache 33from tempfile import NamedTemporaryFile 34 35from oeqa.selftest.case import OESelftestTestCase 36from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, runqemu 37from oeqa.core.decorator.oeid import OETestID 38 39 40@lru_cache(maxsize=32) 41def get_host_arch(recipe): 42 """A cached call to get_bb_var('HOST_ARCH', <recipe>)""" 43 return get_bb_var('HOST_ARCH', recipe) 44 45 46def only_for_arch(archs, image='core-image-minimal'): 47 """Decorator for wrapping test cases that can be run only for specific target 48 architectures. A list of compatible architectures is passed in `archs`. 49 Current architecture will be determined by parsing bitbake output for 50 `image` recipe. 51 """ 52 def wrapper(func): 53 @wraps(func) 54 def wrapped_f(*args, **kwargs): 55 arch = get_host_arch(image) 56 if archs and arch not in archs: 57 raise unittest.SkipTest("Testcase arch dependency not met: %s" % arch) 58 return func(*args, **kwargs) 59 wrapped_f.__name__ = func.__name__ 60 return wrapped_f 61 return wrapper 62 63 64class WicTestCase(OESelftestTestCase): 65 """Wic test class.""" 66 67 image_is_ready = False 68 wicenv_cache = {} 69 70 def setUpLocal(self): 71 """This code is executed before each test method.""" 72 self.resultdir = self.builddir + "/wic-tmp/" 73 super(WicTestCase, self).setUpLocal() 74 75 # Do this here instead of in setUpClass as the base setUp does some 76 # clean up which can result in the native tools built earlier in 77 # setUpClass being unavailable. 78 if not WicTestCase.image_is_ready: 79 if get_bb_var('USE_NLS') == 'yes': 80 bitbake('wic-tools') 81 else: 82 self.skipTest('wic-tools cannot be built due its (intltool|gettext)-native dependency and NLS disable') 83 84 bitbake('core-image-minimal') 85 WicTestCase.image_is_ready = True 86 87 rmtree(self.resultdir, ignore_errors=True) 88 89 def tearDownLocal(self): 90 """Remove resultdir as it may contain images.""" 91 rmtree(self.resultdir, ignore_errors=True) 92 super(WicTestCase, self).tearDownLocal() 93 94 def _get_image_env_path(self, image): 95 """Generate and obtain the path to <image>.env""" 96 if image not in WicTestCase.wicenv_cache: 97 self.assertEqual(0, bitbake('%s -c do_rootfs_wicenv' % image).status) 98 bb_vars = get_bb_vars(['STAGING_DIR', 'MACHINE'], image) 99 stdir = bb_vars['STAGING_DIR'] 100 machine = bb_vars['MACHINE'] 101 WicTestCase.wicenv_cache[image] = os.path.join(stdir, machine, 'imgdata') 102 return WicTestCase.wicenv_cache[image] 103 104class Wic(WicTestCase): 105 106 @OETestID(1552) 107 def test_version(self): 108 """Test wic --version""" 109 runCmd('wic --version') 110 111 @OETestID(1208) 112 def test_help(self): 113 """Test wic --help and wic -h""" 114 runCmd('wic --help') 115 runCmd('wic -h') 116 117 @OETestID(1209) 118 def test_createhelp(self): 119 """Test wic create --help""" 120 runCmd('wic create --help') 121 122 @OETestID(1210) 123 def test_listhelp(self): 124 """Test wic list --help""" 125 runCmd('wic list --help') 126 127 @OETestID(1553) 128 def test_help_create(self): 129 """Test wic help create""" 130 runCmd('wic help create') 131 132 @OETestID(1554) 133 def test_help_list(self): 134 """Test wic help list""" 135 runCmd('wic help list') 136 137 @OETestID(1215) 138 def test_help_overview(self): 139 """Test wic help overview""" 140 runCmd('wic help overview') 141 142 @OETestID(1216) 143 def test_help_plugins(self): 144 """Test wic help plugins""" 145 runCmd('wic help plugins') 146 147 @OETestID(1217) 148 def test_help_kickstart(self): 149 """Test wic help kickstart""" 150 runCmd('wic help kickstart') 151 152 @OETestID(1555) 153 def test_list_images(self): 154 """Test wic list images""" 155 runCmd('wic list images') 156 157 @OETestID(1556) 158 def test_list_source_plugins(self): 159 """Test wic list source-plugins""" 160 runCmd('wic list source-plugins') 161 162 @OETestID(1557) 163 def test_listed_images_help(self): 164 """Test wic listed images help""" 165 output = runCmd('wic list images').output 166 imagelist = [line.split()[0] for line in output.splitlines()] 167 for image in imagelist: 168 runCmd('wic list %s help' % image) 169 170 @OETestID(1213) 171 def test_unsupported_subcommand(self): 172 """Test unsupported subcommand""" 173 self.assertNotEqual(0, runCmd('wic unsupported', ignore_status=True).status) 174 175 @OETestID(1214) 176 def test_no_command(self): 177 """Test wic without command""" 178 self.assertEqual(1, runCmd('wic', ignore_status=True).status) 179 180 @OETestID(1211) 181 def test_build_image_name(self): 182 """Test wic create wictestdisk --image-name=core-image-minimal""" 183 cmd = "wic create wictestdisk --image-name=core-image-minimal -o %s" % self.resultdir 184 runCmd(cmd) 185 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 186 187 @OETestID(1157) 188 @only_for_arch(['i586', 'i686', 'x86_64']) 189 def test_gpt_image(self): 190 """Test creation of core-image-minimal with gpt table and UUID boot""" 191 cmd = "wic create directdisk-gpt --image-name core-image-minimal -o %s" % self.resultdir 192 runCmd(cmd) 193 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct"))) 194 195 @OETestID(1346) 196 @only_for_arch(['i586', 'i686', 'x86_64']) 197 def test_iso_image(self): 198 """Test creation of hybrid iso image with legacy and EFI boot""" 199 config = 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\ 200 'MACHINE_FEATURES_append = " efi"\n'\ 201 'DEPENDS_pn-core-image-minimal += "syslinux"\n' 202 self.append_config(config) 203 bitbake('core-image-minimal core-image-minimal-initramfs') 204 self.remove_config(config) 205 cmd = "wic create mkhybridiso --image-name core-image-minimal -o %s" % self.resultdir 206 runCmd(cmd) 207 self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.direct"))) 208 self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.iso"))) 209 210 @OETestID(1348) 211 @only_for_arch(['i586', 'i686', 'x86_64']) 212 def test_qemux86_directdisk(self): 213 """Test creation of qemux-86-directdisk image""" 214 cmd = "wic create qemux86-directdisk -e core-image-minimal -o %s" % self.resultdir 215 runCmd(cmd) 216 self.assertEqual(1, len(glob(self.resultdir + "qemux86-directdisk-*direct"))) 217 218 @OETestID(1350) 219 @only_for_arch(['i586', 'i686', 'x86_64']) 220 def test_mkefidisk(self): 221 """Test creation of mkefidisk image""" 222 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir 223 runCmd(cmd) 224 self.assertEqual(1, len(glob(self.resultdir + "mkefidisk-*direct"))) 225 226 @OETestID(1385) 227 @only_for_arch(['i586', 'i686', 'x86_64']) 228 def test_bootloader_config(self): 229 """Test creation of directdisk-bootloader-config image""" 230 config = 'DEPENDS_pn-core-image-minimal += "syslinux"\n' 231 self.append_config(config) 232 bitbake('core-image-minimal') 233 self.remove_config(config) 234 cmd = "wic create directdisk-bootloader-config -e core-image-minimal -o %s" % self.resultdir 235 runCmd(cmd) 236 self.assertEqual(1, len(glob(self.resultdir + "directdisk-bootloader-config-*direct"))) 237 238 @OETestID(1560) 239 @only_for_arch(['i586', 'i686', 'x86_64']) 240 def test_systemd_bootdisk(self): 241 """Test creation of systemd-bootdisk image""" 242 config = 'MACHINE_FEATURES_append = " efi"\n' 243 self.append_config(config) 244 bitbake('core-image-minimal') 245 self.remove_config(config) 246 cmd = "wic create systemd-bootdisk -e core-image-minimal -o %s" % self.resultdir 247 runCmd(cmd) 248 self.assertEqual(1, len(glob(self.resultdir + "systemd-bootdisk-*direct"))) 249 250 @OETestID(1561) 251 def test_sdimage_bootpart(self): 252 """Test creation of sdimage-bootpart image""" 253 cmd = "wic create sdimage-bootpart -e core-image-minimal -o %s" % self.resultdir 254 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal') 255 self.write_config('IMAGE_BOOT_FILES = "%s"\n' % kimgtype) 256 runCmd(cmd) 257 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct"))) 258 259 @OETestID(1562) 260 @only_for_arch(['i586', 'i686', 'x86_64']) 261 def test_default_output_dir(self): 262 """Test default output location""" 263 for fname in glob("directdisk-*.direct"): 264 os.remove(fname) 265 config = 'DEPENDS_pn-core-image-minimal += "syslinux"\n' 266 self.append_config(config) 267 bitbake('core-image-minimal') 268 self.remove_config(config) 269 cmd = "wic create directdisk -e core-image-minimal" 270 runCmd(cmd) 271 self.assertEqual(1, len(glob("directdisk-*.direct"))) 272 273 @OETestID(1212) 274 @only_for_arch(['i586', 'i686', 'x86_64']) 275 def test_build_artifacts(self): 276 """Test wic create directdisk providing all artifacts.""" 277 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'], 278 'wic-tools') 279 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'], 280 'core-image-minimal')) 281 bbvars = {key.lower(): value for key, value in bb_vars.items()} 282 bbvars['resultdir'] = self.resultdir 283 runCmd("wic create directdisk " 284 "-b %(staging_datadir)s " 285 "-k %(deploy_dir_image)s " 286 "-n %(recipe_sysroot_native)s " 287 "-r %(image_rootfs)s " 288 "-o %(resultdir)s" % bbvars) 289 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct"))) 290 291 @OETestID(1264) 292 def test_compress_gzip(self): 293 """Test compressing an image with gzip""" 294 runCmd("wic create wictestdisk " 295 "--image-name core-image-minimal " 296 "-c gzip -o %s" % self.resultdir) 297 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.gz"))) 298 299 @OETestID(1265) 300 def test_compress_bzip2(self): 301 """Test compressing an image with bzip2""" 302 runCmd("wic create wictestdisk " 303 "--image-name=core-image-minimal " 304 "-c bzip2 -o %s" % self.resultdir) 305 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.bz2"))) 306 307 @OETestID(1266) 308 def test_compress_xz(self): 309 """Test compressing an image with xz""" 310 runCmd("wic create wictestdisk " 311 "--image-name=core-image-minimal " 312 "--compress-with=xz -o %s" % self.resultdir) 313 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.xz"))) 314 315 @OETestID(1267) 316 def test_wrong_compressor(self): 317 """Test how wic breaks if wrong compressor is provided""" 318 self.assertEqual(2, runCmd("wic create wictestdisk " 319 "--image-name=core-image-minimal " 320 "-c wrong -o %s" % self.resultdir, 321 ignore_status=True).status) 322 323 @OETestID(1558) 324 def test_debug_short(self): 325 """Test -D option""" 326 runCmd("wic create wictestdisk " 327 "--image-name=core-image-minimal " 328 "-D -o %s" % self.resultdir) 329 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 330 331 @OETestID(1658) 332 def test_debug_long(self): 333 """Test --debug option""" 334 runCmd("wic create wictestdisk " 335 "--image-name=core-image-minimal " 336 "--debug -o %s" % self.resultdir) 337 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 338 339 @OETestID(1563) 340 def test_skip_build_check_short(self): 341 """Test -s option""" 342 runCmd("wic create wictestdisk " 343 "--image-name=core-image-minimal " 344 "-s -o %s" % self.resultdir) 345 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 346 347 @OETestID(1671) 348 def test_skip_build_check_long(self): 349 """Test --skip-build-check option""" 350 runCmd("wic create wictestdisk " 351 "--image-name=core-image-minimal " 352 "--skip-build-check " 353 "--outdir %s" % self.resultdir) 354 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 355 356 @OETestID(1564) 357 def test_build_rootfs_short(self): 358 """Test -f option""" 359 runCmd("wic create wictestdisk " 360 "--image-name=core-image-minimal " 361 "-f -o %s" % self.resultdir) 362 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 363 364 @OETestID(1656) 365 def test_build_rootfs_long(self): 366 """Test --build-rootfs option""" 367 runCmd("wic create wictestdisk " 368 "--image-name=core-image-minimal " 369 "--build-rootfs " 370 "--outdir %s" % self.resultdir) 371 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 372 373 @OETestID(1268) 374 @only_for_arch(['i586', 'i686', 'x86_64']) 375 def test_rootfs_indirect_recipes(self): 376 """Test usage of rootfs plugin with rootfs recipes""" 377 runCmd("wic create directdisk-multi-rootfs " 378 "--image-name=core-image-minimal " 379 "--rootfs rootfs1=core-image-minimal " 380 "--rootfs rootfs2=core-image-minimal " 381 "--outdir %s" % self.resultdir) 382 self.assertEqual(1, len(glob(self.resultdir + "directdisk-multi-rootfs*.direct"))) 383 384 @OETestID(1269) 385 @only_for_arch(['i586', 'i686', 'x86_64']) 386 def test_rootfs_artifacts(self): 387 """Test usage of rootfs plugin with rootfs paths""" 388 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'], 389 'wic-tools') 390 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'], 391 'core-image-minimal')) 392 bbvars = {key.lower(): value for key, value in bb_vars.items()} 393 bbvars['wks'] = "directdisk-multi-rootfs" 394 bbvars['resultdir'] = self.resultdir 395 runCmd("wic create %(wks)s " 396 "--bootimg-dir=%(staging_datadir)s " 397 "--kernel-dir=%(deploy_dir_image)s " 398 "--native-sysroot=%(recipe_sysroot_native)s " 399 "--rootfs-dir rootfs1=%(image_rootfs)s " 400 "--rootfs-dir rootfs2=%(image_rootfs)s " 401 "--outdir %(resultdir)s" % bbvars) 402 self.assertEqual(1, len(glob(self.resultdir + "%(wks)s-*.direct" % bbvars))) 403 404 @OETestID(1661) 405 def test_exclude_path(self): 406 """Test --exclude-path wks option.""" 407 408 oldpath = os.environ['PATH'] 409 os.environ['PATH'] = get_bb_var("PATH", "wic-tools") 410 411 try: 412 wks_file = 'temp.wks' 413 with open(wks_file, 'w') as wks: 414 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal') 415 wks.write(""" 416part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path usr 417part /usr --source rootfs --ondisk mmcblk0 --fstype=ext4 --rootfs-dir %s/usr 418part /etc --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path bin/ --rootfs-dir %s/usr""" 419 % (rootfs_dir, rootfs_dir)) 420 runCmd("wic create %s -e core-image-minimal -o %s" \ 421 % (wks_file, self.resultdir)) 422 423 os.remove(wks_file) 424 wicout = glob(self.resultdir + "%s-*direct" % 'temp') 425 self.assertEqual(1, len(wicout)) 426 427 wicimg = wicout[0] 428 429 # verify partition size with wic 430 res = runCmd("parted -m %s unit b p 2>/dev/null" % wicimg) 431 432 # parse parted output which looks like this: 433 # BYT;\n 434 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n 435 # 1:0.00MiB:200MiB:200MiB:ext4::;\n 436 partlns = res.output.splitlines()[2:] 437 438 self.assertEqual(3, len(partlns)) 439 440 for part in [1, 2, 3]: 441 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part) 442 partln = partlns[part-1].split(":") 443 self.assertEqual(7, len(partln)) 444 start = int(partln[1].rstrip("B")) / 512 445 length = int(partln[3].rstrip("B")) / 512 446 runCmd("dd if=%s of=%s skip=%d count=%d" % 447 (wicimg, part_file, start, length)) 448 449 def extract_files(debugfs_output): 450 """ 451 extract file names from the output of debugfs -R 'ls -p', 452 which looks like this: 453 454 /2/040755/0/0/.//\n 455 /2/040755/0/0/..//\n 456 /11/040700/0/0/lost+found^M//\n 457 /12/040755/1002/1002/run//\n 458 /13/040755/1002/1002/sys//\n 459 /14/040755/1002/1002/bin//\n 460 /80/040755/1002/1002/var//\n 461 /92/040755/1002/1002/tmp//\n 462 """ 463 # NOTE the occasional ^M in file names 464 return [line.split('/')[5].strip() for line in \ 465 debugfs_output.strip().split('/\n')] 466 467 # Test partition 1, should contain the normal root directories, except 468 # /usr. 469 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \ 470 os.path.join(self.resultdir, "selftest_img.part1")) 471 files = extract_files(res.output) 472 self.assertIn("etc", files) 473 self.assertNotIn("usr", files) 474 475 # Partition 2, should contain common directories for /usr, not root 476 # directories. 477 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \ 478 os.path.join(self.resultdir, "selftest_img.part2")) 479 files = extract_files(res.output) 480 self.assertNotIn("etc", files) 481 self.assertNotIn("usr", files) 482 self.assertIn("share", files) 483 484 # Partition 3, should contain the same as partition 2, including the bin 485 # directory, but not the files inside it. 486 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \ 487 os.path.join(self.resultdir, "selftest_img.part3")) 488 files = extract_files(res.output) 489 self.assertNotIn("etc", files) 490 self.assertNotIn("usr", files) 491 self.assertIn("share", files) 492 self.assertIn("bin", files) 493 res = runCmd("debugfs -R 'ls -p bin' %s 2>/dev/null" % \ 494 os.path.join(self.resultdir, "selftest_img.part3")) 495 files = extract_files(res.output) 496 self.assertIn(".", files) 497 self.assertIn("..", files) 498 self.assertEqual(2, len(files)) 499 500 for part in [1, 2, 3]: 501 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part) 502 os.remove(part_file) 503 504 finally: 505 os.environ['PATH'] = oldpath 506 507 @OETestID(1662) 508 def test_exclude_path_errors(self): 509 """Test --exclude-path wks option error handling.""" 510 wks_file = 'temp.wks' 511 512 # Absolute argument. 513 with open(wks_file, 'w') as wks: 514 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path /usr") 515 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 516 % (wks_file, self.resultdir), ignore_status=True).status) 517 os.remove(wks_file) 518 519 # Argument pointing to parent directory. 520 with open(wks_file, 'w') as wks: 521 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path ././..") 522 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 523 % (wks_file, self.resultdir), ignore_status=True).status) 524 os.remove(wks_file) 525 526class Wic2(WicTestCase): 527 528 @OETestID(1496) 529 def test_bmap_short(self): 530 """Test generation of .bmap file -m option""" 531 cmd = "wic create wictestdisk -e core-image-minimal -m -o %s" % self.resultdir 532 runCmd(cmd) 533 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct"))) 534 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap"))) 535 536 @OETestID(1655) 537 def test_bmap_long(self): 538 """Test generation of .bmap file --bmap option""" 539 cmd = "wic create wictestdisk -e core-image-minimal --bmap -o %s" % self.resultdir 540 runCmd(cmd) 541 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct"))) 542 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap"))) 543 544 @OETestID(1347) 545 def test_image_env(self): 546 """Test generation of <image>.env files.""" 547 image = 'core-image-minimal' 548 imgdatadir = self._get_image_env_path(image) 549 550 bb_vars = get_bb_vars(['IMAGE_BASENAME', 'WICVARS'], image) 551 basename = bb_vars['IMAGE_BASENAME'] 552 self.assertEqual(basename, image) 553 path = os.path.join(imgdatadir, basename) + '.env' 554 self.assertTrue(os.path.isfile(path)) 555 556 wicvars = set(bb_vars['WICVARS'].split()) 557 # filter out optional variables 558 wicvars = wicvars.difference(('DEPLOY_DIR_IMAGE', 'IMAGE_BOOT_FILES', 559 'INITRD', 'INITRD_LIVE', 'ISODIR')) 560 with open(path) as envfile: 561 content = dict(line.split("=", 1) for line in envfile) 562 # test if variables used by wic present in the .env file 563 for var in wicvars: 564 self.assertTrue(var in content, "%s is not in .env file" % var) 565 self.assertTrue(content[var]) 566 567 @OETestID(1559) 568 def test_image_vars_dir_short(self): 569 """Test image vars directory selection -v option""" 570 image = 'core-image-minimal' 571 imgenvdir = self._get_image_env_path(image) 572 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools") 573 574 runCmd("wic create wictestdisk " 575 "--image-name=%s -v %s -n %s -o %s" 576 % (image, imgenvdir, native_sysroot, 577 self.resultdir)) 578 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct"))) 579 580 @OETestID(1665) 581 def test_image_vars_dir_long(self): 582 """Test image vars directory selection --vars option""" 583 image = 'core-image-minimal' 584 imgenvdir = self._get_image_env_path(image) 585 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools") 586 587 runCmd("wic create wictestdisk " 588 "--image-name=%s " 589 "--vars %s " 590 "--native-sysroot %s " 591 "--outdir %s" 592 % (image, imgenvdir, native_sysroot, 593 self.resultdir)) 594 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct"))) 595 596 @OETestID(1351) 597 @only_for_arch(['i586', 'i686', 'x86_64']) 598 def test_wic_image_type(self): 599 """Test building wic images by bitbake""" 600 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\ 601 'MACHINE_FEATURES_append = " efi"\n' 602 self.append_config(config) 603 self.assertEqual(0, bitbake('wic-image-minimal').status) 604 self.remove_config(config) 605 606 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE']) 607 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE'] 608 machine = bb_vars['MACHINE'] 609 prefix = os.path.join(deploy_dir, 'wic-image-minimal-%s.' % machine) 610 # check if we have result image and manifests symlinks 611 # pointing to existing files 612 for suffix in ('wic', 'manifest'): 613 path = prefix + suffix 614 self.assertTrue(os.path.islink(path)) 615 self.assertTrue(os.path.isfile(os.path.realpath(path))) 616 617 @OETestID(1424) 618 @only_for_arch(['i586', 'i686', 'x86_64']) 619 def test_qemu(self): 620 """Test wic-image-minimal under qemu""" 621 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\ 622 'MACHINE_FEATURES_append = " efi"\n' 623 self.append_config(config) 624 self.assertEqual(0, bitbake('wic-image-minimal').status) 625 self.remove_config(config) 626 627 with runqemu('wic-image-minimal', ssh=False) as qemu: 628 cmd = "mount |grep '^/dev/' | cut -f1,3 -d ' ' | sort" 629 status, output = qemu.run_serial(cmd) 630 self.assertEqual(output, '/dev/root /\r\n/dev/sda1 /boot\r\n/dev/sda3 /media\r\n/dev/sda4 /mnt') 631 cmd = "grep UUID= /etc/fstab" 632 status, output = qemu.run_serial(cmd) 633 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 634 self.assertEqual(output, 'UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba\t/media\text4\tdefaults\t0\t0') 635 636 @only_for_arch(['i586', 'i686', 'x86_64']) 637 @OETestID(1852) 638 def test_qemu_efi(self): 639 """Test core-image-minimal efi image under qemu""" 640 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "mkefidisk.wks"\n' 641 self.append_config(config) 642 self.assertEqual(0, bitbake('core-image-minimal ovmf').status) 643 self.remove_config(config) 644 645 with runqemu('core-image-minimal', ssh=False, 646 runqemuparams='ovmf', image_fstype='wic') as qemu: 647 cmd = "grep sda. /proc/partitions |wc -l" 648 status, output = qemu.run_serial(cmd) 649 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 650 self.assertEqual(output, '3') 651 652 @staticmethod 653 def _make_fixed_size_wks(size): 654 """ 655 Create a wks of an image with a single partition. Size of the partition is set 656 using --fixed-size flag. Returns a tuple: (path to wks file, wks image name) 657 """ 658 with NamedTemporaryFile("w", suffix=".wks", delete=False) as tempf: 659 wkspath = tempf.name 660 tempf.write("part " \ 661 "--source rootfs --ondisk hda --align 4 --fixed-size %d " 662 "--fstype=ext4\n" % size) 663 wksname = os.path.splitext(os.path.basename(wkspath))[0] 664 665 return wkspath, wksname 666 667 @OETestID(1847) 668 def test_fixed_size(self): 669 """ 670 Test creation of a simple image with partition size controlled through 671 --fixed-size flag 672 """ 673 wkspath, wksname = Wic2._make_fixed_size_wks(200) 674 675 runCmd("wic create %s -e core-image-minimal -o %s" \ 676 % (wkspath, self.resultdir)) 677 os.remove(wkspath) 678 wicout = glob(self.resultdir + "%s-*direct" % wksname) 679 self.assertEqual(1, len(wicout)) 680 681 wicimg = wicout[0] 682 683 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools") 684 685 # verify partition size with wic 686 res = runCmd("parted -m %s unit mib p 2>/dev/null" % wicimg, 687 native_sysroot=native_sysroot) 688 689 # parse parted output which looks like this: 690 # BYT;\n 691 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n 692 # 1:0.00MiB:200MiB:200MiB:ext4::;\n 693 partlns = res.output.splitlines()[2:] 694 695 self.assertEqual(1, len(partlns)) 696 self.assertEqual("1:0.00MiB:200MiB:200MiB:ext4::;", partlns[0]) 697 698 @OETestID(1848) 699 def test_fixed_size_error(self): 700 """ 701 Test creation of a simple image with partition size controlled through 702 --fixed-size flag. The size of partition is intentionally set to 1MiB 703 in order to trigger an error in wic. 704 """ 705 wkspath, wksname = Wic2._make_fixed_size_wks(1) 706 707 self.assertEqual(1, runCmd("wic create %s -e core-image-minimal -o %s" \ 708 % (wkspath, self.resultdir), ignore_status=True).status) 709 os.remove(wkspath) 710 wicout = glob(self.resultdir + "%s-*direct" % wksname) 711 self.assertEqual(0, len(wicout)) 712 713 @only_for_arch(['i586', 'i686', 'x86_64']) 714 @OETestID(1854) 715 def test_rawcopy_plugin_qemu(self): 716 """Test rawcopy plugin in qemu""" 717 # build ext4 and wic images 718 for fstype in ("ext4", "wic"): 719 config = 'IMAGE_FSTYPES = "%s"\nWKS_FILE = "test_rawcopy_plugin.wks.in"\n' % fstype 720 self.append_config(config) 721 self.assertEqual(0, bitbake('core-image-minimal').status) 722 self.remove_config(config) 723 724 with runqemu('core-image-minimal', ssh=False, image_fstype='wic') as qemu: 725 cmd = "grep sda. /proc/partitions |wc -l" 726 status, output = qemu.run_serial(cmd) 727 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 728 self.assertEqual(output, '2') 729 730 @OETestID(1853) 731 def test_rawcopy_plugin(self): 732 """Test rawcopy plugin""" 733 img = 'core-image-minimal' 734 machine = get_bb_var('MACHINE', img) 735 with NamedTemporaryFile("w", suffix=".wks") as wks: 736 wks.writelines(['part /boot --active --source bootimg-pcbios\n', 737 'part / --source rawcopy --sourceparams="file=%s-%s.ext4" --use-uuid\n'\ 738 % (img, machine), 739 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n']) 740 wks.flush() 741 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir) 742 runCmd(cmd) 743 wksname = os.path.splitext(os.path.basename(wks.name))[0] 744 out = glob(self.resultdir + "%s-*direct" % wksname) 745 self.assertEqual(1, len(out)) 746 747 @OETestID(1849) 748 def test_fs_types(self): 749 """Test filesystem types for empty and not empty partitions""" 750 img = 'core-image-minimal' 751 with NamedTemporaryFile("w", suffix=".wks") as wks: 752 wks.writelines(['part ext2 --fstype ext2 --source rootfs\n', 753 'part btrfs --fstype btrfs --source rootfs --size 40M\n', 754 'part squash --fstype squashfs --source rootfs\n', 755 'part swap --fstype swap --size 1M\n', 756 'part emptyvfat --fstype vfat --size 1M\n', 757 'part emptymsdos --fstype msdos --size 1M\n', 758 'part emptyext2 --fstype ext2 --size 1M\n', 759 'part emptybtrfs --fstype btrfs --size 150M\n']) 760 wks.flush() 761 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir) 762 runCmd(cmd) 763 wksname = os.path.splitext(os.path.basename(wks.name))[0] 764 out = glob(self.resultdir + "%s-*direct" % wksname) 765 self.assertEqual(1, len(out)) 766 767 @OETestID(1851) 768 def test_kickstart_parser(self): 769 """Test wks parser options""" 770 with NamedTemporaryFile("w", suffix=".wks") as wks: 771 wks.writelines(['part / --fstype ext3 --source rootfs --system-id 0xFF '\ 772 '--overhead-factor 1.2 --size 100k\n']) 773 wks.flush() 774 cmd = "wic create %s -e core-image-minimal -o %s" % (wks.name, self.resultdir) 775 runCmd(cmd) 776 wksname = os.path.splitext(os.path.basename(wks.name))[0] 777 out = glob(self.resultdir + "%s-*direct" % wksname) 778 self.assertEqual(1, len(out)) 779 780 @OETestID(1850) 781 def test_image_bootpart_globbed(self): 782 """Test globbed sources with image-bootpart plugin""" 783 img = "core-image-minimal" 784 cmd = "wic create sdimage-bootpart -e %s -o %s" % (img, self.resultdir) 785 config = 'IMAGE_BOOT_FILES = "%s*"' % get_bb_var('KERNEL_IMAGETYPE', img) 786 self.append_config(config) 787 runCmd(cmd) 788 self.remove_config(config) 789 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct"))) 790 791 @OETestID(1855) 792 def test_sparse_copy(self): 793 """Test sparse_copy with FIEMAP and SEEK_HOLE filemap APIs""" 794 libpath = os.path.join(get_bb_var('COREBASE'), 'scripts', 'lib', 'wic') 795 sys.path.insert(0, libpath) 796 from filemap import FilemapFiemap, FilemapSeek, sparse_copy, ErrorNotSupp 797 with NamedTemporaryFile("w", suffix=".wic-sparse") as sparse: 798 src_name = sparse.name 799 src_size = 1024 * 10 800 sparse.truncate(src_size) 801 # write one byte to the file 802 with open(src_name, 'r+b') as sfile: 803 sfile.seek(1024 * 4) 804 sfile.write(b'\x00') 805 dest = sparse.name + '.out' 806 # copy src file to dest using different filemap APIs 807 for api in (FilemapFiemap, FilemapSeek, None): 808 if os.path.exists(dest): 809 os.unlink(dest) 810 try: 811 sparse_copy(sparse.name, dest, api=api) 812 except ErrorNotSupp: 813 continue # skip unsupported API 814 dest_stat = os.stat(dest) 815 self.assertEqual(dest_stat.st_size, src_size) 816 # 8 blocks is 4K (physical sector size) 817 self.assertEqual(dest_stat.st_blocks, 8) 818 os.unlink(dest) 819 820 @OETestID(1857) 821 def test_wic_ls(self): 822 """Test listing image content using 'wic ls'""" 823 runCmd("wic create wictestdisk " 824 "--image-name=core-image-minimal " 825 "-D -o %s" % self.resultdir) 826 images = glob(self.resultdir + "wictestdisk-*.direct") 827 self.assertEqual(1, len(images)) 828 829 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 830 831 # list partitions 832 result = runCmd("wic ls %s -n %s" % (images[0], sysroot)) 833 self.assertEqual(3, len(result.output.split('\n'))) 834 835 # list directory content of the first partition 836 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot)) 837 self.assertEqual(6, len(result.output.split('\n'))) 838 839 @OETestID(1856) 840 def test_wic_cp(self): 841 """Test copy files and directories to the the wic image.""" 842 runCmd("wic create wictestdisk " 843 "--image-name=core-image-minimal " 844 "-D -o %s" % self.resultdir) 845 images = glob(self.resultdir + "wictestdisk-*.direct") 846 self.assertEqual(1, len(images)) 847 848 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 849 850 # list directory content of the first partition 851 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot)) 852 self.assertEqual(6, len(result.output.split('\n'))) 853 854 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile: 855 testfile.write("test") 856 857 # copy file to the partition 858 runCmd("wic cp %s %s:1/ -n %s" % (testfile.name, images[0], sysroot)) 859 860 # check if file is there 861 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot)) 862 self.assertEqual(7, len(result.output.split('\n'))) 863 self.assertTrue(os.path.basename(testfile.name) in result.output) 864 865 # prepare directory 866 testdir = os.path.join(self.resultdir, 'wic-test-cp-dir') 867 testsubdir = os.path.join(testdir, 'subdir') 868 os.makedirs(os.path.join(testsubdir)) 869 copy(testfile.name, testdir) 870 871 # copy directory to the partition 872 runCmd("wic cp %s %s:1/ -n %s" % (testdir, images[0], sysroot)) 873 874 # check if directory is there 875 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot)) 876 self.assertEqual(8, len(result.output.split('\n'))) 877 self.assertTrue(os.path.basename(testdir) in result.output) 878 879 @OETestID(1858) 880 def test_wic_rm(self): 881 """Test removing files and directories from the the wic image.""" 882 runCmd("wic create mkefidisk " 883 "--image-name=core-image-minimal " 884 "-D -o %s" % self.resultdir) 885 images = glob(self.resultdir + "mkefidisk-*.direct") 886 self.assertEqual(1, len(images)) 887 888 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 889 890 # list directory content of the first partition 891 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot)) 892 self.assertIn('\nBZIMAGE ', result.output) 893 self.assertIn('\nEFI <DIR> ', result.output) 894 895 # remove file 896 runCmd("wic rm %s:1/bzimage -n %s" % (images[0], sysroot)) 897 898 # remove directory 899 runCmd("wic rm %s:1/efi -n %s" % (images[0], sysroot)) 900 901 # check if they're removed 902 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot)) 903 self.assertNotIn('\nBZIMAGE ', result.output) 904 self.assertNotIn('\nEFI <DIR> ', result.output) 905 906 @OETestID(1922) 907 def test_mkfs_extraopts(self): 908 """Test wks option --mkfs-extraopts for empty and not empty partitions""" 909 img = 'core-image-minimal' 910 with NamedTemporaryFile("w", suffix=".wks") as wks: 911 wks.writelines( 912 ['part ext2 --fstype ext2 --source rootfs --mkfs-extraopts "-D -F -i 8192"\n', 913 "part btrfs --fstype btrfs --source rootfs --size 40M --mkfs-extraopts='--quiet'\n", 914 'part squash --fstype squashfs --source rootfs --mkfs-extraopts "-no-sparse -b 4096"\n', 915 'part emptyvfat --fstype vfat --size 1M --mkfs-extraopts "-S 1024 -s 64"\n', 916 'part emptymsdos --fstype msdos --size 1M --mkfs-extraopts "-S 1024 -s 64"\n', 917 'part emptyext2 --fstype ext2 --size 1M --mkfs-extraopts "-D -F -i 8192"\n', 918 'part emptybtrfs --fstype btrfs --size 100M --mkfs-extraopts "--mixed -K"\n']) 919 wks.flush() 920 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir) 921 runCmd(cmd) 922 wksname = os.path.splitext(os.path.basename(wks.name))[0] 923 out = glob(self.resultdir + "%s-*direct" % wksname) 924 self.assertEqual(1, len(out)) 925 926 def test_expand_mbr_image(self): 927 """Test wic write --expand command for mbr image""" 928 # build an image 929 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n' 930 self.append_config(config) 931 self.assertEqual(0, bitbake('core-image-minimal').status) 932 933 # get path to the image 934 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE']) 935 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE'] 936 machine = bb_vars['MACHINE'] 937 image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine) 938 939 self.remove_config(config) 940 941 try: 942 # expand image to 1G 943 new_image_path = None 944 with NamedTemporaryFile(mode='wb', suffix='.wic.exp', 945 dir=deploy_dir, delete=False) as sparse: 946 sparse.truncate(1024 ** 3) 947 new_image_path = sparse.name 948 949 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 950 cmd = "wic write -n %s --expand 1:0 %s %s" % (sysroot, image_path, new_image_path) 951 runCmd(cmd) 952 953 # check if partitions are expanded 954 orig = runCmd("wic ls %s -n %s" % (image_path, sysroot)) 955 exp = runCmd("wic ls %s -n %s" % (new_image_path, sysroot)) 956 orig_sizes = [int(line.split()[3]) for line in orig.output.split('\n')[1:]] 957 exp_sizes = [int(line.split()[3]) for line in exp.output.split('\n')[1:]] 958 self.assertEqual(orig_sizes[0], exp_sizes[0]) # first partition is not resized 959 self.assertTrue(orig_sizes[1] < exp_sizes[1]) 960 961 # Check if all free space is partitioned 962 result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path)) 963 self.assertTrue("0 B, 0 bytes, 0 sectors" in result.output) 964 965 os.rename(image_path, image_path + '.bak') 966 os.rename(new_image_path, image_path) 967 968 # Check if it boots in qemu 969 with runqemu('core-image-minimal', ssh=False) as qemu: 970 cmd = "ls /etc/" 971 status, output = qemu.run_serial('true') 972 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 973 finally: 974 if os.path.exists(new_image_path): 975 os.unlink(new_image_path) 976 if os.path.exists(image_path + '.bak'): 977 os.rename(image_path + '.bak', image_path) 978 979 def test_wic_ls_ext(self): 980 """Test listing content of the ext partition using 'wic ls'""" 981 runCmd("wic create wictestdisk " 982 "--image-name=core-image-minimal " 983 "-D -o %s" % self.resultdir) 984 images = glob(self.resultdir + "wictestdisk-*.direct") 985 self.assertEqual(1, len(images)) 986 987 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 988 989 # list directory content of the second ext4 partition 990 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot)) 991 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset( 992 set(line.split()[-1] for line in result.output.split('\n') if line))) 993 994 def test_wic_cp_ext(self): 995 """Test copy files and directories to the ext partition.""" 996 runCmd("wic create wictestdisk " 997 "--image-name=core-image-minimal " 998 "-D -o %s" % self.resultdir) 999 images = glob(self.resultdir + "wictestdisk-*.direct") 1000 self.assertEqual(1, len(images)) 1001 1002 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1003 1004 # list directory content of the ext4 partition 1005 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot)) 1006 dirs = set(line.split()[-1] for line in result.output.split('\n') if line) 1007 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(dirs)) 1008 1009 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile: 1010 testfile.write("test") 1011 1012 # copy file to the partition 1013 runCmd("wic cp %s %s:2/ -n %s" % (testfile.name, images[0], sysroot)) 1014 1015 # check if file is there 1016 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot)) 1017 newdirs = set(line.split()[-1] for line in result.output.split('\n') if line) 1018 self.assertEqual(newdirs.difference(dirs), set([os.path.basename(testfile.name)])) 1019 1020 def test_wic_rm_ext(self): 1021 """Test removing files from the ext partition.""" 1022 runCmd("wic create mkefidisk " 1023 "--image-name=core-image-minimal " 1024 "-D -o %s" % self.resultdir) 1025 images = glob(self.resultdir + "mkefidisk-*.direct") 1026 self.assertEqual(1, len(images)) 1027 1028 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1029 1030 # list directory content of the /etc directory on ext4 partition 1031 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot)) 1032 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line]) 1033 1034 # remove file 1035 runCmd("wic rm %s:2/etc/fstab -n %s" % (images[0], sysroot)) 1036 1037 # check if it's removed 1038 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot)) 1039 self.assertTrue('fstab' not in [line.split()[-1] for line in result.output.split('\n') if line]) 1040