|
| 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