-
Notifications
You must be signed in to change notification settings - Fork 660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding pymongo functional tests #340
Merged
c24t
merged 14 commits into
open-telemetry:master
from
hectorhdzg:hectorhdzg/pymongoFunc
Feb 7, 2020
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c4c81ba
Adding pymongo functional tests
hectorhdzg b8353da
Adding pymongo functional tests
hectorhdzg d416250
Merge branch 'hectorhdzg/pymongoFunc' of https://github.com/hectorhdz…
hectorhdzg 1c94a7d
Testing mongodb travis service
hectorhdzg 678ce57
Run docker locally only
hectorhdzg dc093e9
WIP
hectorhdzg d021fd8
Using docker compose to start containers
hectorhdzg beb8d72
Merging with latest
hectorhdzg 8426325
Fixing lint
hectorhdzg 6527d40
Adding docker-tests in envList
hectorhdzg 967f5a3
Adding docker-tests in python 3.7
hectorhdzg 6d16d9a
Updating pymongo tests net field names
hectorhdzg 3a902e1
Fix Lint
hectorhdzg 176cb73
Addressing comments
hectorhdzg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
version: '3' | ||
|
||
services: | ||
otmongo: | ||
ports: | ||
- "27017:27017" | ||
image: mongo:latest |
108 changes: 108 additions & 0 deletions
108
ext/opentelemetry-ext-docker-tests/tests/pymongo/test_pymongo_functional.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Copyright 2020, OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
import typing | ||
import unittest | ||
|
||
from pymongo import MongoClient | ||
|
||
from opentelemetry import trace as trace_api | ||
from opentelemetry.ext.pymongo import trace_integration | ||
from opentelemetry.sdk.trace import Span, Tracer, TracerSource | ||
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor | ||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import ( | ||
InMemorySpanExporter, | ||
) | ||
|
||
MONGODB_HOST = os.getenv("MONGODB_HOST ", "localhost") | ||
MONGODB_PORT = int(os.getenv("MONGODB_PORT ", "27017")) | ||
MONGODB_DB_NAME = os.getenv("MONGODB_DB_NAME ", "opentelemetry-tests") | ||
MONGODB_COLLECTION_NAME = "test" | ||
|
||
|
||
class TestFunctionalPymongo(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls._tracer_source = TracerSource() | ||
cls._tracer = Tracer(cls._tracer_source, None) | ||
cls._span_exporter = InMemorySpanExporter() | ||
cls._span_processor = SimpleExportSpanProcessor(cls._span_exporter) | ||
cls._tracer_source.add_span_processor(cls._span_processor) | ||
trace_integration(cls._tracer) | ||
client = MongoClient( | ||
MONGODB_HOST, MONGODB_PORT, serverSelectionTimeoutMS=2000 | ||
) | ||
db = client[MONGODB_DB_NAME] | ||
cls._collection = db[MONGODB_COLLECTION_NAME] | ||
|
||
def setUp(self): | ||
self._span_exporter.clear() | ||
|
||
def validate_spans(self): | ||
spans = self._span_exporter.get_finished_spans() | ||
self.assertEqual(len(spans), 2) | ||
for span in spans: | ||
if span.name == "rootSpan": | ||
root_span = span | ||
else: | ||
pymongo_span = span | ||
self.assertIsInstance(span.start_time, int) | ||
self.assertIsInstance(span.end_time, int) | ||
self.assertIsNot(root_span, None) | ||
self.assertIsNot(pymongo_span, None) | ||
self.assertIsNotNone(pymongo_span.parent) | ||
self.assertEqual(pymongo_span.parent.name, root_span.name) | ||
self.assertIs(pymongo_span.kind, trace_api.SpanKind.CLIENT) | ||
self.assertEqual( | ||
pymongo_span.attributes["db.instance"], MONGODB_DB_NAME | ||
) | ||
self.assertEqual( | ||
pymongo_span.attributes["net.peer.name"], MONGODB_HOST | ||
) | ||
self.assertEqual( | ||
pymongo_span.attributes["net.peer.port"], MONGODB_PORT | ||
) | ||
|
||
def test_insert(self): | ||
"""Should create a child span for insert | ||
""" | ||
with self._tracer.start_as_current_span("rootSpan"): | ||
self._collection.insert_one( | ||
{"name": "testName", "value": "testValue"} | ||
) | ||
self.validate_spans() | ||
|
||
def test_update(self): | ||
"""Should create a child span for update | ||
""" | ||
with self._tracer.start_as_current_span("rootSpan"): | ||
self._collection.update_one( | ||
{"name": "testName"}, {"$set": {"value": "someOtherValue"}} | ||
) | ||
self.validate_spans() | ||
|
||
def test_find(self): | ||
"""Should create a child span for find | ||
""" | ||
with self._tracer.start_as_current_span("rootSpan"): | ||
self._collection.find_one() | ||
self.validate_spans() | ||
|
||
def test_delete(self): | ||
"""Should create a child span for delete | ||
""" | ||
with self._tracer.start_as_current_span("rootSpan"): | ||
self._collection.delete_one({"name": "testName"}) | ||
self.validate_spans() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By this point you require
pymongo_span
androot_span
to be defined. They may not be ifspans
only includes 2 spans whosename
attribute is"rootSpan"
or if it includes 2 spans whose name is not. If any of those happen, you'll get aNameError
in a test case when you want to get onlyAssertionError
s. Instead for looping throughspans
looking for spans whosename
attribute matches, it should be asserted thatspans
actually contains one span whosename
attribute is"rootSpan"
and another one whosename
attribute is not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The actual test I wanted to include is that rootSpan is parent of other spans created, that is why I'm saving the reference for it so I can assert later, the check of an extra span being created in pymongo is already taken care in self.assertEqual(len(spans), 2) because all tests start with a rootSpan only, are you suggesting to add more check regarding these spans?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, what I am pointing out is that your test case requires
self._span_exporter.get_finished_spans()
to deliver you 2 spans, one named"rootSpan"
and other not, and since it requires that, it is probably something that you want to specifically test.