Skip to content

Commit

Permalink
bug-1887640: add "cloud" to /__version__ output
Browse files Browse the repository at this point in the history
This adds a "cloud" key to `/__version__` output so we can easily
determine which cloud the service is running in.
  • Loading branch information
willkg committed May 2, 2024
1 parent fa3d14c commit 1a674d2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
14 changes: 12 additions & 2 deletions antenna/health_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections import OrderedDict
import json
import logging
import os

import falcon
import markus
Expand Down Expand Up @@ -35,11 +36,20 @@ def __init__(self, basedir):
def on_get(self, req, resp):
"""Implement GET HTTP request."""
mymetrics.incr("version.count")
version_info = json.dumps(get_version_info(self.basedir))
version_info = get_version_info(self.basedir)
# FIXME(willkg): there's no cloud provider environment variable to use, so
# we'll cheat and look at whether there's a "gcs" in
# CRASHMOVER_CRASHSTORAGE_CLASS; this is termporary and we can remove it
# once we've finished the GCP migration
version_info["cloud"] = (
"GCP"
if "gcs" in os.environ.get("CRASHMOVER_CRASHSTORAGE_CLASS", "")
else "AWS"
)

resp.content_type = "application/json; charset=utf-8"
resp.status = falcon.HTTP_200
resp.text = version_info
resp.text = json.dumps(version_info)


class LBHeartbeatResource:
Expand Down
1 change: 0 additions & 1 deletion bin/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ echo ">>> pytest"

export PYTHONPATH=/app/:${PYTHONPATH:-}
PYTEST="$(which pytest)"
PYTHON="$(which python)"

# Wait for services to be ready (both have the same endpoint url)
urlwait "${CRASHMOVER_CRASHPUBLISH_ENDPOINT_URL}" 15
Expand Down
10 changes: 8 additions & 2 deletions systemtest/test_dockerflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ def test_version(self, baseurl):
data = resp.json()
assert isinstance(data, dict)
data_keys = list(sorted(data.keys()))
# It's empty in the local dev environment, but has 4 keys in the server
# It's got "cloud" in the local dev environment and 5 keys in a server
# environment
assert data_keys == [] or data_keys == ["build", "commit", "source", "version"]
assert data_keys == ["cloud"] or data_keys == [
"build",
"cloud",
"commit",
"source",
"version",
]

def test_heartbeat(self, baseurl):
resp = requests.get(baseurl + "__heartbeat__", timeout=5)
Expand Down
49 changes: 46 additions & 3 deletions tests/unittest/test_health_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

from contextlib import contextmanager
import json
import os


@contextmanager
def gcs_crashstorage_envvar():
# NOTE(willkg): we do this in a goofy way here because it means we don't actually
# have to set this in the environment which causes the app to fail at startup
# because the bucket doesn't exist
key = "CRASHMOVER_CRASHSTORAGE_CLASS"
os.environ[key] = "antenna.ext.gcs.crashstorage.GcsCrashStorage"

yield

del os.environ[key]


class TestHealthChecks:
def test_no_version(self, client, tmpdir):
Expand All @@ -10,9 +27,20 @@ def test_no_version(self, client, tmpdir):
client.rebuild_app({"BASEDIR": str(tmpdir)})

result = client.simulate_get("/__version__")
assert result.content == b"{}"
version_info = {"cloud": "AWS"}
assert json.loads(result.content) == version_info

def test_no_version_gcp(self, client, tmpdir):
# Set basedir here to tmpdir which we *know* doesn't have a
# version.json in it.
client.rebuild_app({"BASEDIR": str(tmpdir)})

with gcs_crashstorage_envvar():
result = client.simulate_get("/__version__")
version_info = {"cloud": "GCP"}
assert json.loads(result.content) == version_info

def test_version(self, client, tmpdir):
def test_version_aws(self, client, tmpdir):
client.rebuild_app({"BASEDIR": str(tmpdir)})

# NOTE(willkg): The actual version.json has other things in it,
Expand All @@ -22,7 +50,22 @@ def test_version(self, client, tmpdir):
version_path.write('{"commit": "ou812"}')

result = client.simulate_get("/__version__")
assert result.content == b'{"commit": "ou812"}'
version_info = {"commit": "ou812", "cloud": "AWS"}
assert json.loads(result.content) == version_info

def test_version_gcp(self, client, tmpdir):
client.rebuild_app({"BASEDIR": str(tmpdir)})

# NOTE(willkg): The actual version.json has other things in it,
# but our endpoint just spits out the file verbatim, so we
# can test with whatever.
version_path = tmpdir.join("/version.json")
version_path.write('{"commit": "ou812"}')

with gcs_crashstorage_envvar():
result = client.simulate_get("/__version__")
version_info = {"commit": "ou812", "cloud": "GCP"}
assert json.loads(result.content) == version_info

def test_lb_heartbeat(self, client):
resp = client.simulate_get("/__lbheartbeat__")
Expand Down

0 comments on commit 1a674d2

Please sign in to comment.