Skip to content

Commit df8525e

Browse files
committed
run kafka bin/ scripts for test fixtures
1 parent 39b68eb commit df8525e

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

test/fixtures.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ def test_resource(cls, filename):
116116
return path
117117
return os.path.join(cls.project_root, "servers", "resources", "default", filename)
118118

119+
@classmethod
120+
def run_script(cls, script, *args):
121+
result = [os.path.join(cls.kafka_root, 'bin', script)]
122+
result.extend([str(arg) for arg in args])
123+
return result
124+
119125
@classmethod
120126
def kafka_run_class_args(cls, *args):
121127
result = [os.path.join(cls.kafka_root, 'bin', 'kafka-run-class.sh')]
@@ -202,6 +208,7 @@ def open(self):
202208
# Configure Zookeeper child process
203209
template = self.test_resource("zookeeper.properties")
204210
properties = self.tmp_dir.join("zookeeper.properties")
211+
# Consider replacing w/ run_script('zookeper-server-start.sh', ...)
205212
args = self.kafka_run_class_args("org.apache.zookeeper.server.quorum.QuorumPeerMain",
206213
properties.strpath)
207214
env = self.kafka_run_class_env()
@@ -348,17 +355,12 @@ def _jaas_config(self):
348355

349356
def _add_scram_user(self):
350357
self.out("Adding SCRAM credentials for user {} to zookeeper.".format(self.broker_user))
351-
args = self.kafka_run_class_args(
352-
"kafka.admin.ConfigCommand",
353-
"--zookeeper",
354-
"%s:%d/%s" % (self.zookeeper.host,
355-
self.zookeeper.port,
356-
self.zk_chroot),
357-
"--alter",
358-
"--entity-type", "users",
359-
"--entity-name", self.broker_user,
360-
"--add-config",
361-
"{}=[password={}]".format(self.sasl_mechanism, self.broker_password),
358+
args = self.run_script('kafka-configs.sh',
359+
'--alter',
360+
'--entity-type', 'users',
361+
'--entity-name', self.broker_user,
362+
'--add-config',
363+
'{}=[password={}]'.format(self.sasl_mechanism, self.broker_password),
362364
)
363365
env = self.kafka_run_class_env()
364366
proc = subprocess.Popen(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@@ -390,13 +392,12 @@ def out(self, message):
390392

391393
def _create_zk_chroot(self):
392394
self.out("Creating Zookeeper chroot node...")
393-
args = self.kafka_run_class_args("org.apache.zookeeper.ZooKeeperMain",
394-
"-server",
395-
"%s:%d" % (self.zookeeper.host,
396-
self.zookeeper.port),
397-
"create",
398-
"/%s" % (self.zk_chroot,),
399-
"kafka-python")
395+
args = self.run_script('zookeeper-shell.sh',
396+
'%s:%d' % (self.zookeeper.host,
397+
self.zookeeper.port),
398+
'create',
399+
'/%s' % (self.zk_chroot,),
400+
'kafka-python')
400401
env = self.kafka_run_class_env()
401402
proc = subprocess.Popen(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
402403

@@ -416,6 +417,7 @@ def start(self):
416417
properties_template = self.test_resource("kafka.properties")
417418
jaas_conf_template = self.test_resource("kafka_server_jaas.conf")
418419

420+
# Consider replacing w/ run_script('kafka-server-start.sh', ...)
419421
args = self.kafka_run_class_args("kafka.Kafka", properties.strpath)
420422
env = self.kafka_run_class_env()
421423
if self.sasl_enabled:
@@ -590,17 +592,17 @@ def _create_topic_via_admin_api(self, topic_name, num_partitions, replication_fa
590592
raise errors.for_code(error_code)
591593

592594
def _create_topic_via_cli(self, topic_name, num_partitions, replication_factor):
593-
args = self.kafka_run_class_args('kafka.admin.TopicCommand',
594-
'--zookeeper', '%s:%s/%s' % (self.zookeeper.host,
595-
self.zookeeper.port,
596-
self.zk_chroot),
597-
'--create',
598-
'--topic', topic_name,
599-
'--partitions', self.partitions \
600-
if num_partitions is None else num_partitions,
601-
'--replication-factor', self.replicas \
602-
if replication_factor is None \
603-
else replication_factor)
595+
args = self.run_script('kafka-topics.sh',
596+
'--zookeeper', '%s:%s/%s' % (self.zookeeper.host,
597+
self.zookeeper.port,
598+
self.zk_chroot),
599+
'--create',
600+
'--topic', topic_name,
601+
'--partitions', self.partitions \
602+
if num_partitions is None else num_partitions,
603+
'--replication-factor', self.replicas \
604+
if replication_factor is None \
605+
else replication_factor)
604606
if env_kafka_version() >= (0, 10):
605607
args.append('--if-not-exists')
606608
env = self.kafka_run_class_env()
@@ -614,12 +616,12 @@ def _create_topic_via_cli(self, topic_name, num_partitions, replication_factor):
614616
raise RuntimeError("Failed to create topic %s" % (topic_name,))
615617

616618
def get_topic_names(self):
617-
args = self.kafka_run_class_args('kafka.admin.TopicCommand',
619+
args = self.run_script('kafka-topics.sh',
618620
'--zookeeper', '%s:%s/%s' % (self.zookeeper.host,
619621
self.zookeeper.port,
620622
self.zk_chroot),
621623
'--list'
622-
)
624+
)
623625
env = self.kafka_run_class_env()
624626
env.pop('KAFKA_LOG4J_OPTS')
625627
proc = subprocess.Popen(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

0 commit comments

Comments
 (0)