You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2020/07/05 12:53:46 UTC

[skywalking-python] 01/01: Chore: add missing `log` method and simplify test codes

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

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

commit 3defedcd63a1c18bbfc8e32f7a0578e151c1b736
Author: kezhenxu94 <ke...@163.com>
AuthorDate: Sun Jul 5 20:53:20 2020 +0800

    Chore: add missing `log` method and simplify test codes
---
 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, 28 insertions(+), 43 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..fa60b02 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, abspath
 
 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..185efdd 100644
--- a/tests/plugin/sw_flask/test_flask.py
+++ b/tests/plugin/sw_flask/test_flask.py
@@ -15,12 +15,8 @@
 # limitations under the License.
 #
 
-import os
 import time
 import unittest
-from os.path import abspath, dirname
-
-from testcontainers.compose import DockerCompose
 
 from tests.plugin import BasePluginTest
 
@@ -28,15 +24,14 @@ from tests.plugin import BasePluginTest
 class TestPlugin(BasePluginTest):
     @classmethod
     def setUpClass(cls):
-        cls.compose = DockerCompose(filepath=dirname(abspath(__file__)))
-        cls.compose.start()
+        BasePluginTest.setUpClass()
 
         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__':