Skip to content

Commit

Permalink
fix: component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
therightstuff committed Oct 11, 2023
1 parent 0fb3347 commit a3ad86c
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 122 deletions.
34 changes: 4 additions & 30 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import sys
import tempfile
import time
from src.test.test_utils.processes import kill_process
from typing import List, Optional, Union
from xml.etree import ElementTree

import nox
import requests
import yaml

from src.test.test_utils.processes import kill_process

# Ensure nox can load local packages
repo_dir = os.path.dirname(__file__)
if repo_dir not in sys.path:
Expand Down Expand Up @@ -408,20 +409,6 @@ def component_tests_attr_max_size(
session.install("-r", OTHER_REQUIREMENTS)

try:
session.run(
"sh",
"./scripts/start_uvicorn",
env={
"AUTOWRAPT_BOOTSTRAP": "lumigo_opentelemetry",
"LUMIGO_DEBUG_SPANDUMP": temp_file,
"OTEL_SERVICE_NAME": "app",
"OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT": "1",
},
external=True,
) # One happy day we will have https://github.com/wntrblm/nox/issues/198

wait_for_app_start()

session.run(
"pytest",
"--tb",
Expand All @@ -436,7 +423,7 @@ def component_tests_attr_max_size(
},
)
finally:
kill_process_and_clean_outputs(temp_file, "uvicorn", session)
clean_outputs(temp_file, session)


def component_tests_execution_tags(
Expand All @@ -454,19 +441,6 @@ def component_tests_execution_tags(
session.install("-r", OTHER_REQUIREMENTS)

try:
session.run(
"sh",
"./scripts/start_uvicorn",
env={
"AUTOWRAPT_BOOTSTRAP": "lumigo_opentelemetry",
"LUMIGO_DEBUG_SPANDUMP": temp_file,
"OTEL_SERVICE_NAME": "app",
},
external=True,
) # One happy day we will have https://github.com/wntrblm/nox/issues/198

wait_for_app_start()

session.run(
"pytest",
"--tb",
Expand All @@ -480,7 +454,7 @@ def component_tests_execution_tags(
},
)
finally:
kill_process_and_clean_outputs(temp_file, "uvicorn", session)
clean_outputs(temp_file, session)


@nox.session()
Expand Down
47 changes: 47 additions & 0 deletions src/test/components/tests/app_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os
import subprocess
import sys
from pathlib import Path
from test.test_utils.processes import kill_process


class FastApiSample(object):
def __init__(self):
cwd = Path(__file__).parent.parent
print(f"cwd = {cwd}")
env = {
**os.environ,
"AUTOWRAPT_BOOTSTRAP": "lumigo_opentelemetry",
"LUMIGO_DEBUG_SPANDUMP": os.environ["LUMIGO_DEBUG_SPANDUMP"],
"OTEL_SERVICE_NAME": "fastapi_test_app",
}
print(f"env = {env}")
venv_bin_path = Path(sys.executable).parent
print(f"venv_bin_path = {venv_bin_path}")
cmd = f". {venv_bin_path}/activate; uvicorn app:app --port 8000"
print(f"cmd = {cmd}")
self.process = subprocess.Popen(
cmd,
cwd=cwd,
env=env,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
is_app_running = False
for line in self.process.stderr:
if "Uvicorn running" in str(line):
print(f"FastApiSample app: {line}")
is_app_running = True
break
print(line)
if not is_app_running:
raise Exception("FastApiSample app failed to start")

def __enter__(self):
return self

def __exit__(self, *args):
# because we need a shell to run uvicorn we need to kill multiple processs,
# but not the process group because that includes the test process as well
kill_process("uvicorn")
45 changes: 25 additions & 20 deletions src/test/components/tests/test_attr_max_size.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
import unittest
from test.test_utils.span_exporter import wait_for_exporter
from test.test_utils.spans_parser import SpansContainer

import requests

from .app_runner import FastApiSample


class TestLargeSpans(unittest.TestCase):
def test_large_span_attribute_size_max_size_env_var_was_set(self):
response = requests.get("http://localhost:8020/invoke-requests-large-response")
response.raise_for_status()

body = response.json()
with FastApiSample():
response = requests.get(
"http://localhost:8020/invoke-requests-large-response"
)
response.raise_for_status()

assert body is not None
body = response.json()

wait_for_exporter()
assert body is not None

spans_container = SpansContainer.get_spans_from_file()
self.assertEqual(4, len(spans_container.spans))
spans_container = SpansContainer.get_spans_from_file(
wait_time_sec=10, expected_span_count=4
)
self.assertEqual(4, len(spans_container.spans))

# assert root
root = spans_container.get_first_root()
self.assertIsNotNone(root)
root_attributes = root["attributes"]
self.assertEqual(root_attributes["http.status_code"], 200)
# assert root
root = spans_container.get_first_root()
self.assertIsNotNone(root)
root_attributes = root["attributes"]
self.assertEqual(root_attributes["http.status_code"], 200)

self.assert_attribute_length(root_attributes, 1)
self.assert_attribute_length(root_attributes, 1)

# assert child spans
children = spans_container.get_non_internal_children()
self.assertEqual(1, len(children))
child_attributes = children[0]["attributes"]
self.assert_attribute_length(child_attributes, 1)
# assert child spans
children = spans_container.get_non_internal_children()
self.assertEqual(1, len(children))
child_attributes = children[0]["attributes"]
self.assert_attribute_length(child_attributes, 1)

def assert_attribute_length(self, child_attributes: dict, length: int) -> None:
for k, v in child_attributes.items():
if isinstance(v, str):
assert len(v) == length
assert len(v) == length
61 changes: 35 additions & 26 deletions src/test/components/tests/test_execution_tags.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
import unittest
from test.test_utils.span_exporter import wait_for_exporter
from test.test_utils.spans_parser import SpansContainer

import requests

from .app_runner import FastApiSample


class TestExecutionTags(unittest.TestCase):
def test_execution_tag(self):
response = requests.get("http://localhost:8020/invoke-request")
response.raise_for_status()

body = response.json()

assert body is not None

wait_for_exporter()

spans_container = SpansContainer.get_spans_from_file()
self.assertEqual(4, len(spans_container.spans))

# assert root
root = spans_container.get_first_root()
self.assertIsNotNone(root)
root_attributes = root["attributes"]
self.assertEqual(
type(root_attributes["lumigo.execution_tags.response_len"]), int
)
self.assertEqual(type(root_attributes["lumigo.execution_tags.response"]), str)
self.assertEqual(root_attributes["lumigo.execution_tags.app"], True)
self.assertEqual(
root_attributes["lumigo.execution_tags.app.response"], "success"
)
self.assertEqual(root_attributes["lumigo.execution_tags.foo"], ["bar", "baz"])
with FastApiSample():
response = requests.get("http://localhost:8020/invoke-request")
response.raise_for_status()

body = response.json()

assert body is not None

spans_container = SpansContainer.get_spans_from_file(
wait_time_sec=10, expected_span_count=4
)
self.assertEqual(4, len(spans_container.spans))

# assert root
root = spans_container.get_first_root()
self.assertIsNotNone(root)
root_attributes = root["attributes"]
self.assertEqual(
type(root_attributes["lumigo.execution_tags.response_len"]), int
)
self.assertEqual(
type(root_attributes["lumigo.execution_tags.response"]), str
)
self.assertEqual(root_attributes["lumigo.execution_tags.app"], True)
self.assertEqual(
root_attributes["lumigo.execution_tags.app.response"], "success"
)
self.assertEqual(
root_attributes["lumigo.execution_tags.foo"], ["bar", "baz"]
)
self.assertEqual(
root_attributes["lumigo.execution_tags.foo"], ["bar", "baz"]
)
47 changes: 47 additions & 0 deletions src/test/integration/fastapi/tests/app_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os
import subprocess
import sys
from pathlib import Path
from test.test_utils.processes import kill_process


class FastApiSample(object):
def __init__(self):
cwd = Path(__file__).parent.parent
print(f"cwd = {cwd}")
env = {
**os.environ,
"AUTOWRAPT_BOOTSTRAP": "lumigo_opentelemetry",
"LUMIGO_DEBUG_SPANDUMP": os.environ["LUMIGO_DEBUG_SPANDUMP"],
"OTEL_SERVICE_NAME": "fastapi_test_app",
}
print(f"env = {env}")
venv_bin_path = Path(sys.executable).parent
print(f"venv_bin_path = {venv_bin_path}")
cmd = f". {venv_bin_path}/activate; uvicorn app:app --port 8000"
print(f"cmd = {cmd}")
self.process = subprocess.Popen(
cmd,
cwd=cwd,
env=env,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
is_app_running = False
for line in self.process.stderr:
if "Uvicorn running" in str(line):
print(f"FastApiSample app: {line}")
is_app_running = True
break
print(line)
if not is_app_running:
raise Exception("FastApiSample app failed to start")

def __enter__(self):
return self

def __exit__(self, *args):
# because we need a shell to run uvicorn we need to kill multiple processs,
# but not the process group because that includes the test process as well
kill_process("uvicorn")
47 changes: 1 addition & 46 deletions src/test/integration/fastapi/tests/test_fastapi.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,10 @@
import os
import subprocess
import sys
import unittest
from pathlib import Path
from test.test_utils.processes import kill_process
from test.test_utils.span_exporter import wait_for_exporter
from test.test_utils.spans_parser import SpansContainer

import requests


class FastApiSample(object):
def __init__(self):
cwd = Path(__file__).parent.parent
print(f"cwd = {cwd}")
env = {
**os.environ,
"AUTOWRAPT_BOOTSTRAP": "lumigo_opentelemetry",
"LUMIGO_DEBUG_SPANDUMP": os.environ["LUMIGO_DEBUG_SPANDUMP"],
"OTEL_SERVICE_NAME": "fastapi_test_app",
}
print(f"env = {env}")
venv_bin_path = Path(sys.executable).parent
print(f"venv_bin_path = {venv_bin_path}")
cmd = f". {venv_bin_path}/activate; uvicorn app:app --port 8000"
print(f"cmd = {cmd}")
self.process = subprocess.Popen(
cmd,
cwd=cwd,
env=env,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
is_app_running = False
for line in self.process.stderr:
if "Uvicorn running" in str(line):
print(f"FastApiSample app: {line}")
is_app_running = True
break
print(line)
if not is_app_running:
raise Exception("FastApiSample app failed to start")

def __enter__(self):
return self

def __exit__(self, *args):
# because we need a shell to run uvicorn we need to kill multiple processs,
# but not the process group because that includes the test process as well
kill_process("uvicorn")
from .app_runner import FastApiSample


class TestFastApiSpans(unittest.TestCase):
Expand Down

0 comments on commit a3ad86c

Please sign in to comment.