1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
#
# Command-line arguments handling tests for generator
#
# Copyright (C) 2018 Canonical, Ltd.
# Author: Mathieu Trudel-Lapierre <mathieu.trudel.lapierre@canonical.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import subprocess
from .base import TestBase, exe_generate, OVS_CLEANUP
class TestConfigArgs(TestBase):
'''Config file argument handling'''
def test_no_files(self):
subprocess.check_call([exe_generate, '--root-dir', self.workdir.name])
self.assertEqual(os.listdir(self.workdir.name), ['run'])
self.assert_nm_udev(None)
def test_no_configs(self):
self.generate('network:\n version: 2')
# should not write any files
self.assertCountEqual(os.listdir(self.workdir.name), ['etc', 'run'])
self.assert_networkd(None)
self.assert_networkd_udev(None)
self.assert_nm(None)
self.assert_nm_udev(None)
self.assert_ovs({'cleanup.service': OVS_CLEANUP % {'iface': 'cleanup'}})
# should not touch -wait-online
service_dir = os.path.join(self.workdir.name, 'run', 'systemd', 'system')
override = os.path.join(service_dir, 'systemd-networkd-wait-online.service.d', '10-netplan.conf')
self.assertFalse(os.path.isfile(override))
def test_empty_config(self):
self.generate('')
# should not write any files
self.assertCountEqual(os.listdir(self.workdir.name), ['etc', 'run'])
self.assert_networkd(None)
self.assert_networkd_udev(None)
self.assert_nm(None)
self.assert_nm_udev(None)
self.assert_ovs({'cleanup.service': OVS_CLEANUP % {'iface': 'cleanup'}})
def test_generate_fails_during_try(self):
os.makedirs(self.rundir, mode=0o700, exist_ok=True)
open(os.path.join(self.rundir, "netplan-try.ready"), "w").close()
self.generate('network:\n version: 2', expect_fail=True)
def test_file_args(self):
conf = os.path.join(self.workdir.name, 'config')
with open(conf, 'w') as f:
f.write('network: {}')
# when specifying custom files, it should ignore the global config
self.generate('''network:
version: 2
ethernets:
eth0:
dhcp4: true''', extra_args=[conf])
# There is one systemd service unit 'netplan-ovs-cleanup.service' in /run,
# which will always be created
self.assertEqual(set(os.listdir(self.workdir.name)), {'config', 'etc', 'run'})
self.assert_networkd(None)
self.assert_networkd_udev(None)
self.assert_nm(None)
self.assert_nm_udev(None)
def test_file_args_notfound(self):
err = self.generate('''network:
version: 2
ethernets:
eth0:
dhcp4: true''', expect_fail=True, extra_args=['/non/existing/config'])
self.assertEqual(err, 'Cannot stat /non/existing/config: No such file or directory\n')
self.assertEqual(os.listdir(self.workdir.name), ['etc'])
def test_help(self):
conf = os.path.join(self.workdir.name, 'etc', 'netplan', 'a.yaml')
os.makedirs(os.path.dirname(conf))
with open(conf, 'w') as f:
f.write('''network:
version: 2
ethernets:
eth0:
dhcp4: true''')
os.chmod(conf, mode=0o600)
p = subprocess.Popen([exe_generate, '--root-dir', self.workdir.name, '--help'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True)
(out, err) = p.communicate()
self.assertEqual(err, '')
self.assertEqual(p.returncode, 0)
self.assertIn('Usage:', out)
self.assertEqual(os.listdir(self.workdir.name), ['etc'])
def test_unknown_cli_args(self):
p = subprocess.Popen([exe_generate, '--foo'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True)
(out, err) = p.communicate()
self.assertIn('nknown option --foo', err)
self.assertNotEqual(p.returncode, 0)
def test_output_mkdir_error(self):
conf = os.path.join(self.workdir.name, 'config')
with open(conf, 'w') as f:
f.write('''network:
version: 2
ethernets:
eth0:
dhcp4: true''')
err = self.generate('', extra_args=['--root-dir', '/proc/foo', conf], expect_fail=True)
# can be /proc/foor/run/systemd/{network,system}
self.assertIn('cannot create directory /proc/foo/run/systemd/', err)
def test_systemd_generator(self):
conf = os.path.join(self.confdir, 'a.yaml')
os.makedirs(os.path.dirname(conf))
with open(conf, 'w') as f:
f.write('''network:
version: 2
ethernets:
eth99:
dhcp4: true
eth98:
dhcp4: true
optional: true
lo:
addresses: ["127.0.0.1/8", "::1/128"]
vlans:
eth99.42: # link-local is ignored for bridge-members
link: eth99
id: 42
link-local: [ipv4, ipv6]
eth99.43: # no link-local, but routable IP
link: eth99
id: 43
link-local: []
addresses: [10.0.0.2/24]
eth99.44: # link-loal, but no routable IP
link: eth99
id: 44
link-local: [ipv6]
eth99.45: # ignore-carrier & no link-local, but routable IP
link: eth99
id: 45
link-local: []
ignore-carrier: true
addresses: [10.0.0.3/24]
eth99.46: # routable, but optional. So no wait-online log message about this bond member
link: eth99
id: 46
dhcp4: true
optional: true
bonds:
bond0:
dhcp4: true
interfaces: [eth99.42, eth99.43, et99.46]''')
os.chmod(conf, mode=0o600)
outdir = os.path.join(self.workdir.name, 'out')
os.mkdir(outdir)
generator = os.path.join(self.workdir.name, 'systemd', 'system-generators', 'netplan')
os.makedirs(os.path.dirname(generator))
os.symlink(exe_generate, generator)
local_env = os.environ.copy()
local_env['G_MESSAGES_DEBUG'] = 'all'
out = subprocess.check_output([generator, '--root-dir', self.workdir.name, outdir, outdir, outdir],
stderr=subprocess.STDOUT, text=True, env=local_env)
n = os.path.join(self.workdir.name, 'run', 'systemd', 'network', '10-netplan-eth99.network')
self.assertTrue(os.path.exists(n))
os.unlink(n)
n = os.path.join(self.workdir.name, 'run', 'systemd', 'network', '10-netplan-eth98.network')
self.assertTrue(os.path.exists(n))
os.unlink(n)
n = os.path.join(self.workdir.name, 'run', 'systemd', 'network', '10-netplan-lo.network')
self.assertTrue(os.path.exists(n))
os.unlink(n)
n = os.path.join(self.workdir.name, 'run', 'systemd', 'network', '10-netplan-bond0.network')
self.assertTrue(os.path.exists(n))
os.unlink(n)
# check log message about bonds wait-online
self.assertIn('Not all bond members need to be connected for bond0 to be ready.', out)
self.assertIn('Consider marking eth99.42 as "optional: true", to avoid blocking systemd-networkd-wait-online.', out)
self.assertNotIn('making eth99.43 as "optional: true"', out) # routable
self.assertNotIn('making eth99.46 as "optional: true"', out) # optional
# should auto-enable networkd and -wait-online
service_dir = os.path.join(self.workdir.name, 'run', 'systemd', 'system')
self.assertTrue(os.path.islink(os.path.join(
outdir, 'multi-user.target.wants', 'systemd-networkd.service')))
self.assertTrue(os.path.islink(os.path.join(
outdir, 'network-online.target.wants', 'systemd-networkd-wait-online.service')))
override = os.path.join(service_dir, 'systemd-networkd-wait-online.service.d', '10-netplan.conf')
self.assertTrue(os.path.isfile(override))
with open(override, 'r') as f:
# eth99 does not exist on the system, so will not be listed
self.assertEqual(f.read(), '''[Unit]
ConditionPathIsSymbolicLink=/run/systemd/generator/network-online.target.wants/systemd-networkd-wait-online.service
[Service]
ExecStart=
ExecStart=/lib/systemd/systemd-networkd-wait-online -i eth99.43:carrier -i lo:carrier \
-i eth99.42:carrier -i eth99.44:degraded -i bond0:degraded
ExecStart=/lib/systemd/systemd-networkd-wait-online --any -o routable -i eth99.43 -i eth99.45 -i bond0\n''')
# should be a no-op the second time while the stamp exists
out = subprocess.check_output([generator, '--root-dir', self.workdir.name, outdir, outdir, outdir],
stderr=subprocess.STDOUT, text=True)
self.assertFalse(os.path.exists(n))
self.assertIn('netplan generate already ran', out)
# after removing the stamp it generates again, and not trip over the
# existing enablement symlink
os.unlink(os.path.join(outdir, 'netplan.stamp'))
subprocess.check_output([generator, '--root-dir', self.workdir.name, outdir, outdir, outdir])
self.assertTrue(os.path.exists(n))
def test_systemd_generator_all_optional(self):
conf = os.path.join(self.confdir, 'a.yaml')
os.makedirs(os.path.dirname(conf))
with open(conf, 'w') as f:
f.write('''network:
version: 2
ethernets:
eth0:
dhcp4: true
optional: true''')
outdir = os.path.join(self.workdir.name, 'out')
os.mkdir(outdir)
generator = os.path.join(self.workdir.name, 'systemd', 'system-generators', 'netplan')
os.makedirs(os.path.dirname(generator))
os.symlink(exe_generate, generator)
subprocess.check_call([generator, '--root-dir', self.workdir.name, outdir, outdir, outdir])
n = os.path.join(self.workdir.name, 'run', 'systemd', 'network', '10-netplan-eth0.network')
self.assertTrue(os.path.exists(n))
os.unlink(n)
# should auto-enable networkd but not -wait-online
service_dir = os.path.join(self.workdir.name, 'run', 'systemd', 'system')
self.assertTrue(os.path.islink(os.path.join(
outdir, 'multi-user.target.wants', 'systemd-networkd.service')))
self.assertFalse(os.path.islink(os.path.join(
outdir, 'network-online.target.wants', 'systemd-networkd-wait-online.service')))
override = os.path.join(service_dir, 'systemd-networkd-wait-online.service.d', '10-netplan.conf')
self.assertTrue(os.path.isfile(override))
with open(override, 'r') as f:
self.assertEqual(f.read(), '''[Unit]
ConditionPathIsSymbolicLink=/run/systemd/generator/network-online.target.wants/systemd-networkd-wait-online.service
''')
def test_systemd_wait_online_only_non_routable(self):
conf = os.path.join(self.confdir, 'a.yaml')
os.makedirs(os.path.dirname(conf))
with open(conf, 'w') as f:
f.write('''network:
version: 2
ethernets:
nomatchfound: # non-optional, but cannot be matched to a physical interface on the test runner
dhcp4: true
vlans:
eth99.44:
link: nomatchfound
id: 44
link-local: [ipv6]''')
outdir = os.path.join(self.workdir.name, 'out')
os.mkdir(outdir)
generator = os.path.join(self.workdir.name, 'systemd', 'system-generators', 'netplan')
os.makedirs(os.path.dirname(generator))
os.symlink(exe_generate, generator)
service_dir = os.path.join(self.workdir.name, 'run', 'systemd', 'system')
override = os.path.join(service_dir, 'systemd-networkd-wait-online.service.d', '10-netplan.conf')
subprocess.check_call([generator, '--root-dir', self.workdir.name, outdir, outdir, outdir])
self.assertTrue(os.path.isfile(override))
with open(override, 'r') as f:
self.assertEqual(f.read(), '''[Unit]
ConditionPathIsSymbolicLink=/run/systemd/generator/network-online.target.wants/systemd-networkd-wait-online.service
[Service]
ExecStart=
ExecStart=/lib/systemd/systemd-networkd-wait-online -i eth99.44:degraded
''')
def test_systemd_wait_online_only_routable(self):
conf = os.path.join(self.confdir, 'a.yaml')
os.makedirs(os.path.dirname(conf))
with open(conf, 'w') as f:
f.write('''network:
version: 2
bridges:
br0:
dhcp4: true''')
outdir = os.path.join(self.workdir.name, 'out')
os.mkdir(outdir)
generator = os.path.join(self.workdir.name, 'systemd', 'system-generators', 'netplan')
os.makedirs(os.path.dirname(generator))
os.symlink(exe_generate, generator)
subprocess.check_call([generator, '--root-dir', self.workdir.name, outdir, outdir, outdir])
n = os.path.join(self.workdir.name, 'run', 'systemd', 'network', '10-netplan-br0.network')
self.assertTrue(os.path.exists(n))
os.unlink(n)
service_dir = os.path.join(self.workdir.name, 'run', 'systemd', 'system')
override = os.path.join(service_dir, 'systemd-networkd-wait-online.service.d', '10-netplan.conf')
subprocess.check_call([generator, '--root-dir', self.workdir.name, outdir, outdir, outdir])
self.assertTrue(os.path.isfile(override))
with open(override, 'r') as f:
self.assertEqual(f.read(), '''[Unit]
ConditionPathIsSymbolicLink=/run/systemd/generator/network-online.target.wants/systemd-networkd-wait-online.service
[Service]
ExecStart=
ExecStart=/lib/systemd/systemd-networkd-wait-online -i br0:degraded
ExecStart=/lib/systemd/systemd-networkd-wait-online --any -o routable -i br0
''')
def test_systemd_generator_noconf(self):
outdir = os.path.join(self.workdir.name, 'out')
os.mkdir(outdir)
generator = os.path.join(self.workdir.name, 'systemd', 'system-generators', 'netplan')
os.makedirs(os.path.dirname(generator))
os.symlink(exe_generate, generator)
subprocess.check_call([generator, '--root-dir', self.workdir.name, outdir, outdir, outdir])
# no enablement symlink here
self.assertEqual(os.listdir(outdir), ['netplan.stamp'])
def test_systemd_generator_badcall(self):
outdir = os.path.join(self.workdir.name, 'out')
os.mkdir(outdir)
generator = os.path.join(self.workdir.name, 'systemd', 'system-generators', 'netplan')
os.makedirs(os.path.dirname(generator))
os.symlink(exe_generate, generator)
try:
subprocess.check_output([generator, '--root-dir', self.workdir.name],
stderr=subprocess.STDOUT, text=True)
self.fail("direct systemd generator call is expected to fail, but succeeded.") # pragma: nocover
except subprocess.CalledProcessError as e:
self.assertEqual(e.returncode, 1)
self.assertIn('can not be called directly', e.output)
def test_systemd_generator_escaping(self):
conf = os.path.join(self.confdir, 'a.yaml')
os.makedirs(os.path.dirname(conf))
with open(conf, 'w') as f:
f.write('''network:
version: 2
ethernets:
lo:
match:
name: lo
set-name: "a ; b\\t; c\\t; d \\n 123 ; echo "
addresses: ["127.0.0.1/8", "::1/128"]''')
os.chmod(conf, mode=0o600)
outdir = os.path.join(self.workdir.name, 'out')
os.mkdir(outdir)
generator = os.path.join(self.workdir.name, 'systemd', 'system-generators', 'netplan')
os.makedirs(os.path.dirname(generator))
os.symlink(exe_generate, generator)
subprocess.check_call([generator, '--root-dir', self.workdir.name, outdir, outdir, outdir])
n = os.path.join(self.workdir.name, 'run', 'systemd', 'network', '10-netplan-lo.network')
self.assertTrue(os.path.exists(n))
os.unlink(n)
# should auto-enable networkd and -wait-online
service_dir = os.path.join(self.workdir.name, 'run', 'systemd', 'system')
self.assertTrue(os.path.islink(os.path.join(
outdir, 'multi-user.target.wants', 'systemd-networkd.service')))
self.assertTrue(os.path.islink(os.path.join(
outdir, 'network-online.target.wants', 'systemd-networkd-wait-online.service')))
override = os.path.join(service_dir, 'systemd-networkd-wait-online.service.d', '10-netplan.conf')
self.assertTrue(os.path.isfile(override))
with open(override, 'r') as f:
# eth99 does not exist on the system, so will not be listed
self.assertEqual(f.read(), '''[Unit]
ConditionPathIsSymbolicLink=/run/systemd/generator/network-online.target.wants/systemd-networkd-wait-online.service
[Service]
ExecStart=
ExecStart=/lib/systemd/systemd-networkd-wait-online -i a \\; b\\t; c\\t; d \\n 123 \\; echo :degraded
ExecStart=/lib/systemd/systemd-networkd-wait-online --any -o routable -i a \\; b\\t; c\\t; d \\n 123 \\; echo \n''')
# should be a no-op the second time while the stamp exists
out = subprocess.check_output([generator, '--root-dir', self.workdir.name, outdir, outdir, outdir],
stderr=subprocess.STDOUT, text=True)
self.assertFalse(os.path.exists(n))
self.assertIn('netplan generate already ran', out)
# after removing the stamp it generates again, and not trip over the
# existing enablement symlink
os.unlink(os.path.join(outdir, 'netplan.stamp'))
subprocess.check_output([generator, '--root-dir', self.workdir.name, outdir, outdir, outdir])
self.assertTrue(os.path.exists(n))
|