-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kafka_user: use infratest to verify message write/read
- Loading branch information
Showing
5 changed files
with
163 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import pytest | ||
|
||
def pytest_runtest_setup(item): | ||
"""Run tests only when under molecule with testinfra installed.""" | ||
try: | ||
import testinfra | ||
except ImportError: | ||
pytest.skip("Test requires testinfra", allow_module_level=True) | ||
|
||
if "MOLECULE_INVENTORY_FILE" in os.environ: | ||
pytest.testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( | ||
os.environ["MOLECULE_INVENTORY_FILE"] | ||
).get_hosts("executors") | ||
else: | ||
pytest.skip( | ||
"Test should run only from inside molecule.", allow_module_level=True | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import pytest | ||
import time | ||
import uuid | ||
|
||
from kafka import KafkaConsumer, KafkaProducer, TopicPartition | ||
|
||
testinfra_hosts = ["ansible://executors"] | ||
|
||
|
||
# --------------- | ||
# Common (TO BE SHARED) | ||
# --------------- | ||
def get_bootstrap_servers(host, listener_name='PLAINTEXT'): | ||
return host.ansible.get_variables()['bootstrap_servers'][listener_name.lower()] | ||
|
||
|
||
def execute_kafka_info(host, resource, check=False): | ||
|
||
module_args = { | ||
'resource': resource, | ||
} | ||
|
||
module_args.update({ | ||
'bootstrap_servers': get_bootstrap_servers(host) | ||
}) | ||
|
||
return host.ansible('kafka_info', "{{ module_args }}", | ||
check=check, | ||
extra_vars={ 'module_args': module_args }) | ||
|
||
|
||
def produce_messages(topic, messages, **config): | ||
correlation_id = bytes(str(uuid.uuid4()), 'utf-8') | ||
|
||
producer = KafkaProducer(**config) | ||
|
||
record_metas = [] | ||
record_headers = [('correlation_id', correlation_id)] | ||
|
||
for message in messages: | ||
fut = producer.send(topic, bytes(message, 'utf-8'), headers=record_headers) | ||
record_metas.append(fut.get()) | ||
|
||
producer.flush() | ||
producer.close() | ||
|
||
return correlation_id, record_metas | ||
|
||
|
||
def consume_messages(topic, topic_partitions=1, max_messages=1, max_wait_timeout=120, | ||
poll_timeout_ms=100, poll_max_records=10, | ||
from_beginning=False, record_filter=None, **config): | ||
|
||
consumer = KafkaConsumer(**config) | ||
|
||
|
||
# manually assign topic partitions | ||
consumer.assign([TopicPartition(topic, partition) | ||
for partition in range(0, topic_partitions)]) | ||
|
||
if from_beginning: | ||
consumer.seek_to_beginning() | ||
|
||
|
||
messages = [] | ||
start_ts = time.time() # expressed in seconds | ||
while True: | ||
if len(messages) >= max_messages or (time.time() - start_ts) > max_wait_timeout: | ||
break | ||
|
||
batch = consumer.poll(timeout_ms=poll_timeout_ms, max_records=poll_max_records) | ||
|
||
for subscription, records in batch.items(): | ||
for record in filter(record_filter, records): | ||
messages.append(record.value.decode('utf-8')) | ||
|
||
return messages | ||
|
||
|
||
|
||
# --------------- | ||
# Actual user test | ||
# --------------- | ||
def test_user_info_should_return_created_users(host): | ||
|
||
kafka_user_info = execute_kafka_info(host, 'user') | ||
|
||
|
||
assert kafka_user_info['ansible_module_results'] is not None | ||
assert kafka_user_info['ansible_module_results']['users'] is not None | ||
|
||
users = kafka_user_info['ansible_module_results']['users'] | ||
|
||
assert users['alice'] is not None | ||
assert users['alice'][0]['iterations'] == 4096 | ||
assert users['alice'][0]['mechanism'] == "SCRAM-SHA-512" | ||
|
||
assert users['bob'] is not None | ||
assert users['bob'][0]['iterations'] == 5000 | ||
assert users['bob'][0]['mechanism'] == "SCRAM-SHA-256" | ||
|
||
|
||
def test_client_should_be_able_to_write_and_read_messages(host): | ||
topic = 'scram-test-topic' | ||
|
||
messages = [ | ||
'Test Message #1', | ||
'Test Message #2', | ||
'Test Message #3' | ||
] | ||
|
||
config = { | ||
'bootstrap_servers': get_bootstrap_servers(host, listener_name='OUTSIDE_SASL_PLAINTEXT'), | ||
'security_protocol': 'SASL_PLAINTEXT', | ||
'sasl_mechanism': 'SCRAM-SHA-512', | ||
'sasl_plain_username': 'alice', | ||
'sasl_plain_password': 'changeit' | ||
} | ||
|
||
produce_messages(topic, messages, **config) | ||
actual_messages = consume_messages(topic, from_beginning=True, max_messages=3, **config) | ||
|
||
print(actual_messages) | ||
|
||
assert set(actual_messages) == set(messages) |
This file was deleted.
Oops, something went wrong.