From 168ce97ee3f8afa9cb335da66dd7ae90521a3c34 Mon Sep 17 00:00:00 2001 From: Hector Hernandez Guzman Date: Tue, 21 Apr 2020 10:51:47 -0700 Subject: [PATCH 1/4] Add better reliability in E2E tests --- .../tests/mysql/test_mysql_functional.py | 12 ++++++---- .../tests/postgres/test_psycopg_functional.py | 12 ++++++---- .../tests/pymongo/test_pymongo_functional.py | 24 ++++++++++++------- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py b/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py index 305bea00bf1..b00ed08a366 100644 --- a/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py +++ b/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py @@ -85,18 +85,22 @@ def validate_spans(self): def test_execute(self): """Should create a child span for execute """ - with self._tracer.start_as_current_span("rootSpan"): + with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( + Exception + ): self._cursor.execute("CREATE TABLE IF NOT EXISTS test (id INT)") - self.validate_spans() + self.validate_spans() def test_executemany(self): """Should create a child span for executemany """ - with self._tracer.start_as_current_span("rootSpan"): + with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( + Exception + ): data = ["1", "2", "3"] stmt = "INSERT INTO test (id) VALUES (%s)" self._cursor.executemany(stmt, data) - self.validate_spans() + self.validate_spans() def test_callproc(self): """Should create a child span for callproc diff --git a/ext/opentelemetry-ext-docker-tests/tests/postgres/test_psycopg_functional.py b/ext/opentelemetry-ext-docker-tests/tests/postgres/test_psycopg_functional.py index d58e332c1bc..392bc71b048 100644 --- a/ext/opentelemetry-ext-docker-tests/tests/postgres/test_psycopg_functional.py +++ b/ext/opentelemetry-ext-docker-tests/tests/postgres/test_psycopg_functional.py @@ -90,20 +90,24 @@ def validate_spans(self): def test_execute(self): """Should create a child span for execute method """ - with self._tracer.start_as_current_span("rootSpan"): + with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( + Exception + ): self._cursor.execute( "CREATE TABLE IF NOT EXISTS test (id integer)" ) - self.validate_spans() + self.validate_spans() def test_executemany(self): """Should create a child span for executemany """ - with self._tracer.start_as_current_span("rootSpan"): + with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( + Exception + ): data = ("1", "2", "3") stmt = "INSERT INTO test (id) VALUES (%s)" self._cursor.executemany(stmt, data) - self.validate_spans() + self.validate_spans() def test_callproc(self): """Should create a child span for callproc diff --git a/ext/opentelemetry-ext-docker-tests/tests/pymongo/test_pymongo_functional.py b/ext/opentelemetry-ext-docker-tests/tests/pymongo/test_pymongo_functional.py index 7b018dab253..10e6a434157 100644 --- a/ext/opentelemetry-ext-docker-tests/tests/pymongo/test_pymongo_functional.py +++ b/ext/opentelemetry-ext-docker-tests/tests/pymongo/test_pymongo_functional.py @@ -78,31 +78,39 @@ def validate_spans(self): def test_insert(self): """Should create a child span for insert """ - with self._tracer.start_as_current_span("rootSpan"): + with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( + Exception + ): self._collection.insert_one( {"name": "testName", "value": "testValue"} ) - self.validate_spans() + self.validate_spans() def test_update(self): """Should create a child span for update """ - with self._tracer.start_as_current_span("rootSpan"): + with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( + Exception + ): self._collection.update_one( {"name": "testName"}, {"$set": {"value": "someOtherValue"}} ) - self.validate_spans() + self.validate_spans() def test_find(self): """Should create a child span for find """ - with self._tracer.start_as_current_span("rootSpan"): + with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( + Exception + ): self._collection.find_one() - self.validate_spans() + self.validate_spans() def test_delete(self): """Should create a child span for delete """ - with self._tracer.start_as_current_span("rootSpan"): + with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( + Exception + ): self._collection.delete_one({"name": "testName"}) - self.validate_spans() + self.validate_spans() From e2a42f527ec2f1db42bf871893bd48d509d755de Mon Sep 17 00:00:00 2001 From: Hector Hernandez Guzman Date: Tue, 21 Apr 2020 14:08:31 -0700 Subject: [PATCH 2/4] Ignore exceptions from external libraries --- .../tests/mysql/test_mysql_functional.py | 34 +++++++---- .../tests/postgres/test_psycopg_functional.py | 34 ++++++----- .../tests/pymongo/test_pymongo_functional.py | 56 +++++++++++-------- 3 files changed, 74 insertions(+), 50 deletions(-) diff --git a/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py b/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py index b00ed08a366..f61ad64777b 100644 --- a/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py +++ b/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging import os import time import unittest @@ -26,6 +27,8 @@ InMemorySpanExporter, ) +logger = logging.getLogger(__name__) + MYSQL_USER = os.getenv("MYSQL_USER ", "testuser") MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD ", "testpassword") MYSQL_HOST = os.getenv("MYSQL_HOST ", "localhost") @@ -33,6 +36,7 @@ MYSQL_DB_NAME = os.getenv("MYSQL_DB_NAME ", "opentelemetry-tests") +# pylint: disable=broad-except class TestFunctionalMysql(unittest.TestCase): @classmethod def setUpClass(cls): @@ -85,22 +89,28 @@ def validate_spans(self): def test_execute(self): """Should create a child span for execute """ - with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( - Exception - ): - self._cursor.execute("CREATE TABLE IF NOT EXISTS test (id INT)") - self.validate_spans() + try: + with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( + Exception + ): + self._cursor.execute("CREATE TABLE IF NOT EXISTS test (id INT)") + except Exception as ex: + logger.warning("Failed to execute with mysql. %s", str(ex)) + self.validate_spans() def test_executemany(self): """Should create a child span for executemany """ - with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( - Exception - ): - data = ["1", "2", "3"] - stmt = "INSERT INTO test (id) VALUES (%s)" - self._cursor.executemany(stmt, data) - self.validate_spans() + try: + with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( + Exception + ): + data = ["1", "2", "3"] + stmt = "INSERT INTO test (id) VALUES (%s)" + self._cursor.executemany(stmt, data) + except Exception as ex: + logger.warning("Failed to executemany with mysql. %s", str(ex)) + self.validate_spans() def test_callproc(self): """Should create a child span for callproc diff --git a/ext/opentelemetry-ext-docker-tests/tests/postgres/test_psycopg_functional.py b/ext/opentelemetry-ext-docker-tests/tests/postgres/test_psycopg_functional.py index 392bc71b048..a911b7c370c 100644 --- a/ext/opentelemetry-ext-docker-tests/tests/postgres/test_psycopg_functional.py +++ b/ext/opentelemetry-ext-docker-tests/tests/postgres/test_psycopg_functional.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging import os import time import unittest @@ -26,6 +27,8 @@ InMemorySpanExporter, ) +logger = logging.getLogger(__name__) + POSTGRES_HOST = os.getenv("POSTGRESQL_HOST ", "localhost") POSTGRES_PORT = int(os.getenv("POSTGRESQL_PORT ", "5432")) POSTGRES_DB_NAME = os.getenv("POSTGRESQL_DB_NAME ", "opentelemetry-tests") @@ -33,6 +36,7 @@ POSTGRES_USER = os.getenv("POSTGRESQL_HOST ", "testuser") +# pylint: disable=broad-except class TestFunctionalPsycopg(unittest.TestCase): @classmethod def setUpClass(cls): @@ -90,24 +94,26 @@ def validate_spans(self): def test_execute(self): """Should create a child span for execute method """ - with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( - Exception - ): - self._cursor.execute( - "CREATE TABLE IF NOT EXISTS test (id integer)" - ) - self.validate_spans() + try: + with self._tracer.start_as_current_span("rootSpan"): + self._cursor.execute( + "CREATE TABLE IF NOT EXISTS test (id integer)" + ) + except Exception as ex: + logger.warning("Failed to execute with pyscopg2. %s", str(ex)) + self.validate_spans() def test_executemany(self): """Should create a child span for executemany """ - with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( - Exception - ): - data = ("1", "2", "3") - stmt = "INSERT INTO test (id) VALUES (%s)" - self._cursor.executemany(stmt, data) - self.validate_spans() + try: + with self._tracer.start_as_current_span("rootSpan"): + data = ("1", "2", "3") + stmt = "INSERT INTO test2 (id) VALUES (%s)" + self._cursor.executemany(stmt, data) + except Exception as ex: + logger.warning("Failed to executemany with pyscopg2. %s", str(ex)) + self.validate_spans() def test_callproc(self): """Should create a child span for callproc diff --git a/ext/opentelemetry-ext-docker-tests/tests/pymongo/test_pymongo_functional.py b/ext/opentelemetry-ext-docker-tests/tests/pymongo/test_pymongo_functional.py index 10e6a434157..0efc20283f8 100644 --- a/ext/opentelemetry-ext-docker-tests/tests/pymongo/test_pymongo_functional.py +++ b/ext/opentelemetry-ext-docker-tests/tests/pymongo/test_pymongo_functional.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging import os import typing import unittest @@ -26,12 +27,15 @@ InMemorySpanExporter, ) +logger = logging.getLogger(__name__) + 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" +# pylint: disable=broad-except class TestFunctionalPymongo(unittest.TestCase): @classmethod def setUpClass(cls): @@ -78,39 +82,43 @@ def validate_spans(self): def test_insert(self): """Should create a child span for insert """ - with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( - Exception - ): - self._collection.insert_one( - {"name": "testName", "value": "testValue"} - ) - self.validate_spans() + try: + with self._tracer.start_as_current_span("rootSpan"): + self._collection.insert_one( + {"name": "testName", "value": "testValue"} + ) + except Exception as ex: + logger.warning("Failed to insert with pymongo. %s", str(ex)) + self.validate_spans() def test_update(self): """Should create a child span for update """ - with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( - Exception - ): - self._collection.update_one( - {"name": "testName"}, {"$set": {"value": "someOtherValue"}} - ) - self.validate_spans() + try: + with self._tracer.start_as_current_span("rootSpan"): + self._collection.update_one( + {"name": "testName"}, {"$set": {"value": "someOtherValue"}} + ) + except Exception as ex: + logger.warning("Failed to update with pymongo. %s", str(ex)) + self.validate_spans() def test_find(self): """Should create a child span for find """ - with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( - Exception - ): - self._collection.find_one() - self.validate_spans() + try: + with self._tracer.start_as_current_span("rootSpan"): + self._collection.find_one() + except Exception as ex: + logger.warning("Failed to find with pymongo. %s", str(ex)) + self.validate_spans() def test_delete(self): """Should create a child span for delete """ - with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( - Exception - ): - self._collection.delete_one({"name": "testName"}) - self.validate_spans() + try: + with self._tracer.start_as_current_span("rootSpan"): + self._collection.delete_one({"name": "testName"}) + except Exception as ex: + logger.warning("Failed to delete with pymongo. %s", str(ex)) + self.validate_spans() From cb8961e1777d1f0690c7a3ccb75e7a3fff222079 Mon Sep 17 00:00:00 2001 From: Hector Hernandez Guzman Date: Tue, 21 Apr 2020 14:23:38 -0700 Subject: [PATCH 3/4] Fixing lint --- .../tests/mysql/test_mysql_functional.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py b/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py index f61ad64777b..fc3aea03ad6 100644 --- a/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py +++ b/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py @@ -90,10 +90,12 @@ def test_execute(self): """Should create a child span for execute """ try: - with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( - Exception - ): - self._cursor.execute("CREATE TABLE IF NOT EXISTS test (id INT)") + with self._tracer.start_as_current_span( + "rootSpan" + ), self.assertRaises(Exception): + self._cursor.execute( + "CREATE TABLE IF NOT EXISTS test (id INT)" + ) except Exception as ex: logger.warning("Failed to execute with mysql. %s", str(ex)) self.validate_spans() @@ -102,9 +104,9 @@ def test_executemany(self): """Should create a child span for executemany """ try: - with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( - Exception - ): + with self._tracer.start_as_current_span( + "rootSpan" + ), self.assertRaises(Exception): data = ["1", "2", "3"] stmt = "INSERT INTO test (id) VALUES (%s)" self._cursor.executemany(stmt, data) From a9a56f353f09f312e7e3a69984dbe74a51279c43 Mon Sep 17 00:00:00 2001 From: Hector Hernandez Guzman Date: Tue, 21 Apr 2020 17:41:07 -0700 Subject: [PATCH 4/4] WIP --- .../tests/mysql/test_mysql_functional.py | 20 +++++------- .../tests/postgres/test_psycopg_functional.py | 21 ++++++------ .../tests/pymongo/test_pymongo_functional.py | 32 +++++++++---------- 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py b/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py index 46c86dc7633..c22d208cc70 100644 --- a/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py +++ b/ext/opentelemetry-ext-docker-tests/tests/mysql/test_mysql_functional.py @@ -78,29 +78,25 @@ def validate_spans(self): def test_execute(self): """Should create a child span for execute """ - try: - with self._tracer.start_as_current_span( - "rootSpan" - ), self.assertRaises(Exception): + with self._tracer.start_as_current_span("rootSpan"): + try: self._cursor.execute( "CREATE TABLE IF NOT EXISTS test (id INT)" ) - except Exception as ex: - logger.warning("Failed to execute with mysql. %s", str(ex)) + except Exception as ex: + logger.warning("Failed to execute with mysql. %s", str(ex)) self.validate_spans() def test_executemany(self): """Should create a child span for executemany """ - try: - with self._tracer.start_as_current_span( - "rootSpan" - ), self.assertRaises(Exception): + with self._tracer.start_as_current_span("rootSpan"): + try: data = ["1", "2", "3"] stmt = "INSERT INTO test (id) VALUES (%s)" self._cursor.executemany(stmt, data) - except Exception as ex: - logger.warning("Failed to executemany with mysql. %s", str(ex)) + except Exception as ex: + logger.warning("Failed to executemany with mysql. %s", str(ex)) self.validate_spans() def test_callproc(self): diff --git a/ext/opentelemetry-ext-docker-tests/tests/postgres/test_psycopg_functional.py b/ext/opentelemetry-ext-docker-tests/tests/postgres/test_psycopg_functional.py index fc72e8b9b8e..b3dada3a16d 100644 --- a/ext/opentelemetry-ext-docker-tests/tests/postgres/test_psycopg_functional.py +++ b/ext/opentelemetry-ext-docker-tests/tests/postgres/test_psycopg_functional.py @@ -83,25 +83,28 @@ def validate_spans(self): def test_execute(self): """Should create a child span for execute method """ - try: - with self._tracer.start_as_current_span("rootSpan"): + + with self._tracer.start_as_current_span("rootSpan"): + try: self._cursor.execute( "CREATE TABLE IF NOT EXISTS test (id integer)" ) - except Exception as ex: - logger.warning("Failed to execute with pyscopg2. %s", str(ex)) + except Exception as ex: + logger.warning("Failed to execute with pyscopg2. %s", str(ex)) self.validate_spans() def test_executemany(self): """Should create a child span for executemany """ - try: - with self._tracer.start_as_current_span("rootSpan"): + with self._tracer.start_as_current_span("rootSpan"): + try: data = ("1", "2", "3") - stmt = "INSERT INTO test2 (id) VALUES (%s)" + stmt = "INSERT INTO test (id) VALUES (%s)" self._cursor.executemany(stmt, data) - except Exception as ex: - logger.warning("Failed to executemany with pyscopg2. %s", str(ex)) + except Exception as ex: + logger.warning( + "Failed to executemany with pyscopg2. %s", str(ex) + ) self.validate_spans() def test_callproc(self): diff --git a/ext/opentelemetry-ext-docker-tests/tests/pymongo/test_pymongo_functional.py b/ext/opentelemetry-ext-docker-tests/tests/pymongo/test_pymongo_functional.py index 55ad47012f2..91c53359c87 100644 --- a/ext/opentelemetry-ext-docker-tests/tests/pymongo/test_pymongo_functional.py +++ b/ext/opentelemetry-ext-docker-tests/tests/pymongo/test_pymongo_functional.py @@ -70,43 +70,43 @@ def validate_spans(self): def test_insert(self): """Should create a child span for insert """ - try: - with self._tracer.start_as_current_span("rootSpan"): + with self._tracer.start_as_current_span("rootSpan"): + try: self._collection.insert_one( {"name": "testName", "value": "testValue"} ) - except Exception as ex: - logger.warning("Failed to insert with pymongo. %s", str(ex)) + except Exception as ex: + logger.warning("Failed to insert with pymongo. %s", str(ex)) self.validate_spans() def test_update(self): """Should create a child span for update """ - try: - with self._tracer.start_as_current_span("rootSpan"): + with self._tracer.start_as_current_span("rootSpan"): + try: self._collection.update_one( {"name": "testName"}, {"$set": {"value": "someOtherValue"}} ) - except Exception as ex: - logger.warning("Failed to update with pymongo. %s", str(ex)) + except Exception as ex: + logger.warning("Failed to update with pymongo. %s", str(ex)) self.validate_spans() def test_find(self): """Should create a child span for find """ - try: - with self._tracer.start_as_current_span("rootSpan"): + with self._tracer.start_as_current_span("rootSpan"): + try: self._collection.find_one() - except Exception as ex: - logger.warning("Failed to find with pymongo. %s", str(ex)) + except Exception as ex: + logger.warning("Failed to find with pymongo. %s", str(ex)) self.validate_spans() def test_delete(self): """Should create a child span for delete """ - try: - with self._tracer.start_as_current_span("rootSpan"): + with self._tracer.start_as_current_span("rootSpan"): + try: self._collection.delete_one({"name": "testName"}) - except Exception as ex: - logger.warning("Failed to delete with pymongo. %s", str(ex)) + except Exception as ex: + logger.warning("Failed to delete with pymongo. %s", str(ex)) self.validate_spans()