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