Skip to content

Commit f5e387e

Browse files
committed
MINOR: Add DynamicLogConfig validation tests
1 parent d356645 commit f5e387e

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.kafka.server.config;
18+
19+
import org.apache.kafka.common.Reconfigurable;
20+
import org.apache.kafka.common.config.ConfigException;
21+
import org.apache.kafka.raft.KRaftConfigs;
22+
import org.apache.kafka.server.common.DirectoryEventHandler;
23+
import org.apache.kafka.server.log.remote.storage.RemoteLogManagerConfig;
24+
import org.apache.kafka.storage.internals.log.LogManager;
25+
26+
import org.junit.jupiter.api.Test;
27+
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertThrows;
33+
import static org.mockito.Mockito.mock;
34+
35+
public class DynamicLogConfigTest {
36+
37+
private final DynamicLogConfig dynamicLogConfig = new DynamicLogConfig(
38+
mock(LogManager.class),
39+
mock(DirectoryEventHandler.class)
40+
);
41+
42+
@Test
43+
public void testValidateLogLocalRetentionMs() {
44+
assertInvalidConfig(
45+
Map.of(
46+
ServerLogConfigs.LOG_RETENTION_TIME_MILLIS_CONFIG, "1000",
47+
RemoteLogManagerConfig.LOG_LOCAL_RETENTION_MS_PROP, "-1"
48+
),
49+
"Invalid value -1 for configuration " + RemoteLogManagerConfig.LOG_LOCAL_RETENTION_MS_PROP +
50+
": Value must not be -1 as " + ServerLogConfigs.LOG_RETENTION_TIME_MILLIS_CONFIG +
51+
" value is set as 1000."
52+
);
53+
54+
assertInvalidConfig(
55+
Map.of(
56+
ServerLogConfigs.LOG_RETENTION_TIME_MILLIS_CONFIG, "1000",
57+
RemoteLogManagerConfig.LOG_LOCAL_RETENTION_MS_PROP, "1001"
58+
),
59+
"Invalid value 1001 for configuration " + RemoteLogManagerConfig.LOG_LOCAL_RETENTION_MS_PROP +
60+
": Value must not be more than " + ServerLogConfigs.LOG_RETENTION_TIME_MILLIS_CONFIG +
61+
" property value: 1000"
62+
);
63+
}
64+
65+
@Test
66+
public void testValidateLogLocalRetentionBytes() {
67+
assertInvalidConfig(
68+
Map.of(
69+
ServerLogConfigs.LOG_RETENTION_BYTES_CONFIG, "1000",
70+
RemoteLogManagerConfig.LOG_LOCAL_RETENTION_BYTES_PROP, "-1"
71+
),
72+
"Invalid value -1 for configuration " + RemoteLogManagerConfig.LOG_LOCAL_RETENTION_BYTES_PROP +
73+
": Value must not be -1 as " + ServerLogConfigs.LOG_RETENTION_BYTES_CONFIG +
74+
" value is set as 1000."
75+
);
76+
77+
assertInvalidConfig(
78+
Map.of(
79+
ServerLogConfigs.LOG_RETENTION_BYTES_CONFIG, "1000",
80+
RemoteLogManagerConfig.LOG_LOCAL_RETENTION_BYTES_PROP, "1001"
81+
),
82+
"Invalid value 1001 for configuration " + RemoteLogManagerConfig.LOG_LOCAL_RETENTION_BYTES_PROP +
83+
": Value must not be more than " + ServerLogConfigs.LOG_RETENTION_BYTES_CONFIG +
84+
" property value: 1000"
85+
);
86+
}
87+
88+
private void assertInvalidConfig(Map<String, Object> overrides, String expectedMessage) {
89+
ConfigException exception = assertThrows(
90+
ConfigException.class,
91+
() -> dynamicLogConfig.validateReconfiguration(kafkaConfig(overrides))
92+
);
93+
assertEquals(expectedMessage, exception.getMessage());
94+
}
95+
96+
private static AbstractKafkaConfig kafkaConfig(Map<String, Object> overrides) {
97+
Map<String, Object> props = new HashMap<>();
98+
props.put(KRaftConfigs.PROCESS_ROLES_CONFIG, "broker");
99+
props.put(KRaftConfigs.NODE_ID_CONFIG, "1");
100+
props.put(KRaftConfigs.CONTROLLER_LISTENER_NAMES_CONFIG, "CONTROLLER");
101+
props.putAll(overrides);
102+
return new AbstractKafkaConfig(AbstractKafkaConfig.CONFIG_DEF, props, Map.of(), false) {
103+
@Override
104+
public void addReconfigurable(Reconfigurable reconfigurable) {
105+
}
106+
107+
@Override
108+
public void removeReconfigurable(Reconfigurable reconfigurable) {
109+
}
110+
};
111+
}
112+
}

0 commit comments

Comments
 (0)