This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-python.git


The following commit(s) were added to refs/heads/master by this push:
     new aaa8522  Chore: add missing `log` method and simplify test codes (#34)
aaa8522 is described below

commit aaa852238136065c0bad6f74859589385f5278e8
Author: kezhenxu94 <[email protected]>
AuthorDate: Sun Jul 5 21:20:08 2020 +0800

    Chore: add missing `log` method and simplify test codes (#34)
---
 skywalking/trace/span/__init__.py           |  5 +++++
 tests/plugin/__init__.py                    | 16 ++++++++++++++--
 tests/plugin/sw_flask/test_flask.py         | 11 +++++------
 tests/plugin/sw_http/test_http.py           | 13 ++-----------
 tests/plugin/sw_http_wsgi/test_http_wsgi.py | 13 ++-----------
 tests/plugin/sw_requests/test_request.py    | 13 ++-----------
 6 files changed, 30 insertions(+), 41 deletions(-)

diff --git a/skywalking/trace/span/__init__.py 
b/skywalking/trace/span/__init__.py
index ec77a9b..87ba9ec 100644
--- a/skywalking/trace/span/__init__.py
+++ b/skywalking/trace/span/__init__.py
@@ -81,6 +81,11 @@ class Span(ABC):
         ])]
         return self
 
+    def log(self, ex: Exception) -> 'Span':
+        self.error_occurred = True
+        self.logs.append(Log(items=LogItem(key='Traceback', val=str(ex))))
+        return self
+
     def tag(self, tag: Tag) -> 'Span':
         if not tag.overridable:
             self.tags.append(deepcopy(tag))
diff --git a/tests/plugin/__init__.py b/tests/plugin/__init__.py
index 5bb74c8..e6d912d 100644
--- a/tests/plugin/__init__.py
+++ b/tests/plugin/__init__.py
@@ -14,11 +14,12 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-
+import inspect
 import os
 import unittest
 from abc import ABC
 from collections import namedtuple
+from os.path import dirname
 
 import requests
 from requests import Response
@@ -32,6 +33,13 @@ class BasePluginTest(unittest.TestCase, ABC):
     compose = None  # type: DockerCompose
 
     @classmethod
+    def setUpClass(cls):
+        cls.compose = DockerCompose(filepath=dirname(inspect.getfile(cls)))
+        cls.compose.start()
+
+        cls.compose.wait_for(cls.url(cls.collector_address()))
+
+    @classmethod
     def tearDownClass(cls):
         cls.compose.stop()
 
@@ -57,8 +65,12 @@ class BasePluginTest(unittest.TestCase, ABC):
         # type: () -> ServicePort
         return ServicePort(service='collector', port='12800')
 
-    def validate(self, expected_file_name):
+    def validate(self, expected_file_name=None):
         # type: (str) -> Response
+
+        if expected_file_name is None:
+            expected_file_name = 
os.path.join(dirname(inspect.getfile(self.__class__)), 'expected.data.yml')
+
         with open(expected_file_name) as expected_data_file:
             response = requests.post(
                 url=self.__class__.url(self.__class__.collector_address(), 
path='/dataValidate'),
diff --git a/tests/plugin/sw_flask/test_flask.py 
b/tests/plugin/sw_flask/test_flask.py
index dfeaaa6..8187451 100644
--- a/tests/plugin/sw_flask/test_flask.py
+++ b/tests/plugin/sw_flask/test_flask.py
@@ -14,11 +14,10 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-
-import os
+import inspect
 import time
 import unittest
-from os.path import abspath, dirname
+from os.path import dirname
 
 from testcontainers.compose import DockerCompose
 
@@ -28,15 +27,15 @@ from tests.plugin import BasePluginTest
 class TestPlugin(BasePluginTest):
     @classmethod
     def setUpClass(cls):
-        cls.compose = DockerCompose(filepath=dirname(abspath(__file__)))
+        cls.compose = DockerCompose(filepath=dirname(inspect.getfile(cls)))
         cls.compose.start()
 
         cls.compose.wait_for(cls.url(('consumer', '9090'), 'users'))
 
-    def test_request_plugin(self):
+    def test_plugin(self):
         time.sleep(3)
 
-        
self.validate(expected_file_name=os.path.join(dirname(abspath(__file__)), 
'expected.data.yml'))
+        self.validate()
 
 
 if __name__ == '__main__':
diff --git a/tests/plugin/sw_http/test_http.py 
b/tests/plugin/sw_http/test_http.py
index 856db04..05b42d2 100644
--- a/tests/plugin/sw_http/test_http.py
+++ b/tests/plugin/sw_http/test_http.py
@@ -15,31 +15,22 @@
 # limitations under the License.
 #
 
-import os
 import time
 import unittest
-from os.path import abspath, dirname
 
 import requests
-from testcontainers.compose import DockerCompose
 
 from tests.plugin import BasePluginTest
 
 
 class TestPlugin(BasePluginTest):
-    @classmethod
-    def setUpClass(cls):
-        cls.compose = DockerCompose(filepath=dirname(abspath(__file__)))
-        cls.compose.start()
 
-        cls.compose.wait_for(cls.url(cls.collector_address()))
-
-    def test_request_plugin(self):
+    def test_plugin(self):
         print('traffic: ', requests.post(url=self.url(('consumer', '9090'))))
 
         time.sleep(3)
 
-        
self.validate(expected_file_name=os.path.join(dirname(abspath(__file__)), 
'expected.data.yml'))
+        self.validate()
 
 
 if __name__ == '__main__':
diff --git a/tests/plugin/sw_http_wsgi/test_http_wsgi.py 
b/tests/plugin/sw_http_wsgi/test_http_wsgi.py
index 856db04..05b42d2 100644
--- a/tests/plugin/sw_http_wsgi/test_http_wsgi.py
+++ b/tests/plugin/sw_http_wsgi/test_http_wsgi.py
@@ -15,31 +15,22 @@
 # limitations under the License.
 #
 
-import os
 import time
 import unittest
-from os.path import abspath, dirname
 
 import requests
-from testcontainers.compose import DockerCompose
 
 from tests.plugin import BasePluginTest
 
 
 class TestPlugin(BasePluginTest):
-    @classmethod
-    def setUpClass(cls):
-        cls.compose = DockerCompose(filepath=dirname(abspath(__file__)))
-        cls.compose.start()
 
-        cls.compose.wait_for(cls.url(cls.collector_address()))
-
-    def test_request_plugin(self):
+    def test_plugin(self):
         print('traffic: ', requests.post(url=self.url(('consumer', '9090'))))
 
         time.sleep(3)
 
-        
self.validate(expected_file_name=os.path.join(dirname(abspath(__file__)), 
'expected.data.yml'))
+        self.validate()
 
 
 if __name__ == '__main__':
diff --git a/tests/plugin/sw_requests/test_request.py 
b/tests/plugin/sw_requests/test_request.py
index 856db04..05b42d2 100644
--- a/tests/plugin/sw_requests/test_request.py
+++ b/tests/plugin/sw_requests/test_request.py
@@ -15,31 +15,22 @@
 # limitations under the License.
 #
 
-import os
 import time
 import unittest
-from os.path import abspath, dirname
 
 import requests
-from testcontainers.compose import DockerCompose
 
 from tests.plugin import BasePluginTest
 
 
 class TestPlugin(BasePluginTest):
-    @classmethod
-    def setUpClass(cls):
-        cls.compose = DockerCompose(filepath=dirname(abspath(__file__)))
-        cls.compose.start()
 
-        cls.compose.wait_for(cls.url(cls.collector_address()))
-
-    def test_request_plugin(self):
+    def test_plugin(self):
         print('traffic: ', requests.post(url=self.url(('consumer', '9090'))))
 
         time.sleep(3)
 
-        
self.validate(expected_file_name=os.path.join(dirname(abspath(__file__)), 
'expected.data.yml'))
+        self.validate()
 
 
 if __name__ == '__main__':

Reply via email to