You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by sh...@apache.org on 2022/12/30 11:11:55 UTC

[cloudstack] branch main updated (97c7a0743cc -> 194b0b4610b)

This is an automated email from the ASF dual-hosted git repository.

shwstppr pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


    from 97c7a0743cc README: Replace Travis badge with Github Actions
     add d5f01005416 utils: fix NetUtils method to retrieve all IPs for a CIDR (#7026)
     new 194b0b4610b Merge remote-tracking branch 'apache/4.17' into main

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../java/com/cloud/network/NetworkModelImpl.java   |  2 +-
 .../main/java/com/cloud/utils/net/NetUtils.java    |  6 +--
 .../java/com/cloud/utils/net/NetUtilsTest.java     | 52 ++++++++++++++++++++++
 3 files changed, 55 insertions(+), 5 deletions(-)


[cloudstack] 01/01: Merge remote-tracking branch 'apache/4.17' into main

Posted by sh...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

shwstppr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack.git

commit 194b0b4610b18985c9fc9d08bb4986dd669f0ae0
Merge: 97c7a0743cc d5f01005416
Author: Abhishek Kumar <ab...@gmail.com>
AuthorDate: Fri Dec 30 16:27:43 2022 +0530

    Merge remote-tracking branch 'apache/4.17' into main

 .../java/com/cloud/network/NetworkModelImpl.java   |  2 +-
 .../main/java/com/cloud/utils/net/NetUtils.java    |  6 +--
 .../java/com/cloud/utils/net/NetUtilsTest.java     | 52 ++++++++++++++++++++++
 3 files changed, 55 insertions(+), 5 deletions(-)

diff --cc utils/src/test/java/com/cloud/utils/net/NetUtilsTest.java
index 2fa8496a235,4c716501863..53e2e3ae376
--- a/utils/src/test/java/com/cloud/utils/net/NetUtilsTest.java
+++ b/utils/src/test/java/com/cloud/utils/net/NetUtilsTest.java
@@@ -33,13 -33,15 +33,17 @@@ import static org.junit.Assert.assertTr
  
  import java.math.BigInteger;
  import java.net.InetAddress;
 +import java.net.NetworkInterface;
 +import java.net.SocketException;
  import java.net.UnknownHostException;
+ import java.util.ArrayList;
+ import java.util.List;
+ import java.util.Set;
  import java.util.SortedSet;
  import java.util.TreeSet;
+ import java.util.stream.Collectors;
  
 -import org.apache.log4j.Logger;
 +import org.junit.Assert;
  import org.junit.Test;
  
  import com.cloud.utils.exception.CloudRuntimeException;
@@@ -47,17 -49,11 +51,19 @@@ import com.cloud.utils.net.NetUtils.Sup
  import com.googlecode.ipv6.IPv6Address;
  import com.googlecode.ipv6.IPv6Network;
  
 -public class NetUtilsTest {
 +import org.junit.runner.RunWith;
 +import org.mockito.Mockito;
 +import org.powermock.api.mockito.PowerMockito;
 +import org.powermock.core.classloader.annotations.PowerMockIgnore;
 +import org.powermock.core.classloader.annotations.PrepareForTest;
 +import org.powermock.modules.junit4.PowerMockRunner;
 +
  
 -    private static final Logger s_logger = Logger.getLogger(NetUtilsTest.class);
 +@RunWith(PowerMockRunner.class)
 +@PowerMockIgnore({"jdk.xml.internal.*", "javax.xml.parsers.*", "org.xml.sax.*", "org.w3c.dom.*"})
 +public class NetUtilsTest {
+     private static final String WIDE_SHARED_NET_CIDR_IP = "10.20.0.0";
+     private static final List<String> WIDE_SHARED_NET_USED_IPS = List.of("10.20.0.22", "10.20.1.22", "10.20.2.22");
  
      @Test
      public void testGetRandomIpFromCidrWithSize24() throws Exception {
@@@ -750,47 -748,49 +756,93 @@@
          assertEquals("255.255.240.0", NetUtils.cidr2Netmask("169.254.240.0/20"));
      }
  
+     private void runTestGetAllIpsFromCidr(int cidrSize, int maxIps, boolean usedIpPresent, int resultSize) {
+         Set<Long> usedIps = new TreeSet<>();
+         if (usedIpPresent) {
+             for (String ip : WIDE_SHARED_NET_USED_IPS) {
+                 usedIps.add(NetUtils.ip2Long(ip));
+             }
+         }
+         Set<Long> result = NetUtils.getAllIpsFromCidr(WIDE_SHARED_NET_CIDR_IP, cidrSize, usedIps, maxIps);
+         assertNotNull(result);
+         assertEquals(resultSize, result.size());
+         if (usedIpPresent) {
+             for (String ip : WIDE_SHARED_NET_USED_IPS) {
+                 assertFalse(result.contains(NetUtils.ip2Long(ip)));
+             }
+         }
+     }
+ 
+     @Test
+     public void testGetAllIpsFromCidrNoneUsedNoLimit() {
+         runTestGetAllIpsFromCidr(22, -1, false, 1022);
+     }
+ 
+     @Test
+     public void testGetAllIpsFromCidrNoneUsedLimit() {
+         runTestGetAllIpsFromCidr(22, 255, false, 255);
+     }
+ 
+     @Test
+     public void testGetAllIpsFromCidrNoneUsedLessLimit() {
+         runTestGetAllIpsFromCidr(22, 10, false, 10);
+     }
+ 
+ 
+     @Test
+     public void testGetAllIpsFromCidrUsedNoLimit() {
+         runTestGetAllIpsFromCidr(22, -1, true, 1022 - WIDE_SHARED_NET_USED_IPS.size());
+     }
+ 
+     @Test
+     public void testGetAllIpsFromCidrUsedLimit() {
+         runTestGetAllIpsFromCidr(22, 50, true, 50);
+         List<String> usedIpsInRange = new ArrayList<>(WIDE_SHARED_NET_USED_IPS);
+         usedIpsInRange = usedIpsInRange.stream().filter(x -> x.startsWith("10.20.0.")).collect(Collectors.toList());
+         runTestGetAllIpsFromCidr(24, 255, true, 254 - usedIpsInRange.size());
+     }
++
 +    @Test
 +    public void getNetworkInterfaceTestReturnNullWhenStringIsNull() {
 +        NetworkInterface result = NetUtils.getNetworkInterface(null);
 +        Assert.assertNull(result);
 +    }
 +
 +    @Test
 +    public void getNetworkInterfaceTestReturnNullWhenStringIsEmpty() {
 +        NetworkInterface result = NetUtils.getNetworkInterface("     ");
 +        Assert.assertNull(result);
 +    }
 +
 +    @Test
 +    @PrepareForTest(NetUtils.class)
 +    public void getNetworkInterfaceTestReturnNullWhenGetByNameReturnsNull() throws SocketException {
 +        PowerMockito.mockStatic(NetworkInterface.class);
 +        PowerMockito.when(NetworkInterface.getByName(Mockito.anyString())).thenReturn(null);
 +        NetworkInterface result = NetUtils.getNetworkInterface("  test   ");
 +
 +        Assert.assertNull(result);
 +    }
 +
 +    @Test
 +    @PrepareForTest(NetUtils.class)
 +    public void getNetworkInterfaceTestReturnNullWhenGetByNameThrowsException() throws SocketException {
 +        PowerMockito.mockStatic(NetworkInterface.class);
 +        PowerMockito.when(NetworkInterface.getByName(Mockito.anyString())).thenThrow(SocketException.class);
 +        NetworkInterface result = NetUtils.getNetworkInterface("  test   ");
 +
 +        Assert.assertNull(result);
 +    }
 +
 +    @Test
 +    @PrepareForTest(NetUtils.class)
 +    public void getNetworkInterfaceTestReturnInterfaceReturnedByGetByName() throws SocketException {
 +        NetworkInterface expected = PowerMockito.mock(NetworkInterface.class);
 +        PowerMockito.mockStatic(NetworkInterface.class);
 +        PowerMockito.when(NetworkInterface.getByName(Mockito.anyString())).thenReturn(expected);
 +
 +        NetworkInterface result = NetUtils.getNetworkInterface("  test   ");
 +
 +        Assert.assertEquals(expected, result);
 +    }
  }