~psivaa/uci-engine/lander-jenkins-sub-charm-with-plugin

« back to all changes in this revision

Viewing changes to tests/run.py

  • Committer: Andy Doan
  • Date: 2014-04-04 16:00:36 UTC
  • mfrom: (422 uci-engine)
  • mto: This revision was merged to the branch mainline in revision 423.
  • Revision ID: andy.doan@canonical.com-20140404160036-cve1123qiu8o7dvf
merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
236
236
 
237
237
 
238
238
def needs_tunnel():
239
 
    """Check to see if juju's Swift bucket exists, and the IP it refers to is
240
 
    pointed at by a sshuttle instance."""
241
 
 
242
 
    log.info("Checking to see if a tunnel is needed.")
 
239
    """Check to see if a tunnel is required and up.
 
240
 
 
241
    When required, it depends on juju's Swift bucket existing, and the IP it
 
242
    refers to is pointed at by a sshuttle instance.
 
243
    """
 
244
    if not int(os.environ['OS_REQUIRES_TUNNEL']):
 
245
        return 0
 
246
 
 
247
    log.info("Checking to see if a tunnel needs to be setup.")
243
248
 
244
249
    instance = get_bootstrap_nova_instance()
245
250
    ip_addr = get_bootstrap_ip(instance, 1)
323
328
 
324
329
 
325
330
def get_bootstrap_ip(instance, max_tries=120):
326
 
    """Wait for the juju bootstrap node to boot and obtain an IP address so we
327
 
    can tunnel through it.
328
 
 
329
 
    Raises NotFound if the instance has not been spawned."""
 
331
    """Wait for the juju bootstrap node to boot and obtain an IP address.
 
332
 
 
333
    This is a pre-requisite to setup a tunnel.
 
334
 
 
335
    :raises: NotFound if the instance has not been spawned.
 
336
 
 
337
    :raises: Exception if no IP can be found after max_tries*3 seconds.
 
338
    """
330
339
 
331
340
    log.info("Getting the bootstrap IP address.")
332
341
 
336
345
              'service_type': 'compute'}
337
346
    nt = client.Client(*args, **kwargs)
338
347
 
339
 
    net = ''
340
348
    tries = 0
341
349
    while tries < max_tries:
342
350
        # Raises NotFound if the instance does not exist.
343
351
        server = nt.servers.get(instance)
344
 
        net = server.networks.get('canonistack')
345
 
        if net:
346
 
            net = net[0]
347
 
            # Don't sleep anymore. We have our IP; break out of the loop.
348
 
            break
 
352
        networks = server.networks.values()
 
353
        if networks:
 
354
            # We use a single network and don't care how it's named
 
355
            net = networks[0]
 
356
            if net:
 
357
                # Either we have a single address or the last one has been
 
358
                # added to make the instance reachable (floating or public).
 
359
                net = net[-1]
 
360
                # Don't sleep anymore. We have our IP, let's move on
 
361
                return net
349
362
 
350
363
        tries += 1
351
364
        if tries != max_tries:
352
365
            time.sleep(3)
353
 
    else:
354
 
        raise Exception('Timed out waiting for bootstrap IP.')
355
 
 
356
 
    return net
 
366
    raise Exception('Timed out waiting for bootstrap IP.')
357
367
 
358
368
 
359
369
def destroy_tunnel():
440
450
 
441
451
 
442
452
def check_nova_env():
443
 
    """Ensure we have the right keys set in the environment for talking to
444
 
    Canonistack."""
 
453
    """Ensure we have the right keys set in the environment.
445
454
 
446
 
    if os.environ.get('OS_USERNAME'):
447
 
        return True
448
 
    else:
449
 
        return False
 
455
    We assume that the targeted cloud is openstack based.
 
456
    """
 
457
    for key in ('OS_USERNAME', 'OS_REQUIRES_TUNNEL'):
 
458
        if os.environ.get(key, None) is None:
 
459
            msg = ('Please source a novarc file before running this harness.'
 
460
                   ' {} is not currently set'.format(key))
 
461
            log.error(msg)
 
462
            sys.exit(1)
450
463
 
451
464
 
452
465
def check_juju_version():
473
486
 
474
487
 
475
488
def main():
 
489
    check_nova_env()
 
490
 
476
491
    test_dir = os.path.dirname(sys.argv[0])
477
492
    deploy.check_environment()
478
493
    tests = get_tests(test_dir)
479
494
    if not tests:
480
 
        sys.exit(0)
 
495
        # Running no tests is an error
 
496
        sys.exit(1)
481
497
 
482
498
    if not check_juju_version():
483
499
        msg = 'Please install a more recent version of juju (1.16.5 or later).'
484
500
        log.error(msg)
485
501
        sys.exit(1)
486
502
 
487
 
    if not check_nova_env():
488
 
        msg = 'Please source a novarc file before running this harness.'
489
 
        log.error(msg)
490
 
        sys.exit(1)
491
 
 
492
503
    if not check_sudoers():
493
504
        msg = 'Please run:\necho "$USER %s" > %s' % (SUDOERS, SUDOERS_FILE)
494
505
        log.error(msg)
508
519
 
509
520
    if needs_bootstrap():
510
521
        bootstrap()
511
 
        wait_then_tunnel()
512
 
    elif needs_tunnel():
 
522
    if needs_tunnel():
513
523
        wait_then_tunnel()
514
524
 
515
525
    destroy_services()