Skip to content

Commit 112cf55

Browse files
Sergio Herreraseherv
authored andcommitted
Test DaprClient directly
Signed-off-by: Sergio Herrera <627709+seherv@users.noreply.github.com>
1 parent 4fb9631 commit 112cf55

20 files changed

Lines changed: 676 additions & 6 deletions

.github/workflows/validate_examples.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,8 @@ jobs:
152152
./dist/linux_amd64/release/placement --healthz-port 9091 &
153153
cd ..
154154
- name: Check examples
155+
run: |
156+
tox -e examples
157+
- name: Run integration tests
155158
run: |
156159
tox -e integration

tests/examples/conftest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
EXAMPLES_DIR = REPO_ROOT / 'examples'
1313

1414

15+
def pytest_configure(config: pytest.Config) -> None:
16+
config.addinivalue_line('markers', 'example_dir(name): set the example directory for a test')
17+
18+
1519
class DaprRunner:
1620
"""Helper to run `dapr run` commands and capture output."""
1721

tests/examples/test_configuration.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import pytest
55

6+
REDIS_CONTAINER = 'dapr_redis'
7+
68
EXPECTED_LINES = [
79
'Got key=orderId1 value=100 version=1 metadata={}',
810
'Got key=orderId2 value=200 version=1 metadata={}',
@@ -15,13 +17,13 @@
1517
def redis_config():
1618
"""Seed configuration values in Redis before the test."""
1719
subprocess.run(
18-
'docker exec dapr_redis redis-cli SET orderId1 "100||1"',
20+
f'docker exec {REDIS_CONTAINER} redis-cli SET orderId1 "100||1"',
1921
shell=True,
2022
check=True,
2123
capture_output=True,
2224
)
2325
subprocess.run(
24-
'docker exec dapr_redis redis-cli SET orderId2 "200||1"',
26+
f'docker exec {REDIS_CONTAINER} redis-cli SET orderId2 "200||1"',
2527
shell=True,
2628
check=True,
2729
capture_output=True,
@@ -36,7 +38,7 @@ def test_configuration(dapr, redis_config):
3638
)
3739
# Update Redis to trigger the subscription notification
3840
subprocess.run(
39-
'docker exec dapr_redis redis-cli SET orderId2 "210||2"',
41+
f'docker exec {REDIS_CONTAINER} redis-cli SET orderId2 "210||2"',
4042
shell=True,
4143
check=True,
4244
capture_output=True,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""gRPC method handler for invoke integration tests."""
2+
3+
from dapr.ext.grpc import App, InvokeMethodRequest, InvokeMethodResponse
4+
5+
app = App()
6+
7+
8+
@app.method(name='my-method')
9+
def my_method(request: InvokeMethodRequest) -> InvokeMethodResponse:
10+
return InvokeMethodResponse(b'INVOKE_RECEIVED', 'text/plain; charset=UTF-8')
11+
12+
13+
app.run(50051)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Pub/sub subscriber that persists received messages to state store.
2+
3+
Used by integration tests to verify message delivery without relying on stdout.
4+
"""
5+
6+
import json
7+
8+
from cloudevents.sdk.event import v1
9+
from dapr.ext.grpc import App
10+
11+
from dapr.clients import DaprClient
12+
from dapr.clients.grpc._response import TopicEventResponse
13+
14+
app = App()
15+
16+
17+
@app.subscribe(pubsub_name='pubsub', topic='TOPIC_A')
18+
def handle_topic_a(event: v1.Event) -> TopicEventResponse:
19+
data = json.loads(event.Data())
20+
with DaprClient() as d:
21+
d.save_state('statestore', f'received-topic-a-{data["id"]}', event.Data())
22+
return TopicEventResponse('success')
23+
24+
25+
app.run(50051)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: configurationstore
5+
spec:
6+
type: configuration.redis
7+
metadata:
8+
- name: redisHost
9+
value: localhost:6379
10+
- name: redisPassword
11+
value: ""
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: localsecretstore
5+
spec:
6+
type: secretstores.local.file
7+
metadata:
8+
- name: secretsFile
9+
# Relative to the Dapr process CWD (tests/integration/), set by
10+
# DaprTestEnvironment via cwd=INTEGRATION_DIR.
11+
value: secrets.json
12+
- name: nestedSeparator
13+
value: ":"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: lockstore
5+
spec:
6+
type: lock.redis
7+
metadata:
8+
- name: redisHost
9+
value: localhost:6379
10+
- name: redisPassword
11+
value: ""
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: pubsub
5+
spec:
6+
type: pubsub.redis
7+
version: v1
8+
metadata:
9+
- name: redisHost
10+
value: localhost:6379
11+
- name: redisPassword
12+
value: ""
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: statestore
5+
spec:
6+
type: state.redis
7+
version: v1
8+
metadata:
9+
- name: redisHost
10+
value: localhost:6379
11+
- name: redisPassword
12+
value: ""

0 commit comments

Comments
 (0)