aboutsummaryrefslogtreecommitdiff
path: root/networks/hs_instance_stop.py
blob: 01ed8651e3d023dbe906e45b54f4fdc78dedd21b (plain)
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
from chutney.Testing import *
from chutney.TorNet import *

from stem.control import EventType

import time

def hs_instance_stop(network):
    result = True

    hs_nodes = network.get("h")
    clients = network.get("c")

    if len(clients) % len(hs_nodes) != 0:
        logging.error("There must be a equal number of clients per hs_node")
        return False

    clients_per_node = len(clients) / len(hs_nodes)

    if clients_per_node < 1:
        logging.error("Not enough clients")
        return False

    logging.info("Clients per node %d" % clients_per_node)

    # To ensure the ..
    client_groups = zip(*(iter(clients),) * clients_per_node)

    initial_nodes = network.get("a") + network.get("r") + network.get("c")

    if not all([ n.getController().start() for n in initial_nodes ]):
        return False

    nodes_by_fingerprint = get_node_fingerprints(network.get("a") + network.get("r"))

    logging.info("All initial nodes running")

    logging.info("There are %d nodes to start" % len(hs_nodes))

    # Start each node, one by one
    for i, hs_node in enumerate(hs_nodes):
        logging.info("Starting node %d" % i)

        if not hs_node.getController().start():
            logging.error("Error starting node %d" %i)
            return False # Stop on failure

        track_introduction_points(hs_node)

        node_published_descriptor = threading.Event()

        def hs_node_listener(logevent):
            if "Successfully uploaded v2 rend descriptors" in logevent.message:
                node_published_descriptor.set()

        hs_node.getStemController().add_event_listener(hs_node_listener, EventType.INFO)

        node_published_descriptor.wait()

        hs_node.getStemController().remove_event_listener(hs_node_listener)

        # Now begin testing
        logging.info("Testing with the %d client group" % i)
        result &= connection_test(client_groups[i], i + 1)

        result &= check_same_intro_points()

    # Stop all but the first node in turn, checking that all currently active
    # nodes are reachable, and that all active nodes have the same introduction
    # points
    for i, hs_node in reversed(list(enumerate(hs_nodes))[1:]):
        logging.info("Stopping node %d" % i)

        hs_node.getController().stop()

        time.sleep(1)

        # Now begin testing

        for c_group in range(len(hs_nodes)):
            logging.info("Testing with the %d client group" % c_group)
            expected_nodes = min(c_group, i)
            result &= connection_test(client_groups[c_group], expected_nodes)

        result &= check_same_intro_points()

    return result