|
| 1 | +import requests |
| 2 | + |
| 3 | +frontend_url = "http://localhost:8000/" |
| 4 | +proxy_root_url = "http://localhost:8888/" |
| 5 | +proxy_instances_url = "{}instances".format(proxy_root_url) |
| 6 | + |
| 7 | +def test_if_proxy_returns_404_on_not_root(): |
| 8 | + response = requests.get("http://localhost:8888/") |
| 9 | + assert response.status_code == 404 |
| 10 | + |
| 11 | +def test_if_proxy_instances_returns_200(): |
| 12 | + response = requests.get(proxy_instances_url) |
| 13 | + assert response.status_code == 200 |
| 14 | + |
| 15 | +def test_if_proxy_instances_content_type_is_application_json(): |
| 16 | + response = requests.get(proxy_instances_url) |
| 17 | + assert response.headers["Content-Type"] == "application/json" |
| 18 | + |
| 19 | +def test_if_all_fields_are_defined(): |
| 20 | + response = requests.get(proxy_instances_url) |
| 21 | + response_body = response.json() |
| 22 | + for instance in response_body: |
| 23 | + assert {"hostname", "version", "isProxy", "isActive"} == instance.keys() |
| 24 | + |
| 25 | +def test_number_of_proxy_instances(): |
| 26 | + response = requests.get(proxy_instances_url) |
| 27 | + response_body = response.json() |
| 28 | + instance_list = list(filter(lambda instance: instance["isProxy"] == True, response_body)) |
| 29 | + assert len(instance_list) == 4 |
| 30 | + |
| 31 | + |
| 32 | +def test_number_of_backend_instances(): |
| 33 | + response = requests.get(proxy_instances_url) |
| 34 | + response_body = response.json() |
| 35 | + instance_list = list(filter(lambda instance: instance["isProxy"] == False, response_body)) |
| 36 | + assert len(instance_list) == 5 |
| 37 | + |
| 38 | +def get_hostnames(): |
| 39 | + response = requests.get(proxy_instances_url) |
| 40 | + response_body = response.json() |
| 41 | + return [instance["hostname"] for instance in response_body] |
| 42 | + |
| 43 | +def test_instance_hostnames_are_stable(): |
| 44 | + initial_set = get_hostnames() |
| 45 | + for _ in range(1, 21): |
| 46 | + assert get_hostnames() == initial_set |
| 47 | + |
| 48 | +def test_only_two_instances_are_active(): |
| 49 | + response = requests.get(proxy_instances_url) |
| 50 | + response_body = response.json() |
| 51 | + instance_list = list(filter(lambda instance: instance["isActive"] == True, response_body)) |
| 52 | + assert len(instance_list) == 2 |
| 53 | + |
| 54 | +def test_only_one_proxy_is_active(): |
| 55 | + response = requests.get(proxy_instances_url) |
| 56 | + response_body = response.json() |
| 57 | + instance_list = list(filter(lambda instance: instance["isActive"] == True and |
| 58 | + instance["isProxy"] == True, response_body)) |
| 59 | + assert len(instance_list) == 1 |
| 60 | + |
| 61 | +def test_only_one_backend_is_active(): |
| 62 | + response = requests.get(proxy_instances_url) |
| 63 | + response_body = response.json() |
| 64 | + instance_list = list(filter(lambda instance: instance["isActive"] == True and |
| 65 | + instance["isProxy"] == False, response_body)) |
| 66 | + assert len(instance_list) == 1 |
| 67 | + |
| 68 | +def test_only_one_backend_is_active(): |
| 69 | + response = requests.get(proxy_instances_url) |
| 70 | + response_body = response.json() |
| 71 | + instance_list = list(filter(lambda instance: instance["isActive"] == True and |
| 72 | + instance["isProxy"] == False, response_body)) |
| 73 | + assert len(instance_list) == 1 |
| 74 | + |
| 75 | +def test_active_proxy_changes(): |
| 76 | + instance_hostnames = set() |
| 77 | + for _ in range(1, 51): |
| 78 | + response = requests.get(proxy_instances_url) |
| 79 | + response_body = response.json() |
| 80 | + instance = next(filter(lambda instance: instance["isActive"] == True and |
| 81 | + instance["isProxy"] == False, response_body)) |
| 82 | + instance_hostnames.add(instance["hostname"]) |
| 83 | + assert len(instance_hostnames) > 1 |
| 84 | + |
| 85 | +def test_if_frontend_returns_200(): |
| 86 | + response = requests.get(frontend_url) |
| 87 | + assert response.status_code == 200 |
0 commit comments