You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2020/06/12 14:36:58 UTC

[skywalking-python] branch master updated: Add verifications of `refs` in plugin tests (#12)

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 83b5197  Add verifications of `refs` in plugin tests (#12)
83b5197 is described below

commit 83b519781fb6c71ae525b98bfa24996ffce5e915
Author: kezhenxu94 <ke...@apache.org>
AuthorDate: Fri Jun 12 22:36:52 2020 +0800

    Add verifications of `refs` in plugin tests (#12)
---
 docs/PluginTest.md                                 |  6 +--
 tests/plugin/{http => sw_http}/__init__.py         |  0
 tests/plugin/{http => sw_http}/docker-compose.yml  | 26 ++++++++++-
 .../{sw_requests => sw_http}/expected.data.yml     | 51 +++++++++++++++++-----
 .../services/__init__.py}                          | 21 ---------
 .../provider.py => sw_http/services/consumer.py}   | 19 +++++---
 .../plugin/{http => sw_http/services}/provider.py  |  0
 tests/plugin/{http => sw_http}/test_http.py        |  2 +-
 tests/plugin/sw_requests/docker-compose.yml        | 26 ++++++++++-
 tests/plugin/sw_requests/expected.data.yml         | 51 +++++++++++++++++-----
 .../{http => sw_requests/services}/__init__.py     |  1 -
 .../{provider.py => services/consumer.py}          | 17 +++-----
 .../{http => sw_requests/services}/provider.py     |  0
 tests/plugin/sw_requests/test_request.py           |  2 +-
 14 files changed, 156 insertions(+), 66 deletions(-)

diff --git a/docs/PluginTest.md b/docs/PluginTest.md
index a10ea80..f267157 100644
--- a/docs/PluginTest.md
+++ b/docs/PluginTest.md
@@ -27,6 +27,6 @@ they match. This can be done through the `/dataValidate` of the mock collector,
 
 If we want to test the plugin for the built-in library `http`, we will:
 
-1. Build a tested service, which sets up an http server by `http` library, and exposes an http endpoint to be triggered in the test codes, say `/trigger`, take [this provider service](../tests/plugin/http/provider.py) as example.
-1. Compose a `docker-compose.yml` file, orchestrating the service built in step 1 and the mock collector, take [this `docker-compose.yml`](../tests/plugin/http/docker-compose.yml) as example.
-1. Write test codes to trigger the endpoint int step 1, and send the expected data file to the mock collector to verify, take [this test](../tests/plugin/http/test_request.py) as example.
+1. Build a tested service, which sets up an http server by `http` library, and exposes an http endpoint to be triggered in the test codes, say `/trigger`, take [this provider service](../tests/plugin/sw_http/services/provider.py) as example.
+1. Compose a `docker-compose.yml` file, orchestrating the service built in step 1 and the mock collector, take [this `docker-compose.yml`](../tests/plugin/sw_http/docker-compose.yml) as example.
+1. Write test codes to trigger the endpoint int step 1, and send the expected data file to the mock collector to verify, take [this test](../tests/plugin/sw_http/test_http.py) as example.
diff --git a/tests/plugin/http/__init__.py b/tests/plugin/sw_http/__init__.py
similarity index 100%
copy from tests/plugin/http/__init__.py
copy to tests/plugin/sw_http/__init__.py
diff --git a/tests/plugin/http/docker-compose.yml b/tests/plugin/sw_http/docker-compose.yml
similarity index 69%
rename from tests/plugin/http/docker-compose.yml
rename to tests/plugin/sw_http/docker-compose.yml
index c43fee3..3f0d305 100644
--- a/tests/plugin/http/docker-compose.yml
+++ b/tests/plugin/sw_http/docker-compose.yml
@@ -42,13 +42,37 @@ services:
     ports:
       - 9091:9091
     volumes:
-      - ./provider.py:/app/provider.py
+      - ./services/provider.py:/app/provider.py
     environment:
       SW_AGENT_COLLECTOR_BACKEND_SERVICES: collector:19876
     command: ['python3', '/app/provider.py']
     depends_on:
       collector:
         condition: service_healthy
+    healthcheck:
+      test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/9091"]
+      interval: 5s
+      timeout: 60s
+      retries: 120
+
+  consumer:
+    build:
+      context: ../../../
+      dockerfile: tests/plugin/docker/Dockerfile.agent
+    networks:
+      - beyond
+    ports:
+      - 9090:9090
+    volumes:
+      - ./services/consumer.py:/app/consumer.py
+    environment:
+      SW_AGENT_COLLECTOR_BACKEND_SERVICES: collector:19876
+    command: ['python3', '/app/consumer.py']
+    depends_on:
+      collector:
+        condition: service_healthy
+      provider:
+        condition: service_healthy
 
 networks:
   beyond:
diff --git a/tests/plugin/sw_requests/expected.data.yml b/tests/plugin/sw_http/expected.data.yml
similarity index 57%
copy from tests/plugin/sw_requests/expected.data.yml
copy to tests/plugin/sw_http/expected.data.yml
index 4cfacd5..b0f2208 100644
--- a/tests/plugin/sw_requests/expected.data.yml
+++ b/tests/plugin/sw_http/expected.data.yml
@@ -21,33 +21,64 @@ segmentItems:
     segments:
       - segmentId: not null
         spans:
-          - operationName: /apache/skywalking
+          - operationName: /users
+            operationId: 0
+            parentSpanId: -1
+            spanId: 0
+            spanLayer: Http
+            tags:
+              - key: http.method
+                value: POST
+            refs:
+              - parentEndpoint: /
+                networkAddress: provider:9091
+                refType: CrossProcess
+                parentSpanId: 1
+                parentTraceSegmentId: not null
+                parentServiceInstance: not null
+                parentService: consumer
+                traceId: not null
+            startTime: gt 0
+            endTime: gt 0
+            componentId: 7000
+            spanType: Entry
+            peer: not null
+            skipAnalysis: false
+  - serviceName: consumer
+    segmentSize: 1
+    segments:
+      - segmentId: not null
+        spans:
+          - operationName: /users
             operationId: 0
             parentSpanId: 0
             spanId: 1
             spanLayer: Http
+            tags:
+              - key: http.method
+                value: POST
+              - key: url
+                value: http://provider:9091/users
+              - key: status.code
+                value: '200'
             startTime: gt 0
             endTime: gt 0
             componentId: 7000
-            isError: false
             spanType: Exit
-            peer: not null
+            peer: provider:9091
             skipAnalysis: false
-            tags:
-              - {key: http.method, value: GET}
-              - {key: url, value: https://github.com/apache/skywalking}
-              - {key: status.code, value: '200'}
           - operationName: /
             operationId: 0
             parentSpanId: -1
             spanId: 0
             spanLayer: Http
+            tags:
+              - key: http.method
+                value: POST
             startTime: gt 0
             endTime: gt 0
             componentId: 7000
-            isError: false
             spanType: Entry
             peer: not null
             skipAnalysis: false
-            tags:
-              - {key: http.method, value: POST}
+
diff --git a/tests/plugin/http/expected.data.yml b/tests/plugin/sw_http/services/__init__.py
similarity index 60%
rename from tests/plugin/http/expected.data.yml
rename to tests/plugin/sw_http/services/__init__.py
index 644f42b..b1312a0 100644
--- a/tests/plugin/http/expected.data.yml
+++ b/tests/plugin/sw_http/services/__init__.py
@@ -14,24 +14,3 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-
-segmentItems:
-  - serviceName: provider
-    segmentSize: 1
-    segments:
-      - segmentId: not null
-        spans:
-          - operationName: /
-            operationId: 0
-            parentSpanId: -1
-            spanId: 0
-            spanLayer: Http
-            startTime: gt 0
-            endTime: gt 0
-            componentId: 7000
-            isError: false
-            spanType: Entry
-            peer: not null
-            skipAnalysis: false
-            tags:
-              - {key: http.method, value: POST}
diff --git a/tests/plugin/http/provider.py b/tests/plugin/sw_http/services/consumer.py
similarity index 70%
copy from tests/plugin/http/provider.py
copy to tests/plugin/sw_http/services/consumer.py
index 7af884a..43f60bd 100644
--- a/tests/plugin/http/provider.py
+++ b/tests/plugin/sw_http/services/consumer.py
@@ -15,12 +15,12 @@
 # limitations under the License.
 #
 
-import time
+from urllib import request
 
 from skywalking import agent, config
 
 if __name__ == '__main__':
-    config.service_name = 'provider'
+    config.service_name = 'consumer'
     config.logging_level = 'DEBUG'
     agent.start()
 
@@ -28,16 +28,21 @@ if __name__ == '__main__':
     from http.server import BaseHTTPRequestHandler
 
     class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
-
         def do_POST(self):
-            time.sleep(0.5)
             self.send_response(200)
-            self.send_header('Content-Type', 'application/json')
+            self.send_header('Content-Type', 'application/json; charset=utf-8')
             self.end_headers()
-            self.wfile.write('{"song": "Despacito", "artist": "Luis Fonsi"}'.encode('ascii'))
 
-    PORT = 9091
+            data = '{"name": "whatever"}'.encode('utf8')
+            req = request.Request('http://provider:9091/users')
+            req.add_header('Content-Type', 'application/json; charset=utf-8')
+            req.add_header('Content-Length', str(len(data)))
+            with request.urlopen(req, data):
+                self.wfile.write(data)
+
+    PORT = 9090
     Handler = SimpleHTTPRequestHandler
 
     with socketserver.TCPServer(("", PORT), Handler) as httpd:
+        print("serving at port", PORT)
         httpd.serve_forever()
diff --git a/tests/plugin/http/provider.py b/tests/plugin/sw_http/services/provider.py
similarity index 100%
copy from tests/plugin/http/provider.py
copy to tests/plugin/sw_http/services/provider.py
diff --git a/tests/plugin/http/test_http.py b/tests/plugin/sw_http/test_http.py
similarity index 94%
rename from tests/plugin/http/test_http.py
rename to tests/plugin/sw_http/test_http.py
index 160879e..eacaa98 100644
--- a/tests/plugin/http/test_http.py
+++ b/tests/plugin/sw_http/test_http.py
@@ -35,7 +35,7 @@ class TestRequestPlugin(BasePluginTest):
         cls.compose.wait_for(cls.url(cls.collector_address()))
 
     def test_request_plugin(self):
-        print('traffic: ', requests.post(url=self.url(('provider', '9091'))))
+        print('traffic: ', requests.post(url=self.url(('consumer', '9090'))))
 
         time.sleep(3)
 
diff --git a/tests/plugin/sw_requests/docker-compose.yml b/tests/plugin/sw_requests/docker-compose.yml
index c43fee3..3f0d305 100644
--- a/tests/plugin/sw_requests/docker-compose.yml
+++ b/tests/plugin/sw_requests/docker-compose.yml
@@ -42,13 +42,37 @@ services:
     ports:
       - 9091:9091
     volumes:
-      - ./provider.py:/app/provider.py
+      - ./services/provider.py:/app/provider.py
     environment:
       SW_AGENT_COLLECTOR_BACKEND_SERVICES: collector:19876
     command: ['python3', '/app/provider.py']
     depends_on:
       collector:
         condition: service_healthy
+    healthcheck:
+      test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/9091"]
+      interval: 5s
+      timeout: 60s
+      retries: 120
+
+  consumer:
+    build:
+      context: ../../../
+      dockerfile: tests/plugin/docker/Dockerfile.agent
+    networks:
+      - beyond
+    ports:
+      - 9090:9090
+    volumes:
+      - ./services/consumer.py:/app/consumer.py
+    environment:
+      SW_AGENT_COLLECTOR_BACKEND_SERVICES: collector:19876
+    command: ['python3', '/app/consumer.py']
+    depends_on:
+      collector:
+        condition: service_healthy
+      provider:
+        condition: service_healthy
 
 networks:
   beyond:
diff --git a/tests/plugin/sw_requests/expected.data.yml b/tests/plugin/sw_requests/expected.data.yml
index 4cfacd5..b0f2208 100644
--- a/tests/plugin/sw_requests/expected.data.yml
+++ b/tests/plugin/sw_requests/expected.data.yml
@@ -21,33 +21,64 @@ segmentItems:
     segments:
       - segmentId: not null
         spans:
-          - operationName: /apache/skywalking
+          - operationName: /users
+            operationId: 0
+            parentSpanId: -1
+            spanId: 0
+            spanLayer: Http
+            tags:
+              - key: http.method
+                value: POST
+            refs:
+              - parentEndpoint: /
+                networkAddress: provider:9091
+                refType: CrossProcess
+                parentSpanId: 1
+                parentTraceSegmentId: not null
+                parentServiceInstance: not null
+                parentService: consumer
+                traceId: not null
+            startTime: gt 0
+            endTime: gt 0
+            componentId: 7000
+            spanType: Entry
+            peer: not null
+            skipAnalysis: false
+  - serviceName: consumer
+    segmentSize: 1
+    segments:
+      - segmentId: not null
+        spans:
+          - operationName: /users
             operationId: 0
             parentSpanId: 0
             spanId: 1
             spanLayer: Http
+            tags:
+              - key: http.method
+                value: POST
+              - key: url
+                value: http://provider:9091/users
+              - key: status.code
+                value: '200'
             startTime: gt 0
             endTime: gt 0
             componentId: 7000
-            isError: false
             spanType: Exit
-            peer: not null
+            peer: provider:9091
             skipAnalysis: false
-            tags:
-              - {key: http.method, value: GET}
-              - {key: url, value: https://github.com/apache/skywalking}
-              - {key: status.code, value: '200'}
           - operationName: /
             operationId: 0
             parentSpanId: -1
             spanId: 0
             spanLayer: Http
+            tags:
+              - key: http.method
+                value: POST
             startTime: gt 0
             endTime: gt 0
             componentId: 7000
-            isError: false
             spanType: Entry
             peer: not null
             skipAnalysis: false
-            tags:
-              - {key: http.method, value: POST}
+
diff --git a/tests/plugin/http/__init__.py b/tests/plugin/sw_requests/services/__init__.py
similarity index 99%
rename from tests/plugin/http/__init__.py
rename to tests/plugin/sw_requests/services/__init__.py
index 6222972..b1312a0 100644
--- a/tests/plugin/http/__init__.py
+++ b/tests/plugin/sw_requests/services/__init__.py
@@ -14,4 +14,3 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-
diff --git a/tests/plugin/sw_requests/provider.py b/tests/plugin/sw_requests/services/consumer.py
similarity index 80%
rename from tests/plugin/sw_requests/provider.py
rename to tests/plugin/sw_requests/services/consumer.py
index 36db31c..ff7ec9b 100644
--- a/tests/plugin/sw_requests/provider.py
+++ b/tests/plugin/sw_requests/services/consumer.py
@@ -15,34 +15,31 @@
 # limitations under the License.
 #
 
-import time
-
 import requests
 
 from skywalking import agent, config
 
 if __name__ == '__main__':
-    config.service_name = 'provider'
+    config.service_name = 'consumer'
     config.logging_level = 'DEBUG'
     agent.start()
 
     import socketserver
     from http.server import BaseHTTPRequestHandler
 
-    class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
 
+    class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
         def do_POST(self):
-            time.sleep(0.5)
-            requests.get("https://github.com/apache/skywalking")
             self.send_response(200)
-            self.send_header('Content-Type', 'application/json')
+            self.send_header('Content-Type', 'application/json; charset=utf-8')
             self.end_headers()
-            self.wfile.write('{"song": "Despacito", "artist": "Luis Fonsi"}'.encode('ascii'))
-
 
+            res = requests.post("http://provider:9091/users")
+            self.wfile.write(str(res.json()).encode('utf8'))
 
-    PORT = 9091
+    PORT = 9090
     Handler = SimpleHTTPRequestHandler
 
     with socketserver.TCPServer(("", PORT), Handler) as httpd:
+        print("serving at port", PORT)
         httpd.serve_forever()
diff --git a/tests/plugin/http/provider.py b/tests/plugin/sw_requests/services/provider.py
similarity index 100%
rename from tests/plugin/http/provider.py
rename to tests/plugin/sw_requests/services/provider.py
diff --git a/tests/plugin/sw_requests/test_request.py b/tests/plugin/sw_requests/test_request.py
index 160879e..eacaa98 100644
--- a/tests/plugin/sw_requests/test_request.py
+++ b/tests/plugin/sw_requests/test_request.py
@@ -35,7 +35,7 @@ class TestRequestPlugin(BasePluginTest):
         cls.compose.wait_for(cls.url(cls.collector_address()))
 
     def test_request_plugin(self):
-        print('traffic: ', requests.post(url=self.url(('provider', '9091'))))
+        print('traffic: ', requests.post(url=self.url(('consumer', '9090'))))
 
         time.sleep(3)