Skip to content

Commit 5b96204

Browse files
pkoscikAdrian-Sciepura
authored andcommitted
[#92992] tests: Add rpc_pickle test module
1 parent 3c3679d commit 5b96204

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

.ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ api-test:
7878
- ./tests/test-order.sh
7979
- ./tests/test-overrides.sh
8080
- ./tests/test-plugins.sh
81+
- ./tests/test-rpc-pickle.sh
8182
artifacts:
8283
paths:
8384
- srv/protoplaster/*

tests/custom_modules/rpc_pickle/__init__.py

Whitespace-only changes.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from protoplaster.tools.tools import trigger_on_remote
2+
from protoplaster.conf.module import ModuleName
3+
from rpc_pickle.utils import remote_pickle_echo, CustomPickleObject
4+
5+
6+
@ModuleName("rpc_pickle")
7+
class TestPickleRPC:
8+
9+
def configure(self):
10+
assert hasattr(self, "dev"), "dev parameter required in yaml"
11+
12+
def test_custom_objects_rpc(self):
13+
"""
14+
Test sending and receiving non-JSON serializable objects over RPC.
15+
Sends: bytes, set
16+
Receives: CustomPickleObject instance
17+
"""
18+
test_bytes = b"\x00\xFF\xAAtest_bytes"
19+
test_set = {"apple", "banana", 42}
20+
21+
print(f"[{self.dev}] Sending bytes and set to remote via RPC...")
22+
23+
result = trigger_on_remote(self.dev, remote_pickle_echo,
24+
[test_bytes, test_set])
25+
26+
print(f"[{self.dev}] Received result: {result}")
27+
print(f"[{self.dev}] Result type: {type(result)}")
28+
29+
assert isinstance(result, CustomPickleObject), \
30+
f"Expected CustomPickleObject, got {type(result)}"
31+
32+
assert result.byte_data == test_bytes, \
33+
f"Byte data mismatch: expected {test_bytes}, got {result.byte_data}"
34+
35+
assert result.set_data == test_set, \
36+
f"Set data mismatch: expected {test_set}, got {result.set_data}"
37+
38+
print(
39+
"SUCCESS: Pickle RPC serialization verified for bytes, sets, and custom classes!"
40+
)
41+
42+
def name(self):
43+
return f"Pickle RPC Serialization Test ({self.dev})"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class CustomPickleObject:
2+
"""
3+
A custom class to test that pickle can reconstruct arbitrary
4+
objects across the RPC calls.
5+
"""
6+
7+
def __init__(self, byte_data: bytes, set_data: set):
8+
self.byte_data = byte_data
9+
self.set_data = set_data
10+
11+
def __repr__(self):
12+
return f"CustomPickleObject(bytes={self.byte_data}, set={self.set_data})"
13+
14+
15+
def remote_pickle_echo(byte_arg: bytes, set_arg: set) -> CustomPickleObject:
16+
"""
17+
Executes on the remote device.
18+
Verifies the types of incoming arguments and returns a custom class instance.
19+
"""
20+
print(f"remote_pickle_echo received: bytes={byte_arg}, set={set_arg}")
21+
22+
assert isinstance(byte_arg, bytes), f"Expected bytes, got {type(byte_arg)}"
23+
assert isinstance(set_arg, set), f"Expected set, got {type(set_arg)}"
24+
return CustomPickleObject(byte_data=byte_arg, set_data=set_arg)

tests/test-rpc-pickle.sh

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/bin/bash
2+
3+
set -e
4+
mkdir -p /tmp/protoplaster/pickle_node/{tests,reports,artifacts}
5+
mkdir -p /tmp/protoplaster/pickle_main/{tests,reports,artifacts}
6+
OUTPUT_DIR="test_pickle_output"
7+
TEST_DIR="$( dirname "${BASH_SOURCE[0]}" )"
8+
mkdir -p "$OUTPUT_DIR"
9+
10+
cat <<EOF > /tmp/protoplaster/pickle_devices.yaml
11+
node1: localhost:2140
12+
EOF
13+
14+
cat <<EOF > /tmp/protoplaster/rpc-pickle-test.yml
15+
tests:
16+
pickle_test:
17+
- rpc_pickle:
18+
dev: "node1"
19+
20+
test-suites:
21+
local:
22+
tests:
23+
- pickle_test
24+
EOF
25+
26+
echo "Starting Protoplaster nodes for Pickle RPC test..."
27+
28+
# Start Worker Node on port 2140
29+
protoplaster --test-dir /tmp/protoplaster/pickle_node/tests \
30+
--reports-dir /tmp/protoplaster/pickle_node/reports \
31+
--artifacts-dir /tmp/protoplaster/pickle_node/artifacts \
32+
--custom-tests $TEST_DIR/custom_modules \
33+
--port 2140 --dut &
34+
PID_WORKER=$!
35+
36+
# Start Main Node on port 5001
37+
protoplaster --test-dir /tmp/protoplaster/pickle_main/tests \
38+
--reports-dir /tmp/protoplaster/pickle_main/reports \
39+
--artifacts-dir /tmp/protoplaster/pickle_main/artifacts \
40+
--external-devices /tmp/protoplaster/pickle_devices.yaml \
41+
--custom-tests $TEST_DIR/custom_modules \
42+
--port 5001 --server &
43+
PID_MAIN=$!
44+
45+
# Cleanup function
46+
function finish {
47+
echo "Stopping servers..."
48+
kill $PID_WORKER $PID_MAIN 2>/dev/null || true
49+
}
50+
trap finish EXIT
51+
52+
# Wait for servers
53+
echo "Waiting for servers to start..."
54+
for port in 2140 5001; do
55+
while ! curl -s http://localhost:$port/api/v1/configs > /dev/null; do
56+
sleep 1
57+
done
58+
done
59+
60+
# Upload config
61+
CONFIG_FILE="rpc-pickle-test.yml"
62+
for port in 2140 5001; do
63+
echo "Uploading config to port $port..."
64+
NAME=$(curl -s -X POST http://localhost:$port/api/v1/configs -F "file=@/tmp/protoplaster/$CONFIG_FILE" | jq -r '.name')
65+
if [ "$NAME" != "$CONFIG_FILE" ] ; then
66+
echo "Config upload failed on port $port!"
67+
exit 1
68+
fi
69+
done
70+
71+
# Trigger test run
72+
echo "Triggering Pickle RPC run..."
73+
curl -s -X POST http://localhost:5001/api/v1/test-runs \
74+
-H "Content-Type: application/json" \
75+
-d "{\"config_name\": \"$CONFIG_FILE\"}" > /dev/null
76+
77+
sleep 2
78+
79+
REPORT_FILE="$OUTPUT_DIR/pickle_test_report.csv"
80+
RUN_ID=$(curl -s http://localhost:5001/api/v1/test-runs | jq -r '.[-1].id')
81+
82+
# Wait for finish
83+
STATUS=""
84+
while [ "$STATUS" != "finished" ] && [ "$STATUS" != "failed" ]; do
85+
STATUS=$(curl -s http://localhost:5001/api/v1/test-runs/$RUN_ID | jq -r '.status')
86+
sleep 1
87+
done
88+
89+
# Get report
90+
curl -s http://localhost:5001/api/v1/test-runs/$RUN_ID/report > "$REPORT_FILE"
91+
92+
if ! grep "test_custom_objects_rpc" "$REPORT_FILE" | grep -q "passed"; then
93+
echo "FAILURE: Pickle RPC test did not pass!"
94+
cat "$REPORT_FILE"
95+
exit 1
96+
fi
97+
98+
echo "SUCCESS: Pickle RPC test passed."
99+
echo "---------------------------------------------------"
100+
echo "Results saved in: $OUTPUT_DIR"
101+
echo "---------------------------------------------------"

0 commit comments

Comments
 (0)