|
Packit |
2997f0 |
#!/usr/bin/env python
|
|
Packit |
2997f0 |
#
|
|
Packit |
2997f0 |
#
|
|
Packit |
2997f0 |
# Cluster testing helper
|
|
Packit |
2997f0 |
#
|
|
Packit |
2997f0 |
# Requires:
|
|
Packit |
2997f0 |
# trivup python module
|
|
Packit |
2997f0 |
# gradle in your PATH
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
from trivup.trivup import Cluster, UuidAllocator
|
|
Packit |
2997f0 |
from trivup.apps.ZookeeperApp import ZookeeperApp
|
|
Packit |
2997f0 |
from trivup.apps.KafkaBrokerApp import KafkaBrokerApp
|
|
Packit |
2997f0 |
from trivup.apps.KerberosKdcApp import KerberosKdcApp
|
|
Packit |
2997f0 |
from trivup.apps.SslApp import SslApp
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
import os, sys, json, argparse
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
class LibrdkafkaTestCluster(Cluster):
|
|
Packit |
2997f0 |
def __init__(self, version, conf={}, num_brokers=3, debug=False):
|
|
Packit |
2997f0 |
"""
|
|
Packit |
2997f0 |
@brief Create, deploy and start a Kafka cluster using Kafka \p version
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
Supported \p conf keys:
|
|
Packit |
2997f0 |
* security.protocol - PLAINTEXT, SASL_PLAINTEXT, SASL_SSL
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
\p conf dict is passed to KafkaBrokerApp classes, etc.
|
|
Packit |
2997f0 |
"""
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
super(LibrdkafkaTestCluster, self).__init__(self.__class__.__name__,
|
|
Packit |
2997f0 |
os.environ.get('TRIVUP_ROOT', 'tmp'), debug=debug)
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
# Enable SSL if desired
|
|
Packit |
2997f0 |
if 'SSL' in conf.get('security.protocol', ''):
|
|
Packit |
2997f0 |
self.ssl = SslApp(self, conf)
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
self.brokers = list()
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
# One ZK (from Kafka repo)
|
|
Packit |
2997f0 |
ZookeeperApp(self)
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
# Start Kerberos KDC if GSSAPI (Kerberos) is configured
|
|
Packit |
2997f0 |
if 'GSSAPI' in conf.get('sasl_mechanisms', []):
|
|
Packit |
2997f0 |
kdc = KerberosKdcApp(self, 'MYREALM')
|
|
Packit |
2997f0 |
# Kerberos needs to be started prior to Kafka so that principals
|
|
Packit |
2997f0 |
# and keytabs are available at the time of Kafka config generation.
|
|
Packit |
2997f0 |
kdc.start()
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
# Brokers
|
|
Packit |
2997f0 |
defconf = {'replication_factor': min(num_brokers, 3), 'num_partitions': 4, 'version': version,
|
|
Packit |
2997f0 |
'security.protocol': 'PLAINTEXT'}
|
|
Packit |
2997f0 |
defconf.update(conf)
|
|
Packit |
2997f0 |
self.conf = defconf
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
for n in range(0, num_brokers):
|
|
Packit |
2997f0 |
self.brokers.append(KafkaBrokerApp(self, defconf))
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
def bootstrap_servers (self):
|
|
Packit |
2997f0 |
""" @return Kafka bootstrap servers based on security.protocol """
|
|
Packit |
2997f0 |
all_listeners = (','.join(self.get_all('advertised_listeners', '', KafkaBrokerApp))).split(',')
|
|
Packit |
2997f0 |
return ','.join([x for x in all_listeners if x.startswith(self.conf.get('security.protocol'))])
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
def result2color (res):
|
|
Packit |
2997f0 |
if res == 'PASSED':
|
|
Packit |
2997f0 |
return '\033[42m'
|
|
Packit |
2997f0 |
elif res == 'FAILED':
|
|
Packit |
2997f0 |
return '\033[41m'
|
|
Packit |
2997f0 |
else:
|
|
Packit |
2997f0 |
return ''
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
def print_test_report_summary (name, report):
|
|
Packit |
2997f0 |
""" Print summary for a test run. """
|
|
Packit |
2997f0 |
passed = report.get('PASSED', False)
|
|
Packit |
2997f0 |
if passed:
|
|
Packit |
2997f0 |
resstr = '\033[42mPASSED\033[0m'
|
|
Packit |
2997f0 |
else:
|
|
Packit |
2997f0 |
resstr = '\033[41mFAILED\033[0m'
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
print('%6s %-50s: %s' % (resstr, name, report.get('REASON', 'n/a')))
|
|
Packit |
2997f0 |
if not passed:
|
|
Packit |
2997f0 |
# Print test details
|
|
Packit |
2997f0 |
for name,test in report.get('tests', {}).iteritems():
|
|
Packit |
2997f0 |
testres = test.get('state', '')
|
|
Packit |
2997f0 |
if testres == 'SKIPPED':
|
|
Packit |
2997f0 |
continue
|
|
Packit |
2997f0 |
print('%s --> %-20s \033[0m' % \
|
|
Packit |
2997f0 |
('%s%s\033[0m' % \
|
|
Packit |
2997f0 |
(result2color(test.get('state', 'n/a')),
|
|
Packit |
2997f0 |
test.get('state', 'n/a')),
|
|
Packit |
2997f0 |
test.get('name', 'n/a')))
|
|
Packit |
2997f0 |
print('%8s --> %s/%s' %
|
|
Packit |
2997f0 |
('', report.get('root_path', '.'), 'stderr.log'))
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
def print_report_summary (fullreport):
|
|
Packit |
2997f0 |
""" Print summary from a full report suite """
|
|
Packit |
2997f0 |
suites = fullreport.get('suites', list())
|
|
Packit |
2997f0 |
print('#### Full test suite report (%d suite(s))' % len(suites))
|
|
Packit |
2997f0 |
for suite in suites:
|
|
Packit |
2997f0 |
for version,report in suite.get('version', {}).iteritems():
|
|
Packit |
2997f0 |
print_test_report_summary('%s @ %s' % \
|
|
Packit |
2997f0 |
(suite.get('name','n/a'), version),
|
|
Packit |
2997f0 |
report)
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
pass_cnt = fullreport.get('pass_cnt', -1)
|
|
Packit |
2997f0 |
if pass_cnt == 0:
|
|
Packit |
2997f0 |
pass_clr = ''
|
|
Packit |
2997f0 |
else:
|
|
Packit |
2997f0 |
pass_clr = '\033[42m'
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
fail_cnt = fullreport.get('fail_cnt', -1)
|
|
Packit |
2997f0 |
if fail_cnt == 0:
|
|
Packit |
2997f0 |
fail_clr = ''
|
|
Packit |
2997f0 |
else:
|
|
Packit |
2997f0 |
fail_clr = '\033[41m'
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
print('#### %d suites %sPASSED\033[0m, %d suites %sFAILED\033[0m' % \
|
|
Packit |
2997f0 |
(pass_cnt, pass_clr, fail_cnt, fail_clr))
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
if __name__ == '__main__':
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
parser = argparse.ArgumentParser(description='Show test suite report')
|
|
Packit |
2997f0 |
parser.add_argument('report', type=str, nargs=1,
|
|
Packit |
2997f0 |
help='Show summary from test suites report file')
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
args = parser.parse_args()
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
passed = False
|
|
Packit |
2997f0 |
with open(args.report[0], 'r') as f:
|
|
Packit |
2997f0 |
passed = print_report_summary(json.load(f))
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
if passed:
|
|
Packit |
2997f0 |
sys.exit(0)
|
|
Packit |
2997f0 |
else:
|
|
Packit |
2997f0 |
sys.exit(1)
|