Debian Codegraph @

tests/integration/admin/test_incremental_alter_configs.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright 2023 Confluent Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import time
17
18 from confluent_kafka.admin import ConfigResource, \
19 ConfigEntry, ResourceType, \
20 AlterConfigOpType
21
22 from tests.common import TestUtils
23
24
25 def assert_expected_config_entries(fs, num_fs, expected):
26 """
27 Verify that the list of non-default entries corresponds
28 to the expected one for each config resource.
29 """
30 assert len(fs.items()) == num_fs
31 for res, f in fs.items():
32 configs = f.result()
33 entries = sorted([str(entry) for entry in configs.values()
34 if not entry.is_default])
35 assert entries == expected[res]
36
37
38 def assert_operation_succeeded(fs, num_fs):
39 """
40 Verify that the operation succeeded
41 for each resource.
42 """
43 assert len(fs.items()) == num_fs
44 for _, f in fs.items():
45 assert f.result() is None # empty, but raises exception on failure
46
47
48 def test_incremental_alter_configs(kafka_cluster):
49 """
50 Incrementally change the configuration entries of two topics
51 and verify that the configuration description corresponds.
52 """
53
54 topic_prefix = "test-topic"
55 topic_prefix2 = "test-topic2"
56 num_partitions = 2
57 topic_config = {"compression.type": "gzip"}
58
59 our_topic = kafka_cluster.create_topic_and_wait_propogation(topic_prefix,
60 {
61 "num_partitions": num_partitions,
62 "config": topic_config,
63 "replication_factor": 1,
64 })
65 our_topic2 = kafka_cluster.create_topic_and_wait_propogation(topic_prefix2,
66 {
67 "num_partitions": num_partitions,
68 "config": topic_config,
69 "replication_factor": 1,
70 })
71
72 admin_client = kafka_cluster.admin()
73
74 res1 = ConfigResource(
75 ResourceType.TOPIC,
76 our_topic,
77 incremental_configs=[
78 ConfigEntry("cleanup.policy", "compact",
79 incremental_operation=AlterConfigOpType.APPEND),
80 ConfigEntry("retention.ms", "10000",
81 incremental_operation=AlterConfigOpType.SET)
82 ]
83 )
84 res2 = ConfigResource(
85 ResourceType.TOPIC,
86 our_topic2,
87 incremental_configs=[
88 ConfigEntry("cleanup.policy", "delete",
89 incremental_operation=AlterConfigOpType.SUBTRACT),
90 ConfigEntry("retention.ms", "5000",
91 incremental_operation=AlterConfigOpType.SET)
92 ]
93 )
94 expected = {
95 res1: ['cleanup.policy="delete,compact"',
96 'compression.type="gzip"',
97 'retention.ms="10000"'],
98 res2: ['cleanup.policy=""',
99 'compression.type="gzip"',
100 'retention.ms="5000"']
101 }
102
103 #
104 # Incrementally alter some configuration values
105 #
106 fs = admin_client.incremental_alter_configs([res1, res2])
107
108 assert_operation_succeeded(fs, 2)
109
110 time.sleep(1)
111
112 #
113 # Get current topic config
114 #
115 fs = admin_client.describe_configs([res1, res2])
116
117 # Assert expected config entries.
118 assert_expected_config_entries(fs, 2, expected)
119
120 #
121 # Delete an entry and change a second one.
122 #
123 res2 = ConfigResource(
124 ResourceType.TOPIC,
125 our_topic2,
126 incremental_configs=[
127 ConfigEntry("compression.type", None,
128 incremental_operation=AlterConfigOpType.DELETE),
129 ConfigEntry("retention.ms", "10000",
130 incremental_operation=AlterConfigOpType.SET)
131 ]
132 )
133 expected[res2] = ['cleanup.policy=""',
134 'retention.ms="10000"']
135
136 #
137 # Incrementally alter some configuration values
138 #
139 fs = admin_client.incremental_alter_configs([res2])
140
141 assert_operation_succeeded(fs, 1)
142
143 time.sleep(1)
144
145 #
146 # Get current topic config
147 #
148 fs = admin_client.describe_configs([res2])
149
150 # Assert expected config entries.
151 assert_expected_config_entries(fs, 1, expected)
152
153 # TODO: enable this test for the classic run too, when
154 # Confluent Platform test cluster is upgraded to 8.0.0
155 if TestUtils.use_group_protocol_consumer():
156 group_id = "test-group"
157
158 res_group = ConfigResource(
159 ResourceType.GROUP,
160 group_id,
161 incremental_configs=[
162 ConfigEntry("consumer.session.timeout.ms", "50000",
163 incremental_operation=AlterConfigOpType.SET)
164 ]
165 )
166
167 expected[res_group] = ['consumer.session.timeout.ms="50000"']
168
169 #
170 # Incrementally alter some configuration values
171 #
172 fs = admin_client.incremental_alter_configs([res_group])
173
174 assert_operation_succeeded(fs, 1)
175
176 time.sleep(1)
177
178 #
179 # Get current group config
180 #
181 fs = admin_client.describe_configs([res_group])
182
183 # Assert expected config entries.
184 assert_expected_config_entries(fs, 1, expected)