245
242
result = ssh(client, source, 'more nc_listen.out')
246
243
if result.rstrip() != msg:
247
244
raise ValueError("Wrong or missing message: %r" % result.rstrip())
251
247
def private_address(client, host):
252
248
default_route = ssh(client, host, 'ip -4 -o route list 0/0')
253
log.info("Default route from {}: {}".format(host, default_route))
254
route_match = re.search(r'([\w-]+)\s*$', default_route)
255
if route_match is None:
256
raise JujuAssertionError(
257
"Failed to find device in {}".format(default_route))
258
device = route_match.group(1)
259
log.info("Fetching the device IP of {}".format(device))
249
device = re.search(r'(\w+)\s*$', default_route).group(1)
260
250
device_ip = ssh(client, host, 'ip -4 -o addr show {}'.format(device))
261
log.info("Device IP for {}: {}".format(host, device_ip))
262
ip_match = re.search(r'inet\s+(\S+)/\d+\s', device_ip)
264
raise JujuAssertionError(
265
"Failed to find ip for device: {}".format(device))
266
return ip_match.group(1)
251
return re.search(r'inet\s+(\S+)/\d+\s', device_ip).group(1)
269
254
def assess_address_range(client, targets):
303
285
:param targets: machine IDs of machines to test
304
286
:return: None; raises ValueError on failure
306
log.info('Assessing internet connection.')
307
289
for target in targets:
308
log.info("Assessing internet connection for {}".format(target))
309
290
routes = ssh(client, target, 'ip route show')
311
292
d = re.search(r'^default\s+via\s+([\d\.]+)\s+', routes, re.MULTILINE)
313
294
rc = client.juju('ssh', ('--proxy', target,
314
'ping -c1 -q ' + d.group(1)), check=False)
295
'ping -c1 -q ' + d.group(1)), check=False)
316
297
raise ValueError('%s unable to ping default route' % target)
318
299
raise ValueError("Default route not found")
322
302
def _assessment_iteration(client, containers):
345
325
test_containers = [
346
326
containers[container_type][hosts[0]][0],
347
327
containers[container_type][hosts[1]][0],
349
329
_assessment_iteration(client, test_containers)
351
331
if KVM_MACHINE in types and LXC_MACHINE in types:
352
332
test_containers = [
353
333
containers[LXC_MACHINE][hosts[0]][0],
354
334
containers[KVM_MACHINE][hosts[0]][0],
356
336
_assessment_iteration(client, test_containers)
358
338
# Test with an LXC and a KVM on different machines
359
339
test_containers = [
360
340
containers[LXC_MACHINE][hosts[0]][0],
361
341
containers[KVM_MACHINE][hosts[1]][0],
363
343
_assessment_iteration(client, test_containers)
366
def get_uptime(client, host):
367
uptime_pattern = re.compile(r'.*(\d+)')
368
uptime_output = ssh(client, host, 'uptime -p')
369
log.info('uptime -p: {}'.format(uptime_output))
370
match = uptime_pattern.match(uptime_output)
372
return int(match.group(1))
377
346
def assess_container_networking(client, types):
378
347
"""Runs _assess_address_allocation, reboots hosts, repeat.
380
348
:param client: Juju client
381
349
:param types: Container types to test
384
log.info("Setting up test.")
385
352
hosts, containers = make_machines(client, types)
386
353
status = client.wait_for_started().status
387
log.info("Setup complete.")
388
log.info("Test started.")
390
355
_assess_container_networking(client, types, hosts, containers)
392
# Reboot all hosted modelled machines then the controller.
393
log.info("Instrumenting reboot of all machines.")
396
log.info("Restarting hosted machine: {}".format(host))
398
'run', ('--machine', host, 'sudo shutdown -r now'))
399
client.juju('show-action-status', ('--name', 'juju-run'))
401
log.info("Restarting controller machine 0")
402
controller_client = client.get_controller_client()
403
controller_status = controller_client.get_status()
404
controller_host = controller_status.status['machines']['0']['dns-name']
405
first_uptime = get_uptime(controller_client, '0')
406
ssh(controller_client, '0', 'sudo shutdown -r now')
407
except subprocess.CalledProcessError as e:
409
"Error running shutdown:\nstdout: %s\nstderr: %s",
410
e.output, getattr(e, 'stderr', None))
413
# Wait for the controller to shut down if it has not yet restarted.
414
# This ensure the call to wait_for_started happens after each host
416
second_uptime = get_uptime(controller_client, '0')
417
if second_uptime > first_uptime:
418
wait_for_port(controller_host, 22, closed=True, timeout=300)
357
# Reboot all hosts apart from machine 0 because we use machine 0 to jump
358
# through for some hosts.
359
for host in hosts[1:]:
360
ssh(client, host, 'sudo reboot')
362
# Finally reboot machine 0
363
ssh(client, hosts[0], 'sudo reboot')
365
# Wait for the state server to shut down. This prevents us from calling
366
# wait_for_started before machine 0 has shut down, which can cause us
367
# to think that we have finished rebooting before we actually have.
368
hostname = status['machines'][hosts[0]]['dns-name']
369
wait_for_port(hostname, 22, closed=True)
419
371
client.wait_for_started()
421
373
# Once Juju is up it can take a little while before ssh responds.
422
374
for host in hosts:
423
375
hostname = status['machines'][host]['dns-name']
424
376
wait_for_port(hostname, 22, timeout=240)
425
log.info("Reboot complete and all hosts ready for retest.")
427
378
_assess_container_networking(client, types, hosts, containers)
431
381
class _CleanedContext: