Skip to content

Commit

Permalink
ci(py): plugins testcase
Browse files Browse the repository at this point in the history
-  ci for #552
  • Loading branch information
eeliu committed Nov 17, 2023
1 parent 2313b34 commit 165a120
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 82 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: jwlawson/actions-setup-cmake@v1.6
with:
cmake-version: "3.16.x"
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
Expand All @@ -115,10 +112,17 @@ jobs:
# ref https://github.com/pypa/setuptools/issues/3198
pip install -e .
python -m unittest discover -s src/PY/test
- if: matrix.python-version == '3.8'
run : |
pip install -r plugins/PY/requirements.txt
python -m unittest discover -s plugins/PY/pinpointPy
python-plugins:
timeout-minutes: 20
needs: Python
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: start test environment
run: docker-compose -f "testapps/compose.yaml" run python-plugins
- name: Stop containers
if: always()
run: docker-compose -f "testapps/compose.yaml" down
Collector-agent:
strategy:
matrix:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging


class Test_Flask(unittest.TestCase):
class Test_case(unittest.TestCase):
@classmethod
def setUpClass(cls):
use_thread_local_context()
Expand Down
2 changes: 1 addition & 1 deletion plugins/PY/pinpointPy/libs/_MySQLdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def monkey_patch():
interceptor.enable()

except ImportError as e:
get_logger.debug(f'exception at {e}')
get_logger().debug(f'exception at {e}')


__all__ = ['monkey_patch']
Expand Down
21 changes: 13 additions & 8 deletions plugins/PY/pinpointPy/libs/_MysqlConnector/CMysqlPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,23 @@ class CMysqlPlugin(Common.PinTrace):
def __init__(self, name):
super().__init__(name)

# -> tuple[Any, tuple[Any, ...], dict[str, Any]]:
def onBefore(self, parentId, *args, **kwargs):
parentId, args, kwargs = super().onBefore(parentId, *args, **kwargs)
###############################################################
trace_id, args, kwargs = super().onBefore(parentId, *args, **kwargs)
pinpoint.add_trace_header(
Defines.PP_INTERCEPTOR_NAME, self.getUniqueName(), parentId)
Defines.PP_INTERCEPTOR_NAME, self.getUniqueName(), trace_id)
pinpoint.add_trace_header(
Defines.PP_SERVER_TYPE, Defines.PP_MYSQL, parentId)
pinpoint.add_trace_header(Defines.PP_SQL_FORMAT, args[1], parentId)
###############################################################
Defines.PP_SERVER_TYPE, Defines.PP_MYSQL, trace_id)
pinpoint.add_trace_header(Defines.PP_SQL_FORMAT, args[1], trace_id)
import sys
if 'unittest' in sys.modules.keys():
from mysql.connector.cursor_cext import CMySQLCursor
if isinstance(args[0], CMySQLCursor):
cursor = args[0]
cursor._pinpoint_ = True
dst = self.get_cursor_host(args[0])
pinpoint.add_trace_header(Defines.PP_DESTINATION, dst, parentId)
return args, kwargs
pinpoint.add_trace_header(Defines.PP_DESTINATION, dst, trace_id)
return trace_id, args, kwargs

def onEnd(self, traceId, ret):
super().onEnd(traceId, ret)
Expand Down
8 changes: 4 additions & 4 deletions plugins/PY/pinpointPy/libs/_MysqlConnector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
def monkey_patch():
try:
from mysql.connector.cursor import MySQLCursor, MySQLCursorPrepared
# from mysql.connector.cursor_cext import CMySQLCursor, CMySQLCursorPrepared
from mysql.connector.cursor_cext import CMySQLCursor, CMySQLCursorPrepared
from .MysqlPlugin import MysqlPlugin
# from .CMysqlPlugin import CMysqlPlugin
from .CMysqlPlugin import CMysqlPlugin

Interceptors = [
Interceptor(MySQLCursor, 'execute', MysqlPlugin),
Interceptor(MySQLCursorPrepared, 'execute', MysqlPlugin),
# Interceptor(CMySQLCursor, 'execute', CMysqlPlugin),
# Interceptor(CMySQLCursorPrepared, 'execute', CMysqlPlugin),
Interceptor(CMySQLCursor, 'execute', CMysqlPlugin),
Interceptor(CMySQLCursorPrepared, 'execute', CMysqlPlugin),
]
for interceptor in Interceptors:
interceptor.enable()
Expand Down
1 change: 1 addition & 0 deletions plugins/PY/pinpointPy/libs/_MysqlConnector/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def test_query(self):

# Select the employees getting a raise
curA.execute(query, (date(2000, 1, 1), date(2000, 12, 31)))
print(curA)
self.assertTrue(curA._pinpoint_)
now = datetime.now()
tomorrow = now + timedelta(1)
Expand Down
2 changes: 1 addition & 1 deletion plugins/PY/pinpointPy/libs/_requests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def monkey_patch():
for interceptor in Interceptors:
interceptor.enable()

except ImportError:
except ImportError as e:
get_logger().info(f'exception at {e}')


Expand Down
6 changes: 3 additions & 3 deletions plugins/PY/pinpointPy/libs/_requests/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def setUpClass(cls):
@PinTransaction("testcase", GenTestHeader())
def test_post(self):
import requests
url = 'http://httpbin:8999/anything'
url = 'http://httpbin/anything'
proto = {'a': 'proto'}
body = requests.post(url, json=proto)
httpbin = create_http_bin_response(body.text)
Expand All @@ -24,14 +24,14 @@ def test_post(self):
@PinTransaction("testcase", GenTestHeader())
def test_get(self):
import requests
body = requests.get('http://httpbin:8999/anything')
body = requests.get('http://httpbin/anything')
httpbin = create_http_bin_response(body.text)
self.assertIn(Defines.PP_HEADER_PINPOINT_SPANID, httpbin.headers)

@PinTransaction("testcase", GenTestHeader())
def test_patch(self):
import requests
body = requests.patch('http://httpbin:8999/anything')
body = requests.patch('http://httpbin/anything')
httpbin = create_http_bin_response(body.text)
self.assertIn(Defines.PP_HEADER_PINPOINT_SPANID, httpbin.headers)

Expand Down
4 changes: 2 additions & 2 deletions plugins/PY/pinpointPy/libs/_urllib/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setUpClass(cls):
@PinTransaction("testcase", GenTestHeader())
def test_str_url(self):
import urllib.request
with urllib.request.urlopen('http://httpbin:8999/anything') as response:
with urllib.request.urlopen('http://httpbin/anything') as response:
body = response.read()
httpbin = create_http_bin_response(body)
self.assertIn(Defines.PP_HEADER_PINPOINT_SPANID, httpbin.headers)
Expand All @@ -24,7 +24,7 @@ def test_req(self):
import urllib.parse
import urllib.request

url = 'http://httpbin:8999/anything'
url = 'http://httpbin/anything'
user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
values = {'name': 'Michael Foord',
'location': 'Northampton',
Expand Down
2 changes: 1 addition & 1 deletion plugins/PY/readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Update requirements.txt

```
pip freeze requirements.txt
pip freeze >requirements.txt
```
51 changes: 6 additions & 45 deletions plugins/PY/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,51 +1,12 @@
annotated-types==0.6.0
anyio==3.7.1
asgiref==3.7.2
astor==0.8.1
async-timeout==4.0.3
backports.zoneinfo==0.2.1
blinker==1.6.3
bottle==0.12.25
build==1.0.3
certifi==2023.7.22
charset-normalizer==3.3.0
click==8.1.7
coverage==7.3.2
Django==4.2.7
djangorestframework==3.14.0
docopt==0.6.2
exceptiongroup==1.1.3
fastapi==0.104.0
fastapi==0.104.1
flask==3.0.0
h11==0.14.0
httpcore==0.18.0
httpx==0.25.0
idna==3.4
importlib-metadata==6.8.0
iniconfig==2.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
mysql-connector-repackaged==0.3.1
packaging==23.2
# -e git+https://github.com/eeliu/pinpoint-c-agent.git@0ae7a82e2d394938942073b226f4febd4c5f92da#egg=pinpointPy
pluggy==1.3.0
pydantic==2.4.2
pydantic-core==2.10.1
httpx==0.25.1
mysql-connector-python==8.2.0
pymongo==4.6.0
PyMySQL==1.1.0
pyproject-hooks==1.0.0
pytest==7.4.2
pytz==2023.3.post1
redis==5.0.1
requests==2.31.0
rfc3986==1.5.0
sniffio==1.3.0
sqlparse==0.4.4
starlette==0.27.0
SQLAlchemy==2.0.23
starlette-context==0.3.6
tomli==2.0.1
typing-extensions==4.8.0
urllib3==2.0.7
werkzeug==3.0.1
yarg==0.1.9
zipp==3.17.0
mysqlclient==2.2.0
44 changes: 35 additions & 9 deletions testapps/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ services:
ports:
- 6389:6379
healthcheck:
test: [ "CMD", "redis-cli", "--raw", "incr", "ping" ]
test: [ "CMD", "redis-cli", "ping" ]
interval: 5s
timeout: 10s
retries: 100
timeout: 1s
retries: 1

testapp-flask:
container_name: flask
Expand Down Expand Up @@ -105,12 +105,15 @@ services:
- 8188:8000

httpbin:
image: kennethreitz/httpbin:latest
build:
dockerfile: testapps/httpbin.dockerfile
context: ../
restart: always
# container_name: httpbin
# hostname: httpbin
# ports:
# - 8999:80
healthcheck:
test: curl -f http://localhost
interval: 5s
timeout: 10s
retries: 50

dev-collector:
restart: always
Expand All @@ -129,4 +132,27 @@ services:
- PP_Log_Level=DEBUG
- PP_ADDRESS=0.0.0.0@10000
ports:
- 10000:10000
- 10000:10000

mongodb:
image: 'mongo'
restart: always
hostname: mongodb
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/test --quiet
# ports:
# - 27017:27017

python-plugins:
build:
dockerfile: testapps/python-plugins.dockerfile
context: ../
profiles:
- testing
depends_on:
redis:
condition: service_healthy
httpbin:
condition: service_healthy
mongodb:
condition: service_healthy
3 changes: 3 additions & 0 deletions testapps/httpbin.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM kennethreitz/httpbin:latest
USER root
RUN apt update && apt install -y curl
14 changes: 14 additions & 0 deletions testapps/python-plugins.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM python:3.8

COPY setup.py /pinpoint-c-agent/setup.py
COPY common/ /pinpoint-c-agent/common
COPY README /pinpoint-c-agent/README
COPY plugins/PY /pinpoint-c-agent/plugins/PY
COPY src/PY /pinpoint-c-agent/src/PY


RUN pip install -r /pinpoint-c-agent/plugins/PY/requirements.txt
RUN cd /pinpoint-c-agent && pip install -e .
CMD ["python","-m","unittest","discover","-s","/pinpoint-c-agent/plugins/PY/pinpointPy/"]
# CMD [ "python","-m","unittest","pinpointPy.libs._MysqlConnector.test_case.Test_Case" ]
# CMD [ "bash" ]

0 comments on commit 165a120

Please sign in to comment.