diff --git a/server/src/main/java/com/cloud/network/IpAddressManagerImpl.java b/server/src/main/java/com/cloud/network/IpAddressManagerImpl.java index b7a6846422fb..88e178bc25a6 100644 --- a/server/src/main/java/com/cloud/network/IpAddressManagerImpl.java +++ b/server/src/main/java/com/cloud/network/IpAddressManagerImpl.java @@ -963,6 +963,16 @@ public PublicIp fetchNewPublicIp(final long dcId, final Long podId, final List listAvailablePublicIps(final long dcId, final Long podId, final List vlanDbIds, final Account owner, final VlanType vlanUse, final Long guestNetworkId, final boolean sourceNat, final boolean assign, final boolean allocate, final String requestedIp, final String requestedGateway, final boolean isSystem, @@ -1114,7 +1124,7 @@ public List listAvailablePublicIps(final long dcId, final Long podI throw ex; } logger.warn(errorMessage.toString()); - InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", DataCenter.class, dcId); + InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException(getInsufficientAddressCapacityMessage(vlanUse, network, requestedIp), DataCenter.class, dcId); ex.addProxyObject(ApiDBUtils.findZoneById(dcId).getUuid()); throw ex; } diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java index b2e20256cc53..9edc935651a0 100644 --- a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java @@ -4563,7 +4563,7 @@ private UserVm getCheckedUserVmResource(DataCenter zone, String hostName, String Map> dhcpOptionMap, Map datadiskTemplateToDiskOfferringMap, Map userVmOVFPropertiesMap, boolean dynamicScalingEnabled, String vmType, VMTemplateVO template, HypervisorType hypervisorType, long accountId, ServiceOfferingVO offering, boolean isIso, - Long rootDiskOfferingId, Long rootDiskKmsKeyId, long volumesSize, Volume volume, Snapshot snapshot) throws ResourceAllocationException { + Long rootDiskOfferingId, Long rootDiskKmsKeyId, long volumesSize, Volume volume, Snapshot snapshot) throws ResourceAllocationException, InsufficientCapacityException { if (!VirtualMachineManager.ResourceCountRunningVMsonly.value() && !(VALIDATION_VM.equals(vmType) && !EnforceResourceLimitOnValidationVm.valueIn(accountId))) { List resourceLimitHostTags = resourceLimitService.getResourceLimitHostTags(offering, template); try (CheckedReservation vmReservation = new CheckedReservation(owner, ResourceType.user_vm, resourceLimitHostTags, 1l, reservationDao, resourceLimitService); @@ -4573,7 +4573,7 @@ private UserVm getCheckedUserVmResource(DataCenter zone, String hostName, String new CheckedReservation(owner, ResourceType.gpu, resourceLimitHostTags, Long.valueOf(offering.getGpuCount()), reservationDao, resourceLimitService) : null; ) { return getUncheckedUserVmResource(zone, hostName, displayName, owner, diskOfferingId, diskSize, dataDiskInfoList, networkList, securityGroupIdList, group, httpmethod, userData, userDataId, userDataDetails, sshKeyPairs, caller, requestedIps, defaultIps, isDisplayVm, keyboard, affinityGroupIdList, customParameters, customId, dhcpOptionMap, datadiskTemplateToDiskOfferringMap, userVmOVFPropertiesMap, dynamicScalingEnabled, vmType, template, hypervisorType, accountId, offering, isIso, rootDiskOfferingId, rootDiskKmsKeyId, volumesSize, volume, snapshot); - } catch (ResourceAllocationException | CloudRuntimeException e) { + } catch (ResourceAllocationException | InsufficientCapacityException | CloudRuntimeException e) { throw e; } catch (Exception e) { logger.error("error during resource reservation and allocation", e); @@ -4635,7 +4635,7 @@ private UserVm getUncheckedUserVmResource(DataCenter zone, String hostName, Stri Map> dhcpOptionMap, Map datadiskTemplateToDiskOfferringMap, Map userVmOVFPropertiesMap, boolean dynamicScalingEnabled, String vmType, VMTemplateVO template, HypervisorType hypervisorType, long accountId, ServiceOfferingVO offering, boolean isIso, - Long rootDiskOfferingId, Long rootDiskKmsKeyId, long volumesSize, Volume volume, Snapshot snapshot) throws ResourceAllocationException { + Long rootDiskOfferingId, Long rootDiskKmsKeyId, long volumesSize, Volume volume, Snapshot snapshot) throws ResourceAllocationException, InsufficientCapacityException { List checkedReservations = new ArrayList<>(); @@ -4918,7 +4918,7 @@ private UserVm getUncheckedUserVmResource(DataCenter zone, String hostName, Stri assignInstanceToGroup(group, id); return vm; - } catch (ResourceAllocationException | CloudRuntimeException e) { + } catch (ResourceAllocationException | InsufficientCapacityException | CloudRuntimeException e) { throw e; } catch (Exception e) { logger.error("error during resource reservation and allocation", e); diff --git a/server/src/test/java/com/cloud/network/IpAddressManagerTest.java b/server/src/test/java/com/cloud/network/IpAddressManagerTest.java index d12419b66e0a..059c7415dd07 100644 --- a/server/src/test/java/com/cloud/network/IpAddressManagerTest.java +++ b/server/src/test/java/com/cloud/network/IpAddressManagerTest.java @@ -63,6 +63,7 @@ import com.cloud.network.vpc.dao.VpcDao; import com.cloud.network.vpc.VpcVO; import com.cloud.network.vpc.VpcManager; +import com.cloud.dc.Vlan.VlanType; import com.cloud.dc.dao.VlanDao; import com.cloud.offerings.NetworkOfferingVO; import com.cloud.offerings.dao.NetworkOfferingDao; @@ -640,4 +641,38 @@ public void applyRulesVpcRuleErrorHandlingWithContinueOnErrorTrue() throws Resou assertFalse(result); } + + private Network networkNamed(String name) { + Network network = mock(Network.class); + when(network.getName()).thenReturn(name); + return network; + } + + @Test + public void insufficientAddressCapacityMessageNamesTheNetworkWhoseIpRangesAreFull() { + String message = ipAddressManager.getInsufficientAddressCapacityMessage(VlanType.DirectAttached, networkNamed("shared-net"), null); + + Assert.assertEquals("Insufficient address capacity: no free IP address left in the IP ranges of network shared-net", message); + } + + @Test + public void insufficientAddressCapacityMessageNamesTheRequestedIpAndNetwork() { + String message = ipAddressManager.getInsufficientAddressCapacityMessage(VlanType.DirectAttached, networkNamed("shared-net"), "10.1.1.20"); + + Assert.assertEquals("Insufficient address capacity: the requested IP address 10.1.1.20 is not available in the IP ranges of network shared-net", message); + } + + @Test + public void insufficientAddressCapacityMessageIsUnchangedForPublicIps() { + String message = ipAddressManager.getInsufficientAddressCapacityMessage(VlanType.VirtualNetwork, networkNamed("isolated-net"), null); + + Assert.assertEquals("Insufficient address capacity", message); + } + + @Test + public void insufficientAddressCapacityMessageIsUnchangedWithoutANetwork() { + String message = ipAddressManager.getInsufficientAddressCapacityMessage(VlanType.DirectAttached, null, null); + + Assert.assertEquals("Insufficient address capacity", message); + } } diff --git a/server/src/test/java/com/cloud/network/guru/GuestNetworkGuruAllocateTest.java b/server/src/test/java/com/cloud/network/guru/GuestNetworkGuruAllocateTest.java new file mode 100644 index 000000000000..34e7f0e8368a --- /dev/null +++ b/server/src/test/java/com/cloud/network/guru/GuestNetworkGuruAllocateTest.java @@ -0,0 +1,134 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.network.guru; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.cloud.dc.DataCenter; +import com.cloud.dc.DataCenterVO; +import com.cloud.dc.dao.DataCenterDao; +import com.cloud.exception.InsufficientAddressCapacityException; +import com.cloud.exception.InsufficientVirtualNetworkCapacityException; +import com.cloud.network.IpAddressManager; +import com.cloud.network.Network; +import com.cloud.network.Network.GuestType; +import com.cloud.network.NetworkModel; +import com.cloud.network.Networks.TrafficType; +import com.cloud.network.PhysicalNetwork; +import com.cloud.offering.NetworkOffering; +import com.cloud.offerings.dao.NetworkOfferingDao; +import com.cloud.utils.Pair; +import com.cloud.vm.VirtualMachine; +import com.cloud.vm.VirtualMachineProfile; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.Silent.class) +public class GuestNetworkGuruAllocateTest { + + public static class TestGuestNetworkGuru extends GuestNetworkGuru { + @Override + protected boolean canHandle(NetworkOffering offering, DataCenter.NetworkType networkType, PhysicalNetwork physicalNetwork) { + return false; + } + } + + @Mock + private DataCenterDao dcDao; + @Mock + private IpAddressManager ipAddrMgr; + @Mock + private NetworkModel networkModel; + @Mock + private NetworkOfferingDao networkOfferingDao; + + @InjectMocks + private TestGuestNetworkGuru guru = new TestGuestNetworkGuru(); + + private static final long ZONE_ID = 1L; + private static final long NETWORK_ID = 100L; + private static final long OFFERING_ID = 11L; + + private VirtualMachineProfile userVmProfile() { + VirtualMachine vmInstance = Mockito.mock(VirtualMachine.class); + when(vmInstance.getType()).thenReturn(VirtualMachine.Type.User); + VirtualMachineProfile vm = Mockito.mock(VirtualMachineProfile.class); + when(vm.getVirtualMachine()).thenReturn(vmInstance); + when(vm.getType()).thenReturn(VirtualMachine.Type.User); + return vm; + } + + private Network guestNetwork(GuestType guestType) { + Network network = Mockito.mock(Network.class); + when(network.getTrafficType()).thenReturn(TrafficType.Guest); + when(network.getGuestType()).thenReturn(guestType); + when(network.getSpecifyIpRanges()).thenReturn(true); + when(network.getDataCenterId()).thenReturn(ZONE_ID); + when(network.getId()).thenReturn(NETWORK_ID); + when(network.getNetworkOfferingId()).thenReturn(OFFERING_ID); + when(network.getGateway()).thenReturn("10.0.0.1"); + return network; + } + + @Test + public void testAllocateIsolatedWithSpecifyIpRangesAllocatesFromTheNetworkIpRanges() + throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException { + Network network = guestNetwork(GuestType.Isolated); + when(network.getCidr()).thenReturn("10.0.0.0/24"); + VirtualMachineProfile vm = userVmProfile(); + + DataCenterVO dc = Mockito.mock(DataCenterVO.class); + when(dcDao.findById(ZONE_ID)).thenReturn(dc); + when(ipAddrMgr.acquireGuestIpAddress(eq(network), isNull())).thenReturn("10.0.0.200"); + when(networkModel.getValidNetworkCidr(network)).thenReturn("10.0.0.0/24"); + when(networkModel.getNetworkIp4Dns(eq(network), eq(dc))).thenReturn(new Pair<>("8.8.8.8", null)); + when(networkModel.getNextAvailableMacAddressInNetwork(NETWORK_ID)).thenReturn("02:00:00:aa:bb:cc"); + when(networkOfferingDao.isIpv6Supported(OFFERING_ID)).thenReturn(false); + + guru.allocate(network, null, vm); + + verify(ipAddrMgr, times(1)).allocateDirectIp(any(), eq(dc), eq(vm), eq(network), isNull(), isNull()); + verify(ipAddrMgr, never()).acquireGuestIpAddress(any(), any()); + } + + @Test + public void testAllocateSharedWithSpecifyIpRangesUsesDirectIp() + throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException { + Network network = guestNetwork(GuestType.Shared); + VirtualMachineProfile vm = userVmProfile(); + + DataCenterVO dc = Mockito.mock(DataCenterVO.class); + when(dcDao.findById(ZONE_ID)).thenReturn(dc); + when(networkModel.getNextAvailableMacAddressInNetwork(NETWORK_ID)).thenReturn("02:00:00:aa:bb:cc"); + when(networkOfferingDao.isIpv6Supported(OFFERING_ID)).thenReturn(false); + + guru.allocate(network, null, vm); + + verify(ipAddrMgr, times(1)).allocateDirectIp(any(), eq(dc), eq(vm), eq(network), isNull(), isNull()); + verify(ipAddrMgr, never()).acquireGuestIpAddress(any(), any()); + } +}