Skip to content

Commit 0ac84b0

Browse files
committed
Move dhcp lease timeout to ConfigKey, set it to 'zone' scope, and add some tests.
1 parent bb72490 commit 0ac84b0

File tree

5 files changed

+246
-12
lines changed

5 files changed

+246
-12
lines changed

engine/api/src/main/java/org/apache/cloudstack/engine/orchestration/service/NetworkOrchestrationService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public interface NetworkOrchestrationService {
9191
ConfigKey<Integer> NetworkThrottlingRate = new ConfigKey<>("Network", Integer.class, NetworkThrottlingRateCK, "200",
9292
"Default data transfer rate in megabits per second allowed in network.", true, ConfigKey.Scope.Zone);
9393

94+
ConfigKey<Integer> DhcpLeaseTimeout = new ConfigKey<>("Network", Integer.class, "dhcp.lease.timeout", "0",
95+
"DHCP lease time in seconds for VMs. Use 0 for infinite lease time (default). A non-zero value sets the lease duration in seconds.",
96+
true, ConfigKey.Scope.Zone, "0-");
97+
9498
ConfigKey<Boolean> PromiscuousMode = new ConfigKey<>("Advanced", Boolean.class, "network.promiscuous.mode", "false",
9599
"Whether to allow or deny promiscuous mode on NICs for applicable network elements such as for vswitch/dvswitch portgroups.", true);
96100

server/src/main/java/com/cloud/configuration/Config.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -351,15 +351,6 @@ public enum Config {
351351
ConfigKey.Kind.CSV,
352352
null),
353353

354-
DhcpLeaseTimeout(
355-
"Network",
356-
ManagementServer.class,
357-
Integer.class,
358-
"dhcp.lease.timeout",
359-
"0",
360-
"DHCP lease time in seconds for VMs. Use 0 for infinite lease time (default). A non-zero value sets the lease duration in seconds.",
361-
null),
362-
363354
//VPN
364355
RemoteAccessVpnPskLength(
365356
"Network",

server/src/main/java/com/cloud/network/router/CommandSetupHelper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,8 @@ public void createDhcpEntryCommand(final VirtualRouter router, final UserVm vm,
296296
dhcpCommand.setDefault(nic.isDefaultNic());
297297
dhcpCommand.setRemove(remove);
298298

299-
// Set DHCP lease timeout from global config (0 = infinite)
300-
String leaseTimeStr = _configDao.getValue(Config.DhcpLeaseTimeout.key());
301-
Long leaseTime = (leaseTimeStr != null) ? Long.parseLong(leaseTimeStr) : 0L;
299+
// Set DHCP lease timeout from zone-scoped config (0 = infinite)
300+
Long leaseTime = (long) NetworkOrchestrationService.DhcpLeaseTimeout.valueIn(router.getDataCenterId());
302301
dhcpCommand.setLeaseTime(leaseTime);
303302

304303
dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));

server/src/test/java/com/cloud/network/router/CommandSetupHelperTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import com.cloud.vm.NicVO;
4747
import com.cloud.vm.VirtualMachine;
4848
import com.cloud.vm.dao.NicDao;
49+
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
50+
import org.apache.cloudstack.framework.config.ConfigKey;
4951
import org.apache.cloudstack.network.BgpPeerVO;
5052
import org.apache.cloudstack.network.dao.BgpPeerDetailsDao;
5153
import org.junit.Assert;
@@ -271,4 +273,52 @@ public void testCreateBgpPeersCommandsForVpc() {
271273
Assert.assertTrue(cmd instanceof SetBgpPeersCommand);
272274
Assert.assertEquals(4, ((SetBgpPeersCommand) cmd).getBpgPeers().length);
273275
}
276+
277+
@Test
278+
public void testDhcpLeaseTimeoutDefaultValue() {
279+
// Test that the default value is 0 (infinite)
280+
Integer defaultValue = NetworkOrchestrationService.DhcpLeaseTimeout.value();
281+
Assert.assertEquals("Default DHCP lease timeout should be 0 (infinite)", 0, defaultValue.intValue());
282+
}
283+
284+
@Test
285+
public void testDhcpLeaseTimeoutAcceptsZero() {
286+
// Test that 0 value is accepted (infinite lease)
287+
ConfigKey<Integer> configKey = NetworkOrchestrationService.DhcpLeaseTimeout;
288+
Assert.assertNotNull("ConfigKey should not be null", configKey);
289+
Assert.assertEquals("ConfigKey default should be 0", "0", configKey.defaultValue());
290+
}
291+
292+
@Test
293+
public void testDhcpLeaseTimeoutAcceptsPositiveValues() {
294+
// Test that positive values are accepted
295+
ConfigKey<Integer> configKey = NetworkOrchestrationService.DhcpLeaseTimeout;
296+
Assert.assertNotNull("ConfigKey should not be null", configKey);
297+
// Verify the validator allows positive values
298+
String validator = configKey.range();
299+
Assert.assertNotNull("Validator should not be null", validator);
300+
Assert.assertEquals("Validator should be '0-' (>= 0)", "0-", validator);
301+
}
302+
303+
@Test
304+
public void testDhcpLeaseTimeoutValidatorRejectsNegative() {
305+
// Test that the validator is configured to reject negative values
306+
ConfigKey<Integer> configKey = NetworkOrchestrationService.DhcpLeaseTimeout;
307+
String validator = configKey.range();
308+
Assert.assertEquals("Validator should enforce minimum of 0", "0-", validator);
309+
}
310+
311+
@Test
312+
public void testDhcpLeaseTimeoutHasZoneScope() {
313+
// Test that the ConfigKey has Zone scope
314+
ConfigKey<Integer> configKey = NetworkOrchestrationService.DhcpLeaseTimeout;
315+
Assert.assertEquals("ConfigKey should have Zone scope", ConfigKey.Scope.Zone, configKey.scope());
316+
}
317+
318+
@Test
319+
public void testDhcpLeaseTimeoutIsDynamic() {
320+
// Test that the ConfigKey is dynamic (can be updated at runtime)
321+
ConfigKey<Integer> configKey = NetworkOrchestrationService.DhcpLeaseTimeout;
322+
Assert.assertTrue("ConfigKey should be dynamic", configKey.isDynamic());
323+
}
274324
}
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with 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,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.network.router;
18+
19+
import com.cloud.agent.api.routing.DhcpEntryCommand;
20+
import com.cloud.agent.manager.Commands;
21+
import com.cloud.dc.DataCenterVO;
22+
import com.cloud.dc.dao.DataCenterDao;
23+
import com.cloud.network.NetworkModel;
24+
import com.cloud.network.dao.NetworkDao;
25+
import com.cloud.network.dao.NetworkDetailsDao;
26+
import com.cloud.offerings.dao.NetworkOfferingDao;
27+
import com.cloud.offerings.dao.NetworkOfferingDetailsDao;
28+
import com.cloud.user.UserVmVO;
29+
import com.cloud.vm.NicVO;
30+
import com.cloud.vm.VirtualMachine;
31+
import com.cloud.vm.dao.NicDao;
32+
import com.cloud.vm.dao.UserVmDao;
33+
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
34+
import org.apache.cloudstack.framework.config.ConfigKey;
35+
import org.junit.Assert;
36+
import org.junit.Before;
37+
import org.junit.Test;
38+
import org.junit.runner.RunWith;
39+
import org.mockito.InjectMocks;
40+
import org.mockito.Mock;
41+
import org.mockito.Mockito;
42+
import org.mockito.junit.MockitoJUnitRunner;
43+
import org.springframework.test.util.ReflectionTestUtils;
44+
45+
import static org.mockito.ArgumentMatchers.any;
46+
import static org.mockito.ArgumentMatchers.anyBoolean;
47+
import static org.mockito.ArgumentMatchers.anyLong;
48+
import static org.mockito.Mockito.when;
49+
50+
/**
51+
* Integration tests for DHCP lease timeout functionality.
52+
* Tests the end-to-end flow from ConfigKey through DhcpEntryCommand creation.
53+
*/
54+
@RunWith(MockitoJUnitRunner.class)
55+
public class DhcpLeaseTimeoutIntegrationTest {
56+
57+
@InjectMocks
58+
protected CommandSetupHelper commandSetupHelper = new CommandSetupHelper();
59+
60+
@Mock
61+
NicDao nicDao;
62+
@Mock
63+
NetworkDao networkDao;
64+
@Mock
65+
NetworkModel networkModel;
66+
@Mock
67+
NetworkOfferingDao networkOfferingDao;
68+
@Mock
69+
NetworkOfferingDetailsDao networkOfferingDetailsDao;
70+
@Mock
71+
NetworkDetailsDao networkDetailsDao;
72+
@Mock
73+
RouterControlHelper routerControlHelper;
74+
@Mock
75+
DataCenterDao dcDao;
76+
@Mock
77+
UserVmDao userVmDao;
78+
79+
private VirtualRouter mockRouter;
80+
private UserVmVO mockVm;
81+
private NicVO mockNic;
82+
private DataCenterVO mockDc;
83+
84+
@Before
85+
public void setUp() {
86+
ReflectionTestUtils.setField(commandSetupHelper, "_nicDao", nicDao);
87+
ReflectionTestUtils.setField(commandSetupHelper, "_networkDao", networkDao);
88+
ReflectionTestUtils.setField(commandSetupHelper, "_networkModel", networkModel);
89+
ReflectionTestUtils.setField(commandSetupHelper, "_networkOfferingDao", networkOfferingDao);
90+
ReflectionTestUtils.setField(commandSetupHelper, "networkOfferingDetailsDao", networkOfferingDetailsDao);
91+
ReflectionTestUtils.setField(commandSetupHelper, "networkDetailsDao", networkDetailsDao);
92+
ReflectionTestUtils.setField(commandSetupHelper, "_routerControlHelper", routerControlHelper);
93+
ReflectionTestUtils.setField(commandSetupHelper, "_dcDao", dcDao);
94+
95+
// Create common mocks
96+
mockRouter = Mockito.mock(VirtualRouter.class);
97+
mockVm = Mockito.mock(UserVmVO.class);
98+
mockNic = Mockito.mock(NicVO.class);
99+
mockDc = Mockito.mock(DataCenterVO.class);
100+
101+
// Setup default mock behaviors
102+
when(mockRouter.getId()).thenReturn(100L);
103+
when(mockRouter.getInstanceName()).thenReturn("r-100-VM");
104+
when(mockRouter.getDataCenterId()).thenReturn(1L);
105+
106+
when(mockVm.getId()).thenReturn(200L);
107+
when(mockVm.getHostName()).thenReturn("test-vm");
108+
109+
when(mockNic.getId()).thenReturn(300L);
110+
when(mockNic.getMacAddress()).thenReturn("02:00:0a:0b:0c:0d");
111+
when(mockNic.getIPv4Address()).thenReturn("10.1.1.10");
112+
when(mockNic.getIPv6Address()).thenReturn(null);
113+
when(mockNic.getNetworkId()).thenReturn(400L);
114+
when(mockNic.isDefaultNic()).thenReturn(true);
115+
116+
when(dcDao.findById(anyLong())).thenReturn(mockDc);
117+
when(routerControlHelper.getRouterControlIp(anyLong())).thenReturn("10.1.1.1");
118+
when(routerControlHelper.getRouterIpInNetwork(anyLong(), anyLong())).thenReturn("10.1.1.1");
119+
when(networkModel.getExecuteInSeqNtwkElmtCmd()).thenReturn(false);
120+
}
121+
122+
@Test
123+
public void testDhcpEntryCommandContainsLeaseTime() {
124+
// Test that DhcpEntryCommand includes the lease time from ConfigKey
125+
Commands cmds = new Commands(null);
126+
commandSetupHelper.createDhcpEntryCommand(mockRouter, mockVm, mockNic, false, cmds);
127+
128+
Assert.assertEquals("Should have one DHCP command", 1, cmds.size());
129+
DhcpEntryCommand dhcpCmd = (DhcpEntryCommand) cmds.toCommands()[0];
130+
Assert.assertNotNull("DHCP command should not be null", dhcpCmd);
131+
Assert.assertNotNull("Lease time should not be null", dhcpCmd.getLeaseTime());
132+
133+
// Default value should be 0 (infinite)
134+
Assert.assertEquals("Default lease time should be 0", Long.valueOf(0L), dhcpCmd.getLeaseTime());
135+
}
136+
137+
@Test
138+
public void testDhcpEntryCommandUsesZoneScopedValue() {
139+
// Test that the command uses zone-scoped configuration
140+
Long zoneId = mockRouter.getDataCenterId();
141+
Integer expectedLeaseTime = NetworkOrchestrationService.DhcpLeaseTimeout.valueIn(zoneId);
142+
143+
Commands cmds = new Commands(null);
144+
commandSetupHelper.createDhcpEntryCommand(mockRouter, mockVm, mockNic, false, cmds);
145+
146+
DhcpEntryCommand dhcpCmd = (DhcpEntryCommand) cmds.toCommands()[0];
147+
Assert.assertEquals("Lease time should match zone-scoped config",
148+
expectedLeaseTime.longValue(), dhcpCmd.getLeaseTime().longValue());
149+
}
150+
151+
@Test
152+
public void testInfiniteLeaseWithZeroValue() {
153+
// Test that 0 value represents infinite lease
154+
ConfigKey<Integer> configKey = NetworkOrchestrationService.DhcpLeaseTimeout;
155+
Assert.assertEquals("Default value should be 0 for infinite lease", "0", configKey.defaultValue());
156+
157+
Commands cmds = new Commands(null);
158+
commandSetupHelper.createDhcpEntryCommand(mockRouter, mockVm, mockNic, false, cmds);
159+
160+
DhcpEntryCommand dhcpCmd = (DhcpEntryCommand) cmds.toCommands()[0];
161+
Assert.assertEquals("Lease time 0 represents infinite lease", Long.valueOf(0L), dhcpCmd.getLeaseTime());
162+
}
163+
164+
@Test
165+
public void testDhcpCommandForNonDefaultNic() {
166+
// Test DHCP command creation for non-default NIC
167+
when(mockNic.isDefaultNic()).thenReturn(false);
168+
169+
Commands cmds = new Commands(null);
170+
commandSetupHelper.createDhcpEntryCommand(mockRouter, mockVm, mockNic, false, cmds);
171+
172+
DhcpEntryCommand dhcpCmd = (DhcpEntryCommand) cmds.toCommands()[0];
173+
Assert.assertNotNull("DHCP command should be created for non-default NIC", dhcpCmd);
174+
Assert.assertNotNull("Lease time should be set even for non-default NIC", dhcpCmd.getLeaseTime());
175+
Assert.assertFalse("Command should reflect non-default NIC", dhcpCmd.isDefault());
176+
}
177+
178+
@Test
179+
public void testDhcpCommandWithRemoveFlag() {
180+
// Test DHCP command with remove flag set
181+
Commands cmds = new Commands(null);
182+
commandSetupHelper.createDhcpEntryCommand(mockRouter, mockVm, mockNic, true, cmds);
183+
184+
DhcpEntryCommand dhcpCmd = (DhcpEntryCommand) cmds.toCommands()[0];
185+
Assert.assertNotNull("DHCP command should be created even with remove flag", dhcpCmd);
186+
Assert.assertTrue("Remove flag should be set", dhcpCmd.isRemove());
187+
// Lease time should still be included even for removal
188+
Assert.assertNotNull("Lease time should be present even for removal", dhcpCmd.getLeaseTime());
189+
}
190+
}

0 commit comments

Comments
 (0)