Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
masaruhoshi committed Feb 8, 2022
1 parent 849472c commit fa39d81
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
14 changes: 12 additions & 2 deletions connaisseur/flask_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def handle_alert_config_error(err):
"mutate_requests_total",
"Total number of mutate requests",
labels={
"allowed": lambda r: r.get_json(silent=True)["response"]["allowed"],
"status_code": lambda r: r.get_json(silent=True)["response"]["status"]["code"],
"allowed": lambda r: metrics_label(r, "allowed"),
"status_code": lambda r: metrics_label(r, "status_code"),
},
)
def mutate():
Expand Down Expand Up @@ -92,6 +92,16 @@ def mutate():
return jsonify(response)


def metrics_label(response, label):
l = response.get_json(silent=True)
if l:
if label == "allowed":
return l["response"]["allowed"]
elif label == "status_code":
return l["response"]["status"]["code"]
return l


# health probe
@APP.route("/health", methods=["GET", "POST"])
@metrics.do_not_track()
Expand Down
17 changes: 10 additions & 7 deletions tests/test_flask_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ def test_mutate_calls_send_alert_for_invalid_admission_request(


def test_healthz():
assert pytest.fa.healthz() == ("", 200)
with pytest.fa.APP.test_request_context():
assert pytest.fa.healthz() == ("", 200)


def test_readyz():
assert pytest.fa.readyz() == ("", 200)
with pytest.fa.APP.test_request_context():
assert pytest.fa.readyz() == ("", 200)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -231,8 +233,9 @@ def test_error_handler(

mocker.patch("connaisseur.flask_application.__admit", return_value=True)
mock_function = mocker.patch(**function)
client = pytest.fa.APP.test_client()
mock_request_data = fix.get_admreq("deployments")
response = client.post("/mutate", json=mock_request_data)
assert response.status_code == 500
assert response.get_data().decode() == err
with pytest.fa.APP.test_request_context():
client = pytest.fa.APP.test_client()
mock_request_data = fix.get_admreq("deployments")
response = client.post("/mutate", json=mock_request_data)
assert response.status_code == 500
assert response.get_data().decode() == err

0 comments on commit fa39d81

Please sign in to comment.