Skip to content

Commit

Permalink
WIP Assert Aerie host version
Browse files Browse the repository at this point in the history
  • Loading branch information
cartermak committed Aug 30, 2024
1 parent 2414cc9 commit f0c9ef1
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/aerie_cli/aerie_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

from attrs import define, field

COMPATIBLE_AERIE_VERSIONS = [
"2.18.0"
]


def process_gateway_response(resp: requests.Response) -> dict:
"""Throw a RuntimeError if the Gateway response is malformed or contains errors
Expand All @@ -18,15 +22,12 @@ def process_gateway_response(resp: requests.Response) -> dict:
dict: Contents of response JSON
"""
if not resp.ok:
raise RuntimeError(f"Bad response from Aerie Gateway.")
raise RuntimeError("Bad response from Aerie Gateway")

try:
resp_json = resp.json()
except requests.exceptions.JSONDecodeError:
raise RuntimeError(f"Failed to get response JSON")

if "success" in resp_json.keys() and not resp_json["success"]:
raise RuntimeError(f"Aerie Gateway request was not successful")
raise RuntimeError("Bad response from Aerie Gateway")

return resp_json

Expand Down Expand Up @@ -278,6 +279,28 @@ def authenticate(self, username: str, password: str = None):
if not self.check_auth():
raise RuntimeError(f"Failed to open session")

def check_aerie_version(self) -> None:
"""Assert that the Aerie host is a compatible version
Raises a `RuntimeError` if the host appears to be incompatible.
"""

resp = self.session.get(self.gateway_url + "/version")

try:
resp_json = process_gateway_response(resp)
host_version = resp_json["version"]
except (RuntimeError, KeyError):
# If the Gateway responded, the route doesn't exist
if resp.text and "Aerie Gateway" in resp.text:
raise RuntimeError("Unknown Aerie host version")

# Otherwise, it could just be a failed connection
raise

if host_version not in COMPATIBLE_AERIE_VERSIONS:
raise RuntimeError(f"Incompatible Aerie version: {host_version}")


@define
class ExternalAuthConfiguration:
Expand Down
70 changes: 70 additions & 0 deletions tests/unit_tests/test_aerie_host.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from typing import Dict
import pytest
import requests

from aerie_cli.aerie_host import AerieHost, COMPATIBLE_AERIE_VERSIONS


class MockResponse:
def __init__(self, json: Dict, text: str, ok: bool) -> None:
self.json_data = json
self.text = text
self.ok = ok

def json(self) -> Dict:
if self.json_data is None:
raise requests.exceptions.JSONDecodeError("", "", 0)
return self.json_data


class MockSession:

def __init__(self, mock_response: MockResponse) -> None:
self.mock_response = mock_response

def get(self, *args, **kwargs) -> MockResponse:
return self.mock_response

def post(self, *args, **kwargs) -> MockResponse:
return self.mock_response


def get_mock_aerie_host(json: Dict = None, text: str = None, ok: bool = True) -> AerieHost:
mock_response = MockResponse(json, text, ok)
mock_session = MockSession(mock_response)
return AerieHost("", "", mock_session)


def test_check_aerie_version():
aerie_host = get_mock_aerie_host(
json={"version": COMPATIBLE_AERIE_VERSIONS[0]})

aerie_host.check_aerie_version()


def test_check_invalid_version():
aerie_host = get_mock_aerie_host(json={"version": "1.0.0"})

with pytest.raises(RuntimeError) as e:
aerie_host.check_aerie_version()

assert "Incompatible Aerie version: 1.0.0" in str(e.value)


def test_no_version_endpoint():
aerie_host = get_mock_aerie_host(text="blah Aerie Gateway blah", ok=True)

with pytest.raises(RuntimeError) as e:
aerie_host.check_aerie_version()

assert "Unknown Aerie host version" in str(e.value)


def test_version_broken_gateway():
aerie_host = get_mock_aerie_host(
text="502 Bad Gateway or something", ok=True)

with pytest.raises(RuntimeError) as e:
aerie_host.check_aerie_version()

assert "Bad response from Aerie Gateway" in str(e.value)

0 comments on commit f0c9ef1

Please sign in to comment.