|
| 1 | +import os |
| 2 | + |
| 3 | +import traceback |
| 4 | +from multiprocessing import Process |
| 5 | +import time |
| 6 | + |
| 7 | +from behave import * |
| 8 | +from nose.tools.trivial import ok_ |
| 9 | + |
| 10 | +from of.broker.features import run_broker_testing |
| 11 | +from of.common.messaging.utils import call_api, register_at_broker |
| 12 | + |
| 13 | +use_step_matcher("re") |
| 14 | + |
| 15 | +_log_prefix = "Tester - Broker Cycles :" |
| 16 | +script_dir = os.path.dirname(__file__) |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +@given("the broker is started") |
| 21 | +def step_impl(context): |
| 22 | + """ |
| 23 | + :type context behave.runner.Context |
| 24 | + """ |
| 25 | + try: |
| 26 | + print(_log_prefix + "Starting broker process.") |
| 27 | + context.broker_process = Process(name="broker_testing", target=run_broker_testing, daemon=False) |
| 28 | + context.broker_process.start() |
| 29 | + if context.broker_process.exitcode: |
| 30 | + ok_(False, "Failed starting the process, it terminated within wait time.") |
| 31 | + else: |
| 32 | + ok_(True) |
| 33 | + except Exception as e: |
| 34 | + print(_log_prefix + "Error starting broker: " + str(e) + "\nTraceback:" + traceback.format_exc()) |
| 35 | + ok_(False) |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +@then("a termination on linux should return zero as exit code") |
| 42 | +def step_impl(context): |
| 43 | + """ |
| 44 | + :type context behave.runner.Context |
| 45 | + """ |
| 46 | + print(_log_prefix + "Terminating the broker process") |
| 47 | + context.broker_process.terminate() |
| 48 | + try: |
| 49 | + context.broker_process.join(timeout=10) |
| 50 | + except: |
| 51 | + ok_(False, "Broker process did not exit within 5 seconds") |
| 52 | + if os.name == "nt": |
| 53 | + print("Terminations cannot be gracefully catched and acted upon on windows") |
| 54 | + ok_(True) |
| 55 | + else: |
| 56 | + if context.broker_process.exitcode != 0: |
| 57 | + ok_(False, "Broker process exited with a nonzero exit code: " + str(context.broker_process.exitcode)) |
| 58 | + else: |
| 59 | + ok_(True) |
| 60 | + |
| 61 | + |
| 62 | +@step("the broker is told to restart using the API") |
| 63 | +def step_impl(context): |
| 64 | + """ |
| 65 | + :type context behave.runner.Context |
| 66 | + """ |
| 67 | + print(_log_prefix + "Telling Broker to restart using a call to broker_control...") |
| 68 | + call_api(_url="https://127.0.0.1:8080/admin/broker_control", |
| 69 | + _session_id=context.session["session_id"], |
| 70 | + _data={"command": "restart", "reason": "Testing restarting the broker"}, |
| 71 | + _verify_SSL=False |
| 72 | + ) |
| 73 | + print(_log_prefix + "Waiting for process to exit...") |
| 74 | + |
| 75 | + context.broker_process.join(timeout=2) |
| 76 | + ok_(True) |
| 77 | + |
| 78 | +@step("the broker is told to stop using the API") |
| 79 | +def step_impl(context): |
| 80 | + """ |
| 81 | + :type context behave.runner.Context |
| 82 | + """ |
| 83 | + print(_log_prefix + "Telling Broker to stop using a call to stop_broker...") |
| 84 | + call_api(_url="https://127.0.0.1:8080/admin/broker_control", |
| 85 | + _session_id=context.session["session_id"], |
| 86 | + _data={"command": "stop", "reason": "Testing stopping the broker"}, |
| 87 | + _verify_SSL=False |
| 88 | + ) |
| 89 | + ok_(True) |
| 90 | + |
| 91 | +@step("a get environment request should fail") |
| 92 | +def step_impl(context): |
| 93 | + """ |
| 94 | + :type context behave.runner.Context |
| 95 | + """ |
| 96 | + try: |
| 97 | + _response = call_api(_url="https://127.0.0.1:8080/get_broker_environment", |
| 98 | + _session_id=context.session["session_id"], |
| 99 | + _data={}, _verify_SSL=False) |
| 100 | + except Exception as e: |
| 101 | + ok_(True, str(e)) |
| 102 | + else: |
| 103 | + |
| 104 | + os.kill(_response["systemPid"], 9) |
| 105 | + ok_(False, "The broker could still be reached after being told to shut down, killed pid "+ str(_response["systemPid"] + " manually.")) |
| 106 | + |
| 107 | + |
| 108 | +@step("we wait (?P<seconds>.+) seconds") |
| 109 | +def step_impl(context, seconds): |
| 110 | + """ |
| 111 | + :type context behave.runner.Context |
| 112 | + """ |
| 113 | + time.sleep(float(seconds)) |
| 114 | + ok_(True) |
| 115 | + |
| 116 | + |
| 117 | +@step("there is a web_socket peer with the address (?P<address>.+)") |
| 118 | +def step_impl(context, address): |
| 119 | + """ |
| 120 | + :type context behave.runner.Context |
| 121 | + """ |
| 122 | + print("Tester: Calling get_peers and checking for the address " + str(address)) |
| 123 | + try: |
| 124 | + _peers = call_api(_url="https://127.0.0.1:8080/admin/get_peers", |
| 125 | + _session_id=context.session["session_id"], |
| 126 | + _data={}, _verify_SSL=False |
| 127 | + ) |
| 128 | + except Exception as e: |
| 129 | + ok_(False, "An error occurred contacting server:" + str(e)) |
| 130 | + if _peers: |
| 131 | + for _peer in _peers: |
| 132 | + |
| 133 | + if "web_socket" in _peer and _peer["address"] == address and _peer["web_socket"] == "removed for serialization": |
| 134 | + ok_(True) |
| 135 | + return |
| 136 | + ok_(False, "No " + str(address) + " peer registered.") |
| 137 | + else: |
| 138 | + ok_(False, "No response from the server.") |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +@step("it is it possible to register an (?P<peer_type>.+) at the broker") |
| 143 | +def step_impl(context, peer_type): |
| 144 | + """ |
| 145 | + :type context behave.runner.Context |
| 146 | + """ |
| 147 | + |
| 148 | + try: |
| 149 | + context.session = register_at_broker(_address="test", _type=peer_type, _server="https://127.0.0.1:8080", |
| 150 | + _username="tester", _password="test", _verify_SSL=False) |
| 151 | + |
| 152 | + if context.session["session_id"]: |
| 153 | + print("Successfully registered at broker, got sessionid: " + str(context.session["session_id"])) |
| 154 | + ok_(True) |
| 155 | + else: |
| 156 | + ok_(False, "No session Id returned.") |
| 157 | + except ConnectionError as e: |
| 158 | + ok_(False, "'Connection Error registering:" + str(e)) |
| 159 | + except Exception as e: |
| 160 | + ok_(False, "Error registering:" + str(e)) |
| 161 | + |
| 162 | + |
0 commit comments