Skip to content

Commit a00ec77

Browse files
committed
Add tests
1 parent 24e92b6 commit a00ec77

5 files changed

Lines changed: 107 additions & 4 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ jenkins_password
1313
/.idea
1414

1515
.env
16+
17+
__pycache__
18+
.pytest_cache

test/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.PHONY: test intialize_services install_dependencies all
2+
3+
all: intialize_services install_dependencies test
4+
5+
test:
6+
PATH=$$PATH:~/.local/bin pytest run_tests.py
7+
8+
intialize_services:
9+
docker-compose -f ../.deployment/docker-compose.yml up -d backend redis caddy
10+
docker-compose -f ../.deployment/docker-compose.yml exec caddy sh -c "while ! wget backend:8080/healthcheck -q --spider; do sleep 1; done"
11+
docker-compose -f ../.deployment/docker-compose.yml up -d
12+
13+
install_dependencies:
14+
pip3 install -r requirements.txt

test/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
requests
2+
pytest
3+
requests_html

test/run_tests.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

testing/run_artillery.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)