Exemple #1
0
def choose_best_instance_test():
    """
    Test to choose the best instance according to comparator : priority > is_free=False > is_free=True
    """
    instances_list = [
        FakeInstance('fr-nw', is_free=True, priority=0),
        FakeInstance('fr-nw-c', is_free=True, priority=0),
        FakeInstance('fr-auv', is_free=True, priority=0),
    ]
    assert choose_best_instance(instances_list).name == 'fr-auv'

    instances_list[1].is_free = False
    assert choose_best_instance(instances_list).name == 'fr-nw-c'

    instances_list.append(FakeInstance('fr-bre', is_free=True, priority=1000))
    assert choose_best_instance(instances_list).name == 'fr-bre'
Exemple #2
0
def compute_regions(args):
    """
    method computing the region the journey has to be computed on
    The complexity comes from the fact that the regions in jormungandr can overlap.

    return the kraken instance key

    rules are easy:
    we fetch the different regions the user can use for 'origin' and 'destination'
    we do the intersection and sort the list
    """
    _region = None
    possible_regions = set()
    from_regions = set()
    to_regions = set()
    if args['origin']:
        from_regions = set(i_manager.key_of_id(args['origin'], only_one=False))
        #Note: if the key_of_id does not find any region, it raises a RegionNotFoundException

    if args['destination']:
        to_regions = set(
            i_manager.key_of_id(args['destination'], only_one=False))

    if not from_regions:
        #we didn't get any origin, the region is in the destination's list
        possible_regions = to_regions
    elif not to_regions:
        #we didn't get any origin, the region is in the destination's list
        possible_regions = from_regions
    else:
        #we need the intersection set
        possible_regions = from_regions.intersection(to_regions)

    logging.debug("orig region = {o}, dest region = {d} => set = {p}".format(
        o=from_regions, d=to_regions, p=possible_regions))

    if not possible_regions:
        raise RegionNotFound(
            custom_msg="cannot find a region with {o} and {d} in the same time"
            .format(o=args['origin'], d=args['destination']))

    sorted_regions = list(possible_regions)

    _region = choose_best_instance(sorted_regions)

    return _region