Skip to content

Commit 7d932e5

Browse files
GutoVeroneziGutoVeronezi
andauthored
engine/schema: Fix API deleteTrafficType not filtering physical network (#6510)
While deleting a traffic type, ACS validates if there is any VM related to it. However, if we have several physical networks containing a traffic type, ACS does not filter the physical network to do the validation. For instance, if we have two (2) physical networks containing the traffic type Guest, the first one having VMs related, and the second not having VMs related, if we try to remove the second traffic type, ACS give us the message The Traffic Type is not deletable because there are existing networks with this traffic type:Guest. The API deleteTrafficType was designed to filter the physical network where the traffic type is, however, due to a typo this filtering was not been applied correctly. This PR intends to fix this typo to honor the API behavior. In an advanced zone I created 4 physical networks, one for each traffic type (Public, Guest, Management, Storage). I instantiated some VMs so they get guest IPs. In the Public physical network I added a Guest traffic type. I tried to remove the new Guest traffic type from Public physical network, which did not have any VMs related to it, and, before the changes, I was getting the message The Traffic Type is not deletable because there are existing networks with this traffic type:Guest. After the changes, I could remove successfully the new Guest traffic type via API deleteTrafficType. I also tried to remove the Guest traffic type which had VMs related to it, however, as expected, I received the The Traffic Type is not deletable... message. I also created a unit test to validate the data retrieving. Co-authored-by: GutoVeronezi <daniel@scclouds.com.br>
1 parent 6607a98 commit 7d932e5

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

engine/schema/src/main/java/com/cloud/network/dao/NetworkDaoImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ public List<NetworkVO> listByPhysicalNetwork(final long physicalNetworkId) {
572572
public List<NetworkVO> listByPhysicalNetworkTrafficType(final long physicalNetworkId, final TrafficType trafficType) {
573573
final SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
574574
sc.setParameters("trafficType", trafficType);
575-
sc.setParameters("physicalNetworkId", physicalNetworkId);
575+
sc.setParameters("physicalNetwork", physicalNetworkId);
576576
return listBy(sc);
577577
}
578578

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.cloud.network.dao;
21+
22+
import com.cloud.network.Networks;
23+
import com.cloud.utils.db.SearchBuilder;
24+
import com.cloud.utils.db.SearchCriteria;
25+
import org.junit.Assert;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.mockito.Mock;
29+
import org.mockito.Mockito;
30+
import org.powermock.api.mockito.PowerMockito;
31+
import org.powermock.modules.junit4.PowerMockRunner;
32+
33+
import java.util.List;
34+
35+
@RunWith(PowerMockRunner.class)
36+
public class NetworkDaoImplTest {
37+
38+
@Mock
39+
SearchBuilder<NetworkVO> searchBuilderNetworkVoMock;
40+
41+
@Mock
42+
SearchCriteria<NetworkVO> searchCriteriaNetworkVoMock;
43+
44+
@Mock
45+
List<NetworkVO> listNetworkVoMock;
46+
47+
@Test
48+
public void listByPhysicalNetworkTrafficTypeTestSetParametersValidation() throws Exception {
49+
NetworkDaoImpl networkDaoImplSpy = PowerMockito.spy(new NetworkDaoImpl());
50+
51+
networkDaoImplSpy.AllFieldsSearch = searchBuilderNetworkVoMock;
52+
Mockito.doReturn(searchCriteriaNetworkVoMock).when(searchBuilderNetworkVoMock).create();
53+
Mockito.doNothing().when(searchCriteriaNetworkVoMock).setParameters(Mockito.anyString(), Mockito.any());
54+
PowerMockito.doReturn(listNetworkVoMock).when(networkDaoImplSpy, "listBy", Mockito.any(SearchCriteria.class));
55+
56+
long expectedPhysicalNetwork = 2513l;
57+
58+
for (Networks.TrafficType trafficType : Networks.TrafficType.values()) {
59+
List<NetworkVO> result = networkDaoImplSpy.listByPhysicalNetworkTrafficType(expectedPhysicalNetwork, trafficType);
60+
Assert.assertEquals(listNetworkVoMock, result);
61+
Mockito.verify(searchCriteriaNetworkVoMock).setParameters("trafficType", trafficType);
62+
}
63+
64+
Mockito.verify(searchCriteriaNetworkVoMock, Mockito.times(Networks.TrafficType.values().length)).setParameters("physicalNetwork", expectedPhysicalNetwork);
65+
}
66+
}

0 commit comments

Comments
 (0)