Skip to content

Commit 57dfd86

Browse files
hectorhdzgtoumorokoshi
authored andcommitted
Add pymongo functional tests (open-telemetry#340)
1 parent 4d1ec47 commit 57dfd86

File tree

5 files changed

+141
-2
lines changed

5 files changed

+141
-2
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ python:
1414
# allow_failures:
1515
# - python: '3.8-dev'
1616

17+
services:
18+
- docker
19+
1720
install:
1821
- pip install tox-travis
1922

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: '3'
2+
3+
services:
4+
otmongo:
5+
ports:
6+
- "27017:27017"
7+
image: mongo:latest
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Copyright 2020, OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import typing
17+
import unittest
18+
19+
from pymongo import MongoClient
20+
21+
from opentelemetry import trace as trace_api
22+
from opentelemetry.ext.pymongo import trace_integration
23+
from opentelemetry.sdk.trace import Span, Tracer, TracerSource
24+
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
25+
from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
26+
InMemorySpanExporter,
27+
)
28+
29+
MONGODB_HOST = os.getenv("MONGODB_HOST ", "localhost")
30+
MONGODB_PORT = int(os.getenv("MONGODB_PORT ", "27017"))
31+
MONGODB_DB_NAME = os.getenv("MONGODB_DB_NAME ", "opentelemetry-tests")
32+
MONGODB_COLLECTION_NAME = "test"
33+
34+
35+
class TestFunctionalPymongo(unittest.TestCase):
36+
@classmethod
37+
def setUpClass(cls):
38+
cls._tracer_source = TracerSource()
39+
cls._tracer = Tracer(cls._tracer_source, None)
40+
cls._span_exporter = InMemorySpanExporter()
41+
cls._span_processor = SimpleExportSpanProcessor(cls._span_exporter)
42+
cls._tracer_source.add_span_processor(cls._span_processor)
43+
trace_integration(cls._tracer)
44+
client = MongoClient(
45+
MONGODB_HOST, MONGODB_PORT, serverSelectionTimeoutMS=2000
46+
)
47+
db = client[MONGODB_DB_NAME]
48+
cls._collection = db[MONGODB_COLLECTION_NAME]
49+
50+
def setUp(self):
51+
self._span_exporter.clear()
52+
53+
def validate_spans(self):
54+
spans = self._span_exporter.get_finished_spans()
55+
self.assertEqual(len(spans), 2)
56+
for span in spans:
57+
if span.name == "rootSpan":
58+
root_span = span
59+
else:
60+
pymongo_span = span
61+
self.assertIsInstance(span.start_time, int)
62+
self.assertIsInstance(span.end_time, int)
63+
self.assertIsNot(root_span, None)
64+
self.assertIsNot(pymongo_span, None)
65+
self.assertIsNotNone(pymongo_span.parent)
66+
self.assertEqual(pymongo_span.parent.name, root_span.name)
67+
self.assertIs(pymongo_span.kind, trace_api.SpanKind.CLIENT)
68+
self.assertEqual(
69+
pymongo_span.attributes["db.instance"], MONGODB_DB_NAME
70+
)
71+
self.assertEqual(
72+
pymongo_span.attributes["net.peer.name"], MONGODB_HOST
73+
)
74+
self.assertEqual(
75+
pymongo_span.attributes["net.peer.port"], MONGODB_PORT
76+
)
77+
78+
def test_insert(self):
79+
"""Should create a child span for insert
80+
"""
81+
with self._tracer.start_as_current_span("rootSpan"):
82+
self._collection.insert_one(
83+
{"name": "testName", "value": "testValue"}
84+
)
85+
self.validate_spans()
86+
87+
def test_update(self):
88+
"""Should create a child span for update
89+
"""
90+
with self._tracer.start_as_current_span("rootSpan"):
91+
self._collection.update_one(
92+
{"name": "testName"}, {"$set": {"value": "someOtherValue"}}
93+
)
94+
self.validate_spans()
95+
96+
def test_find(self):
97+
"""Should create a child span for find
98+
"""
99+
with self._tracer.start_as_current_span("rootSpan"):
100+
self._collection.find_one()
101+
self.validate_spans()
102+
103+
def test_delete(self):
104+
"""Should create a child span for delete
105+
"""
106+
with self._tracer.start_as_current_span("rootSpan"):
107+
self._collection.delete_one({"name": "testName"})
108+
self.validate_spans()

ext/opentelemetry-ext-pymongo/tests/test_pymongo_integration.py renamed to ext/opentelemetry-ext-pymongo/tests/test_pymongo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from opentelemetry.util import time_ns
2121

2222

23-
class TestPymongoIntegration(unittest.TestCase):
23+
class TestPymongo(unittest.TestCase):
2424
def test_trace_integration(self):
2525
mock_register = mock.Mock()
2626
patch = mock.patch(

tox.ini

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ envlist =
1515
py37-tracecontext
1616
py37-{mypy,mypyinstalled}
1717
docs
18+
docker-tests
1819

1920
[travis]
2021
python =
21-
3.7: py37, lint, docs
22+
3.7: py37, lint, docs, docker-tests
2223

2324
[testenv]
2425
deps =
@@ -155,3 +156,23 @@ commands_pre =
155156

156157
commands =
157158
{toxinidir}/scripts/tracecontext-integration-test.sh
159+
160+
[testenv:docker-tests]
161+
deps =
162+
pytest
163+
docker-compose >= 1.25.2
164+
pymongo ~= 3.1
165+
166+
changedir =
167+
ext/opentelemetry-ext-docker-tests/tests
168+
169+
commands_pre =
170+
pip install -e {toxinidir}/opentelemetry-api \
171+
-e {toxinidir}/opentelemetry-sdk \
172+
-e {toxinidir}/ext/opentelemetry-ext-pymongo
173+
- docker-compose up -d
174+
commands =
175+
pytest {posargs}
176+
177+
commands_post =
178+
docker-compose down

0 commit comments

Comments
 (0)