Skip to content

Commit

Permalink
Enforce ssh url when running pytest on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
actualwitch committed Nov 8, 2023
1 parent 0b859e7 commit cc29bd1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
10 changes: 10 additions & 0 deletions src/autometrics/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import pytest

in_ci = os.getenv("CI", "false") == "true"


@pytest.fixture()
def reset_environment(monkeypatch):
Expand All @@ -15,3 +18,10 @@ def reset_environment(monkeypatch):
importlib.reload(tracker)
# we'll set debug to true to ensure calling init more than once will fail whole test
monkeypatch.setenv("AUTOMETRICS_DEBUG", "true")

# github ci uses https so for tests to pass we force ssh url
if in_ci:
monkeypatch.setenv(
"AUTOMETRICS_REPOSITORY_URL",
"[email protected]:autometrics-dev/autometrics-py.git",
)
1 change: 0 additions & 1 deletion src/autometrics/test_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def test_init():
"""Test that the default settings are set correctly"""
init()
settings = get_settings()
print(settings)
assert settings == {
"histogram_buckets": [
0.005,
Expand Down
42 changes: 18 additions & 24 deletions src/autometrics/tracker/test_concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,23 @@ async def test_concurrency_tracking_prometheus(monkeypatch):
)


# NOTE - Gauges are not supported by the OTEL collector (yet)
# https://github.com/open-telemetry/opentelemetry-python/pull/3306
#
# @pytest.mark.asyncio
# async def test_concurrency_tracking_opentelemetry(monkeypatch):
# # HACK - We need to set the tracker explicitly here, instead of using `init_tracker`
# # because the library was already initialized with the OpenTelemetry tracker
# set_tracker(TrackerType.OPENTELEMETRY)
@pytest.mark.asyncio
async def test_concurrency_tracking_opentelemetry(monkeypatch):
init(tracker="opentelemetry")

# # Create a 200ms async task
# loop = asyncio.get_event_loop()
# task = loop.create_task(sleep(0.2))
# Create a 200ms async task
loop = asyncio.get_event_loop()
task = loop.create_task(sleep(0.2))

# # Await a separate 100ms async task.
# # This way, the 200ms task will still running once this task is done.
# # We have to do this to ensure that the 200ms task is kicked off before we call `generate_latest`
# await sleep(0.1)
# blob = generate_latest()
# await task
# assert blob is not None
# data = blob.decode("utf-8")
# print(data)
# assert (
# f"""# TYPE function_calls_concurrent gauge\nfunction_calls_concurrent{{function="sleep",module="test_concurrency"}} 1.0"""
# in data
# )
# Await a separate 100ms async task.
# This way, the 200ms task will still running once this task is done.
# We have to do this to ensure that the 200ms task is kicked off before we call `generate_latest`
await sleep(0.1)
blob = generate_latest()
await task
assert blob is not None
data = blob.decode("utf-8")
assert (
f"""# TYPE function_calls_concurrent gauge\nfunction_calls_concurrent{{function="sleep",module="autometrics.tracker.test_concurrency",service_name="autometrics"}} 1.0"""
in data
)
11 changes: 6 additions & 5 deletions src/autometrics/tracker/test_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,24 @@ def test_init_prometheus_tracker_set_build_info(monkeypatch):
monkeypatch.delenv("AUTOMETRICS_VERSION", raising=False)
monkeypatch.delenv("AUTOMETRICS_COMMIT", raising=False)
monkeypatch.delenv("AUTOMETRICS_BRANCH", raising=False)
monkeypatch.delenv("AUTOMETRICS_TRACKER", raising=False)


def test_init_otel_tracker_set_build_info(monkeypatch):
"""
Test that init_tracker (for an OTEL tracker) calls set_build_info using env vars.
Note that the OTEL collector translates metrics to Prometheus.
"""
pytest.skip(
"Skipping test because OTEL collector does not create a gauge when it translates UpDownCounter to Prometheus"
)

commit = "a29a178"
version = "0.0.1"
branch = "main"
tracker = "opentelemetry"

monkeypatch.setenv("AUTOMETRICS_COMMIT", commit)
monkeypatch.setenv("AUTOMETRICS_VERSION", version)
monkeypatch.setenv("AUTOMETRICS_BRANCH", branch)
monkeypatch.setenv("AUTOMETRICS_TRACKER", tracker)
init()

otel_tracker = get_tracker()
Expand All @@ -87,9 +87,10 @@ def test_init_otel_tracker_set_build_info(monkeypatch):
assert blob is not None
data = blob.decode("utf-8")

prom_build_info = f"""build_info{{branch="{branch}",commit="{commit}",service_name="autometrics",version="{version}",service_name="autometrics"}} 1.0"""
assert prom_build_info in data
otel_build_info = f"""build_info{{branch="{branch}",commit="{commit}",repository_provider="github",repository_url="[email protected]:autometrics-dev/autometrics-py.git",service_name="autometrics",version="{version}"}} 1.0"""
assert otel_build_info in data

monkeypatch.delenv("AUTOMETRICS_VERSION", raising=False)
monkeypatch.delenv("AUTOMETRICS_COMMIT", raising=False)
monkeypatch.delenv("AUTOMETRICS_BRANCH", raising=False)
monkeypatch.delenv("AUTOMETRICS_TRACKER", raising=False)

0 comments on commit cc29bd1

Please sign in to comment.