forked from Platformatory/kong-event-pub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
104 lines (87 loc) · 3.23 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Important notes: This script assumes you have env vars SASL_USER, SASL_PASSWORD BOOTSTRAP_SERVERS, TOPIC set.
# It is also assumed that the topic itself exists
import requests
import os
import json
from datetime import datetime
from confluent_kafka import Consumer, TopicPartition, KafkaError
import time
def make_order_request():
url = "http://kong:8000/order"
headers = {"Content-Type": "application/json"}
payload = {"foo": "bar", "time": datetime.now().isoformat()}
try:
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print(f"HTTP Error: {err}")
return None
except requests.exceptions.RequestException as err:
print(f"Error: {err}")
return None
else:
return payload
def consume_message(consumer, topic):
topic_partition = TopicPartition(topic, partition=0)
low, high = consumer.get_watermark_offsets(topic_partition)
last_offset = high - 1
consume_from = TopicPartition(topic, partition=0, offset=last_offset)
consumer.assign([consume_from])
while True:
msg = consumer.poll(5.0)
if msg is None:
continue
if msg.error():
if msg.error().code() == KafkaError._PARTITION_EOF:
print(f"Reached end of topic {topic}")
else:
print(f"Error while consuming from topic {topic}: {msg.error()}")
else:
payload = json.loads(msg.value().decode("utf-8"))
return payload
def main():
bootstrap_servers = os.environ.get("BOOTSTRAP_SERVERS")
topic = os.environ.get("TOPIC")
sasl_user = os.environ.get("SASL_USER")
sasl_password = os.environ.get("SASL_PASSWORD")
conf = {
"bootstrap.servers": bootstrap_servers,
"security.protocol": "SASL_SSL",
"sasl.mechanism": "PLAIN",
"sasl.username": sasl_user,
"sasl.password": sasl_password,
"group.id": "somerandomgroupmakesureitisunique",
"auto.offset.reset": "latest",
"enable.auto.commit": False,
}
consumer = Consumer(conf)
passed = 0
failed = 0
for i in range(10):
print(f"Iteration {i+1}:")
# Make order request
payload = make_order_request()
print(f"Order payload sent to Kong: {payload}")
print("Sleeping 5 seconds to let the high watermark advance..")
time.sleep(5)
# Consume message from Kafka
message_payload = consume_message(consumer, topic)
if message_payload is None:
print("No message received from Kafka")
continue
print(f"Received message payload from Kafka: {message_payload}")
ret_request_body = message_payload.get('request').get('body')
print(ret_request_body)
print("--")
print(payload)
if ret_request_body == payload:
print("Assertion passed")
passed += 1
else:
print("Assertion failed")
failed += 1
print(f"\nReport: Passed {passed} times, Failed {failed} times")
if failed > 0:
print("Reason for failures: Received message does not match original order payload")
if __name__ == "__main__":
main()