Skip to content

Commit 0174e58

Browse files
committed
Preparing for final release of 0.0.1
1 parent 7e10c04 commit 0174e58

File tree

9 files changed

+63
-16
lines changed

9 files changed

+63
-16
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,4 @@ if __name__ == "__main__":
2222
agent.run(host='127.0.0.1', port=9002)
2323
```
2424

25-
## TODO:
26-
* Add automated tests of our SPOP protocol implementation
27-
* Our target runtime was Python 3.8 or above, but it is possible this implementation could work with earlier versions.
28-
* Better debug logging / crash handling.
29-
* Document a recommended deployment strategy.
25+

example/agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
@agent.handler("earth-to-mars")
1111
async def handle_earth_to_mars(src: IPv4Address, req_host: str):
12-
return AckPayload().set_txn_var("bubbles", str(src) + req_host)
12+
return AckPayload().set_txn_var("transmission_src", str(src) + req_host)
1313

1414

1515
if __name__ == "__main__":

example/haproxy/haproxy.cfg

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ defaults
99

1010
frontend http
1111
bind 0.0.0.0:9000
12-
13-
filter spoe engine python_spoa config /usr/local/etc/haproxy/python_spoa.spoe.conf
14-
http-response set-header X-bubbles %[var(txn.iprep.bubbles)]
15-
1612
default_backend application_server
1713

1814
backend application_server
1915
mode http
2016
balance roundrobin
17+
18+
filter spoe engine python_spoa config /usr/local/etc/haproxy/python_spoa.spoe.conf
19+
http-response set-header X-Planetary-Transmission %[var(txn.iprep.transmission_src)]
20+
2121
server web1 127.0.0.1:9001 check
2222

2323
backend python_spoa

example/haproxy/python_spoa.spoe.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ spoe-agent python_spoa
1010

1111
spoe-message earth-to-mars
1212
args src=src req_host=req.fhdr(host)
13-
event on-frontend-http-request
13+
event on-backend-http-request

haproxyspoa/spoa_payloads.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def write_list_of_actions(actions: list) -> bytes:
5252
buffer = io.BytesIO()
5353

5454
for action in actions:
55-
_type = int.to_bytes(action.type, 1, byteorder='big', signed=False)
56-
num_args = int.to_bytes(len(action.args), 1, byteorder='big', signed=False)
55+
_type = bytes([action.type])
56+
num_args = bytes([len(action.args)])
5757

5858
buffer.write(_type)
5959
buffer.write(num_args)

haproxyspoa/spoa_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
class SpoaConnection:
19-
19+
2020
def __init__(self, writer: asyncio.StreamWriter, handlers):
2121
self.logger = FlowIdLoggerAdapter(logger, {"flow_id": secrets.token_hex(4)})
2222
self.handlers = handlers
@@ -33,7 +33,7 @@ async def handle_haproxy_notify(self, frame: Frame):
3333
response_futures.append(handler(**notify_payload.messages[msg_key]))
3434

3535
self.logger.info(f"Found {len(response_futures)} matching handlers, awaiting response...")
36-
ack_payloads: List[AckPayload] = await asyncio.gather(*response_futures)
36+
ack_payloads = await asyncio.gather(*response_futures)
3737
ack = AckPayload.create_from_all(*ack_payloads)
3838
payload = ack.to_bytes()
3939

@@ -93,7 +93,7 @@ def wrapper(*args, **kwargs):
9393

9494
async def handle_connection(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
9595
conn = SpoaConnection(writer, self.handlers)
96-
96+
9797
haproxy_hello_frame = await Frame.read_frame(reader)
9898

9999
if not haproxy_hello_frame.headers.is_haproxy_hello():

release.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
python3 setup.py bdist_wheel
3+
twine check dist/*
4+
twine upload dist/*

tests/__init__.py

Whitespace-only changes.

tests/test_payload_parsing.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import io
2+
import ipaddress
3+
import unittest
4+
5+
from haproxyspoa.spoa_data_types import write_string, write_typed_autodetect
6+
from haproxyspoa.spoa_payloads import write_kv_list, parse_kv_list, parse_list_of_messages
7+
8+
9+
class TestPayloadParsing(unittest.TestCase):
10+
11+
def test_write_read_list_of_messages(self):
12+
buffer = io.BytesIO()
13+
14+
message_name = "solar_system_communication"
15+
num_args = 4
16+
args = {
17+
"mercury": write_typed_autodetect(ipaddress.IPv4Address("192.168.1.23")),
18+
"venus": write_typed_autodetect(123456789),
19+
"earth": write_typed_autodetect("looks like a marble"),
20+
"mars": write_typed_autodetect(True),
21+
}
22+
23+
buffer.write(write_string(message_name))
24+
buffer.write(bytes([num_args]))
25+
buffer.write(write_kv_list(args))
26+
27+
self.assertEqual(
28+
buffer.getvalue(),
29+
b'\x1asolar_system_communication\x04\x07mercury\x06\xc0\xa8\x01\x17\x05venus\x04\xf5\xc2\xf8\xd5\x02'
30+
b'\x05earth\x08\x13looks like a marble\x04mars\x04\x01',
31+
)
32+
33+
buffer.seek(0)
34+
messages = parse_list_of_messages(buffer)
35+
36+
self.assertEqual(messages, {
37+
"solar_system_communication": {
38+
"mercury": ipaddress.IPv4Address("192.168.1.23"),
39+
"venus": 123456789,
40+
"earth": "looks like a marble",
41+
"mars": 1
42+
}
43+
})
44+
45+
46+
if __name__ == '__main__':
47+
unittest.main()

0 commit comments

Comments
 (0)