|
| 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.guru; |
| 18 | + |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.ArgumentMatchers.eq; |
| 21 | +import static org.mockito.ArgumentMatchers.isNull; |
| 22 | +import static org.mockito.Mockito.never; |
| 23 | +import static org.mockito.Mockito.times; |
| 24 | +import static org.mockito.Mockito.verify; |
| 25 | +import static org.mockito.Mockito.when; |
| 26 | + |
| 27 | +import com.cloud.dc.DataCenter; |
| 28 | +import com.cloud.dc.DataCenterVO; |
| 29 | +import com.cloud.dc.dao.DataCenterDao; |
| 30 | +import com.cloud.exception.InsufficientAddressCapacityException; |
| 31 | +import com.cloud.exception.InsufficientVirtualNetworkCapacityException; |
| 32 | +import com.cloud.network.IpAddressManager; |
| 33 | +import com.cloud.network.Network; |
| 34 | +import com.cloud.network.Network.GuestType; |
| 35 | +import com.cloud.network.NetworkModel; |
| 36 | +import com.cloud.network.Networks.TrafficType; |
| 37 | +import com.cloud.network.PhysicalNetwork; |
| 38 | +import com.cloud.offering.NetworkOffering; |
| 39 | +import com.cloud.offerings.dao.NetworkOfferingDao; |
| 40 | +import com.cloud.utils.Pair; |
| 41 | +import com.cloud.vm.NicProfile; |
| 42 | +import com.cloud.vm.VirtualMachine; |
| 43 | +import com.cloud.vm.VirtualMachineProfile; |
| 44 | +import org.junit.Assert; |
| 45 | +import org.junit.Test; |
| 46 | +import org.junit.runner.RunWith; |
| 47 | +import org.mockito.InjectMocks; |
| 48 | +import org.mockito.Mock; |
| 49 | +import org.mockito.Mockito; |
| 50 | +import org.mockito.junit.MockitoJUnitRunner; |
| 51 | + |
| 52 | +@RunWith(MockitoJUnitRunner.Silent.class) |
| 53 | +public class GuestNetworkGuruAllocateTest { |
| 54 | + |
| 55 | + // Minimal concrete guru so the abstract base's allocate() can be exercised directly. |
| 56 | + public static class TestGuestNetworkGuru extends GuestNetworkGuru { |
| 57 | + @Override |
| 58 | + protected boolean canHandle(NetworkOffering offering, DataCenter.NetworkType networkType, PhysicalNetwork physicalNetwork) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Mock |
| 64 | + private DataCenterDao dcDao; |
| 65 | + @Mock |
| 66 | + private IpAddressManager ipAddrMgr; |
| 67 | + @Mock |
| 68 | + private NetworkModel networkModel; |
| 69 | + @Mock |
| 70 | + private NetworkOfferingDao networkOfferingDao; |
| 71 | + |
| 72 | + @InjectMocks |
| 73 | + private TestGuestNetworkGuru guru = new TestGuestNetworkGuru(); |
| 74 | + |
| 75 | + private static final long ZONE_ID = 1L; |
| 76 | + private static final long NETWORK_ID = 100L; |
| 77 | + private static final long OFFERING_ID = 11L; |
| 78 | + |
| 79 | + private VirtualMachineProfile userVmProfile() { |
| 80 | + VirtualMachine vmInstance = Mockito.mock(VirtualMachine.class); |
| 81 | + when(vmInstance.getType()).thenReturn(VirtualMachine.Type.User); |
| 82 | + VirtualMachineProfile vm = Mockito.mock(VirtualMachineProfile.class); |
| 83 | + when(vm.getVirtualMachine()).thenReturn(vmInstance); |
| 84 | + when(vm.getType()).thenReturn(VirtualMachine.Type.User); |
| 85 | + return vm; |
| 86 | + } |
| 87 | + |
| 88 | + private Network guestNetwork(GuestType guestType) { |
| 89 | + Network network = Mockito.mock(Network.class); |
| 90 | + when(network.getTrafficType()).thenReturn(TrafficType.Guest); |
| 91 | + when(network.getGuestType()).thenReturn(guestType); |
| 92 | + when(network.getSpecifyIpRanges()).thenReturn(true); |
| 93 | + when(network.getDataCenterId()).thenReturn(ZONE_ID); |
| 94 | + when(network.getId()).thenReturn(NETWORK_ID); |
| 95 | + when(network.getNetworkOfferingId()).thenReturn(OFFERING_ID); |
| 96 | + when(network.getGateway()).thenReturn("10.0.0.1"); |
| 97 | + return network; |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testAllocateIsolatedWithSpecifyIpRangesUsesNetworkCidrNotDirectIp() |
| 102 | + throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException { |
| 103 | + Network network = guestNetwork(GuestType.Isolated); |
| 104 | + when(network.getCidr()).thenReturn("10.0.0.0/8"); |
| 105 | + VirtualMachineProfile vm = userVmProfile(); |
| 106 | + |
| 107 | + DataCenterVO dc = Mockito.mock(DataCenterVO.class); |
| 108 | + when(dcDao.findById(ZONE_ID)).thenReturn(dc); |
| 109 | + when(ipAddrMgr.acquireGuestIpAddress(eq(network), isNull())).thenReturn("10.0.0.5"); |
| 110 | + when(networkModel.getValidNetworkCidr(network)).thenReturn("10.0.0.0/8"); |
| 111 | + when(networkModel.getNetworkIp4Dns(eq(network), eq(dc))).thenReturn(new Pair<>("8.8.8.8", null)); |
| 112 | + when(networkModel.getNextAvailableMacAddressInNetwork(NETWORK_ID)).thenReturn("02:00:00:aa:bb:cc"); |
| 113 | + when(networkOfferingDao.isIpv6Supported(OFFERING_ID)).thenReturn(false); |
| 114 | + |
| 115 | + NicProfile result = guru.allocate(network, null, vm); |
| 116 | + |
| 117 | + verify(ipAddrMgr, never()).allocateDirectIp(any(), any(), any(), any(), any(), any()); |
| 118 | + verify(ipAddrMgr).acquireGuestIpAddress(eq(network), isNull()); |
| 119 | + Assert.assertEquals("10.0.0.5", result.getIPv4Address()); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testAllocateSharedWithSpecifyIpRangesUsesDirectIp() |
| 124 | + throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException { |
| 125 | + Network network = guestNetwork(GuestType.Shared); |
| 126 | + VirtualMachineProfile vm = userVmProfile(); |
| 127 | + |
| 128 | + DataCenterVO dc = Mockito.mock(DataCenterVO.class); |
| 129 | + when(dcDao.findById(ZONE_ID)).thenReturn(dc); |
| 130 | + when(networkModel.getNextAvailableMacAddressInNetwork(NETWORK_ID)).thenReturn("02:00:00:aa:bb:cc"); |
| 131 | + when(networkOfferingDao.isIpv6Supported(OFFERING_ID)).thenReturn(false); |
| 132 | + |
| 133 | + guru.allocate(network, null, vm); |
| 134 | + |
| 135 | + verify(ipAddrMgr, times(1)).allocateDirectIp(any(), eq(dc), eq(vm), eq(network), isNull(), isNull()); |
| 136 | + verify(ipAddrMgr, never()).acquireGuestIpAddress(any(), any()); |
| 137 | + } |
| 138 | +} |
0 commit comments