Skip to content

Commit cf7c09b

Browse files
committed
feat: SDK Compliance
1 parent 7ac63e1 commit cf7c09b

File tree

6 files changed

+514
-0
lines changed

6 files changed

+514
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: SDK Compliance Tests
2+
3+
permissions:
4+
contents: read
5+
packages: read
6+
pull-requests: write
7+
8+
on:
9+
pull_request:
10+
push:
11+
branches:
12+
- master
13+
14+
jobs:
15+
compliance:
16+
name: PostHog SDK compliance tests
17+
continue-on-error: true
18+
uses: PostHog/posthog-sdk-test-harness/.github/workflows/[email protected]
19+
with:
20+
adapter-dockerfile: "Dockerfile"
21+
adapter-context: "sdk_compliance_adapter"
22+
test-harness-version: "latest"

sdk_compliance_adapter/Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM python:3.12-slim
2+
3+
WORKDIR /app
4+
5+
# Copy the SDK source code
6+
COPY . /app/sdk
7+
8+
# Install the SDK from source
9+
RUN cd /app/sdk && pip install --no-cache-dir -e .
10+
11+
# Install adapter dependencies
12+
RUN pip install --no-cache-dir flask python-dateutil
13+
14+
# Copy adapter code
15+
COPY sdk_compliance_adapter/adapter.py /app/adapter.py
16+
17+
# Expose port 8080
18+
EXPOSE 8080
19+
20+
# Set environment variables (will be overridden by docker run)
21+
ENV POSTHOG_HOST=http://localhost:8080
22+
ENV POSTHOG_API_KEY=test_key
23+
ENV PORT=8080
24+
25+
# Run the adapter
26+
CMD ["python", "/app/adapter.py"]

sdk_compliance_adapter/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# PostHog Python SDK Test Adapter
2+
3+
This adapter wraps the posthog-python SDK for compliance testing with the [PostHog SDK Test Harness](https://github.com/PostHog/posthog-sdk-test-harness).
4+
5+
## What is This?
6+
7+
This is a simple Flask app that:
8+
1. Wraps the posthog-python SDK
9+
2. Exposes a REST API for the test harness to control
10+
3. Tracks internal SDK state for test assertions
11+
12+
## Running Tests
13+
14+
Tests run automatically in CI via GitHub Actions. See the test harness repo for details.
15+
16+
### Locally with Docker Compose
17+
18+
```bash
19+
# From the posthog-python/sdk_compliance_adapter directory
20+
docker-compose up --build --abort-on-container-exit
21+
```
22+
23+
This will:
24+
1. Build the Python SDK adapter
25+
2. Pull the test harness image
26+
3. Run all compliance tests
27+
4. Show results
28+
29+
### Manually with Docker
30+
31+
```bash
32+
# Create network
33+
docker network create test-network
34+
35+
# Build and run adapter
36+
docker build -f sdk_compliance_adapter/Dockerfile -t posthog-python-adapter .
37+
docker run -d --name sdk-adapter --network test-network -p 8080:8080 posthog-python-adapter
38+
39+
# Run test harness
40+
docker run --rm \
41+
--name test-harness \
42+
--network test-network \
43+
ghcr.io/posthog/sdk-test-harness:latest \
44+
run --adapter-url http://sdk-adapter:8080 --mock-url http://test-harness:8081
45+
46+
# Cleanup
47+
docker stop sdk-adapter && docker rm sdk-adapter
48+
docker network rm test-network
49+
```
50+
51+
## Adapter Implementation
52+
53+
See [adapter.py](adapter.py) for the implementation.
54+
55+
The adapter implements the standard SDK adapter interface defined in the [test harness CONTRACT](https://github.com/PostHog/posthog-sdk-test-harness/blob/main/CONTRACT.yaml):
56+
57+
- `GET /health` - Return SDK information
58+
- `POST /init` - Initialize SDK with config
59+
- `POST /capture` - Capture an event
60+
- `POST /flush` - Flush pending events
61+
- `GET /state` - Return internal state
62+
- `POST /reset` - Reset SDK state
63+
64+
### Key Implementation Details
65+
66+
**Request Tracking**: The adapter monkey-patches `batch_post` to track all HTTP requests made by the SDK, including retries.
67+
68+
**State Management**: Thread-safe state tracking for events captured vs sent, retry attempts, and errors.
69+
70+
**UUID Tracking**: Extracts and tracks UUIDs from batches to verify deduplication.
71+
72+
## Documentation
73+
74+
For complete documentation on the test harness and how to implement adapters, see:
75+
- [PostHog SDK Test Harness](https://github.com/PostHog/posthog-sdk-test-harness)
76+
- [Adapter Implementation Guide](https://github.com/PostHog/posthog-sdk-test-harness/blob/main/ADAPTER_GUIDE.md)

0 commit comments

Comments
 (0)