Debian Codegraph @

tests/common/__init__.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2024 Confluent Inc.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 import os
20 from confluent_kafka import Consumer
21
22 _GROUP_PROTOCOL_ENV = 'TEST_CONSUMER_GROUP_PROTOCOL'
23 _TRIVUP_CLUSTER_TYPE_ENV = 'TEST_TRIVUP_CLUSTER_TYPE'
24
25
26 def _trivup_cluster_type_kraft():
27 return _TRIVUP_CLUSTER_TYPE_ENV in os.environ and os.environ[_TRIVUP_CLUSTER_TYPE_ENV] == 'kraft'
28
29
30 class TestUtils:
31 @staticmethod
32 def broker_version():
33 return '4.0.0' if TestUtils.use_group_protocol_consumer() else '3.9.0'
34
35 @staticmethod
36 def broker_conf():
37 broker_conf = ['transaction.state.log.replication.factor=1',
38 'transaction.state.log.min.isr=1']
39 if TestUtils.use_group_protocol_consumer():
40 broker_conf.append('group.coordinator.rebalance.protocols=classic,consumer')
41 return broker_conf
42
43 @staticmethod
44 def _broker_major_version():
45 return int(TestUtils.broker_version().split('.')[0])
46
47 @staticmethod
48 def use_kraft():
49 return (TestUtils.use_group_protocol_consumer() or
50 _trivup_cluster_type_kraft())
51
52 @staticmethod
53 def use_group_protocol_consumer():
54 return _GROUP_PROTOCOL_ENV in os.environ and os.environ[_GROUP_PROTOCOL_ENV] == 'consumer'
55
56 @staticmethod
57 def update_conf_group_protocol(conf=None):
58 if TestUtils.can_upgrade_group_protocol_to_consumer(conf):
59 conf['group.protocol'] = 'consumer'
60
61 @staticmethod
62 def can_upgrade_group_protocol_to_consumer(conf):
63 return (conf is not None and 'group.id' in conf and
64 'group.protocol' not in conf and TestUtils.use_group_protocol_consumer())
65
66 @staticmethod
67 def remove_forbidden_conf_group_protocol_consumer(conf):
68 if (conf is None or
69 not TestUtils.use_group_protocol_consumer() or
70 conf.get('group.protocol', 'consumer') != 'consumer'):
71 return
72 forbidden_conf_properties = ["session.timeout.ms",
73 "partition.assignment.strategy",
74 "heartbeat.interval.ms",
75 "group.protocol.type"]
76 for prop in forbidden_conf_properties:
77 if prop in conf:
78 print(f"Skipping setting forbidden configuration {prop} for `CONSUMER` protocol")
79 del conf[prop]
80
81
82 class TestConsumer(Consumer):
83 def __init__(self, conf=None, **kwargs):
84 TestUtils.update_conf_group_protocol(conf)
85 TestUtils.remove_forbidden_conf_group_protocol_consumer(conf)
86 super(TestConsumer, self).__init__(conf, **kwargs)
87
88 def assign(self, partitions):
89 if TestUtils.use_group_protocol_consumer():
90 super(TestConsumer, self).incremental_assign(partitions)
91 else:
92 super(TestConsumer, self).assign(partitions)
93
94 def unassign(self, partitions):
95 if TestUtils.use_group_protocol_consumer():
96 super(TestConsumer, self).incremental_unassign(partitions)
97 else:
98 super(TestConsumer, self).unassign()