|
| 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 org.apache.cloudstack.engine.orchestration; |
| 18 | + |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 21 | +import static org.mockito.ArgumentMatchers.eq; |
| 22 | +import static org.mockito.ArgumentMatchers.isNull; |
| 23 | + |
| 24 | +import java.util.Collections; |
| 25 | + |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Test; |
| 28 | +import org.junit.runner.RunWith; |
| 29 | +import org.mockito.InjectMocks; |
| 30 | +import org.mockito.Mock; |
| 31 | +import org.mockito.Mockito; |
| 32 | +import org.mockito.Spy; |
| 33 | +import org.mockito.junit.MockitoJUnitRunner; |
| 34 | + |
| 35 | +import com.cloud.dc.DataCenter.NetworkType; |
| 36 | +import com.cloud.dc.DataCenterVO; |
| 37 | +import com.cloud.dc.dao.DataCenterDao; |
| 38 | +import com.cloud.network.Network; |
| 39 | +import com.cloud.network.Networks.TrafficType; |
| 40 | +import com.cloud.network.dao.NetworkDao; |
| 41 | +import com.cloud.network.dao.NetworkVO; |
| 42 | +import com.cloud.network.guru.NetworkGuru; |
| 43 | +import com.cloud.vm.NicProfile; |
| 44 | +import com.cloud.vm.NicVO; |
| 45 | +import com.cloud.vm.VirtualMachine.Type; |
| 46 | +import com.cloud.vm.VirtualMachineProfile; |
| 47 | + |
| 48 | +@RunWith(MockitoJUnitRunner.Silent.class) |
| 49 | +public class NetworkOrchestratorNicRaceTest { |
| 50 | + |
| 51 | + @Mock |
| 52 | + NetworkDao _networksDao; |
| 53 | + @Mock |
| 54 | + DataCenterDao _dcDao; |
| 55 | + |
| 56 | + @Spy |
| 57 | + @InjectMocks |
| 58 | + NetworkOrchestrator orchestrator = new NetworkOrchestrator(); |
| 59 | + |
| 60 | + @Test |
| 61 | + public void checkForRaceAndAllocateNicRetriesInsteadOfNpeWhenNoIpRequested() throws Exception { |
| 62 | + Network network = Mockito.mock(Network.class); |
| 63 | + Mockito.when(network.getId()).thenReturn(1L); |
| 64 | + Mockito.when(network.getDataCenterId()).thenReturn(1L); |
| 65 | + Mockito.when(network.getTrafficType()).thenReturn(TrafficType.Guest); |
| 66 | + |
| 67 | + NetworkVO ntwkVO = Mockito.mock(NetworkVO.class); |
| 68 | + Mockito.when(ntwkVO.getGuruName()).thenReturn("TestGuru"); |
| 69 | + Mockito.when(_networksDao.findById(1L)).thenReturn(ntwkVO); |
| 70 | + |
| 71 | + DataCenterVO dcVo = Mockito.mock(DataCenterVO.class); |
| 72 | + Mockito.when(dcVo.getNetworkType()).thenReturn(NetworkType.Advanced); |
| 73 | + Mockito.when(_dcDao.findById(1L)).thenReturn(dcVo); |
| 74 | + |
| 75 | + VirtualMachineProfile vm = Mockito.mock(VirtualMachineProfile.class); |
| 76 | + Mockito.when(vm.getId()).thenReturn(1L); |
| 77 | + Mockito.when(vm.getType()).thenReturn(Type.User); |
| 78 | + |
| 79 | + NicProfile profile = Mockito.mock(NicProfile.class); |
| 80 | + Mockito.when(profile.getIpv4AllocationRaceCheck()).thenReturn(true); |
| 81 | + |
| 82 | + NetworkGuru guru = Mockito.mock(NetworkGuru.class); |
| 83 | + Mockito.when(guru.getName()).thenReturn("TestGuru"); |
| 84 | + Mockito.when(guru.allocate(eq(network), isNull(), eq(vm))).thenReturn(profile); |
| 85 | + orchestrator.setNetworkGurus(Collections.singletonList(guru)); |
| 86 | + |
| 87 | + NicVO persisted = Mockito.mock(NicVO.class); |
| 88 | + // First attempt loses the IP-allocation race (persist returns null); the retry wins (non-null). |
| 89 | + Mockito.doReturn(null).doReturn(persisted) |
| 90 | + .when(orchestrator).persistNicAfterRaceCheck(any(NicVO.class), eq(1L), eq(profile), anyInt()); |
| 91 | + |
| 92 | + // requested == null is the common "no explicit IP" deploy path. Before the fix, losing the race |
| 93 | + // dereferenced the null requested profile and threw NPE instead of retrying the allocation. |
| 94 | + NicVO result = orchestrator.checkForRaceAndAllocateNic(null, network, null, 0, vm); |
| 95 | + |
| 96 | + Assert.assertSame("losing the race should retry and return the persisted NIC, not NPE", persisted, result); |
| 97 | + } |
| 98 | +} |
0 commit comments